Python Forum
Adding a single player mode to my wxOthello game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a single player mode to my wxOthello game
#21
I was wondering if you could post all yuor code so it can be tested. you can post it in github or you can post it here - probably easier to post it on github - either way getting all the code is really helpful in assisting you
Reply
#22
Nice thread im also working on Othello game but built with pygame. i was thinking on implementing minMax algorithm.
Reply
#23
I wrote a simpler scoring heuristic and implemented minimax with alpha-beta pruning. The result is a bot that makes some moves that give me pause, but it still dives into the corners at almost the first opportunity. The pseudocode on the Wikipedia article was a little hard to read. I'm not certain if my implementation of alpha-beta pruning is correct. The scoring heuristic also needs to be significantly improved. This is in another version of the file so the original scoring heuristic is still around. Here's the whole Othello - different scoring logic.pyw file:
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
import wx
import os
from functools import partial
import random
from math import inf
from concurrent import futures
 
thread_pool_executor = futures.ThreadPoolExecutor(max_workers = 1)
 
# The wxPython implementation of an Othello game I wrote for my mother.
# These are displayed in help windows when an item from the help menu is selected:
gameRules = """Game Rules
 
Othello is a game played on a board with an 8x8 grid on it. There are 2 players and 64 gamepieces, enough to fill the entire gameboard. Each piece has a black side and a white side and each player plays as either black or white. The goal is when either the board is filled or their is no valid move available to either player for the majority of the pieces on the board to have your color upturned.
 
The application will automatically assess whether an attempted move is valid but to alleviate the frustration of guessing where one can place a gamepiece, here is a brief explanation:
 
If a space is already occupied, the move is invalid.
 
If there are no adjacent pieces, the move is invalid.
 
When there are adjacent pieces, their must be at least one vertical, horizontal or diagonal row of your opponents pieces with another of your pieces on the other end.
 
When you place a gamepiece, all adjacent vertical, horizontal or diagonal rows of your opponents pieces with another of your pieces at the other end will be flipped.
 
You may occaisionally find yourself in a situation in which there is no valid move available to you but your opponent has a valid move. You can give your opponent your move by clicking their player button. The text in their button will turn red, indicating that it is their turn. If there are no valid moves available to you, it will say so in the status bar.
"""
 
applicationHelp = """Application Help
 
Options Menu:
 
    The options menu provides options for starting a new game,
    saving a game, opening an existing game, saving and quitting and
    quit without saving. A single player gamemode is also listed in
    this menu, but it is not yet implemented.
 
Give Move to Opponent:
 
    There are situations in which the current player has no valid
    move, but their opponent does. In this situation, you give your
    move to your opponent by clicking their player button (one of the
    large buttons at the top or bottom of the window.) The text in
    their button will turn red indicating that it's their move.
 
Options Menu Items:
 
    New Game -- Start a new game.
 
    New Single Player Game -- coming soon
 
    Save Game -- Displays a file navigator to locate and select a save
        file to save the current game. The destination file must be a
        text file (*.txt). The application will remember the path of a
        current saved game until it is closed, so you won't be prompted
        for a save file if you have specified one already. When 'new
        game' is clicked, the path of any previously specified save file is
        forgotten to avoid overwriting a pre-existing saved game.
 
    Save Game As -- Allows you to save a previously saved game
        under a different name. This feature can be used for among
        other things, creating back-ups of your saves.
 
    Open Game -- Displays a file navigator to locate the a save file
        (*.txt) to open a pre-existing game. The filepath of the
        selected game is remembered until the application is closed,
        so you won't be prompted to re-select the file when saving.
 
    Save and Quit -- Automatically saves the game before exiting. If
        no file has been previously specified, the user will be prompted
        for a save file.
 
    Quit Without Saving -- Quits the game without saving.
"""
 
# The program itself is implemented inside a class for encapsulation and to allow import,
# ease of implementation, and readability.
 
class OthelloGameFrame(wx.Frame):
    """
    The Othello game proper. Almost all of the code that makes this app work is in here.
    It uses a pretty standard constructor for a class inheriting from Frame:
 
    __init__(self, *args, **kwArgs) Passing in the size parameter is recommended.
    """
    # I know it is not recommended to create custom IDs but I didn't find wx IDs which
    # corresponded to the actions of these four menu items.
    ID_NEW_SINGLE_PLAYER = wx.NewIdRef(1)
    ID_SAVE_AND_QUIT = wx.NewIdRef(1)
    ID_GAMERULES = wx.NewIdRef(1)
    ID_QUIT_WITHOUT_SAVING = wx.NewIdRef(1)
    # Constants for buttons:
    PLAYER1BUTT = wx.NewIdRef(1)
    PLAYER2BUTT = wx.NewIdRef(1)
    # Color Constants:
    bgAndBoardColor = wx.Colour(0x41, 0xA1, 0x23)
    player1Color = wx.Colour(0x00, 0x00, 0x00)
    player2Color = wx.Colour(0xFF, 0xFF, 0xFF)
    def __init__ (self, *args, **kwArgs):
        wx.Frame.__init__(self, *args, **kwArgs)
        """
        OthelloGameFrame.__init__(self, *args, *kwArgs)
        Returns an object representing the main window of the Othello game app.
        Specifying the size parameter in the arg list is recommended. Otherwise, it
        works just like a normal wx.Frame.
        """
        # non-GUI related instance variables:
        self.saveFile = ""
        self.firstPlayersMove = True
        self.gameAltered = False
        self.robot = False # All instances of classes evaluate to True unless __bool__ is defined and can return False.
                           # Basically, whether the game is single player or 2 player is indicated by whether this object,
                           # whatever its type evaluates to True.
 
        self.SetBackgroundColour(wx.Colour(0x30, 0x30, 0x30))
        self.CreateStatusBar()
 
        # Creating an options menu with all the non-gameplay related options
        # I want to make available to the user.
        self.optionsMenu = wx.Menu()
        menuNewGame = self.optionsMenu.Append(wx.ID_NEW, "&New Game", "Start a new game.")
        menuNewSinglePlayer = self.optionsMenu.Append(self.ID_NEW_SINGLE_PLAYER, "New Single &Player Game", "Start a game against the AI. This feature is currently unavailable.")
        self.optionsMenu.AppendSeparator()
        menuSaveGame = self.optionsMenu.Append(wx.ID_SAVE, "&Save Game", "Save the current game and remember the filename for future saves.")
        menuSaveAs = self.optionsMenu.Append(wx.ID_SAVEAS, "Save Game &As...", "Save a previously save game under a new name.")
        menuOpenGame = self.optionsMenu.Append(wx.ID_OPEN, "&Open Game", "Open a previously saved game.")
        menuSaveAndQuit = self.optionsMenu.Append(self.ID_SAVE_AND_QUIT, "Sa&ve and Quit", "Save the game and quit.")
        menuQuitWithoutSaving = self.optionsMenu.Append(self.ID_QUIT_WITHOUT_SAVING, "Quit &Without Saving", "Close the application without saving the game.")
 
        # The help menu will display instances of HelpWindow containing a helptext
        # appropriate to the menu item selected.
        self.helpMenu = wx.Menu()
        menuShowGamerules = self.helpMenu.Append(self.ID_GAMERULES, "Game&rules", "Explains Othello game rules.")
        menuShowAppHelp = self.helpMenu.Append(wx.ID_HELP, "Application &Help", "How to use this software")
 
        # Create the toolbar.
        self.toolbar = wx.MenuBar()
        self.toolbar.Append(self.optionsMenu, "&Options")
        self.toolbar.Append(self.helpMenu, "&Help")
        self.SetMenuBar(self.toolbar)
 
        # Add Widgets
        player1ButtonFont = wx.Font(30, wx.ROMAN, wx.NORMAL, wx.NORMAL)
        player2ButtonFont = wx.Font(30, wx.ROMAN, wx.NORMAL, wx.NORMAL)
        gameboardButtonFont = wx.Font(12, wx.ROMAN, wx.NORMAL, wx.NORMAL)
 
        # You should see what this code looked like before I put this in!
        # Thank you, yoriz!
        buttonGrid = wx.GridSizer(8, 8, 0, 0)
        buttonGrid.SetHGap(2)
        buttonGrid.SetVGap(2)
         
        self.gameboard = []
        for row in range(8):
            board_columns = []
            for col in range(8):
                btn = wx.Button(self, style=wx.NO_BORDER)
                btn.SetFont(gameboardButtonFont)
                btn.SetBackgroundColour(self.bgAndBoardColor)
                btn.SetForegroundColour(wx.Colour(0xFF, 0x00, 0x00))
                btn.Bind(wx.EVT_BUTTON,
                         partial(self.gameboardButtonClicked, row=row, col=col))
                buttonGrid.Add(btn, 0, wx.EXPAND)
                board_columns.append(btn)
            self.gameboard.append(board_columns)
 
        # Creating the layout:
        self.player1Butt = wx.Button(self, self.PLAYER1BUTT, "Player 1", style = wx.NO_BORDER)
        self.player1Butt.SetFont(player1ButtonFont)
        self.player1Butt.SetBackgroundColour(self.player1Color)
        self.player1Butt.SetForegroundColour(wx.Colour(0xFF, 0x00, 0x00))
        self.player2Butt = wx.Button(self, self.PLAYER2BUTT, "Player 2", style = wx.NO_BORDER)
        self.player2Butt.SetFont(player2ButtonFont)
        self.player2Butt.SetBackgroundColour(self.player2Color)
        self.layout = wx.BoxSizer(wx.VERTICAL)
        self.layout.Add(self.player1Butt, 1, wx.EXPAND)
        self.layout.Add(buttonGrid, 8, wx.CENTRE)
        self.layout.Add(self.player2Butt, 1, wx.EXPAND)
 
        self.SetSizer(self.layout)
 
        # Bind the menu events to their respective callbacks.
        self.Bind(wx.EVT_MENU, self.newGame, menuNewGame)
        self.Bind(wx.EVT_MENU, self.newSinglePlayer, menuNewSinglePlayer)
         
        self.Bind(wx.EVT_MENU, self.saveGame, menuSaveGame)
        self.Bind(wx.EVT_MENU, self.saveAs, menuSaveAs)
        self.Bind(wx.EVT_MENU, self.openGame, menuOpenGame)
        self.Bind(wx.EVT_MENU, self.saveAndQuit, menuSaveAndQuit)
        self.Bind(wx.EVT_MENU, self.quitWithoutSaving, menuQuitWithoutSaving)
 
        self.Bind(wx.EVT_MENU, self.showGameRules, menuShowGamerules)
        self.Bind(wx.EVT_MENU, self.showAppHelp, menuShowAppHelp)
 
        # Bind the player buttons to callbacks
        self.Bind(wx.EVT_BUTTON, self.player1ButtonClicked, id = self.PLAYER1BUTT)
        self.Bind(wx.EVT_BUTTON, self.player2ButtonClicked, id = self.PLAYER2BUTT)
 
        # Bind all close events to self.quitWithoutSaving to ensure the user is always
        # asked whether they're sure they want to quit without saving their game.
        self.Bind(wx.EVT_CLOSE, self.quitWithoutSaving)
 
        self.Show(True)
        self.newGame()
 
    def newGame (self, event = None):
        """
        OthelloGameFrame.newGame(self, event = None)
        Resets the gameboard and resets the saveFile field to prevent an existing game from being overwritten.
        """
        self.saveFile = ""
        self.gameAltered = False
        for row in range(8):
            for col in range(8):
                self.gameboard[row][col].SetBackgroundColour(self.bgAndBoardColor)
 
        self.gameboard[3][3].SetBackgroundColour(self.player2Color)
        self.gameboard[3][4].SetBackgroundColour(self.player1Color)
        self.gameboard[4][3].SetBackgroundColour(self.player1Color)
        self.gameboard[4][4].SetBackgroundColour(self.player2Color)
 
        # For testing purposes:
        #self.gameboard[2][2].SetBackgroundColour(self.player2Color)
        #self.gameboard[2][5].SetBackgroundColour(self.player1Color)
        self.firstPlayersMove = True
        self.player1Butt.SetForegroundColour("RED")
        self.player2Butt.SetForegroundColour("BLACK")
 
    def newSinglePlayer (self, event = None):
        """
        OthelloGameFrame.newSinglePlayer(self, event = None)
        This feature is not yet implemented.
        """
        with SelectDifficultyDialog(self) as dlg:
            if dlg.ShowModal() == wx.ID_OK:
                self.newGame()
                self.robot = PlayerBot(self, dlg.getCurrentDifficulty())
                self.SetStatusText("")
            else:
                self.SetStatusText("New single player aborted.")
                return
 
    def saveGame (self, event = None):
        """
        OthelloGameFrame.saveGame(self, event = None)
        Prompt the user for a file in which to save the game if none has been selected yet
        and save the game
        """
        saveList = [[]]
        for row in range(8):
            for col in range(8):
                # Map each gameboard color to a number: 0 for empty space, 1 for player 1 (black), and 2 for player 2.
                saveList[-1].append(
                    {str(self.bgAndBoardColor): 0, str(self.player1Color): 1, str(self.player2Color):2}[str(self.gameboard[row][col].GetBackgroundColour())])
            if row != 7: saveList.append([])
 
        isSinglePlayer = False;
        if self.robot: isSinglePlayer = True
        saveDict = {"saveList": saveList, "firstPlayersMove": self.firstPlayersMove, "isSinglePlayer": isSinglePlayer} # Save everything in a dictionary.
        if self.robot: saveDict["difficulty"] = self.robot.difficulty
 
        # If no file has been previously selected, use a wx.FileDialog to get the
        # path of the file in which the user wants to save their game.
        if self.saveFile == "":
 
            fd = wx.FileDialog(self, "Select a file", os.getcwd(), "", "*.txt", wx.FD_OPEN)
            if fd.ShowModal() == wx.ID_OK:
                self.saveFile = fd.GetPath()
            else:
                fd.Destroy()
                return
            fd.Destroy()
 
        # Save the game as a string representation of saveDict.
        with open(self.saveFile, "w") as f:
            try:
                f.write(repr(saveDict))
            except FileNotFoundError:
                mdlg = wx.MessageDialog(self, "The currently selected file could not be accessed at this time. Please try again.", "wxOthello", wx.OK)
                mdlg.ShowModal()
                mdlg.Destroy()
                self.saveFile = ""
        self.gameAltered = False
 
    def saveAs (self, event = None):
        """
        OthelloGameFrame.saveAs(self, event = None)
        Save a previously saved game under a different filename.
        """
        self.saveFile = ""
        self.saveGame()
 
    def openGame (self, event = None):
        """
        OthelloGameFrame.openGame(self, event = None)
        Open a previously saved game stored in the format described in the saveGame method
        {"saveList": [Nested lists containing integers mapped to gameboard colors], "firstPlayersMove": True/False}
        """
        # Use wx.FileDialog to get the save file to open.
        fd = wx.FileDialog(self, "Select a file", os.getcwd(), "", "*.txt", wx.FD_OPEN)
        if fd.ShowModal() == wx.ID_OK:
            self.saveFile = fd.GetPath()
        else:
            fd.Destroy()
            return
        fd.Destroy()
 
        # Open the save file and convert its contents into a dictionary.
        with open(self.saveFile, "r") as f:
            try:
                saveDict = eval(f.read())
            except FileNotFoundError:
                mdlg = wx.MessageDialog(self, "The currently selected file could not be accessed at this time. Please try again.", "wxOthello", wx.OK)
                mdlg.ShowModal()
                mdlg.Destroy()
                self.saveFile = ""
                return
            # If the files contents are incompatible with the attempted parse into a dictionary, inform the user.
            except SyntaxError:
                mdlg = wx.MessageDialog(self, "The currently selected file is either corrupted or its contents incompatible with opening in this game.", "wxOthello", wx.OK)
                mdlg.ShowModal()
                mdlg.Destroy()
                self.saveFile = ""
                return
 
        # Load the dictionarys data into the relevant instance variables. When single player mode is implemented,
        # a check for the key "isSinglePlayer" will also need to be added here.
        self.firstPlayersMove = saveDict["firstPlayersMove"]
        if "isSinglePlayer" in saveDict:
            if saveDict["isSinglePlayer"]:
                self.robot = PlayerBot(self, saveDict["difficulty"])
         
        for row in range(8):
            for col in range(8):
                self.gameboard[row][col].SetBackgroundColour([self.bgAndBoardColor, self.player1Color, self.player2Color][saveDict["saveList"][row][col]])
        self.Refresh()
        self.gameAltered = False
 
    def saveAndQuit (self, event = None):
        """
        OthelloGameFrame.saveAndQuit(self, event = None)
        Saves the game and quits.
        """
        self.saveGame()
        self.Destroy()
        exit()
 
    def quitWithoutSaving (self, event = None):
        """
        OthelloGameFrame.quitWithoutSaving(self, event = None)
        If the game has been altered since last save, this function
        asks the user via messageBox whether they are sure they don't
        want to save. It exits the game if they answer yes, calls
        saveGame if they answer no and returns if they click cancel.
        """
        if self.gameAltered:
            usersFinalDecision = wx.MessageBox("All progress you've made in this game will be lost. Are you sure you want to quit without saving? Answering 'no' will open a save dialog if no file was selected previously then exit the game.",
                                               "wxOthello", wx.YES_NO | wx.CANCEL, self)
            if usersFinalDecision == wx.YES:
                self.Destroy()
                exit()
            elif usersFinalDecision == wx.NO:
                self.saveGame()
            elif usersFinalDecision == wx.CANCEL:
                return
        else:
            self.Destroy()
            exit()
 
    def showGameRules (self, event = None):
        """
        OthelloGameFrame.showGameRules(self, event = None)
        This callback displays an instance of HelpWindow that
        displays the rules of Othello as its help text with
        a call to showHelp.
        """
        global gameRules
        self.showHelp(gameRules)
 
    def showAppHelp (self, event = None):
        """
        OthelloGameFrame.showAppHelp(self, event = None)
        This callback displays an instance of HelpWindow that
        displays help information for this application with
        a call to showHelp.
        """
        global applicationHelp
        self.showHelp(applicationHelp)
 
    def showHelp(self, helpText):
        """
        OthelloGameFrame.showHelp(self, helpText)
        Displays an instance of HelpWindow displaying
        the value of helpText.
        """
        with HelpWindow(self, helpText) as hdlg:
            hdlg.ShowModal()
 
    def player1ButtonClicked (self, event = None):
        """
        OthelloGameFrame.player1ButtonClicked(self, event = None)
        Gives the next move to player 1. This feature is
        intended for use when it is player 2s turn and there are
        no moves available to player 2.
        """
        self.firstPlayersMove = True
        self.player1Butt.SetForegroundColour("RED")
        self.player2Butt.SetForegroundColour("BLACK")
 
    def player2ButtonClicked (self, event = None):
        """
        OthelloGameFrame.player2ButtonClicked(self, event = None)
        Gives the next move to player 2. This feature is
        intended for use when it is player 1s turn and there are
        no moves available to player 1.
        """
        self.firstPlayersMove = False
        self.player1Butt.SetForegroundColour("WHITE")
        self.player2Butt.SetForegroundColour("RED")
        if self.robot:
            self.robot.makeMove()
 
    def displayErrorOnButton(row, col, message):
        self.gameboard[row][col].SetLabel(message)
        wx.MilliSleep(1000)
        self.gameboard[row][col].SetLabel("")
 
    def gameboardButtonClicked (self, event = None, row = 0, col = 0):
        """
        OthelloGameFrame.gameboardButtonClicked(self, event = None, row = 0, col = 0)
        This method is called through lambdas bound to the gameboard buttons generated
        in __init__. It displays an error message in the space where the move is
        attempted and returns if a move is invalid. Otherwise, it executes the move,
        checks whether somebody has won, gives the next move to the other player, and
        informs the next player if there is no move available to them in the status bar.
        """
        # self,firstPlayersMove is a boolean indicating whether it's player 1s turn.
        if self.firstPlayersMove:
            me = self.player1Color
            opponent = self.player2Color
 
        else:
            me = self.player2Color
            opponent = self.player1Color
 
        # Detect invalid move attempts, inform the user if their move is invalid and
        # the reason their move is invalid and return from the function.
        moveIsValid, message = self.isValidMove(row, col, me, opponent)
        if not moveIsValid:
            self.gameboard[row][col].SetLabel(message)
            wx.MilliSleep(1000)
            self.gameboard[row][col].SetLabel("")
            return
 
        # Make the move selected by the player.
        self.makeMove(row, col, me, opponent)
        self.gameAltered = True
 
        # The method detectWin returns a tuple with a boolean indicating whether there
        # are no valid moves available to either player and a message string appropriate to
        # the situation of 1: A player 1 victory, 2: A draw, or 3: A player 2 victory.
        winDetected, message = self.detectWin()
        if winDetected:
            m = wx.MessageDialog(self, message, "wxOthello")
            m.ShowModal()
            m.Destroy()
            self.gameAltered = False
 
        # Invert the value of the self.firstPlayersMove flag and change the color of the
        # text in the player 1 and player 2 buttons in a manner according to whose turn it
        # is.
        self.firstPlayersMove = not self.firstPlayersMove
 
        if self.firstPlayersMove:
            self.player1Butt.SetForegroundColour("RED")
            self.player2Butt.SetForegroundColour("BLACK")
        else:
            self.player1Butt.SetForegroundColour("WHITE")
            self.player2Butt.SetForegroundColour("RED")
 
        # Inform the next player if there is no valid move available to them.
        if not self.moveAvailableToPlayer(opponent, me):
 
            if opponent == self.player1Color:
                self.SetStatusText("No move available to player 1.")
            else:
                self.SetStatusText("No move available to player 2.")
        else:
            self.SetStatusText("")
 
        # If in single player mode, let the bot make a move and then return.
        if self.robot and not self.firstPlayersMove:
            self.robot.makeMove()
            return
 
    def moveAvailableToPlayer(self, me, opponent):
        for row in range(8):
            for col in range(8):
                if self.isValidMove(row, col, me, opponent)[0]: return True
        return False
 
    def isValidMove (self, row, col, me, opponent):
        """
        OthelloGameFrame.isValidMove(self, row, col, me, opponent)
        This method returns the tuple (isValidMove, messaage). It tests
        whether a move at a specified position on the gameboard is valid
        for a specific player and if the move is invalid, returns a
        message explaining why the move is invalid.
        """
        # Check whether the grid space is empty
        if self.gameboard[row][col].GetBackgroundColour() != self.bgAndBoardColor:
            return False, "This Space\nIs Occupied!"
 
        # A series of scanning vectors for the 8 scanning directions: up, down, left, right and the four diagonal directions
        scanningDirections = ((-1, 0), (0, 1), (1, 0), (0, -1),
                              (-1, -1), (-1, 1), (1, 1), (1, -1))
 
        # Iterate over the diffetent scanning directions, return True if the move is valid and set message to a message string
        # that explains why the move is invalid, if the move is invalid.
        message = "No Adjacent\nGamepieces!"
        for SDRow, SDCol in scanningDirections:
            currentRow = row + SDRow
            currentCol = col + SDCol
            sawOpponent = False
            while currentRow in range(0, 8) and currentCol in range(0, 8):
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == self.bgAndBoardColor:
                    break
                else:
                    message = "No Pieces\nTo Flip!"
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == opponent: sawOpponent = True
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == me and sawOpponent:
                    return True, "You won't see this message!"
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == me and not sawOpponent: break
 
                currentRow += SDRow
                currentCol += SDCol
         
        return False, message
 
    def makeMove (self, row, col, me, opponent):
        """
        OthelloGameFrame.makeMove(self, row, col, me, opponent)
        Performs a move for a specified player at a specified position.
        """
        # Put down the players gamepiece
        self.gameboard[row][col].SetBackgroundColour(me)
 
        # A series of scanning vectors for the 8 scanning directions: up, down, left, right and the four diagonal directions
        scanningDirections = ((-1, 0), (0, 1), (1, 0), (0, -1),
                              (-1, -1), (-1, 1), (1, 1), (1, -1))
 
        # Iterate over the scanning vectors.
        for SDRow, SDCol in scanningDirections:
            currentRow = row + SDRow
            currentCol = col + SDCol
            sawOpponent = False
            canFlipPieces = False
            # Check whether gamepieces can be flipped in the current scanning direction.
            while currentRow in range(0, 8) and currentCol in range(0, 8):
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == self.bgAndBoardColor: break
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == opponent: sawOpponent = True
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == me and sawOpponent:
                    canFlipPieces = True
                    break
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == me and not sawOpponent: break
                currentRow += SDRow
                currentCol += SDCol
 
            # If gamepieces can be flipped in the current scanning direction, flip the pieces.
            currentRow = row + SDRow
            currentCol = col + SDCol
            while canFlipPieces and currentRow in range(0, 8) and currentCol in range(0, 8):
                if self.gameboard[currentRow][currentCol].GetBackgroundColour() == opponent:
                    self.gameboard[currentRow][currentCol].SetBackgroundColour(me)
                elif self.gameboard[currentRow][currentCol].GetBackgroundColour() == me:
                    break
                else:
                    print("Kyle, you have some debugging to do! This else clause is never supposed to execute. Something has gone horribly wrong!")
                currentRow += SDRow
                currentCol += SDCol
 
    def detectWin (self):
        """
        OthelloGameFrame.detectWin(self)
        This method returns a the tuple (noValidMoves, message), where noValidMoves is a boolean indicating whether there are no more valid moves
        available to either player and message is one of the the strings "The winner is player 1!", if the majority of the pieces on the board are
        black, "This game is a draw!" if player 1 and player 2 have equal numbers of pieces on the board, or "The winner is player 2!" if the
        majority of the pieces on the board are white.
        """
        noValidMoves = True # We begin by assuming that neither player has a valid move available to them.
        player1Count = 0    # Counters for the number of spaces each player has captured.
        player2Count = 0
        # Iterate over the gameboard. Check whether there is a valid move available to either player and
        # count the number of spaces captured by each player.
        for row in range(8):
            for col in range(8):
                if self.isValidMove(row, col, self.player1Color, self.player2Color)[0] or self.isValidMove(row, col, self.player2Color, self.player1Color)[0]: noValidMoves = False
                if self.gameboard[row][col].GetBackgroundColour() == self.player1Color: player1Count += 1
                if self.gameboard[row][col].GetBackgroundColour() == self.player2Color: player2Count += 1
 
        if noValidMoves:
            # Return True and a message indicating who won
            if player1Count > player2Count:
                return True, "The winner is player 1!"
            elif player1Count == player2Count:
                return True, "This game is a draw!"
            elif player1Count < player2Count:
                return True, "The winner is player 2!"
             
        else:
            return False, "You're not supposed to see this message."
 
class PlayerBot:
 
    """
    Instances of this class play as player 2 in single player mode.
    class PlayerBot:
        __init__(self, parent, difficulty)
        parent - an OthelloGameFrame instance
        difficulty - an integer between 0 and 100
    """
    positionalScoringMap = ((500, -200, 99, 80, 80, 99, -200, 500),
                            (-200, -300, -10, -5, -5, -10, -300, -200),
                            (99, -10, 200, 150, 150, 200, -10, 99),
                            (80, -5, 150, 100, 100, 150, -5, 80),
                            (80, -5, 150, 100, 100, 150, -5, 80),
                            (99, -10, 200, 150, 150, 200, -10, 99),
                            (-200, -300, -10, -5, -5, -10, -300, -200),
                            (500, -200, 99, 80, 80, 99, -200, 500))
 
    bdCode = 0 # Constants for encoding empty board spaces, player 1 and player 2 in an internal
    p1Code = 1 # representation of the current game state.
    p2Code = 2
    # Constants for early game, mid game and late game used by the scoring heuristic.
    ST_EARLY = 3
    ST_MID = 4
    ST_LATE = 5
    # Constants for top, left, right, and bottom used by the trap and entrance detection and assessment
    # methods.
    TOP = (0, 2)
    LEFT = (2, 0)
    RIGHT = (2, 6)
    BOTTOM = (6, 2)
 
    def __init__ (self, parent, difficulty):
        self.parent = parent
        self.difficulty = difficulty
        self.gameState = self.getInitialGameState()
        # A list of moves potentially detrimental to the bots midgame.
        self.movesDetrimentalToMidgame = []
 
    def getInitialGameState (self):
        """
        PlayerBot.getInitialGameState (self):
        Returns 8 integer lists nested inside a list representing the initial state of a game
        of Othello, where 0 is an empty space, 1 is player 1, and 2 is player 2.
        """
        gameState = []
        for row in range(8):
            currentRow = []
            for col in range(8):
                currentRow.append(self.bdCode)
            gameState.append(currentRow)
            currentRow = []
        gameState[2][2] = self.p2Code
        gameState[2][3] = self.p1Code
        gameState[3][2] = self.p1Code
        gameState[3][3] = self.p2Code
 
        return gameState
 
    def makeMove(self):
        """
        PlayerBot.makeMove (self)
        This method makes a move in its parent as player2 and surrenders its move to player 1 if there are no
        moves available to it.
        """
        self.updateGameState() # Update the objects internal representation of the current state of the game.
         
        validMoves = self.getAllValidMoves(self.gameState, self.p2Code, self.p1Code)
 
        if validMoves == []: # If there are no moves available to the bot, give player 1 its move.
            self.parent.player1ButtonClicked()
            return
 
        if random.randint(0, 100) <= self.difficulty: # This outer if statement is used to make the occasional 'mistake'; a move
                                                      # selected randomly from the set of all valid moves. The frequency of these
                                                      # 'mistakes' is determined by the difficulty setting.
            _, bestMove = self.miniMax(self.gameState, 5, -inf, inf, True)
 
            self.parent.gameboardButtonClicked(row = bestMove[0], col = bestMove[1])
            return
 
        else:
            # Make a 'mistake'.
            randomMove = validMoves[random.randint(0, len(validMoves) - 1)]
            self.parent.gameboardButtonClicked(row = randomMove[0], col = randomMove[1])
 
        self.updateGameState()
 
    def miniMax (self, gameState, depth, alpha, beta, maximizingPlayer):
        if depth == 0 or (self.getAllValidMoves(gameState, self.p2Code, self.p1Code) == [] and self.getAllValidMoves(gameState, self.p1Code, self.p2Code)):
            return self.scoreGameState(gameState), (0, 0)
 
        if maximizingPlayer:
            allValidMoves = self.getAllValidMoves(gameState, self.p2Code, self.p1Code)
            maxScore = -inf
            bestMove = (0, 0)
            for move in allValidMoves:
                _, gameStateAfterMove = self.scoreMove(move, gameState, self.p2Code, self.p1Code)
                moveScore, _ = self.miniMax(gameStateAfterMove, depth - 1, alpha, beta, False)
                if moveScore > alpha: alpha = moveScore
                if alpha >= beta: break
                if moveScore > maxScore:
                    maxScore = moveScore
                    bestMove = move
            return maxScore, bestMove
 
        else: # Minimizing player
            allValidMoves = self.getAllValidMoves(gameState, self.p1Code, self.p2Code)
            minScore = inf
            bestMove = (0, 0)
            for move in allValidMoves:
                _, gameStateAfterMove = self.scoreMove(move, gameState, self.p1Code, self.p2Code)
                moveScore, _ = self.miniMax(gameStateAfterMove, depth - 1, alpha, beta, True)
                if moveScore < beta: beta = moveScore
                if alpha >= beta: break
                if moveScore < minScore:
                    minScore = moveScore
                    bestMove = move
            return minScore, bestMove
 
    def getAllValidMoves (self, gameState, me, opponent):
        """
        PlayerBot.getAllValidMoves(self, gameState, me, opponent)
        Return a list of all moves which are valid for the player 'me'
        in the given game state.
        """
        validMoves = []
        for row in range(8):
            for col in range(8):
                if self.isValidMove(row, col, me, opponent, gameState): validMoves.append( (row, col) )
 
        return validMoves
 
    def scoreGameState (self, gameState):
        """
        PlayerBot.scoreGameState(self, gameState)
        Used by the minMax algorithm to score a game state using a series of heuristics,
        returning positive score values to game states which are favorable to the bot and
        negative score values for game states which are unfavorable.
        """
        score = 0 # Initialize score to zero.
        me = self.p2Code        # I use the terms 'me' and 'opponent' in this function
        opponent = self.p1Code  # for code clarity. These variables serve no other purpose.
        validMoves = self.getAllValidMoves(gameState, me, opponent)
        # Check whether the bot has no options available.
        if len(validMoves) == 0:
            # Check whether the player has any moves. Return winning or losing score values
            # if there are no moves available to the player.
            if len(self.getAllValidMoves(gameState, opponent, me)) == 0:
                # Score the game.
                myScore = 0
                oppScore = 0
                for row in range(8):
                    for col in range(8):
                        if gameState[row][col] == me: myScore += 1
                        if gameState[row][col] == opponent: oppScore += 1
 
                score += myScore - oppScore
                if score > 0:
                    return 999_999_999_999 + score  # Return this if the bot won.
                elif score == 0:
                    return 0                        # Return 0 in a draw.
                elif score < 0:
                    return -999_999_999_999 + score # Return this if the bot loses.
 
            else: # Issue a score penalty for the bot losing a turn to the opponent.
 
                score -= 250
 
        # Add the positional value of all the bots pieces and subtract the positional
        # value of all the players pieces.
        for row in range(8):
            for col in range(8):
                if gameState[row][col] == me: score += self.positionalScoringMap[row][col]
                if gameState[row][col] == opponent: score -= self.positionalScoringMap[row][col]
 
        # Look for any of the bots pieces shielded from attack by surrounding opponents pieces
        # and add 15 to score for each one.
        score += self.findShieldedPieces(gameState, me, opponent) * 15
 
        # Look for any of the players pieces shielded from attack by surrounding bot pieces and
        # subtract 15 from score for each one.
        score -= self.findShieldedPieces(gameState, opponent, me) * 15
 
        # Look for any bots piece that has been made permanently inaccessible to the player and
        # add 100 to the score for each one.
        score += self.findPermanentPieces(gameState, me, opponent) * 100
 
        # Look for any player piece that has been made permanently inaccessible to the bot and
        # subtract 100 from the score for each one.
        score -= self.findPermanentPieces(gameState, opponent, me) * 100
 
        # Tell the bot Oh, by the way, your goal is to get as many pieces as possible.
        myScore = 0
        oppScore = 0
        for row in range(8):
            for col in range(8):
                if gameState[row][col] == me: myScore += 1
                if gameState[row][col] == opponent: oppScore += 1
        score += (myScore - oppScore) * 10
 
        # Reward to bot for creating more options for itself than the player:
        score += (len(validMoves) - len(self.getAllValidMoves(gameState, opponent, me))) * 30
 
        return score
 
    def findPermanentPieces (self, gameState, me, opponent):
        """
        PlayerBot.findPermanentPieces(self, gameState, me, opponent)
        Returns the number of pieces in a given game state which can
        never be altered
        """
        permanentPieceCount = 0
 
        # The only way pieces can become permanent is if they're wedged into corners, so we
        # only need to see whether a corner has our color in it and if it doesn't, we can ignore
        # it entirely. However, if two non-opposing corners are filled, special care must be used
        # to ensure that permanent pieces aren't counted twice.
        topLeftHorizontalEdge = 0
        topLeftVerticalEdge = 0
 
        topRightHorizontalEdge = 0
        topRightVerticalEdge = 0
         
        bottomLeftVerticalEdge = 0
        bottomLeftHorizontalEdge = 0
 
        bottomRightVerticalEdge = 0
        bottomRightHorizontalEdge = 0
 
        # Count up contiguous rows and columns of our pieces on the edges starting from the corners until we come to an
        # opponent piece or empty space.
        if gameState[0][0] == me:
            row = 0
            for col in range(8):
                if gameState[row][col] == me:
                    topLeftHorizontalEdge += 1
                else:
                    break
            col = 0
            for row in range(8):
                if gameState[row][col] == me:
                    topLeftVerticalEdge += 1
                else:
                    break
 
        if gameState[0][7] == me:
            row = 0
            for col in range(7, -1, -1):
                if gameState[row][col] == me:
                    topRightHorizontalEdge += 1
                else:
                    break
 
            col = 7
            for row in range(8):
                if gameState[row][col] == me:
                    topRightVerticalEdge += 1
                else:
                    break
 
        if gameState[7][0] == me:
            row = 7
            for col in range(8):
                if gameState[row][col] == me:
                    bottomLeftHorizontalEdge += 1
                else:
                    break
            col = 0
            for row in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomLeftVerticalEdge += 1
                else:
                    break
 
        if gameState[7][7] == me:
            row = 7
            for col in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomRightHorizontalEdge += 1
                else:
                    break
            col = 7
            for row in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomRightVerticalEdge += 1
                else:
                    break
        # Some boolean flags to keep track of which gamepieces have already been counted as permanent:
        # The letters T, B, L, R, V and H are top, bottom, left, right, vertical and horizontal respectively.
        TLHedgeCounted = False; TLVedgeCounted = False
        TRHedgeCounted = False; TRVedgeCounted = False
        BLHedgeCounted = False; BLVedgeCounted = False
        BRHedgeCounted = False; BRVedgeCounted = False
 
        # Check whether any of the edges are filled corner to corner and start counting at any filled edges.
        if topLeftHorizontalEdge == 8:
            TRHedgeCounted = True
             
            canBreak = False
            for row in range(8):
                for col in range(8):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        canBreak = True
                        break
                if canBreak: break
         
        if topLeftVerticalEdge == 8 and not TRHedgeCounted:
            BRVedgeCounted = True
 
            canBreak = False
            for col in range(8):
                for row in range(8):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        canBreak = True
                        break
                if canBreak: break
         
        if bottomRightHorizontalEdge == 8:
            BRHedgeCounted = True
 
            canBreak = False
            for row in range(7, -1, -1):
                for col in range(7, -1, -1):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        canBreak = True
                        break
                if canBreak: break
         
        if bottomRightVerticalEdge == 8 and not BRHedgeCounted:
            TRVedgeCounted = True
 
            canBreak = False
            for col in range(7, -1, -1):
                for row in range(7, -1, -1):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        canBreak = True
                        break
                if canBreak: break
        # Count pieces in disconnected corners using the boolean flags to avoid counting pieces twice.
        # We count along the shorter edge, reducing the length of our scan along a diagonal
        if topLeftHorizontalEdge <= topLeftVerticalEdge and not TLHedgeCounted and not TLVedgeCounted:
            permanentPieceCount += topLeftVerticalEdge - topLeftHorizontalEdge
            HScanDist = topLeftVerticalEdge
            for row in range(8):
                for col in range(HScanDist):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        HScanDist = 0
                        break
                if HScanDist > 0:
                    HScanDist -= 1
                else:
                    break
 
            TLHedgeCounted = True
            TLVedgeCounted = True
 
        elif topLeftVerticalEdge <= topLeftHorizontalEdge and not TLHedgeCounted and not TLVedgeCounted:
            permanentPieceCount += topLeftHorizontalEdge - topLeftVerticalEdge
            VScanDist = topLeftVerticalEdge
            for row in range(8):
                for col in range(VScanDist):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        VScanDist = 0
                        break
                if VScanDist > 0:
                    VScanDist -= 1
                else:
                    break
                 
            TLHedgeCounted = True
            TLVedgeCounted = True
 
        if topRightHorizontalEdge <= topRightVerticalEdge and not TRHedgeCounted and not TRVedgeCounted:
            permanentPieceCount += topRightVerticalEdge - topRightHorizontalEdge
            HScanDist = topRightHorizontalEdge
            for row in range(8):
                for col in range(7, -HScanDist - 1, -1):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        HScanDist = 0
                        break
                if HScanDist > 0:
                    HScanDist -= 1
                else:
                    break
 
            TRHedgeCounted = True
            TRVedgeCounted = True
             
        elif topRightVerticalEdge <= topRightHorizontalEdge and not TRHedgeCounted and not TRVedgeCounted:
            permanentPieceCount += topRightHorizontalEdge - topRightVerticalEdge
            VScanDist = topRightVerticalEdge
            for col in range(7, -1, -1):
                for row in range(VScanDist):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        VScanDist = 0
                        break
                if VScanDist > 0:
                    VScanDist -= 1
                else:
                    break
                 
            TRHedgeCounted = True
            TRVedgeCounted = True
 
        if bottomLeftHorizontalEdge <= bottomLeftVerticalEdge and not BLHedgeCounted and not BLVedgeCounted:
            permanentPieceCount += bottomLeftVerticalEdge - bottomLeftHorizontalEdge
            HScanDist = bottomLeftHorizontalEdge
            for row in range(7, -1, -1):
                for col in range(HScanDist):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        HScanDist = 0
                        break
                if HScanDist > 0:
                    HScanDist -= 1
                else:
                    break
 
            BLHedgeCounted = True
            BLVedgeCounted = True
 
        elif bottomLeftVerticalEdge <= bottomLeftHorizontalEdge and not BLVedgeCounted and not BLHedgeCounted:
            permanentPieceCount += bottomLeftHorizontalEdge - bottomLeftVerticalEdge
            VScanDist = bottomLeftVerticalEdge
            for col in range(8):
                for row in range(7, -VScanDist - 1, -1):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        VScanDist = 0
                        break
                if VScanDist > 0:
                    VScanDist -= 1
                else:
                    break
 
            BLVedgeCounted = True
            BLHedgeCounted = True
 
        if bottomRightHorizontalEdge <= bottomRightVerticalEdge and not BRHedgeCounted and not BRVedgeCounted:
            permanentPieceCount += bottomRightVerticalEdge - bottomRightHorizontalEdge
            HScanDist = bottomRightHorizontalEdge
            for row in range(7, -1, -1):
                for col in range(7, -HScanDist -1, -1):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        HScanDist = 0
                        break
                if HScanDist > 0:
                    HScanDist -= 1
                else:
                    break
 
            BRedgeCounted = True
            BLedgeCounted = True
 
        elif bottomRightVerticalEdge <= bottomRightHorizontalEdge and not BRHedgeCounted and not BRVedgeCounted:
            permanentPieceCount += bottomRightVerticalEdge - bottomRightHorizontalEdge
            VScanDist = bottomRightVerticalEdge
            for col in range(7, -1, -1):
                for row in range(VScanDist):
                    if gameState[row][col] == me:
                        permanentPieceCount += 1
                    else:
                        VScanDist = 0
                        break
                if VScanDist > 0:
                    VScanDist -= 1
                else:
                    break
 
            BRHedgeCounted = True
            BRVedgeCounted = True
 
        return permanentPieceCount
 
    def findShieldedPieces (self, gameState, me, opponent):
        """
        PlayerBot.findShieldedPieces(self, gameState, me, opponent)
        Returns the number of pieces which cannot be flipped in the current gamestate.
        """
        shieldedPiecesCount = 0
        movesAvailableToOpponent = self.getAllValidMoves(gameState, me, opponent)
        for sRow in range(8):       # sRow for searchRow, sCol for searchColumn
            for sCol in range(8):
 
                if gameState[sRow][sCol] == me:
 
                    # Try to find a position on the board where the opponent could capture this piece.
                    foundPossibleAttack = False
                    for move in movesAvailableToOpponent:
                        _, gameStateAfterMove = self.scoreMove(move, gameState, me, opponent)
                        if gameStateAfterMove[sRow][sCol] != me: foundPossibleAttack = True
 
                    if not foundPossibleAttack: shieldedPiecesCount += 1
 
        return shieldedPiecesCount
 
    def isValidMove (self, row, col, me, opponent, gameState):
        """
        PlayerBot.isValidMove(self, row, col, me, opponent, gamestate)
        Returns True if the given move is valid for the given gamestate and
        False if the given move is invalid for the given gamestate.
        """
        if gameState[row][col] != self.bdCode:
            return False # If the space where we're trying to move isn't empty, we already know this move is invalid.
 
        scanningDirections = ((-1, 0), (0, 1), (1, 0), (0, -1), # A series of scanning vectors.
                              (-1, -1), (-1, 1), (1, 1), (1, -1))
        for SDRow, SDCol in scanningDirections: # Iterate over the scanning vectors.
            currentRow = row + SDRow # Start scanning at a position offset from the move by one
            currentCol = col + SDCol # along the current scanning vector.
            sawOpponent = False      # The opponents gamepieces haven't yet been seen on this vector.
            while currentRow in range(0, 8) and currentCol in range(0, 8):
                if gameState[currentRow][currentCol] == self.bdCode: break # If the gamespace is empty, we know there are no pieces to flip on this vector.
                if gameState[currentRow][currentCol] == opponent: sawOpponent = True # The opponents gamepieces have been seen.
                if gameState[currentRow][currentCol] == me and sawOpponent:
                    return True # There are at least pieceses on this vector that can be flipped. The move is valid.
                if gameState[currentRow][currentCol] == me and not sawOpponent: break # There are no pieces to flip along this vector. Proceed to the next.
                 
                currentRow += SDRow # Proceed to the next gamespace in the current vector.
                currentCol += SDCol
 
        return False # If we've fallen out of the vector scanning loop, we know the move is invalid.
 
    def updateGameState (self):
        """
        PlayerBot.updateGameState(self)
        Synchronizes the objects gameState attribute with the current state of parent.gameboard.
        """
        for row in range(8):
            for col in range(8): # Iterate over the parents gameboard and insert integer values into self.gameState
                                 # corresponding to black pieces, white pieces and empty spaces.
                if self.parent.gameboard[row][col].GetBackgroundColour() == self.parent.bgAndBoardColor:
                    self.gameState[row][col] = self.bdCode
                elif self.parent.gameboard[row][col].GetBackgroundColour() == self.parent.player1Color:
                    self.gameState[row][col] = self.p1Code
                elif self.parent.gameboard[row][col].GetBackgroundColour() == self.parent.player2Color:
                    self.gameState[row][col] = self.p2Code
 
    def scoreMove (self, possibleMove, gameState, me, opponent):
        """
        PlayerBot.scoreMove (self, possibleMove, gameState, me, opponent)
        Calculate the number of pieces captured by a given move in a given game state
        and return a tuple containing the number of captures at index 0 and the state
        of the game after the move at index 1
        """
        gameState = gameState.copy() # We wouldn't want to alter the value of self.gameState now, would we?
        row, col = possibleMove # Unpack the move parameter to make the code more readable.
        moveScore = 1 # We already know that we at least have the grid space where we placed our piece.
        scanningDirections = ((-1, 0), (0, 1), (1, 0), (0, -1), # A series of scanning vectors
                              (-1, -1), (-1, 1), (1, 1), (1, -1))
        for SDRow, SDCol in scanningDirections: # Scann along all 8 vectors.
            currentRow = row + SDRow # Start at a position offset from the position of the move along the current
            currentCol = col + SDCol # scanning vector.
            sawOpponent = False # None of the opponents gamepieces have been seen on the current scanning vector at this time.
            canCountPieces = False # No row of my opponents pieces with another of my pieces at the other end has been seen on
                                   # on this scanning vector at this time.
            while currentRow in range(0, 8) and currentCol in range(0, 8):
                if gameState[currentRow][currentCol] == self.bdCode: break # If we see an empty space, we know we can't flip pieces on this vector.
                if gameState[currentRow][currentCol] == self.p1Code: sawOpponent = True
                if gameState[currentRow][currentCol] == me and sawOpponent:
                    canCountPieces = True # We now know we can flip pieces on this vector.
                    break # There is no need to continue scanning this vector.
                if gameState[currentRow][currentCol] == me and not sawOpponent: break # If I see another of my pieces without seeing an opponents piece,
                                                                                      # there are no pieces to flip on this vector.
                currentRow += SDRow
                currentCol += SDCol
 
            currentRow = row + SDRow
            currentCol = col + SDCol
            while canCountPieces and currentRow in range(0, 8) and currentCol in range(0, 8):
                if gameState[currentRow][currentCol] == opponent:
                    gameState[currentRow][currentCol] = me # Flip the pieces on this vector and increment the move score.
                    moveScore += 1
                elif gameState[currentRow][currentCol] == me:
                    break
                 
                currentRow += SDRow
                currentCol += SDCol
 
        return moveScore, gameState # Return the tuple
 
class SelectDifficultyDialog (wx.Dialog):
    """
    class SelectDifficultyDialog (wx.Dialog):
        __init__(self, parent)
        This object displays a dialog which prompts the user to set the difficulty
        using a slider. ShowModal returns wx.ID_OK if the user clicks the button
        labeled 'set difficulty' and wx.ID_CANCEL if the user closes the dialog.
        The getCurrentDifficulty method returns the difficulty selected by the user.
    """
    ID_DIFFICULTY_SLIDER = wx.NewIdRef(1) # An ID constant for the difficulty slider used to bind a callback to motion of the slider.
 
    def __init__(self, parent):
        # Create all the widgets
        wx.Dialog.__init__(self, parent = parent, id = -1, title = "Please Select Difficulty")
        self.SetMinSize(wx.Size(400, 290))
        self.prompt = wx.StaticText(self, -1, "Please Use the slider to select difficulty. The further to the right, the harder the game.")
        self.difficultyDescriptor = wx.StaticText(self, -1, "Game Difficulty: 75 - A little challenging")
        self.difficultySlider = wx.Slider(self, self.ID_DIFFICULTY_SLIDER, 75)
        self.okButton = wx.Button(self, wx.ID_OK, "Set Difficulty")
 
        # Insert all the widgets into a BoxSizer to lay them out neatly on the screen.
        self.layout = wx.BoxSizer(wx.VERTICAL)
        self.layout.Add(self.prompt, 1, wx.EXPAND)
        self.layout.Add(self.difficultyDescriptor, 1, wx.EXPAND)
        self.layout.Add(self.difficultySlider, 3, wx.EXPAND)
        self.layout.Add(self.okButton, 2, wx.EXPAND)
        self.SetSizer(self.layout)
        self.Fit()
 
        # Event bindings. For those who aren't familiar with wxPython, wx includes built in ID constants.
        # One of these was assigned to the button: wx.ID_OK. It wasn't created here.
        self.Bind(wx.EVT_COMMAND_SCROLL, self.diffSelectionChanged, id = self.ID_DIFFICULTY_SLIDER)
        self.Bind(wx.EVT_BUTTON, lambda evt: self.EndModal(wx.ID_OK), id = wx.ID_OK)
        self.Bind(wx.EVT_CLOSE, lambda evt: self.EndModal(wx.ID_CANCEL))
 
    def diffSelectionChanged (self, event = None):
        """
        SelectDifficultyDialog.diffSelectionChanged(self, event = None)
        This callback displays the selected difficulty in self.difficultyDescriptor, a StaticText widget
        along with a brief description of the difficulty currently selected.
        """
        currentDifficulty = self.difficultySlider.GetValue()
 
        if currentDifficulty in range(0, 26):
            self.difficultyDescriptor.SetLabel("Game Difficulty: {} - Childs play".format(currentDifficulty))
        elif currentDifficulty in range(25, 51):
            self.difficultyDescriptor.SetLabel("Game Difficulty: {} - My cat could do it.".format(currentDifficulty))
        elif currentDifficulty in range(50, 76):
            self.difficultyDescriptor.SetLabel("Game Difficulty: {} - A little challenging".format(currentDifficulty))
        elif currentDifficulty in range(75, 101):
            self.difficultyDescriptor.SetLabel("Game Difficulty: {} - Only you can win!".format(currentDifficulty))
        else:
            self.difficultyDescriptor.SetLabel("Kyle, you have some debugging to do! Unexpected output: {}".format(currentDifficulty))
 
    def getCurrentDifficulty (self):
        return self.difficultySlider.GetValue()
 
class HelpWindow(wx.Dialog):
    """
    A simple dialog class for displaying help information to the user.
    """
    def __init__ (self, parent, helpText):
        wx.Dialog.__init__(self, parent, -1, helpText.split("\n")[0])
        self.SetMinSize(wx.Size(400, 400))
        self.topExitButton = wx.Button(self, wx.ID_CLOSE, "Close Help")
        self.helpDisplay = wx.TextCtrl(parent = self, id = wx.ID_ANY, value = helpText,
                                       style = wx.TE_MULTILINE | wx.TE_READONLY)
        self.bottomExitButton = wx.Button(self, wx.ID_CLOSE, "Close Help")
        self.layout = wx.BoxSizer(wx.VERTICAL)
        self.layout.Add(self.topExitButton, 1, wx.EXPAND)
        self.layout.Add(self.helpDisplay, 10, wx.EXPAND)
        self.layout.Add(self.bottomExitButton, 1, wx.EXPAND)
        self.SetSizer(self.layout)
 
        self.Fit()
        self.Bind(wx.EVT_BUTTON, self.closeHelp, id = wx.ID_CLOSE)
 
    def closeHelp(self, event = None):
        self.EndModal(wx.ID_OK)
 
if __name__ == "__main__":
    app = wx.App()
    theApp = OthelloGameFrame(parent = None, id = wx.ID_ANY, size = (700, 800), title = "wxOthello")
    app.MainLoop()
I think this is possibly my best bot yet; yes it's still a suicidal idiot but it does often make me think about what I'm doing, so I feel I'm definitely headed in the right direction. The issue is though that the bot is also taking its sweet time and it's blocking the event loop. I still don't understand threading very well. I would greatly appreciate it if any of you could point me in the direction of a good tutorial on the subject.
Reply
#24
I made the PlayerBot.makeMove method run on a separate thread but this introduced another problem: The player can make a move while the minimax is still running. I tried adding this code to my gameboardButtonClicked method but it didn't fix the problem. The string "Not Your Turn" just appears on random buttons for reasons I don't understand and it makes the players move when it's supposed to be returning out of the method before it calls makeMove.
1
2
3
4
5
6
7
# In single player mode, detect attempts by the player to make a move during the
# bots turn and yell at them. This code needs to be fixed.
if self.robot and not self.firstPlayersMove:
    self.gameboard[row][col].SetLabel("Not Your\nTurn!")
    wx.MilliSleep(1000)
    self.gameboard[row][col].SetLabel("")
    return

(Apr-27-2019, 02:15 AM)SheeppOSU Wrote: I was wondering if you could post all yuor code so it can be tested. you can post it in github or you can post it here - probably easier to post it on github - either way getting all the code is really helpful in assisting you

There are many versions of my program in this thread; some are the whole source code and others are the specific areas of the code I've been working on. Right now the goal is to make the bot not unsmart and prevent the user from making a move while the bot is diving into a massive recursion tree.
Reply
#25
I wrote in a gui post:
Al Swiegart has written many free books on python and games. In some linux distributions his games are installed by default and the code is open sourced available to read and tweek. Making Games book can be found here:
https://inventwithpython.com/makinggames.pdf
at the end of the book he has several human vs computer games with code.
You are on the right track keep going.

it's done in pygame but still relevant.
Al has a game called flippy, it's only single player and the way he accomplishes it is
to make a list of possible moves if the list contains a corner that's the one to make.
Else make the longest or most points.
While it's not perfect it will show a way to fallow.
Reply
#26
joe_momma, thanks for that. I'll take a look at it.

I've made something of a breakthrough in multithreading some of my blocking code; specifically in the code that's supposed to display an error message on a gameboard button when a player attempts an invalid move. It works insofar as when an invalid move is attempted, the error message is displayed on the button and the app remains perfectly responsive but the messages are only supposed to display for 1 second and when an invalid move is attempted, the messages are sometimes gone after a second and sometimes they stay around for several seconds or even indefinitely.
the displayErrorOnButton and gameboardButtonClicked methods:
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
class OthelloGameFrame(wx.Frame):
    .
    .
    .
    def displayErrorOnButton(self, row, col, message):
        self.gameboard[row][col].SetLabel(message)
        time.sleep(1)
        self.gameboard[row][col].SetLabel("")
 
    def gameboardButtonClicked (self, event = None, row = 0, col = 0):
        """
        OthelloGameFrame.gameboardButtonClicked(self, event = None, row = 0, col = 0)
        This method is called through lambdas bound to the gameboard buttons generated
        in __init__. It displays an error message in the space where the move is
        attempted and returns if a move is invalid. Otherwise, it executes the move,
        checks whether somebody has won, gives the next move to the other player, and
        informs the next player if there is no move available to them in the status bar.
        """
        # self.firstPlayersMove is a boolean indicating whether it's player 1s turn.
        if self.firstPlayersMove:
            me = self.player1Color
            opponent = self.player2Color
 
        else:
            me = self.player2Color
            opponent = self.player1Color
 
        # In single player mode, detect attempts by the player to make a move during the
        # bots turn and yell at them. This code needs to be fixed.
##        if self.robot and not self.firstPlayersMove:
##            self.gameboard[row][col].SetLabel("Not Your\nTurn!")
##            wx.MilliSleep(1000)
##            self.gameboard[row][col].SetLabel("")
##            return
 
###==============The Problem Code in the gameboardButtonClicked method===================###
 
        # Detect invalid move attempts, inform the user if their move is invalid and
        # the reason their move is invalid and return from the function.
        moveIsValid, message = self.isValidMove(row, col, me, opponent)
        if not moveIsValid:
            t = threading.Thread(target = self.displayErrorOnButton, name = "display_error_thread", args = (row, col, message))
            t.start()
            # self.gameboard[row][col].SetLabel(message)
            # wx.MilliSleep(1000)
            # self.gameboard[row][col].SetLabel("")
            return
###===================================================================================###
        # Make the move selected by the player.
        self.makeMove(row, col, me, opponent)
        self.gameAltered = True
 
        # The method detectWin returns a tuple with a boolean indicating whether there
        # are no valid moves available to either player and a message string appropriate to
        # the situation of 1: A player 1 victory, 2: A draw, or 3: A player 2 victory.
        winDetected, message = self.detectWin()
        if winDetected:
            m = wx.MessageDialog(self, message, "wxOthello")
            m.ShowModal()
            m.Destroy()
            self.gameAltered = False
 
        # Invert the value of the self.firstPlayersMove flag and change the color of the
        # text in the player 1 and player 2 buttons in a manner according to whose turn it
        # is.
        self.firstPlayersMove = not self.firstPlayersMove
 
        if self.firstPlayersMove:
            self.player1Butt.SetForegroundColour("RED")
            self.player2Butt.SetForegroundColour("BLACK")
        else:
            self.player1Butt.SetForegroundColour("WHITE")
            self.player2Butt.SetForegroundColour("RED")
 
        # Inform the next player if there is no valid move available to them.
        if not self.moveAvailableToPlayer(opponent, me):
 
            if opponent == self.player1Color:
                self.SetStatusText("No move available to player 1.")
            else:
                self.SetStatusText("No move available to player 2.")
        else:
            self.SetStatusText("")
 
        # If in single player mode, let the bot make a move and then return.
        if self.robot and not self.firstPlayersMove:
            #t = threading.Thread(target = self.robot.makeMove, name = "PlayerBot_makeMove", args = ())
            #t.start()
            self.robot.makeMove()
            return
    .
    .
    .
I've never done anything with threading before. This is something of a mystery to me. I'm going to try creating a daemon thread that clears all the text from the gameboard buttons every 3 seconds, but this is far from an ideal solution.
Reply
#27
It still makes some awfully dumb moves; it's far from perfect but this is the best I've been able to do so far going to minimax route. It beat me once and it was able to draw a game against me. Unfortunately, with Othello, I think this is as far as I think I can go using minimax. Despite its downsides; like knowing your ML code is buggy only after you spend countless hours training it only to find that it's as stupid as when you started training it; I think it's the only real way to make a bot that consistently wins against a Human opponent.

My code:
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
class PlayerBot:
    .
    .
    .
    def miniMax (self, gameState, depth, alpha, beta, maximizingPlayer):
        """
        PlayerBot.miniMax(self, gameState, depth, alpha, beta, maximizingPlayer)
        An implementation of the recursive miniMax algorithm with alpha-beta pruning.
        This  method returns the tuple (terminalScore, bestMove) where terminalScore is a value
        used internally by the recursive algorithm and bestMove is the best move given the game state
        passed to this method in its initial call. The parameter gameState is the state of the game
        to be evaluated. The depth parameter is the maximizum recursion depth, alpha and beta are
        passed initial values of infinity and negative infinity and maximizingPlayer should be passed
        an initial value of True.
        """
        # If the game reaches a terminal state or maximum recursion depth is reached, return a static evaluation of the final game state with the placeholder value (0, 0).
        if depth == 0 or (not self.getAllValidMoves(gameState, self.p2Code, self.p1Code) and not self.getAllValidMoves(gameState, self.p1Code, self.p2Code)):
            return self.scoreGameState(gameState, maximizingPlayer), (0, 0)
 
        if maximizingPlayer: # - simulating a move by the bot
            allValidMoves = self.getAllValidMoves(gameState, self.p2Code, self.p1Code)
            maxScore = -inf
            if not allValidMoves: # Handle the case of the bot losing its turn to the player.
                return self.miniMax(gameState, depth - 1, alpha, beta, False)
            bestMove = [ allValidMoves[0] ] # Select randomly from all moves with the highest terminal evaluation. This adds an element of unpredictability.
            for move in allValidMoves:
                _, gameStateAfterMove = self.scoreMove(move, gameState, self.p2Code, self.p1Code)
                moveScore, _ = self.miniMax(gameStateAfterMove, depth - 1, alpha, beta, False)
                if moveScore > alpha: alpha = moveScore
                if alpha >= beta: break # alpha-beta cutoff
                if moveScore > maxScore:
                    maxScore = moveScore
                    bestMove = [ move ]
                elif moveScore == maxScore:
                    bestMove.append(move)
            return maxScore, bestMove[random.randint(0, len(bestMove) - 1)]
 
        else: # Minimizing player - simulating a move by the player
            allValidMoves = self.getAllValidMoves(gameState, self.p1Code, self.p2Code)
            minScore = inf
            if not allValidMoves: # Handle the case of the player losing a turn to the bot.
                return self.miniMax(gameState, depth - 1, alpha, beta, True)
            bestMove = [ allValidMoves[0] ] # Select randomly from all moves with the lowest terminal evaluation. This adds an element of unpredictability.
            for move in allValidMoves:
                _, gameStateAfterMove = self.scoreMove(move, gameState, self.p1Code, self.p2Code)
                moveScore, _ = self.miniMax(gameStateAfterMove, depth - 1, alpha, beta, True)
                if moveScore < beta: beta = moveScore
                if alpha >= beta: break # alpha-beta cutoff
                if moveScore < minScore:
                    minScore = moveScore
                    bestMove = [ move ]
                elif moveScore == minScore:
                    bestMove.append(move)
            return minScore, bestMove[random.randint(0, len(bestMove) - 1)]
    .
    .
    .
    def scoreGameState (self, gameState, isBotsTurn):
        """
        PlayerBot.scoreGameState(self, gameState, isBotsTurn)
        Used by the minMax algorithm to score a game state using a series of heuristics to
        compute the probability of the bot winning and return it as a floating point value
        between 0 and 1.
        """
        # This is a probability based scoring heuristic; one designed to calculate the probability
        # of the bot winning rather than assigning arbitrary score values to various strategic successes
        # and failures.
        positionalScoringMap = ((0.25,-0.2,0,0,0,0,-0.2,0.25),
                                (-0.2,-0.25,0,0,0,0,-0.25,-0.2),
                                (0,0,0,0,0,0,0,0),
                                (0,0,0,0,0,0,0,0),
                                (0,0,0,0,0,0,0,0),
                                (0,0,0,0,0,0,0,0),
                                (-0.2,-0.25,0,0,0,0,-0.25,-0.2),
                                (0.25,-0.2,0,0,0,0,-0.2,0.25))
        score = 0.5
        me = self.p2Code
        ILostMyTurn = False
        oppLostTheirTurn = False
        opponent = self.p1Code
        myValidMoves = self.getAllValidMoves(gameState, me, opponent)
        oppValidMoves = self.getAllValidMoves(gameState, opponent, me)
 
        # A dictionary which stores the weights of all strategic variables on the probability of the bot winning.
        # These aren't probabilities; they get converted into probabilities by the following logic
        probDict = {
                    "shieldedPieces": 0.03,
                    "permanentPieces": 0.125,
                    "moveLost": -0.04,
        }
        if not myValidMoves:
            if not oppValidMoves:
                # The game is in a terminal state. If the bot won, return 1.0 for a 100% probability of
                # winning and if the player won, return 0.0 for a 0% probability of the bot winning.
                myScore = 0
                oppScore = 0
                for row in range(8):
                    for col in range(8):
                        if gameState[row][col] == me: myScore += 1
                        if gameState[row][col] == opponent: oppScore += 1
 
                if myScore > oppScore:
                    return 1.0
                elif myScore == oppScore:
                    return 0.5
                else:
                    return 0.0
 
            else:
                # The bot has lost a turn to its opponent. It made a bad move.
                ILostMyTurn = True
 
        if not oppValidMoves: oppLostTheirTurn = True
 
        if ILostMyTurn:
            if isBotsTurn:
                score += probDict["moveLost"]
 
        elif oppLostTheirTurn:
            if not isBotsTurn:
                score -= probDict["moveLost"]
 
        # Count up the positional scores for black and white tiles, add them to the sore if they're the
        # bots tiles and subtract if they're the players tiles.
        for row in range(8):
            for col in range(8):
                if gameState[row][col] == me:
                    score += positionalScoringMap[row][col]
                if gameState[row][col] == opponent:
                    score -= positionalScoringMap[row][col]
 
        score += self.findShieldedPieces(gameState, me, opponent) * probDict["shieldedPieces"]
        score += self.findPermanentPieces(gameState, me, opponent) * probDict["permanentPieces"]
 
        score -= self.findShieldedPieces(gameState, opponent, me) * probDict["shieldedPieces"]
        score -= self.findPermanentPieces(gameState, opponent,me) * probDict["permanentPieces"]
 
        return score
    .
    .
    .
    def findPermanentPieces (self, gameState, me, opponent): # TODO Complete this algorithm.
        """
        PlayerBot.findPermanentPieces(self, gameState, me, opponent)
        Returns the number of pieces in a given game state which can
        never be altered
        """
        permanentPieceCount = 0
 
        # The only way pieces can become permanent is if they're wedged into corners, so we
        # only need to see whether a corner has our color in it and if it doesn't, we can ignore
        # it entirely. However, if two non-opposing corners are filled, special care must be used
        # to ensure that permanent pieces aren't counted twice.
        topLeftHorizontalEdge = 0
        topLeftVerticalEdge = 0
 
        topRightHorizontalEdge = 0
        topRightVerticalEdge = 0
 
        bottomLeftVerticalEdge = 0
        bottomLeftHorizontalEdge = 0
 
        bottomRightVerticalEdge = 0
        bottomRightHorizontalEdge = 0
 
        # Count up contiguous rows and columns of our pieces on the edges starting from the corners until we come to an
        # opponent piece or empty space.
        if gameState[0][0] == me:
            row = 0
            for col in range(8):
                if gameState[row][col] == me:
                    topLeftHorizontalEdge += 1
                else:
                    break
            col = 0
            for row in range(8):
                if gameState[row][col] == me:
                    topLeftVerticalEdge += 1
                else:
                    break
 
        if gameState[0][7] == me:
            row = 0
            for col in range(7, -1, -1):
                if gameState[row][col] == me:
                    topRightHorizontalEdge += 1
                else:
                    break
 
            col = 7
            for row in range(8):
                if gameState[row][col] == me:
                    topRightVerticalEdge += 1
                else:
                    break
 
        if gameState[7][0] == me:
            row = 7
            for col in range(8):
                if gameState[row][col] == me:
                    bottomLeftHorizontalEdge += 1
                else:
                    break
            col = 0
            for row in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomLeftVerticalEdge += 1
                else:
                    break
 
        if gameState[7][7] == me:
            row = 7
            for col in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomRightHorizontalEdge += 1
                else:
                    break
            col = 7
            for row in range(7, -1, -1):
                if gameState[row][col] == me:
                    bottomRightVerticalEdge += 1
                else:
                    break
 
        # The previous algorithm I implemented produced wildly inaccurate numbers and counted permanent pieces that weren't there.
        # I'll come back to this puzzle, but for now, the work of counting all the pieces that are on an edge and protected by a
        # filled corner has already been halfway done. Let's just return that figure for now.
        if topLeftHorizontalEdge != 8:
            topEdge = topLeftHorizontalEdge + topLeftVerticalEdge
        else:
            topEdge = 8
 
        if topLeftVerticalEdge != 8:
            leftEdge = topLeftVerticalEdge + bottomLeftVerticalEdge
        else:
            leftEdge = 8
 
        if topRightVerticalEdge != 8:
            rightEdge = topRightVerticalEdge + bottomRightVerticalEdge
        else:
            rightEdge = 8
 
        if bottomLeftHorizontalEdge != 8:
            bottomEdge = bottomLeftHorizontalEdge + bottomRightHorizontalEdge
        else:
            bottomEdge = 8
 
        # Add up all the edges:
        permanentPieceCount += topEdge + leftEdge + rightEdge + bottomEdge
 
        return permanentPieceCount
 
    def findShieldedPieces (self, gameState, me, opponent):
        """
        PlayerBot.findShieldedPieces(self, gameState, me, opponent)
        Returns the number of pieces which cannot be flipped in the current gamestate.
        """
        shieldedPiecesCount = 0
        movesAvailableToOpponent = self.getAllValidMoves(gameState, me, opponent)
        for sRow in range(8):       # sRow for searchRow, sCol for searchColumn
            for sCol in range(8):
 
                if gameState[sRow][sCol] == me:
 
                    # Try to find a position on the board where the opponent could capture this piece.
                    foundPossibleAttack = False
                    for move in movesAvailableToOpponent:
                        _, gameStateAfterMove = self.scoreMove(move, gameState, me, opponent)
                        if gameStateAfterMove[sRow][sCol] != me: foundPossibleAttack = True
 
                    if not foundPossibleAttack: shieldedPiecesCount += 1
 
        return shieldedPiecesCount
    .
    .
    .
    .
I really want to us ML to make this bot now, but I have no idea where to start. As always, thank you for all your help. The bot wouldn't even have come this far without the advice I've received.
Reply
#28
The only other thing i can think of is creating a few functions. When the bot is doing it's move have it go through these functions. The function will each check the board for a specific circumstance. If the circumstance is true it will carry the move you put in that function. Games like this are really only learned through experience. Since that would take a lot of time there is another idea I have. I made a python bot to talk to. Now I didn't want to just tirelessly put in different responses when someone says a specific thing. So I made a feature where it learns how to talk by how the person response. You could try to use that same concept with your bot. Either option would take a while to input.

AI's are like babies if you think about it. The bot u have here has to act based on specific things. It doesn't have any experience whatsoever.

That's why you can beat it so easily

Hope this helps
Reply
#29
(Apr-30-2019, 11:35 PM)SheeppOSU Wrote:
AI's are like babies if you think about it. The bot u have here has to act based on specific things. It doesn't have any experience whatsoever.

That's why you can beat it so easily

Hope this helps

Quite right. My bot has no experience whatsoever. Nor can it gain experience. I just read a bunch of scholarly articles on the subject and discovered that even back in 1997, when a bot beat a Human world champion for the first time they were already using neural network based machine learning. I think it's reasonable to conclude that I'm not going to get anywhere using minimax. I've greatly underestimated the scale of this challenge and I foresee a great deal of research in my future. Unfortunately, your suggestion of implementing a different function for every possible game state would require 3^60 + 16 different functions. I would spend the rest of my life coding, fill up the hard drive of this computer with code and still never live to see my bot play a respectable game.

I'm thinking I'll use NEAT (NeuroEvolution of Augmenting Topologies) to create my bot. I'll have 64 input nodes for the 64 game spaces and two output nodes for an X and a Y coordinate. I'll need to read a lot of academic papers on the subject to get any more specific than that. I don't even know how I'm going to implement the scoring and training algorithms. One of the papers I looked at said something about the neural networks over-adapting when they train against themselves. I suppose I could solve this problem by introducing a gradient of mutation rates within the population but there are people who have already solved this problem. The bot on my phone plays a mean game and that thing probably handles a Human with kid gloves even on max difficulty. I could even vary the difficulty by saving neural networks at various stages of the training process and using a poorer performing neural network at lower difficulties and a better performer at a higher difficulty. I could even whip out the best performer, (which at the end of the training process might be capable of playing a perfect game) for the highest difficulty. It would make the highest difficulty setting a frustrating nightmare for anybody who tries it, but even being impossible to win against a bot that plays a perfect game, I could only imagine the rush of satisfaction when somebody draws a game against a level 100 bot!
Reply
#30
Ok I see. If someone were to beat a bot like that on 100, not me in a million years, that would be amazing the way you describe it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] adding mouse control to game flash77 7 2,601 May-21-2024, 01:05 AM
Last Post: XavierPlatinum
  get a game to run in full screen mode agencyclearly 1 1,676 May-12-2024, 11:23 AM
Last Post: menator01
  Creating a “Player” class, and then importing it into game onizuka 4 4,361 Sep-01-2020, 06:06 PM
Last Post: onizuka
  Adding an inventory and a combat system to a text based adventure game detkitten 2 9,488 Dec-17-2019, 03:40 AM
Last Post: detkitten
  Adding persistent multiple levels to game michael1789 2 3,425 Nov-16-2019, 01:15 AM
Last Post: michael1789
  Can a player play game created with Python without installing Python? hsunteik 3 6,921 Feb-23-2017, 10:44 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020