Class: RubyTictactoe::Game

Inherits:
Object
  • Object
show all
Includes:
TictactoeConstants
Defined in:
lib/game.rb

Constant Summary

Constants included from TictactoeConstants

TictactoeConstants::AI_PLAYER, TictactoeConstants::COMPUTER_PLAYER, TictactoeConstants::EASY_LEVEL, TictactoeConstants::HARD_LEVEL, TictactoeConstants::HUMAN_PLAYER, TictactoeConstants::MARKER_O, TictactoeConstants::MARKER_X

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Game

Returns a new instance of Game.



6
7
8
9
10
11
12
# File 'lib/game.rb', line 6

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.



5
6
7
# File 'lib/game.rb', line 5

def board
  @board
end

#player_first_moveObject

Returns the value of attribute player_first_move.



5
6
7
# File 'lib/game.rb', line 5

def player_first_move
  @player_first_move
end

#player_oneObject

Returns the value of attribute player_one.



5
6
7
# File 'lib/game.rb', line 5

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



5
6
7
# File 'lib/game.rb', line 5

def player_two
  @player_two
end

#uiObject

Returns the value of attribute ui.



5
6
7
# File 'lib/game.rb', line 5

def ui
  @ui
end

Instance Method Details

#advance_gameObject



14
15
16
17
# File 'lib/game.rb', line 14

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

#current_playerObject



33
34
35
36
37
38
39
40
41
# File 'lib/game.rb', line 33

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



19
20
21
22
23
24
25
# File 'lib/game.rb', line 19

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



43
44
45
# File 'lib/game.rb', line 43

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

#verify_move(cell) ⇒ Object



27
28
29
30
31
# File 'lib/game.rb', line 27

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