Class: Board

Inherits:
Object
  • Object
show all
Defined in:
lib/ultimate_tic_tac_toe/board.rb

Constant Summary collapse

HUMAN =
"X"
COMPUTER =
"O"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid_size) ⇒ Board

Returns a new instance of Board.



8
9
10
11
12
# 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

Instance Attribute Details

#gridObject

Returns the value of attribute grid.



6
7
8
# File 'lib/ultimate_tic_tac_toe/board.rb', line 6

def grid
  @grid
end

#playerObject

Returns the value of attribute player.



6
7
8
# File 'lib/ultimate_tic_tac_toe/board.rb', line 6

def player
  @player
end

#possible_boardsObject

Returns the value of attribute possible_boards.



6
7
8
# File 'lib/ultimate_tic_tac_toe/board.rb', line 6

def possible_boards
  @possible_boards
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/ultimate_tic_tac_toe/board.rb', line 6

def size
  @size
end

Instance Method Details

#computer_movesObject



88
89
90
# File 'lib/ultimate_tic_tac_toe/board.rb', line 88

def computer_moves
	@grid.count(COMPUTER)
end

#current_playerObject



80
81
82
# 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) ⇒ Object



59
60
61
# 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) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ultimate_tic_tac_toe/board.rb', line 55

def occupied?(grid, location)
	grid[location].nil? ? false : grid[location]
end

#player_movesObject



84
85
86
# File 'lib/ultimate_tic_tac_toe/board.rb', line 84

def player_moves
	@grid.count(HUMAN)
end

#simulate_move(board, position, player) ⇒ Object



75
76
77
78
# 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) ⇒ Object



71
72
73
# File 'lib/ultimate_tic_tac_toe/board.rb', line 71

def store_position(position, participant)
	@grid[position] = participant
end

#take_turn(player) ⇒ Object



67
68
69
# File 'lib/ultimate_tic_tac_toe/board.rb', line 67

def take_turn(player)
	player == HUMAN ? COMPUTER : HUMAN
end

#tie?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# 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) ⇒ Object



63
64
65
# File 'lib/ultimate_tic_tac_toe/board.rb', line 63

def unoccupied(position)
	@grid[position].nil?
end

#winner?(player) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_possibilitiesObject



14
15
16
17
18
19
20
21
22
# 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