|
4 | 4 |
|
5 | 5 | This is the gameWindow file. It creates the GUI for the Tic-Tac-Toe game and allows the user to play against Joe. |
6 | 6 |
|
| 7 | +
|
| 8 | +BUILD 7.0.2 |
7 | 9 | ''' |
8 | 10 |
|
| 11 | +from time import sleep |
9 | 12 | import tkinter as tk |
10 | 13 | import keras |
11 | 14 | import numpy as np |
|
27 | 30 | joeWins = ["I win!", "I'm the best!", "I'm unbeatable!", "I'm the champion!", "I'm the king!"] |
28 | 31 | joeLosses = ["You win this time...", "You got lucky...", "I'll get you next time...", "I'll be back!", "I am just an average Joe..."] |
29 | 32 |
|
30 | | -model = keras.models.load_model('6.5.6st/a0.01_g0.9_i10000.keras') |
| 33 | +model = keras.models.load_model('a0.01_g0.9_i10000.keras') |
31 | 34 |
|
32 | 35 | # Globals |
33 | 36 | board = bd.Board() |
@@ -103,6 +106,14 @@ def getJoeMove(board): |
103 | 106 |
|
104 | 107 | # get best action |
105 | 108 | qValues = model.predict(board.vector.reshape(1,-1), verbose=0)[0] |
| 109 | + |
| 110 | + # if first move, choose randomly from top 3 actions |
| 111 | + if np.count_nonzero(board.vector == joeTurn) == 0 and joeTurn == X: |
| 112 | + qValues = np.argsort(qValues) |
| 113 | + qValues = qValues[0:3] # get top 3 actions |
| 114 | + action = np.random.choice(qValues) # choose randomly from top 3 |
| 115 | + |
| 116 | + # otherwise, choose best action |
106 | 117 | for action in np.argsort(qValues)[::-1]: |
107 | 118 | if board.validMove(*divmod(action, 3)): |
108 | 119 | break |
@@ -137,6 +148,9 @@ def resetGame(): |
137 | 148 | buttons[row][col].config(text=" ") |
138 | 149 |
|
139 | 150 | currentPlayer = X |
| 151 | + if joeTurn == X: |
| 152 | + sleep(0.5) |
| 153 | + joeMove() |
140 | 154 | updateStatusLabel() |
141 | 155 |
|
142 | 156 |
|
@@ -184,7 +198,7 @@ def switchJoeTurn(): |
184 | 198 |
|
185 | 199 | # update the switch button text |
186 | 200 | def updateSwitchButton(): |
187 | | - switchButton.config(text=f"Make {determinePlayer(O)} Go First", font=("Helvetica", 16)) |
| 201 | + switchButton.config(text=f"Make {determinePlayer(O)} Go First") |
188 | 202 |
|
189 | 203 |
|
190 | 204 | # update the status label |
|
0 commit comments