Class: TTT::Game
- Inherits:
-
Object
- Object
- TTT::Game
- Defined in:
- lib/ttt/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#db ⇒ Object
Returns the value of attribute db.
-
#history ⇒ Object
Returns the value of attribute history.
-
#player1 ⇒ Object
Returns the value of attribute player1.
-
#player2 ⇒ Object
Returns the value of attribute player2.
Instance Method Summary collapse
- #adjust_move_index(index_diff) ⇒ Object
- #ai_move? ⇒ Boolean
- #board_arr ⇒ Object
- #draw? ⇒ Boolean
- #finished? ⇒ Boolean
- #get_history_board(move_number_limit = history.move_traverser.move_index) ⇒ Object
-
#initialize(options) ⇒ Game
constructor
A new instance of Game.
- #initialize_history ⇒ Object
- #last_player ⇒ Object
- #mark_move(cell, side = current_player.side) ⇒ Object
- #next_move ⇒ Object
- #not_finished? ⇒ Boolean
- #record_move(cell, side = current_player.side) ⇒ Object
- #show_history ⇒ Object
- #switch_player ⇒ Object
- #valid_move?(input) ⇒ Boolean
- #which_board ⇒ Object
- #which_current_player? ⇒ Boolean
- #winner? ⇒ Boolean
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() self.board = [:board] self.player1 = [:player1] self.player2 = [:player2] self.current_player = self.player1 self.history = .fetch(:history) end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
3 4 5 |
# File 'lib/ttt/game.rb', line 3 def board @board end |
#current_player ⇒ Object
Returns the value of attribute current_player.
3 4 5 |
# File 'lib/ttt/game.rb', line 3 def current_player @current_player end |
#db ⇒ Object
Returns the value of attribute db.
3 4 5 |
# File 'lib/ttt/game.rb', line 3 def db @db end |
#history ⇒ Object
Returns the value of attribute history.
3 4 5 |
# File 'lib/ttt/game.rb', line 3 def history @history end |
#player1 ⇒ Object
Returns the value of attribute player1.
3 4 5 |
# File 'lib/ttt/game.rb', line 3 def player1 @player1 end |
#player2 ⇒ Object
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
13 14 15 |
# File 'lib/ttt/game.rb', line 13 def ai_move? current_player.no_gui? end |
#board_arr ⇒ Object
90 91 92 |
# File 'lib/ttt/game.rb', line 90 def board_arr board[] end |
#draw? ⇒ Boolean
86 87 88 |
# File 'lib/ttt/game.rb', line 86 def draw? board.draw_game? end |
#finished? ⇒ 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_history ⇒ Object
54 55 56 |
# File 'lib/ttt/game.rb', line 54 def initialize_history history.initialize_history end |
#last_player ⇒ Object
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_move ⇒ Object
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
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_history ⇒ Object
42 43 44 |
# File 'lib/ttt/game.rb', line 42 def show_history history.show end |
#switch_player ⇒ Object
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
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_board ⇒ Object
17 18 19 |
# File 'lib/ttt/game.rb', line 17 def which_board board.board_type end |
#which_current_player? ⇒ 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
82 83 84 |
# File 'lib/ttt/game.rb', line 82 def winner? board.winner? end |