forked from chacachiene/ChineseChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (140 loc) · 5.22 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
import pygame as p
import setting as s
import loadimg as l
import chessEngine
import button as b
import playWithMachine as pWM
import drawUI as draw
from ml import playwithdeep as pWD
st = False # START GAME OR NOT
pa = False # PLAY AGAIN OR NOT
robo = False # ROBOT OR NOT USE TO AI PLAY WITH RAMDOM
x = -1 # CHOOSE MODE
'''
FUNCTION: startGame
use to start create the board
'''
def startGame():
global st
st= True
'''
FUNCTION: playAgainGame
use to play again
'''
def playAgainGame():
global pa
pa = True
'''
FUNCTION: setup
use to setup the game before play
'''
def setup():
global pa
global st
global x
p.init()
pa = False
st = False
'''
FUNCTION: shutDown
use to quit the game
'''
def shutDown():
p.quit()
'''
FUNCTION: mainLoop
use to run the game
'''
def mainLoop():
p.display.set_caption('Chinese Chess')
screen = p.display.set_mode((s.SCREEN_WIDTH,s.SCREEN_HEIGHT))
gs = chessEngine.State()
clock = p.time.Clock()
run = True
listClick=[]
# this is the list of button
objects=()
backwardBut = b.Button(s.BACKWARD_X, s.BACKWARD_Y, s.BUT_WIDTH, s.BUT_HEIGHT,'re', l.loadButton('backward'), gs.reMoveReal)
nextstepBut = b.Button(s.NEXTSTEP_X, s.NEXTSTEP_Y, s.BUT_WIDTH, s.BUT_HEIGHT,'ne', l.loadButton('nextstep'), gs.nextMoveReal)
reverseBut = b.SButton(s.REVERSE_X, s.REVERSE_Y, s.BUT_WIDTH, s.BUT_HEIGHT,'ex', l.loadButton('reverse'), gs.reverse)
startBut = b.Button(s.START_X, s.START_Y, s.BUT_WIDTH, s.BUT_HEIGHT,'st', l.loadButton('start'), startGame)
playAgainBut = b.SButton(s.REPLAY_X, s.REPLAY_Y, s.BUT_WIDTH, s.BUT_HEIGHT,'pa', l.loadButton('replay'), playAgainGame)
objects += (backwardBut,nextstepBut,reverseBut,startBut,playAgainBut)
# create the mode = -1 mean not choose mode
x = -1
while run:
global st
global pa
global robo
for e in p.event.get():
if x != -1:
draw.drawGameState(screen,gs,st)
for o in objects:
o.process(screen,gs)
if x == -1:
x = draw.drawStart(screen, gs)
if st:
draw.drawGameState(screen,gs,st)
clock.tick(s.MAX_FPS)
p.display.flip()
if x==0:
pass
elif x == 1:
pWM.playWithAI(gs,1) # play with random
elif x == 2:
pWM.playWithAI(gs,2) # play with chaca
elif x ==3:
pWM.playWithAI(gs,3) # play with chacaPro
elif x == 4:
robo = True
if not gs.redMove and not gs.after:
draw.drawFoot(screen,gs)
move = pWM.test(gs) # watch them play
if move != None:
gs.makeMove(move)
elif x == 5:
pWD.playWithChaca(gs) # play with chacachien
# play together
if e.type == p.QUIT:
run = False
elif e.type == p.MOUSEBUTTONDOWN:
if st == False or robo: continue # if not start or robot with robot, not click
start = s.GRID
pos = p.mouse.get_pos() # get the position of mouse
row = int((pos[1]-start[0])//start[2])
col = int((pos[0]-start[1])//start[2])
if row >9 or col >8 or row <0 or col <0:
break
if listClick ==[]:
if (gs.redMove and gs.board[row][col][0] == 'b') or (not gs.redMove and gs.board[row][col][0] == 'r'): break
listClick.append((row,col))
if 0<= row <=9 and 0<= col <=8:
if gs.board[listClick[0][0]][listClick[0][1]]=='---':
listClick =[]
else:
gs.selectedCell = listClick[0]
if len(listClick) ==2:
if listClick[0] == listClick[1]:
listClick =[]
else:
listValid = gs.checkValid(gs.selectedCell)
if listClick[1] in listValid:
move = chessEngine.Move(gs.board,listClick[0], listClick[1])
gs.makeMove(move)
draw.drawGameState(screen,gs,st)
clock.tick(s.MAX_FPS)
p.display.flip()
listClick =[]
gs.selectedCell = ()
# if click play again
if pa:
pa = False
main()
clock.tick(s.MAX_FPS)
p.display.flip()
def main():
setup()
mainLoop()
shutDown()
if __name__ == '__main__':
main()