Class: ThreeDimensionalBoard

Inherits:
Board
  • Object
show all
Defined in:
lib/three_dimensional_board.rb

Instance Attribute Summary

Attributes inherited from Board

#row_length

Instance Method Summary collapse

Methods inherited from Board

#board_is_full?, #check_for_tie, #game_over?, #get_open_spots, #record_choice, #remove_choice, #reset_board, #valid_choice?, #win_is_found

Constructor Details

#initialize(size) ⇒ ThreeDimensionalBoard

Returns a new instance of ThreeDimensionalBoard.



5
6
7
8
9
# File 'lib/three_dimensional_board.rb', line 5

def initialize size
   @board_array = Array.new(size**3, :open)
   @row_length = size
   @row_length_squared = size**2
end

Instance Method Details

#angled_through_center_diagonals_check(board) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/three_dimensional_board.rb', line 182

def angled_through_center_diagonals_check board
	#scores diagonals starting at a front corner, angling through center,
	#ending at back, opposite corner of cube
	if board[0] != :open && board[0] == board[13] && board[13] == board[26]
		return board[0]
	elsif board[2] != :open && board[2] == board[13] && board[13] == board[24]
		return board[2]
	elsif board[6] != :open && board[6] == board[13] && board[13] == board[20]
		return board[6]
	elsif board[8] != :open && board[8] == board[13] && board[13] == board[18]
		return board[8]
	end
	return :continue_game
end

#check_board_statusObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/three_dimensional_board.rb', line 20

def check_board_status
	result = :continue_game
	if (column_result = check_column_win(@board_array)) != :continue_game 
		result = column_result
	elsif (row_result = check_rows_for_win(@board_array)) != :continue_game
		result = row_result
	elsif (diag_result = check_diagonal_wins(@board_array)) != :continue_game
		result = diag_result
	elsif (tie_result = check_for_tie) != :continue_game
		result = tie_result
	end
	return result
end

#check_bottom_top_diagonal(board) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/three_dimensional_board.rb', line 117

def check_bottom_top_diagonal board
	index = 6
	@row_length.times do
		if board[index] != :open
			if board[index] == board[index-2] && board[index-2] == board[index-4]
				return board[index]
			end
		end
		index += 9
	end
	return :continue_game
end

#check_column_win(board) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/three_dimensional_board.rb', line 34

def check_column_win board
	start_index = 0
	@row_length.times do
		@row_length.times do
			if board[start_index] != :open
				if board[start_index] == board[start_index + 3] && board[start_index+3] == board[start_index + 6]
					return board[start_index]
				end
			end
			start_index += 1
		end
		start_index += 6
	end
	return :continue_game
end

#check_diagonal_wins(board) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/three_dimensional_board.rb', line 83

def check_diagonal_wins board
	if (result = check_top_bottom_diagonal(board)) != :continue_game 
		return result
	elsif (result = check_bottom_top_diagonal(board)) != :continue_game
		return result
	elsif (result = horizontal_left_right_diagonal_check(board)) != :continue_game
		return result
	elsif (result = horizontal_right_left_diagonal_check(board)) != :continue_game
		return result
	elsif (result = vertical_top_bottom_diagonal_check(board)) != :continue_game
		return result
	elsif (result = vertical_bottom_top_diagonal_check(board)) != :continue_game
		return result
	elsif (result = angled_through_center_diagonals_check(board)) != :continue_game
		return result 
	else return :continue_game end	
end

#check_front_to_back_row_win(board) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/three_dimensional_board.rb', line 75

def check_front_to_back_row_win board
    for row_number in 0..(@row_length**2)
        row = get_front_back_row(row_number,board).uniq
        return row.pop if win_is_found(row) 
    end
    :continue_game
end

#check_left_right_row_for_win(board) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/three_dimensional_board.rb', line 58

def check_left_right_row_for_win board
    for row_number in 0...(@row_length**2)
        row = get_row(row_number, board).uniq
        return row.pop if win_is_found(row)
    end
    :continue_game
end

#check_rows_for_win(board) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/three_dimensional_board.rb', line 50

def check_rows_for_win board
	if (result = check_left_right_row_for_win(board)) != :continue_game
		return result
	elsif (result = check_front_to_back_row_win(board)) != :continue_game
		return result
	else return :continue_game end
end

#check_top_bottom_diagonal(board) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/three_dimensional_board.rb', line 103

def check_top_bottom_diagonal board
    for diag_number in 0...@row_length
        index = diag_number*@row_length_squared
        sub_board = board.slice(index, @row_length_squared)
        diagonal = get_top_bottom_diagonal(index, sub_board).uniq
        return diagonal.pop if win_is_found(diagonal)
    end
    :continue_game
end

#get_front_back_row(start_index, board) ⇒ Object



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

def get_front_back_row start_index, board
    board.select.with_index{|x,i| x if (i-start_index) % (@row_length**2) == 0}
end

#get_print_resultsObject



11
12
13
14
# File 'lib/three_dimensional_board.rb', line 11

def get_print_results
	#row length needed to show correct 2d version of @board_array array
	[@board_array, 3]
end

#get_row(row_number, board) ⇒ Object



66
67
68
69
# File 'lib/three_dimensional_board.rb', line 66

def get_row row_number, board
    start_index = row_number * @row_length
    board.slice(start_index, @row_length)
end

#get_top_bottom_diagonal(start_index, board) ⇒ Object



113
114
115
# File 'lib/three_dimensional_board.rb', line 113

def get_top_bottom_diagonal start_index, board
		board.select.with_index {|x,i| x if i % (@row_length+1) == 0}
end

#get_typeObject



16
17
18
# File 'lib/three_dimensional_board.rb', line 16

def get_type
    :three_dimensional_board
end

#horizontal_left_right_diagonal_check(board) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/three_dimensional_board.rb', line 130

def horizontal_left_right_diagonal_check board
	index = 0
	@row_length.times do
		if board[index] != :open
			if board[index] == board[index+10] && board[index+10] == board[index+20]
				return board[index]
			end
		end
		index += 3
	end
	return :continue_game
end

#horizontal_right_left_diagonal_check(board) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/three_dimensional_board.rb', line 143

def horizontal_right_left_diagonal_check board
	index = 2
	@row_length.times do
		if board[index] != :open
			if board[index] == board[index+8] && board[index+8] == board[index+16]
				return board[index]
			end
		end
		index += 3
	end
	return :continue_game
end

#vertical_bottom_top_diagonal_check(board) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/three_dimensional_board.rb', line 169

def vertical_bottom_top_diagonal_check board
	index = 6
	@row_length.times do
		if board[index] != :open
			if board[index] == board[index+6] && board[index+6] == board[index+12]
				return board[index]
			end
		end
		index += 1
	end
	return :continue_game
end

#vertical_top_bottom_diagonal_check(board) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/three_dimensional_board.rb', line 156

def vertical_top_bottom_diagonal_check board
	index = 0
	@row_length.times do
		if board[index] != :open
			if board[index] == board[index+12] && board[index+12] == board[index+24]
				return board[index]
			end
		end
		index += 1
	end
	return :continue_game
end