-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
211 lines (159 loc) · 5.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import sys
import sqlite3
from OpenGL.GL import *
from OpenGL.GLUT import *
from gui.BoardLines import BoardLines, BoardLinesTimer
from gui.LetterO import LetterO, LetterO_State_Change
from gui.LetterX import LetterX, LetterX_State_Change
from gui.EndLines import EndLineDraw
from MLPlayer import GameStatus, NextMove
# Global Variable
BOARD_STATE: list = [2 for _ in range(9)]
KEY_PRESSED: bool = False
PRESSED_KEY: bytes = bytes()
GAME_STATUS: bool = True
# Game Reset
def GameReset():
global BOARD_STATE, KEY_PRESSED, PRESSED_KEY, GAME_STATUS
BOARD_STATE = [2 for _ in range(9)]
KEY_PRESSED = False
PRESSED_KEY = bytes()
GAME_STATUS = True
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
BoardLines()
BoardLinesTimer(reset=True)
glutPostRedisplay()
# GamePlay
def GamePlay():
global BOARD_STATE, KEY_PRESSED, PRESSED_KEY, GAME_STATUS
if KEY_PRESSED and PRESSED_KEY.decode("utf-8") == "r":
GameReset()
return
if GAME_STATUS is False:
GameReset()
return
if KEY_PRESSED:
key: int = int(PRESSED_KEY.decode("utf-8"))
KEY_PRESSED = False
else:
return
glutSwapBuffers()
def KeyAction():
global BOARD_STATE, GAME_STATUS
LetterX(LetterX_State_Change(key))
BOARD_STATE[key - 1] = 0
if 2 in BOARD_STATE:
COMPUTER: int = NextMove(BOARD_STATE)
if COMPUTER == -1:
return
BOARD_STATE[COMPUTER] = 1
LetterO(LetterO_State_Change(COMPUTER + 1))
GameRecordStore(win=False)
else:
GAME_STATUS = False
if key == 1 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 2 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 3 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 4 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 5 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 6 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 7 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 8 and BOARD_STATE[key - 1] == 2:
KeyAction()
elif key == 9 and BOARD_STATE[key - 1] == 2:
KeyAction()
# print(BOARD_STATE[0], BOARD_STATE[1], BOARD_STATE[2])
# print(BOARD_STATE[3], BOARD_STATE[4], BOARD_STATE[5])
# print(BOARD_STATE[6], BOARD_STATE[7], BOARD_STATE[8])
# Winning Data Store
def GameRecordStore(win: bool):
global BOARD_STATE
conn = sqlite3.connect("GameRecords.sqlite3")
cursor = conn.cursor()
if win:
query: str = f"INSERT INTO 'records' VALUES({', '.join([str(_) for _ in BOARD_STATE])}, '{str((GameStatus(BOARD_STATE))['Winner'])}');"
else:
query: str = f"INSERT INTO 'records' VALUES({', '.join([str(_) for _ in BOARD_STATE])}, 'STEP');"
cursor.execute(query)
conn.commit()
conn.close()
# Display
def display():
glClear(GL_COLOR_BUFFER_BIT)
BoardLines()
GamePlay()
glutSwapBuffers()
# Reshaping
def reshape(width: GLsizei, height: GLsizei):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-10, 10, -10, 10, -10, 10)
glMatrixMode(GL_MODELVIEW)
# Timer
def timer(_: int):
global BOARD_STATE, GAME_STATUS
glutTimerFunc(1000 // 60, timer, 0)
if BoardLinesTimer(reset=False):
glutPostRedisplay()
result: dict = GameStatus(BOARD_STATE)
if result.get("Winner") is not None:
if GAME_STATUS is True:
glutSwapBuffers()
EndLineDraw(cells=result.get("Cells"))
glutSwapBuffers()
GameRecordStore(win=True)
GAME_STATUS = False
# Initialization
def initialize():
glClearColor(28 / 255, 170 / 255, 156 / 255, 1)
glClear(GL_COLOR_BUFFER_BIT)
# Keyboard Down
def Keyboard_DOWN(key: bytes, x: int, y: int):
# key: str = key.decode("utf-8")
# print(f"Down \"{key}\"")
pass
# Keyboard Up
def Keyboard_UP(key: bytes, x: int, y: int):
global KEY_PRESSED, PRESSED_KEY
KEY_PRESSED = True
PRESSED_KEY = key
# print(f"Up \"{key}\"")
glutPostRedisplay()
# Main
def main():
glutInit(sys.argv)
# Position the window's center
width = height = 400
glutInitWindowPosition(
(glutGet(GLUT_SCREEN_WIDTH) - width) // 2,
(glutGet(GLUT_SCREEN_HEIGHT) - height) // 2,
)
glutInitWindowSize(width, height)
# Create a double-buffer RGBA window. (Single-buffering is possible.
# So is creating an index-mode window.)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
# Create a window, setting its title
glutCreateWindow("Tic Tac Toe (You're X)")
# Initialization
initialize()
# Set the display callback. You can set other callbacks for the keyboard and mouse events.
glutDisplayFunc(display)
# Keyboard
glutKeyboardUpFunc(Keyboard_UP)
glutKeyboardFunc(Keyboard_DOWN)
# Reshape
glutReshapeFunc(reshape)
# Timer
glutTimerFunc(1000, timer, 0)
# Run the GLUT main loop until the user closes the window.
glutMainLoop()
if __name__ == "__main__":
main()