Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/ang_ttt_gem/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Instance Method Summary collapse
- #create_computer_player(mark) ⇒ Object
- #create_human_player(mark) ⇒ Object
- #gather_board_state ⇒ Object
- #get_player_move(player) ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #is_over? ⇒ Boolean
- #make_move(move, mark) ⇒ Object
- #make_move_player(player, move) ⇒ Object
- #move_valid?(move) ⇒ Boolean
- #player_can_make_move?(player) ⇒ Boolean
- #prepare_display_state ⇒ Object
- #result ⇒ Object
- #square_taken?(cell_number) ⇒ Boolean
Constructor Details
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
10 11 12 |
# File 'lib/ang_ttt_gem/game.rb', line 10 def board @board end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
10 11 12 |
# File 'lib/ang_ttt_gem/game.rb', line 10 def players @players end |
Instance Method Details
#create_computer_player(mark) ⇒ Object
19 20 21 |
# File 'lib/ang_ttt_gem/game.rb', line 19 def create_computer_player(mark) @players << ComputerPlayer.new(mark) end |
#create_human_player(mark) ⇒ Object
23 24 25 |
# File 'lib/ang_ttt_gem/game.rb', line 23 def create_human_player(mark) @players << HumanPlayer.new(mark) end |
#gather_board_state ⇒ Object
64 65 66 |
# File 'lib/ang_ttt_gem/game.rb', line 64 def gather_board_state @board.current_state end |
#get_player_move(player) ⇒ Object
27 28 29 30 |
# File 'lib/ang_ttt_gem/game.rb', line 27 def get_player_move(player) index = player - 1 @players[index].get_move(@board) end |
#is_over? ⇒ Boolean
68 69 70 |
# File 'lib/ang_ttt_gem/game.rb', line 68 def is_over? @scoring.winner?(@board) || @scoring.draw?(@board) end |
#make_move(move, mark) ⇒ Object
50 51 52 |
# File 'lib/ang_ttt_gem/game.rb', line 50 def make_move(move,mark) @board.set(move, mark) end |
#make_move_player(player, move) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/ang_ttt_gem/game.rb', line 42 def make_move_player(player, move) index = player - 1 mark = @players[index].mark false unless move_valid?(move) make_move(move,mark) prepare_display_state end |
#move_valid?(move) ⇒ Boolean
38 39 40 |
# File 'lib/ang_ttt_gem/game.rb', line 38 def move_valid?(move) (0..8).include?(move) end |
#player_can_make_move?(player) ⇒ Boolean
32 33 34 35 36 |
# File 'lib/ang_ttt_gem/game.rb', line 32 def player_can_make_move?(player) move = get_player_move(player) return true if move != false return false if move == false end |
#prepare_display_state ⇒ Object
58 59 60 61 62 |
# File 'lib/ang_ttt_gem/game.rb', line 58 def prepare_display_state gather_board_state.each_with_index.map do |cell, index| cell == " " ? (index + 1).to_s : cell end end |
#result ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ang_ttt_gem/game.rb', line 72 def result = :draw @players.each_with_index do |player, i| num = i + 1 if @scoring.winning_mark(@board) == player.mark = "player_#{num}_win".to_sym end end end |
#square_taken?(cell_number) ⇒ Boolean
54 55 56 |
# File 'lib/ang_ttt_gem/game.rb', line 54 def square_taken?(cell_number) @board.cell_occupied?(cell_number) end |