-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.rb
More file actions
195 lines (167 loc) · 4.28 KB
/
view.rb
File metadata and controls
195 lines (167 loc) · 4.28 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
class Board
attr_accessor :active_board, :winning_combinations, :played_moves
attr_reader :board_positions
def initialize(board_size)
@board_size = board_size
@rows = find_rows
@board_positions = (1..square_board).to_a.map{|n| n.to_s}
@active_board = Array.new(square_board, " ")
@winning_combinations = []
find_winning_combinations(@board_positions)
@played_moves = []
end
def find_rows
Math.sqrt(@board_size).floor
end
def square_board
@rows**2
end
def separate_rows(board)
board.each_slice(@rows).to_a
end
def separate_columns(board)
separate_rows(board).transpose
end
def add_leading_space(slice)
slice = slice.map do |number|
if number.to_i / 10 < 1
number = " " + number
else
number
end
end
end
def last_row(index)
index == (@rows - 1)
end
def format_row(row)
row.compact.join("")
end
def print_board(board)
slices = separate_rows(board)
row_fillers = Array.new((@rows - 1), " |")
slices.each.with_index do |slice, index|
slice = add_leading_space(slice)
puts format_row(slice.zip(row_fillers))
unless last_row(index)
puts "===" + ("+===" * (@rows - 1))
end
end
puts
end
def position_to_index(line)
line = line.map do |position|
position = (position.to_i - 1)
end
end
def add_to_winning_combinations(combinations)
combinations.each do |combination|
@winning_combinations << combination
end
end
def find_winning_rows(board)
rows = separate_rows(board)
rows.map do |row|
position_to_index(row)
end
end
def find_winning_columns(board)
columns = separate_columns(board)
columns.map do |column|
position_to_index(column)
end
end
def find_winning_diagonals(board)
indexed_board = []
separate_rows(board).each do |row|
row = position_to_index(row)
indexed_board << row
end
diagonals = (0..(indexed_board.length - 1)).collect { |i| indexed_board[i][i]}, (0..(indexed_board.length - 1)).collect { |i| indexed_board.reverse[i][i]}
end
def find_winning_combinations(board)
add_to_winning_combinations(find_winning_rows(board))
add_to_winning_combinations(find_winning_columns(board))
add_to_winning_combinations(find_winning_diagonals(board))
@winning_combinations
end
def place(player, move)
return false if !valid_move?(move)
return false if @played_moves.include?(move)
@played_moves << move
active_board[move - 1] = player.mark
end
def valid_move?(move)
move.between?(1, square_board)
end
def open_space?(move)
@active_board[move - 1] == " "
end
def random_move
rand(1..(square_board))
end
def has_center_space?
square_board % 2 != 0
end
def find_center_space
if has_center_space?
(square_board / 2) + 1
end
end
def center_space_empty?
center_space = find_center_space
@active_board[center_space] == " "
end
def first_move
if center_space_empty?
find_center_space
else
random_move
end
end
def next_move_win(player)
@winning_combinations.each do |combo|
row_spots = combo - player.player_board(@active_board)
if row_spots.length == 1
move = row_spots.join.to_i + 1
if open_space?(move)
return move
end
end
end
false
end
def best_available(players, current_computer_player)
opponent = players.find{|player| player != current_computer_player}
player_win = next_move_win(current_computer_player)
opponent_block = next_move_win(opponent)
p "Player win #{player_win}"
p "Opponent block #{opponent_block}"
if player_win
p "WIN"
return player_win
elsif opponent_block
p "BLOCK"
return opponent_block
else
p "RANDOM"
random_move
end
end
def winner(players)
@winning_combinations.each do |combo|
if (combo - players[0].player_board(@active_board)).empty?
return players[0].name
elsif (combo - players[1].player_board(@active_board)).empty?
return players[1].name
end
end
return false
end
def tie?(players)
!winner(players) && !(@active_board.include?(" "))
end
def game_finished?(players)
winner(players) || tie?(players)
end
end