class C4::Model::Game
Constants
- COLUMNS
- P1
- P2
- ROWS
- WINNING_LENGTH
Attributes
board[R]
current_player_index[R]
players[R]
Public Class Methods
new()
click to toggle source
# File lib/c4/model/game.rb, line 20 def initialize @board = Board.new(ROWS, COLUMNS) @players = [P1, P2] @current_player_index = 0 end
Public Instance Methods
current_player()
click to toggle source
# File lib/c4/model/game.rb, line 31 def current_player players[current_player_index] end
impasse?()
click to toggle source
# File lib/c4/model/game.rb, line 35 def impasse? board.full? end
play!(column)
click to toggle source
# File lib/c4/model/game.rb, line 26 def play!(column) board.put_stone!(column, current_player) toggle_player end
winner()
click to toggle source
# File lib/c4/model/game.rb, line 39 def winner players.find do |player| player if valid_streak_exists?(player) end end
Private Instance Methods
diagonal_streak_exists?(player)
click to toggle source
# File lib/c4/model/game.rb, line 67 def diagonal_streak_exists?(player) board.diagonals.any? do |row| row.streak?(player, WINNING_LENGTH) end end
horizontal_streak_exists?(player)
click to toggle source
# File lib/c4/model/game.rb, line 61 def horizontal_streak_exists?(player) board.to_a(:row_wise).any? do |row| row.streak?(player, WINNING_LENGTH) end end
toggle_player()
click to toggle source
# File lib/c4/model/game.rb, line 47 def toggle_player @current_player_index = (current_player_index + 1) % 2 end
valid_streak_exists?(player)
click to toggle source
# File lib/c4/model/game.rb, line 51 def valid_streak_exists?(player) vertical_streak_exists?(player) || horizontal_streak_exists?(player) || diagonal_streak_exists?(player) end
vertical_streak_exists?(player)
click to toggle source
# File lib/c4/model/game.rb, line 55 def vertical_streak_exists?(player) board.to_a(:column_wise).any? do |column| column.streak?(player, WINNING_LENGTH) end end