forked from OwenG7/Noughts-and-Crosses
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnacstudentsempty.py
619 lines (496 loc) · 21.1 KB
/
nacstudentsempty.py
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# this module is used to create the game user interface
import pygame
# this module is used to make HTTP requests to your machine learning model
import requests
# this module is used to choose a random colour for the user interface and
# make random choices about moves the computer should make
import random
# this module is used to interact with your machine learning project
from mlforkidsnumbers import MLforKidsNumbers
project = MLforKidsNumbers(
# keys and URLs specific to your project will be added here
)
############################################################################
# Constants that match names in your Machine Learning project
############################################################################
# descriptions of the contents of a space on the game board
EMPTY = "EMPTY"
OPPONENT = "OPPONENT" # for the human, the OPPONENT is the computer
# for the computer, the OPPONENT is the human
PLAYER = "PLAYER" # for the human, the PLAYER is the human
# for the computer, the PLAYER is the computer
# descriptions of the locations on the game board
top_left = "top_left"
top_middle = "top_middle"
top_right = "top_right"
middle_left = "middle_left"
middle_middle = "middle_middle"
middle_right = "middle_right"
bottom_left = "bottom_left"
bottom_middle = "bottom_middle"
bottom_right = "bottom_right"
#
############################################################################
############################################################################
# Converting between labels and numeric values
############################################################################
# training examples refer to a location on the game board
deconvert = {}
deconvert[top_left] = 0
deconvert[top_middle] = 1
deconvert[top_right] = 2
deconvert[middle_left] = 3
deconvert[middle_middle] = 4
deconvert[middle_right] = 5
deconvert[bottom_left] = 6
deconvert[bottom_middle] = 7
deconvert[bottom_right] = 8
############################################################################
# Machine Learning functions
############################################################################
# who the two players are
HUMAN = "HUMAN"
COMPUTER = "COMPUTER"
# Storing a record of what has happened so the computer can learn from it!
# contents of the board at each stage in the game
gamehistory = {
HUMAN : [],
COMPUTER : []
}
# decisions made by each player
decisions = {
HUMAN : [],
COMPUTER : []
}
# Use your machine learning model to decide where the
# computer should move next.
#
# board : list of board spaces with the current state of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
def classify(board):
debug("Predicting the next best move for the computer")
if project.has_model():
# get the current state of the game board
state = get_board_from_perspective(board, COMPUTER)
testvalue = {
"TopLeft" : state[0],
"TopMiddle" : state[1],
"TopRight" : state[2],
"MiddleLeft" : state[3],
"MiddleMiddle" : state[4],
"MiddleRight" : state[5],
"BottomLeft" : state[6],
"BottomMiddle" : state[7],
"BottomRight" : state[8]
}
# send the state of the game board to your machine learning model
predictions = project.classify(testvalue)
# responseData will contain the list of predictions made by the
# machine learning model, starting from the one with the most
# confidence, to the one with the least confidence
for prediction in predictions:
# we can't make a move unless the space is empty, so
# check that first
if is_space_empty(board, prediction["class_name"]):
return prediction
# If we're here, it means that we don't have a machine learning model,
# or possibly none of the predictions made by the model were
# actually empty!
# Pick a random space to move in
spaces = list(deconvert.keys())
for space in random.sample(spaces, len(spaces)):
# we can't make a move unless the space is empty, so
# check that first
if is_space_empty(board, space):
return { "class_name" : space }
# Add a move that resulted in a win to the training data for the
# machine learning model
#
# board : list of board spaces with the current state of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
# who : whose training data this is
# e.g. HUMAN
# name_of_space : name of the space that the move was in
# e.g. bottom_left
def add_to_train(board, who, name_of_space):
print ("Adding the move in %s by %s to the training data" % (name_of_space, who))
# convert the contents of the board into a list of whose symbol
# is in that space, from the perspective of 'who'
# e.g. [ PLAYER, OPPONENT, PLAYER, EMPTY, EMPTY, PLAYER, OPPONENT, PLAYER, OPPONENT ]
data = get_board_from_perspective(board, who)
# the location that they chose to make a move in
label = name_of_space
# add move to the machine learning project training data
project.store(data, label)
# Someone won the game.
# A machine learning model could learn from this...
#
# winner : who won - either HUMAN or COMPUTER
# boardhistory : the contents of the game board at each stage in the game
# winnerdecisions : each of the decisions that the winner made
def learn_from_this(winner, boardhistory, winnerdecisions):
print("%s won the game!" % (winner))
print("Maybe the computer could learn from %s's experience?" % (winner))
for idx in range(len(winnerdecisions)):
print("\nAt the start of move %d the board looked like this:" % (idx + 1))
print(boardhistory[idx])
print("And %s decided to put their mark in %s" % (winner, winnerdecisions[idx]))
############################################################################
# Noughts and Crosses logic
############################################################################
# get the location of a space on the board (an index from 0 to 8)
# using the lookup table 'deconvert'
#
# name_of_space : name of the space to check
# e.g. middle_right
def get_space_location(name_of_space):
# uses the default spelling if found
if name_of_space in deconvert:
return deconvert[name_of_space]
# otherwise tries the overrides
return deconvert[globals()[name_of_space]]
# gets the contents of a space on the board
#
# board : list of board spaces with the contents of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
# name_of_space : name of the space to check
# e.g. middle_right
def get_space_contents(board, name_of_space):
return board[get_space_location(name_of_space)]
# checks to see if a specific space on the board is currently empty
#
# board : list of board spaces with the contents of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
# name_of_space : name of the space to check
# e.g. middle_right
def is_space_empty(board, name_of_space):
return get_space_contents(board, name_of_space) == EMPTY
# Creates the initial state for the game
def create_empty_board():
debug("Creating the initial empty game board state")
return [ EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY ]
# Gets the contents of the board, from the perspective of either
# the human or the computer.
#
# board : list of board spaces with the current state of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
# who : either HUMAN or COMPUTER
#
# Returns the board described as PLAYER or OPPONENT
# e.g. [ PLAYER, OPPONENT, PLAYER, EMPTY, EMPTY, PLAYER, OPPONENT, PLAYER, OPPONENT ]
def get_board_from_perspective(board, who):
convertedboard = []
for move in board:
if move == EMPTY:
# an empty space is an empty space, from anyone's perspective
convertedboard.append(EMPTY)
else:
convertedboard.append(PLAYER if move == who else OPPONENT)
return convertedboard
############################################################################
# Noughts and Crosses user interface functions
############################################################################
# RGB colour codes
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
game_board_coordinates = {}
game_board_coordinates[top_left] = {
"bottom_left_corner": (120, 120),
"top_right_corner": (180, 180),
"top_left_corner": (180, 120),
"bottom_right_corner": (120, 180),
"centre": (150, 150)
}
game_board_coordinates[top_middle] = {
"bottom_left_corner": (220, 120),
"top_right_corner": (280, 180),
"top_left_corner": (220, 180),
"bottom_right_corner": (280, 120),
"centre": (250, 150)
}
game_board_coordinates[top_right] = {
"bottom_left_corner": (320, 120),
"top_right_corner": (380, 180),
"top_left_corner": (320, 180),
"bottom_right_corner": (380, 120),
"centre": (350, 150)
}
game_board_coordinates[middle_left] = {
"bottom_left_corner": (120, 220),
"top_right_corner": (180, 280),
"top_left_corner": (120, 280),
"bottom_right_corner": (180, 220),
"centre": (150, 250)
}
game_board_coordinates[middle_middle] = {
"bottom_left_corner": (220, 220),
"top_right_corner": (280, 280),
"top_left_corner": (220, 280),
"bottom_right_corner": (280, 220),
"centre": (250, 250)
}
game_board_coordinates[middle_right] = {
"bottom_left_corner": (320, 220),
"top_right_corner": (380, 280),
"top_left_corner": (320, 280),
"bottom_right_corner": (380, 220),
"centre": (350, 250)
}
game_board_coordinates[bottom_left] = {
"bottom_left_corner": (120, 320),
"top_right_corner": (180, 380),
"top_left_corner": (120, 380),
"bottom_right_corner": (180, 320),
"centre": (150, 350)
}
game_board_coordinates[bottom_middle] = {
"bottom_left_corner": (220, 320),
"top_right_corner": (280, 380),
"top_left_corner": (220, 380),
"bottom_right_corner": (280, 320),
"centre": (250, 350)
}
game_board_coordinates[bottom_right] = {
"bottom_left_corner": (320, 320),
"top_right_corner": (380, 380),
"top_left_corner": (320, 380),
"bottom_right_corner": (380, 320),
"centre": (350, 350)
}
# Check if someone has won and draws a line to show the winner
# if someone has won
#
# who : Who made the last move? (only need to check if they won
# as a player who hasn't just made a move can't have won)
# e.g. HUMAN or COMPUTER
#
# Returns true if someone won
# Returns false if noone won
def display_winner(screen, board, who):
debug("Checking if %s has won" % (who))
gameover = False
# we use a green line if the human wins, a red line if the computer does
linecolour = GREEN if who == HUMAN else RED
######## Rows ########
if get_space_contents(board, "top_left") == who and get_space_contents(board, "top_middle") == who and get_space_contents(board, "top_right") == who:
pygame.draw.line(screen, linecolour, (100, 150), (400, 150), 10)
gameover = True
if get_space_contents(board, "middle_left") == who and get_space_contents(board, "middle_middle") == who and get_space_contents(board, "middle_right") == who:
pygame.draw.line(screen, linecolour, (100, 250), (400, 250), 10)
gameover = True
if get_space_contents(board, "bottom_left") == who and get_space_contents(board, "bottom_middle") == who and get_space_contents(board, "bottom_right") == who:
pygame.draw.line(screen, linecolour, (100, 350), (400, 350), 10)
gameover = True
######## Columns ########
if get_space_contents(board, "top_left") == who and get_space_contents(board, "middle_left") == who and get_space_contents(board, "bottom_left") == who:
pygame.draw.line(screen, linecolour, (150, 100), (150, 400), 10)
gameover = True
if get_space_contents(board, "top_middle") == who and get_space_contents(board, "middle_middle") == who and get_space_contents(board, "bottom_middle") == who:
pygame.draw.line(screen, linecolour, (250, 100), (250, 400), 10)
gameover = True
if get_space_contents(board, "top_right") == who and get_space_contents(board, "middle_right") == who and get_space_contents(board, "bottom_right") == who:
pygame.draw.line(screen, linecolour, (350, 100), (350, 400), 10)
gameover = True
######## Diagonals #########
if get_space_contents(board, "top_left") == who and get_space_contents(board, "middle_middle") == who and get_space_contents(board, "bottom_right") == who:
pygame.draw.line(screen, linecolour, (100, 100), (400, 400), 15)
gameover = True
if get_space_contents(board, "bottom_left") == who and get_space_contents(board, "middle_middle") == who and get_space_contents(board, "top_right") == who:
pygame.draw.line(screen, linecolour, (400, 100), (100, 400), 15)
gameover = True
if gameover:
# refresh the display if we've drawn any game-over lines
pygame.display.update()
return gameover
# Redraw the UI with a different background colour
#
# board : list of board spaces with the contents of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
def redraw_screen(screen, colour, board):
debug("Changing the background colour")
# fill everything in the new background colour
screen.fill(colour)
# now we've covered everything, we need to redraw
# the game board again
draw_game_board(screen)
# now we need to redraw all of the moves that
# have been made
for spacename in deconvert.keys():
space_code = deconvert[spacename]
if board[space_code] == HUMAN:
draw_move(screen, spacename, "cross")
elif board[space_code] == COMPUTER:
draw_move(screen, spacename, "nought")
# refresh now we've made changes
pygame.display.update()
# Draw the crossed lines that make up a noughts and crosses board
def draw_game_board(screen):
pygame.draw.rect(screen, WHITE, (195, 100, 10, 300))
pygame.draw.rect(screen, WHITE, (295, 100, 10, 300))
pygame.draw.rect(screen, WHITE, (100, 195, 300, 10))
pygame.draw.rect(screen, WHITE, (100, 295, 300, 10))
# Setup the window that will be used to display the game
def prepare_game_window():
debug("Setting up the game user interface")
# sets up the pygame library we'll use to create the game
pygame.init()
# create a window that is 500 pixels wide and 500 pixels high
screen = pygame.display.set_mode((500, 500))
# set the title of the window
pygame.display.set_caption("Machine Learning Noughts and Crosses")
return screen
# Create a random RGB code to be used for the background colour
def generate_random_colour():
debug("Generating a random colour code")
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return [r, g, b]
# Draw a new move on the game board
#
# screen : The PyGame screen to draw the move on
# name_of_space : Name of the space to draw the move on
# e.g. middle_right
# move : The move to draw.
# It will be either "nought" or "cross"
def draw_move(screen, name_of_space, move):
debug("Drawing a move on the game board : %s in %s" % (move, name_of_space))
if move == "nought":
location = game_board_coordinates[name_of_space]["centre"]
pygame.draw.circle(screen, WHITE, location, 35 , 8)
elif move == "cross":
pygame.draw.line(screen, WHITE,
game_board_coordinates[name_of_space]["bottom_left_corner"],
game_board_coordinates[name_of_space]["top_right_corner"],
10)
pygame.draw.line(screen, WHITE,
game_board_coordinates[name_of_space]["top_left_corner"],
game_board_coordinates[name_of_space]["bottom_right_corner"],
10)
pygame.display.update()
# The user has clicked on the game board.
# Which space did they click on?
#
# mx : the x coordinate of their click
# my : the y coordiante of their click
#
# Returns the name of the space they clicked on (e.g. "middle_right")
def get_click_location(mx, my):
debug("Getting location of click in %d,%d" % (mx, my))
if 100 < mx < 400 and 100 < my < 400:
if my < 200:
if mx < 200:
return top_left
elif mx < 300:
return top_middle
else:
return top_right
elif my < 300:
if mx < 200:
return middle_left
elif mx < 300:
return middle_middle
else:
return middle_right
else:
if mx < 200:
return bottom_left
elif mx < 300:
return bottom_middle
else:
return bottom_right
return "none"
# Handle a new move, by either the player or the computer
#
# board : list of board spaces with the contents of each space
# e.g. [ HUMAN, COMPUTER, HUMAN, EMPTY, EMPTY, HUMAN, COMPUTER, HUMAN, COMPUTER ]
# name_of_space : name of the space the move was in
# e.g. middle_right
# identity : whose move this is
# e.g. HUMAN or COMPUTER
#
# returns true if this move ended the game
# returns false if the game should keep going
def game_move(screen, board, name_of_space, identity):
debug("Processing a move for %s who chose %s" % (identity, name_of_space))
# choose the symbol for which player this is
symbol = "cross" if identity == HUMAN else "nought"
# draw a symbol on the board to represent the move
draw_move(screen, name_of_space, symbol)
# update the history of what has happened in case
# we want to learn from it later
gamehistory[identity].append(board.copy())
decisions[identity].append(name_of_space)
# update the board to include the move
movelocation = get_space_location(name_of_space)
board[movelocation] = identity
# have they won the game?
gameover = display_winner(screen, board, identity)
if gameover:
# someone won! maybe an ML project could learn from this
learn_from_this(identity, gamehistory[identity], decisions[identity])
# the game is also over if the board is full (a draw!)
#
# and the board is full if both players together
# have made 9 moves in total
if len(decisions[HUMAN]) + len(decisions[COMPUTER]) >= 9:
gameover = True
return gameover
# the machine learning model's turn
def let_computer_play(screen, board):
computer_move = classify(board)
print(computer_move)
return game_move(screen, board, computer_move["class_name"], COMPUTER)
############################################################################
# Main game logic starts here
############################################################################
def debug(msg):
# if something isn't working, uncomment the line below
# so you get detailed print-outs of everything that
# the program is doing
# print(msg)
pass
debug("Configuration")
debug("Using identities %s %s %s" % (EMPTY, PLAYER, OPPONENT))
debug(deconvert)
debug("Initial startup and setup")
screen = prepare_game_window()
board = create_empty_board()
redraw_screen(screen, generate_random_colour(), board)
debug("Initialising game state variables")
running = True
gameover = False
debug("Deciding who will play first")
computer_goes_first = random.choice([False, True])
if computer_goes_first:
let_computer_play(screen, board)
while running:
# wait for the user to do something...
event = pygame.event.wait()
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and gameover == False:
# what has the user clicked on?
mx, my = pygame.mouse.get_pos()
location_name = get_click_location(mx, my)
if location_name == "none":
# user clicked on none of the spaces so we'll
# change the colour for them instead!
redraw_screen(screen, generate_random_colour(), board)
elif is_space_empty(board, location_name):
# the user clicked on an empty space
gameover = game_move(screen, board, location_name, HUMAN)
# if we're still going, it is the computer's turn next
if gameover == False:
# the computer chooses where to play
gameover = let_computer_play(screen, board)
# ignore anything else the user clicked on while we
# were processing their click, so they don't try to
# sneakily have lots of moves at once
pygame.event.clear()
# explicitly quit pygame to ensure the app terminates correctly
# cf. https://www.pygame.org/wiki/FrequentlyAskedQuestions
pygame.quit()