Class: TTT::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Game

Returns a new instance of Game.



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

def initialize(options)
  self.board          = options[:board]
  self.player1        = options[:player1]
  self.player2        = options[:player2]
  self.current_player = self.player1
  self.history        = options.fetch(:history)
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



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

def board
  @board
end

#current_playerObject

Returns the value of attribute current_player.



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

def current_player
  @current_player
end

#dbObject

Returns the value of attribute db.



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

def db
  @db
end

#historyObject

Returns the value of attribute history.



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

def history
  @history
end

#player1Object

Returns the value of attribute player1.



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

def player1
  @player1
end

#player2Object

Returns the value of attribute player2.



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

def player2
  @player2
end

Instance Method Details

#adjust_move_index(index_diff) ⇒ Object



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

def adjust_move_index(index_diff)
  history.adjust_move_index(index_diff)
end

#ai_move?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ttt/game.rb', line 13

def ai_move?
  current_player.no_gui?
end

#board_arrObject



90
91
92
# File 'lib/ttt/game.rb', line 90

def board_arr
  board[]
end

#draw?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ttt/game.rb', line 86

def draw?
  board.draw_game?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  board.finished?
end

#get_history_board(move_number_limit = history.move_traverser.move_index) ⇒ Object



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

def get_history_board(move_number_limit = history.move_traverser.move_index)
  history.get_history_board(board, move_number_limit)
end

#initialize_historyObject



54
55
56
# File 'lib/ttt/game.rb', line 54

def initialize_history
  history.initialize_history
end

#last_playerObject



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

def last_player
  current_player == player1 ? "Player 2" : "Player 1"
end

#mark_move(cell, side = current_player.side) ⇒ Object



34
35
36
# File 'lib/ttt/game.rb', line 34

def mark_move(cell, side = current_player.side)
  board.update(cell, side)
end

#next_moveObject



21
22
23
24
25
26
27
28
# File 'lib/ttt/game.rb', line 21

def next_move
  return nil unless ai_move?
  input = current_player.move(:board => board)
  mark_move(input)
  record_move(input)
  switch_player
  input
end

#not_finished?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ttt/game.rb', line 78

def not_finished?
  !board.finished?
end

#record_move(cell, side = current_player.side) ⇒ Object



38
39
40
# File 'lib/ttt/game.rb', line 38

def record_move(cell, side = current_player.side)
  history.record_move(cell, side)
end

#show_historyObject



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

def show_history
  history.show
end

#switch_playerObject



58
59
60
61
62
63
64
# File 'lib/ttt/game.rb', line 58

def switch_player
  if player1.side == current_player.side
    self.current_player = self.player2
  else
    self.current_player = self.player1
  end
end

#valid_move?(input) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ttt/game.rb', line 30

def valid_move?(input)
  input.class == Fixnum && 0 <= input && input <= (board[].length - 1) && board.free?(input.to_i)
end

#which_boardObject



17
18
19
# File 'lib/ttt/game.rb', line 17

def which_board
  board.board_type
end

#which_current_player?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/ttt/game.rb', line 66

def which_current_player?
  current_player.equal?(player1) ? "Player 1" : "Player 2"
end

#winner?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ttt/game.rb', line 82

def winner?
  board.winner?
end