-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleChessClient.py
162 lines (136 loc) · 7.38 KB
/
SampleChessClient.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
#!/usr/bin/env python
from ChessBoard import ChessBoard
import os, pygame,math
from pygame.locals import *
from pprint import pprint
class SampleChessClient:
def mainLoop(self):
pygame.init()
pieces = {}
chess = ChessBoard()
board = chess.getBoard()
turn = chess.getTurn()
screen = pygame.display.set_mode((480, 480),1)
pygame.display.set_caption('ChessBoard Client')
# load all images
pieces = [{},{}]
pieces[0]["r"] = pygame.image.load("./img.sample/brw.png")
pieces[0]["n"] = pygame.image.load("./img.sample/bnw.png")
pieces[0]["b"] = pygame.image.load("./img.sample/bbw.png")
pieces[0]["k"] = pygame.image.load("./img.sample/bkw.png")
pieces[0]["q"] = pygame.image.load("./img.sample/bqw.png")
pieces[0]["p"] = pygame.image.load("./img.sample/bpw.png")
pieces[0]["R"] = pygame.image.load("./img.sample/wrw.png")
pieces[0]["N"] = pygame.image.load("./img.sample/wnw.png")
pieces[0]["B"] = pygame.image.load("./img.sample/wbw.png")
pieces[0]["K"] = pygame.image.load("./img.sample/wkw.png")
pieces[0]["Q"] = pygame.image.load("./img.sample/wqw.png")
pieces[0]["P"] = pygame.image.load("./img.sample/wpw.png")
pieces[0]["."] = pygame.image.load("./img.sample/w.png")
pieces[1]["r"] = pygame.image.load("./img.sample/brb.png")
pieces[1]["n"] = pygame.image.load("./img.sample/bnb.png")
pieces[1]["b"] = pygame.image.load("./img.sample/bbb.png")
pieces[1]["k"] = pygame.image.load("./img.sample/bkb.png")
pieces[1]["q"] = pygame.image.load("./img.sample/bqb.png")
pieces[1]["p"] = pygame.image.load("./img.sample/bpb.png")
pieces[1]["R"] = pygame.image.load("./img.sample/wrb.png")
pieces[1]["N"] = pygame.image.load("./img.sample/wnb.png")
pieces[1]["B"] = pygame.image.load("./img.sample/wbb.png")
pieces[1]["K"] = pygame.image.load("./img.sample/wkb.png")
pieces[1]["Q"] = pygame.image.load("./img.sample/wqb.png")
pieces[1]["P"] = pygame.image.load("./img.sample/wpb.png")
pieces[1]["."] = pygame.image.load("./img.sample/b.png")
clock = pygame.time.Clock()
posRect = pygame.Rect(0,0,60,60)
mousePos = [-1,-1]
markPos = [-1,-1]
validMoves = []
gameResults = ["","WHITE WINS!","BLACK WINS!","STALEMATE","DRAW BY THE FIFTHY MOVES RULE","DRAW BY THE THREE REPETITION RULE"]
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
return
elif event.key == K_LEFT:
chess.undo()
elif event.key == K_RIGHT:
chess.redo()
elif event.unicode in ("f","F"):
print chess.getFEN()
elif event.unicode in ("a","A"):
an = chess.getAllTextMoves(chess.AN)
if an:
print "AN: " + ", ".join(an)
elif event.unicode in ("s","S"):
san = chess.getAllTextMoves(chess.SAN)
if san:
print "SAN: " + ", ".join(san)
elif event.unicode in ("l","L"):
lan = chess.getAllTextMoves(chess.LAN)
if lan:
print "LAN: " + ", ".join(lan)
board = chess.getBoard()
turn = chess.getTurn()
markPos[0] = -1
validMoves = []
if not chess.isGameOver():
if event.type == MOUSEMOTION:
mx = event.pos[0]
my = event.pos[1]
mousePos[0] = mx/60
mousePos[1] = my/60
elif event.type == MOUSEBUTTONDOWN:
if mousePos[0] != -1:
if markPos[0] == mousePos[0] and markPos[1] == mousePos[1]:
markPos[0] = -1
validMoves = []
else:
if (turn==ChessBoard.WHITE and board[mousePos[1]][mousePos[0]].isupper()) or \
(turn==ChessBoard.BLACK and board[mousePos[1]][mousePos[0]].islower()):
markPos[0] = mousePos[0]
markPos[1] = mousePos[1]
validMoves = chess.getValidMoves(tuple(markPos))
else:
if markPos[0] != -1:
res = chess.addMove(markPos,mousePos)
if not res and chess.getReason() == chess.MUST_SET_PROMOTION:
chess.setPromotion(chess.QUEEN)
res = chess.addMove(markPos,mousePos)
if res:
#print chess.getLastMove()
print chess.getLastTextMove(chess.SAN)
board = chess.getBoard()
turn = chess.getTurn()
markPos[0] = -1
validMoves = []
if chess.isGameOver():
pygame.display.set_caption("Game Over! (Reason:%s)" % gameResults[chess.getGameResult()])
validMove = []
markPos[0] = -1
markPos[1] = -1
else:
pygame.display.set_caption('ChessBoard Client')
y = 0
for rank in board:
x = 0
for p in rank:
screen.blit(pieces[(x+y)%2][p],(x*60,y*60))
x+=1
y+=1
if markPos[0] != -1:
posRect.left = markPos[0]*60
posRect.top = markPos[1]*60
pygame.draw.rect(screen, (255,255,0),posRect, 4)
for v in validMoves:
posRect.left = v[0]*60
posRect.top = v[1]*60
pygame.draw.rect(screen, (255,255,0),posRect, 4)
pygame.display.flip()
def main():
g = SampleChessClient()
g.mainLoop()
#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()