Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, player_one, player_two, difficulty_level) ⇒ Game



3
4
5
6
7
8
9
10
# File 'lib/game.rb', line 3

def initialize(board, player_one, player_two, difficulty_level)
  @board      = board
  @ui         = UI.new(@board)
  @player_one = player_one
  @player_two = player_two
  @ai         = AI.new
  @difficulty_level = difficulty_level
end

Instance Attribute Details

#aiObject

Returns the value of attribute ai.



2
3
4
# File 'lib/game.rb', line 2

def ai
  @ai
end

#boardObject

Returns the value of attribute board.



2
3
4
# File 'lib/game.rb', line 2

def board
  @board
end

#difficulty_levelObject

Returns the value of attribute difficulty_level.



2
3
4
# File 'lib/game.rb', line 2

def difficulty_level
  @difficulty_level
end

#player_oneObject

Returns the value of attribute player_one.



2
3
4
# File 'lib/game.rb', line 2

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



2
3
4
# File 'lib/game.rb', line 2

def player_two
  @player_two
end

#uiObject

Returns the value of attribute ui.



2
3
4
# File 'lib/game.rb', line 2

def ui
  @ui
end

Instance Method Details

#advance_game(cell, player) ⇒ Object



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

def advance_game(cell, player)
  board.add_marker(player.marker, cell)
  game_status_check
  player.next_player_turn
  ui.next_move_message(current_player) unless board.game_over?
end

#current_playerObject



45
46
47
# File 'lib/game.rb', line 45

def current_player
  player_one.current_player? ? player_one : player_two
end

#exit_gameObject



49
50
51
52
# File 'lib/game.rb', line 49

def exit_game
  ui.display_board
  ui.io.exit
end

#game_status_checkObject



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

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

#get_next_moveObject



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

def get_next_move
  return ui.request_human_move if current_player.player_type == HUMAN_PLAYER
  difficulty_level == HARD_LEVEL ? ai.computer_move(board, current_player) : board.random_cell
end

#invalid_move(cell) ⇒ Object



41
42
43
# File 'lib/game.rb', line 41

def invalid_move(cell)
  board.valid_cell?(cell) ? ui.taken_cell_message(cell) : ui.bad_cell_message(cell)
end

#play!Object



12
13
14
15
16
17
18
19
# File 'lib/game.rb', line 12

def play!
  until board.game_over?
    ui.display_board
    move = get_next_move
    board.available_cell?(move) ? advance_game(move, current_player) : invalid_move(move)
  end
  exit_game
end