-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttt.rb
448 lines (376 loc) · 9.63 KB
/
ttt.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
def tic_tac_toe
board = [_,_,_,
_,_,_,
_,_,_]
display board
puts
puts "Who should go first? Type \"player\" or \"computer\""
puts "without the quotation marks to make your choice."
action( board, first_move )
end
def display( board )
system 'cls'
linewidth = 50
puts(' a b c'.center(linewidth))
puts
for row in 1..3
offset = (row - 1) * 3
puts("#{row}: #{board[offset]}|#{board[offset + 1]}|#{board[offset + 2]}".center(linewidth))
end
end
def action( board, turn)
until check_win( board ).first
if turn == :player
player_action board
turn = next_player turn
elsif turn == :computer
computer_action board
turn = next_player turn
else
puts "turn variable in action method has fucked up"
end
end
display board
puts "#{check_win(board).last} won!"
puts "Play again? (yes/no)"
replay = gets.chomp
if replay =~ /\A[y|yes]\z/i
tic_tac_toe
elsif replay =~ /\A[n|no]\z/i
puts "Bye!"
else
puts "I didn't understand that. Here's a picture of a cat!"
puts
puts " )\\._.,--....,'``. "
puts " .b--. /; _.. \\ _\\ (`._ ,. "
puts "`=,-,-'~~~ `----(,_..'--(,_..'`-.;.' "
puts
end
end
def player_action( board )
# Prompts player for action and marks spot on board
display( board )
puts
puts "Hey hey playa. What's your move?"
puts "Please use the form a1, b2, c3, etc."
prompt
selection = gets.chomp.scan(/\A[abc][123]/).first
if selection.nil?
puts "I didn't understand that. Press enter to try again."
gets
player_action(board)
elsif board[to_index(selection)] == "_"
board[to_index(selection)] = "x"
else
puts "You can't rewrite the board!"
gets
player_action(board)
end
end
def computer_action( board )
# Computer's move
# Insert some sort of AI wizardry
hal( board )
display( board )
puts
puts "Computer: Resistance is futile"
gets
end
def check_win board
ooo = "ooo"
xxx = "xxx"
for index in 0..2
rows = add_row( board, index )
cols = add_col( board, index )
if rows == xxx
return [true, "The player"]
elsif rows == ooo
return [true, "The computer"]
elsif cols == xxx
return [true, "The player"]
elsif cols == ooo
return [true, "The computer"]
end
end
fwd_diag = add_fwd_diag( board )
back_diag = add_back_diag( board )
if fwd_diag == xxx
return [true, :player]
elsif fwd_diag == ooo
return [true, :computer]
elsif back_diag == xxx
return [true, :player]
elsif back_diag == ooo
return [true, :computer]
elsif board.index("_").nil?
return [true, "Nobody"]
else
return [false, false]
end
end
# check_win helpers. Also seen in near_win?().
def add_row( array, index )
offset = index * 3
array[offset] + array[offset + 1] + array[offset + 2]
end
def add_col( array, index )
array[index] + array[index + 3] + array[index + 6]
end
def add_fwd_diag( array )
array[0] + array[4] + array[8]
end
def add_back_diag( array )
array[2] + array[4] + array[6]
end
# Computer player logic. We'll call her...HAL.
def hal( board )
if near_win(board, :computer)
attack(board)
elsif near_win(board, :player)
defend(board)
# elsif check_trap(board) == [true, :computer]
# set_trap(board)
# elsif check_trap(board) == [true, :player]
# block_trap(board)
else
empty_spots = find_all_moves(board, :blank)
board[empty_spots.shuffle.last] = "o"
end
end
# These are kind of extraneous.
def defend( board )
plug_hole( board, :player)
end
def attack( board )
plug_hole( board, :computer )
end
def check_trap( board )
all_moves = find_all_moves(board, :all)
computer_moves = find_all_moves(board, :computer)
player_moves = find_all_moves(board, :player)
if all_moves.length == 1 && player_moves.length == 1
return[true, :player]
elsif player_moves.length == 2 && computer_moves.length == 1
return[true, :player]
elsif all_moves.length == 0
return[true, :computer]
elsif player_moves.length == 1 && computer_moves.length == 1
return[true, :computer]
elsif player_moves.length == 1 && computer_moves.length == 1
return[true, :computer]
elsif player_moves.length == 2 && computer_moves.length == 2
return[true, :computer]
else
return false
end
end
def set_trap( board )
all_moves = find_all_moves(board, :all)
computer_moves = find_all_moves(board, :computer)
player_moves = find_all_moves(board, :player)
avail_moves = find_all_moves(board, :blank)
corners = [0, 2, 6, 8]
sides = [1, 3, 5, 7]
center = 4
if all_moves.length == 0
starter = rand(2)
board[corners.shuffle.last] = "o" if starter == 0
board[center] = "o" if starter == 3 || starter == 1
elsif all_moves.length == 2 && computer_moves.length == 1
if (computer_moves & corners).first
if (player_moves & side).first
board[find_opposite(computer_moves.first)] = "o"
elsif (player_moves & corners).first
if player_moves == find_opposite(computer_moves)
place_any(board,corners)
else
place_any(board,center)
end
elsif player_moves.first == center
place_any(board, computer_moves)
else
puts "The player has failed to make a move?"
end
elsif computer_moves == [center]
if (player_moves & corners).first
place_any(find_opposite(player_moves))
elsif (player_moves & sides).first
place_any_except(find_opposite(player_moves))
else
puts "The player isn't in the side or in the corners."
end
else
puts "If the computer is in a side location, I'ma be pissed."
end
# elsif computer_moves.length == 2
# else
# No else, really.
end
end
def find_opposite( location )
if location.is_a? Array
opposites = []
location.each do |spot|
opposites.push( 0-(spot+1) )
end
return opposites
elsif location.is_a? Fixnum
return 0 - (location+1)
else
puts "Input incorrect. Need Fixnum or Array"
end
end
def block_trap( board )
all_moves = find_all_moves(board, :all)
computer_moves = find_all_moves(board, :computer)
player_moves = find_all_moves(board, :player)
avail_moves = find_all_moves(board, :blank)
corners = [0, 2, 6, 8]
sides = [1, 3, 5, 7]
center = 4
# This violates DRY like crazy.
# This is why I need to learn to use classes. NOOB!
end
# Corners - top left, top right, bottom left, bottom right.
# Sides - top, left, right, bottom
def categorize( array )
hash = {
:corners => [ array[0], array[2], array[6], array[8] ],
:sides => [ array[1], array[3], array[5], array[7] ],
:center => array[4]
}
end
def plug_hole( board, agent)
almost = near_win(board, agent)
if almost
board[ almost ] = "o"
end
end
def find_all_moves( board, agent )
locations = []
copy = board.clone
if agent == :computer
token = "o"
elsif agent == :player
token = "x"
elsif agent == :blank
token = "_"
elsif agent == :all
return find_all_moves(board, :computer) + find_all_moves(board, :player)
else
puts "Input for find_all_moves is incorrect"
end
until copy.index(token).nil?
locations << copy.index(token)
copy[copy.index(token)] = " "
end
return locations
end
def near_win( board, agent )
if agent == :player
test = /x{2}|x_x/
elsif agent == :computer
test = /o{2}|o_o/
end
danger_zone = 0 # Returned and used as array index
# The unless loops prevent failure when rows/cols/diags
# are full. Ex. "xoo"
for index in 0..2
rows = add_row( board, index )
cols = add_col( board, index )
x_coord = rows.index("_")
y_coord = cols.index("_")
if rows =~ test
unless x_coord.nil?
danger_zone = index * 3 + x_coord
return danger_zone
end
elsif cols =~ test
unless y_coord.nil?
danger_zone = index + y_coord * 3
return danger_zone
end
end
end
fwd_diag = add_fwd_diag( board )
back_diag = add_back_diag( board )
if fwd_diag =~ test
unless fwd_diag.index("_").nil?
danger_zone = fwd_diag.index("_") * 4
return danger_zone
end
elsif back_diag =~ test
unless back_diag.index("_").nil?
danger_zone = (back_diag.index("_") + 1) * 2
return danger_zone
end
else
return false
end
end
# Bits and pieces.
def first_move
prompt
choice = gets.chomp
if choice =~ /\A[pP]layer\z/
choice = :player
elsif choice =~ /\A[cC]omputer\z/
choice = :computer
else
puts "Please enter a valid selection."
first_move
end
end
def next_player( turn )
if turn == :player
turn = :computer
elsif turn == :computer
turn = :player
else
puts "ERROR: Turn must be computer or player."
return nil
end
end
def to_index ( selection )
index = 0
case selection[0]
when "a"
index += 0
when "b"
index += 1
when "c"
index += 2
else
puts 'selection[0] is malformed'
end
case selection[1]
when "1"
index += 0
when "2"
index += 3
when "3"
index += 6
else
puts 'selection[1] is malformed'
end
return index
end
def prompt
print "> "
end
def place_any_except( board, *here )
available = find_all_moves(board, :blank)
possible = []
here.each do |dont_moves|
possible || (available - dont_moves)
end
board[possible.shuffle.last] = "o"
end
def place_any( board, *here )
available = find_all_moves(board, :blank)
possible = []
here.each do |moves|
possible || (available & moves)
end
board[possible.shuffle.last] = "o"
end