Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, ui, ai, player_1, player_2) ⇒ Game

Returns a new instance of Game.



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

def initialize(board, ui, ai, player_1, player_2)
  @player_1 = player_1
  @player_2 = player_2
  @board = board
  @ui = ui
  @ai = ai
  @unbeatable_ai = ai
  @runner = Runner.new(ui)
end

Instance Attribute Details

#aiObject (readonly)

Returns the value of attribute ai.



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

def ai
  @ai
end

#boardObject (readonly)

Returns the value of attribute board.



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

def board
  @board
end

#player_1Object

Returns the value of attribute player_1.



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

def player_1
  @player_1
end

#player_2Object

Returns the value of attribute player_2.



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

def player_2
  @player_2
end

#runnerObject (readonly)

Returns the value of attribute runner.



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

def runner
  @runner
end

#uiObject (readonly)

Returns the value of attribute ui.



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

def ui
  @ui
end

#unbeatable_aiObject (readonly)

Returns the value of attribute unbeatable_ai.



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

def unbeatable_ai
  @unbeatable_ai
end

Instance Method Details

#declare_turn(marker) ⇒ Object



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

def declare_turn(marker)
  @ui.puts_turn(marker)
  @ui.print_board(@board.display_board)
  @ui.ask_for_square_to_mark    
end

#find_winnerObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/game.rb', line 49

def find_winner
  winner = @board.winner_on_board?
  open_board = @board.board_open?
 
  if winner == false and open_board
    select_square
  elsif winner != false
    game_over(@ui.puts_winner(winner))
  else
    game_over(@ui.puts_tie)
  end

end

#game_over(final_game_message) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/game.rb', line 63

def game_over(final_game_message)
  @ui.print_board(@board.display_board)
  final_game_message
  @ui.ask_to_restart
  choice = @ui.get_input
  restart?(choice)  
end

#place_marker(square, marker) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/game.rb', line 38

def place_marker(square, marker)
 
  if @board.square_empty?(square) == false 
    @ui.marker_error
    select_square
  end
 
  @board.set_square(square, marker)
  find_winner
end

#play_gameObject



75
76
77
# File 'lib/game.rb', line 75

def play_game
  select_square
end

#restart?(input) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/game.rb', line 71

def restart?(input)
  input == 1 ? @runner.call : exit  
end

#select_squareObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/game.rb', line 24

def select_square
  square = nil
  marker = @board.whose_turn
  declare_turn(marker)
 
  if marker == player_2.marker and @ai.opponent == true
    square = @unbeatable_ai.make_move(@board, marker)
  else
    square = @ui.get_square_to_mark
  end
 
  place_marker(square, marker)
end