Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ttt.rb

Direct Known Subclasses

CLIGame, WebGame

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Game

Returns a new instance of Game.



10
11
12
13
14
15
16
# File 'lib/ruby_ttt.rb', line 10

def initialize(settings)
  @board = settings[:board]
  @player_one = settings[:player_one]
  @player_two = settings[:player_two]
  @player_first_move = settings[:player_first_move]
  @ui = UI.new
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



9
10
11
# File 'lib/ruby_ttt.rb', line 9

def board
  @board
end

#player_first_moveObject

Returns the value of attribute player_first_move.



9
10
11
# File 'lib/ruby_ttt.rb', line 9

def player_first_move
  @player_first_move
end

#player_oneObject

Returns the value of attribute player_one.



9
10
11
# File 'lib/ruby_ttt.rb', line 9

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



9
10
11
# File 'lib/ruby_ttt.rb', line 9

def player_two
  @player_two
end

#uiObject

Returns the value of attribute ui.



9
10
11
# File 'lib/ruby_ttt.rb', line 9

def ui
  @ui
end

Instance Method Details

#advance_gameObject



18
19
20
21
# File 'lib/ruby_ttt.rb', line 18

def advance_game
  game_status_check(current_player.opponent.marker)
  ui.next_move_message(current_player.marker) unless board.game_over?
end

#current_playerObject



37
38
39
40
41
42
43
44
45
# File 'lib/ruby_ttt.rb', line 37

def current_player
  if total_markers(MARKER_X) > total_markers(MARKER_O)
    player_two
  elsif total_markers(MARKER_O) > total_markers(MARKER_X)
    player_one
  else
    player_first_move
  end
end

#game_status_check(marker) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ruby_ttt.rb', line 23

def game_status_check(marker)
  if board.winner?(marker)
    ui.winning_game_message(marker)
  elsif !board.moves_remaining?
    ui.tie_game_message
  end
end

#total_markers(marker) ⇒ Object



47
48
49
# File 'lib/ruby_ttt.rb', line 47

def total_markers(marker)
  board.all_cells.select { |cell, value| value == marker }.count
end

#verify_move(cell) ⇒ Object



31
32
33
34
35
# File 'lib/ruby_ttt.rb', line 31

def verify_move(cell)
  return false if !board.available_cell?(cell)
  current_player.add_marker(board, cell)
  true
end