class Board
Constants
- COMPUTER
- HUMAN
Attributes
grid[RW]
player[RW]
possible_boards[RW]
size[RW]
Public Class Methods
new(grid_size)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 8 def initialize(grid_size) @grid = Array.new(grid_size) @size = @grid.length @possible_boards = [] end
Public Instance Methods
computer_moves()
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 88 def computer_moves @grid.count(COMPUTER) end
current_player()
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 80 def current_player @grid.count(HUMAN) > @grid.count(COMPUTER) ? COMPUTER : HUMAN end
get_move_list(grid)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 59 def get_move_list(grid) grid.each_index.select{|index| grid[index].nil?} end
occupied?(grid, location)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 55 def occupied?(grid, location) grid[location].nil? ? false : grid[location] end
player_moves()
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 84 def player_moves @grid.count(HUMAN) end
simulate_move(board, position, player)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 75 def simulate_move(board, position, player) board.grid[position] = player board end
store_position(position, participant)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 71 def store_position(position, participant) @grid[position] = participant end
take_turn(player)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 67 def take_turn(player) player == HUMAN ? COMPUTER : HUMAN end
tie?()
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 47 def tie? if (player_moves + computer_moves) == grid.length && !winner?("X") && !winner?("O") true else false end end
unoccupied(position)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 63 def unoccupied(position) @grid[position].nil? end
winner?(player)
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 24 def winner?(player) if grid.length == 9 winning_possibilities.each do |win| if grid[win[0]] == player && grid[win[1]] == player && grid[win[2]] == player return true end end elsif grid.length == 16 winning_possibilities.each do |win| if grid[win[0]] == player && grid[win[1]] == player && grid[win[2]] == player && grid[win[3]] == player return true end end elsif grid.length == 25 winning_possibilities.each do |win| if grid[win[0]] == player && grid[win[1]] == player && grid[win[2]] == player && grid[win[3]] == player && grid[win[4]] == player return true end end end false end
winning_possibilities()
click to toggle source
# File lib/ultimate_tic_tac_toe/board.rb, line 14 def winning_possibilities if grid.length == 9 [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]] elsif grid.length == 16 [[0,1,2,3], [4,5,6,7], [8,9,10,11],[12,13,14,15], [0,5,10,15], [3,6,9, 12], [0,4,8,12], [1,5,9,13], [2,6,10,14], [3,7,11,15]] elsif grid.length == 25 [[0,1,2,3,4], [5,6,7,8,9],[10,11,12,13,14],[15,16,17,18,19], [20,21,22,23,24,25], [0,5,10,15,20], [1,6,11,16,21], [2,7,12,17,22], [3,8,13,18,23],[4,9,14,19,24], [0,6,12,18,24],[4,8,12,16,20]] end end