-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessgame.py
177 lines (131 loc) · 4.64 KB
/
chessgame.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
import math
import pygame as flav
import game
import rendering
import ai
import sound
import music
selectedPiece = None
def init():
global newGame, newRendering, CurrentAI # module-level globals (scope NOT shared among all files in program)
newGame = None
newRendering = None
def VisualisePossibleMoves(piece):
possibleMoves = piece.GeneratePossibleMoves(newGame.Turn, True)
newRendering.Markers = []
for move in possibleMoves:
newRendering.Markers.append(move)
newRendering.RenderingChange = True
def ResetSelection():
global selectedPiece
selectedPiece = None
newRendering.Markers = []
newRendering.RenderingChange = True
def MakeMove(piece,move):
global newGame
ResetSelection()
Valuation = newGame.MakeMove(piece, move)
if Valuation:
if Valuation == "s":
newGame.CompletedFlag = "Victory"
elif Valuation == 9999:
newGame.CompletedFlag = "Victory"
else:
newGame.CompletedFlag = "Defeat"
def UndoMove():
ResetSelection()
return newGame.UndoMove()
def AIMove():
print("generating AI move")
AIMove = CurrentAI.GenerateMove()
print("post AI generation")
if AIMove:
MakeMove(AIMove[0], AIMove[1])
else:
print("CHECKMATE")
def selectPiece(mousePos):
if (mousePos[0] < newRendering.BottomRight[0]) and (mousePos[0] > newRendering.TopLeft[0]) and ((mousePos[1] > newRendering.TopLeft[1]) and (mousePos[1] < newRendering.BottomRight[1])):
squarePos = (math.floor((mousePos[0] - newRendering.TopLeft[0])/newRendering.SquareSize[0]), 7 - math.floor((mousePos[1] - newRendering.TopLeft[0])/newRendering.SquareSize[1]))
global selectedPiece
MoveMade = False
if selectedPiece: # moving to a square
for move in newRendering.Markers:
if (move[0] == squarePos[0]) and (move[1] == squarePos[1]):
MakeMove(selectedPiece,move)
MoveMade = True
# selecting a piece
if MoveMade:
newRendering.FullRenderBoard(newGame.BoardState)
flav.display.flip()
sound.PlaySound("Move")
if CurrentAI:
AIMove()
else:
for piece in newGame.BoardState:
if (piece.PositionX == squarePos[0]) and (piece.PositionY == squarePos[1]) and (piece.Side == newGame.Side):
if piece == selectedPiece:
break
selectedPiece = piece
VisualisePossibleMoves(piece)
break
#nl = newGame.GeneratePiece("n", "d", 4, 4)
def DeselectPiece():
global selectedPiece
selectedPiece = None
newRendering.Markers = []
newRendering.RenderingChange = True
running = True
# Input Pre-Loop Setup
InputRegister = { # stores a record of CanRegisterInput
"RightClick":True,
"LeftClick":True
}
def RegisterInput(State, InputName):
if State: # right click
if InputRegister[InputName]:
InputRegister[InputName] = False
return True
else:
InputRegister[InputName] = True
# Music Pre-Loop Setup
def History():
n = 2
strn = "1"
for move in newGame.History:
if ((n % 2) == 0) and strn != "1":
print(strn)
strn = str(int(n/2))
strn += " " + move
n += 1
if (n % 2) == 1:
print(strn)
def Start(Opponent, Screen, ScreenSize):
# Need to load playlists first (songs already preloaded)
global CurrentAI, newGame, newRendering
newGame = game.Game()
newGame.ResetBoard()
newRendering = rendering.Rendering(screen=Screen, screenSize=ScreenSize, decimalScreen = 0.8, decimalPieceFromSquare = 0.9)
newRendering.initialise()
# Choosing AI
if Opponent and Opponent in ai.AIMappings:
CurrentAI = ai.AIMappings[Opponent]
CurrentAI.GameInstance = newGame
else:
CurrentAI = None
def ProcessEvents(self, Events):
if newGame.CompletedFlag:
History()
return newGame.CompletedFlag
for event in Events:
if event.type == flav.KEYDOWN:
if event.key == flav.K_u:
UndoMove()
states = flav.mouse.get_pressed()
if RegisterInput(states[2], "RightClick"):
DeselectPiece()
if RegisterInput(states[0], "LeftClick"):
selectPiece(flav.mouse.get_pos())
def Render(self, Screen):
if newRendering.RenderingChange: # Rendering
newRendering.RenderingChange = False
newRendering.FullRenderBoard(newGame.BoardState)