def testRook(self): self.board = cb.chessBoard() currPos = [] currPos.append(1) # (1,1) currPos.append(1) pieceType = 4 # Rook color = "Black" self.board.grid[currPos[0]][currPos[1]] = pieceType endPos1 = [] # (2,5) : False endPos1.append(2) endPos1.append(5) endPos2 = [] # (7,2) : False endPos2.append(7) endPos2.append(2) endPos3 = [] # (5,1) : True endPos3.append(5) endPos3.append(1) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos1, color)) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos2, color)) self.assertTrue(self.board.turnValid(self.board.grid, currPos, endPos3, color))
def testBishop(self): self.board = cb.chessBoard() currPos = [] currPos.append(1) # (1,1) currPos.append(1) pieceType = 3 # Bishop color = "Black" self.board.grid[currPos[0]][currPos[1]] = pieceType endPos1 = [] # (1,2) : False endPos1.append(1) endPos1.append(2) endPos2 = [] # (4,5) : False endPos2.append(4) endPos2.append(5) endPos3 = [] # (3,3) : True endPos3.append(3) endPos3.append(3) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos1, color)) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos2, color)) self.assertTrue(self.board.turnValid(self.board.grid, currPos, endPos3, color))
def testPawn(self): self.board = cb.chessBoard() currPos = [] currPos.append(1) # (1,1) currPos.append(1) pieceType = 1 # Pawn color = "Black" self.board.grid[currPos[0]][currPos[1]] = pieceType endPos1 = [] # (2,4) : False endPos1.append(2) endPos1.append(4) endPos2 = [] # (1,2) : False endPos2.append(1) endPos2.append(2) endPos3 = [] # (1,2) : True endPos3.append(2) endPos3.append(1) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos1, color)) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos2, color)) self.assertTrue(self.board.turnValid(self.board.grid, currPos, endPos3, color))
def testKnight(self): self.board = cb.chessBoard() currPos = [] currPos.append(1) # (1,1) currPos.append(1) pieceType = 2 # Knight color = "Black" self.board.grid[currPos[0]][currPos[1]] = pieceType endPos1 = [] # (7,7) : False endPos1.append(7) endPos1.append(7) endPos2 = [] # (2,1) : False endPos2.append(1) endPos2.append(2) endPos3 = [] # (3,2) : True endPos3.append(2) endPos3.append(3) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos1, color)) self.assertFalse(self.board.turnValid(self.board.grid, currPos, endPos2, color)) self.assertTrue(self.board.turnValid(self.board.grid, currPos, endPos3, color))
def joinGame(): content = request.json print(content) email = content['email'] gameID = content['gameID'] foundGame = GameService.get_game_by_id(gameID) if foundGame is None: return {"Success": False, "Message": "Could not find game"} game = chessGame() game.gameID = gameID board = foundGame['chessBoard'] game.chessBoard = chessBoard(board['size']) game.chessBoard.piecesWhite = board['piecesWhite'] game.chessBoard.piecesBlack = board['piecesBlack'] game.chessBoard.whoseTurn = 'White' game.chessBoard.gameID = gameID game.chessBoard.whiteQueen = 5 game.chessBoard.blackQueen = 5 game.blackPlayer = foundGame['blackPlayer'] game.completed = False game.winner = '' if not game.playerJoin(email): return {"success": False, "message": "Could not join game"} success, message = GameService.update_game(gameID, game.todict()) return {"success": success, "message": message}
def __init__(self, email, color, size): self.gameID = randint(100000, 999999) # while not Services.GameService.check_game_id_unused(self.gameID): # self.gameID = randint(100000, 999999) self.chessBoard = chessBoard(size) if color.lower() == "white": self.whitePlayer = email elif color.lower() == "black": self.blackPlayer = email else: self.whitePlayer = email return
class App: board = cb.chessBoard() missingPiecesBlack = [] itemClicked = False colors = ['White','Black'] missingPiecesWhite = [] t = 0 comOp = False def __init__(self,master): self.Master=master master.title("Sah") self.canvas = Canvas(self.Master) self.pieces = [tk.PhotoImage(file = RESURSE + 'SquareWhite.gif'),tk.PhotoImage(file=RESURSE +'PawnBlack.gif'),tk.PhotoImage(file=RESURSE+'KnightBlack.gif'),tk.PhotoImage(file=RESURSE+'BishopBlack.gif'), tk.PhotoImage(file=RESURSE+'RookBlack.gif'),tk.PhotoImage(file=RESURSE+'QueenBlack.gif'),tk.PhotoImage(file=RESURSE+'KingBlack.gif'),tk.PhotoImage(file=RESURSE+'PawnWhite.gif'), tk.PhotoImage(file=RESURSE+'KnightWhite.gif'),tk.PhotoImage(file=RESURSE+'BishopWhite.gif'),tk.PhotoImage(file=RESURSE+'RookWhite.gif'),tk.PhotoImage(file=RESURSE+'QueenWhite.gif'), tk.PhotoImage(file=RESURSE+'KingWhite.gif')] self.activePieces = [tk.PhotoImage(file=RESURSE+'SquareWhite.gif'),tk.PhotoImage(file=RESURSE+'PawnBlackAct.gif'),tk.PhotoImage(file=RESURSE+'KnightBlackAct.gif'), tk.PhotoImage(file=RESURSE+'BishopBlackAct.gif'), tk.PhotoImage(file=RESURSE+'RookBlackAct.gif'),tk.PhotoImage(file=RESURSE+'QueenBlackAct.gif'),tk.PhotoImage(file=RESURSE+'KingBlackAct.gif'), tk.PhotoImage(file=RESURSE+'PawnWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'KnightWhiteAct.gif'),tk.PhotoImage(file=RESURSE+'BishopWhiteAct.gif'),tk.PhotoImage(file=RESURSE+'RookWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'QueenWhiteAct.gif'), tk.PhotoImage(file=RESURSE+'KingWhiteAct.gif')] self.emptySpaces = [tk.PhotoImage(file=RESURSE+'SquareWhite.gif'),tk.PhotoImage(file=RESURSE+'SquareGrey.gif'),tk.PhotoImage(file=RESURSE+'SquareActive.gif'), tk.PhotoImage(file=RESURSE+'SquareClicked.gif')] self.frame = Frame(master); self.frame.grid() self.play = Label(self.frame, text = "Impotriva botului sau doi jucatori?") self.play.grid(row=0, column =0, columnspan = 6) self.YesButton = Button(self.frame, text = "Impotriva botului", command = self.Yes) root.bind('<Return>', (lambda e, YesButton=self.YesButton: self.YesButton.invoke())) self.YesButton.grid(row=1,column=0) self.NoButton = Button(self.frame, text='Doi jucatori', command=self.No) self.NoButton.grid(row=1,column =1) def Yes(self): self.comOp = True self.computer = com.player() self.frame.destroy() self.startGame() def No(self): self.frame.destroy() self.startGame() def startGame(self): self.Master.minsize(WIDTH,HEIGHT) self.canvas.config(height=HEIGHT,width=WIDTH,bg = 'black') self.canvas.pack() self.Master.update() self.displayBoard() def displayBoard(self): for i in range(8): for j in range(8): lH = 10 + 96*j lW = 10 + 96*i self.canvas.create_image(lW,lH,image=self.emptySpaces[(i+j)%2],anchor=NW,activeimage = self.emptySpaces[2]) for r in range(8): for c in range(8): lW = 10 + 96*c lH = 10 + 96*r if self.board.grid[r][c] != 0: self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW,activeimage = self.activePieces[self.board.grid[r][c]]) for k in range(12): lH = 10 + 61*k self.canvas.create_image(HEIGHT + 1,lH,image=self.emptySpaces[0],anchor=NW) for l in range(len(self.missingPiecesBlack)): lH = 10 + 96*l self.canvas.create_image(HEIGHT + 1,lH,image=self.pieces[self.missingPiecesBlack[l]],anchor=NW) for n in range(len(self.missingPiecesWhite)): lH = 394 + 96*n self.canvas.create_image(HEIGHT + 1,lH,image=self.pieces[self.missingPiecesWhite[n]],anchor=NW) self.Master.update() def callback(self,event): color = self.colors[self.t%2] if self.itemClicked == False: for r in range(len(self.board.grid)): for c in range(len(self.board.grid[r])): lH = 10 + 96*r lW = 10 + 96*c if event.x in range(lW,lW+90) and event.y in range(lH,lH+90) and self.board.grid[r][c] != 0 and self.board.pieces[self.board.grid[r][c]][1] == color: self.canvas.create_image(lW,lH,image=self.emptySpaces[3],anchor=NW) self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW) self.itemClicked = True self.curPos = [r,c] print (r, c) else: for r in range(len(self.board.grid)): for c in range(len(self.board.grid[r])): lH = 10 + 96*r lW = 10 + 96*c if event.x in range(lW,lW+90) and event.y in range(lH,lH+90): if r == self.curPos[0] and c == self.curPos[1]: self.canvas.create_image(lW,lH,image=self.emptySpaces[(r+c)%2],anchor=NW) self.canvas.create_image(lW,lH,image=self.pieces[self.board.grid[r][c]],anchor=NW,activeimage = self.activePieces[self.board.grid[r][c]]) self.itemClicked = False print (r, c, 'fail') else: self.finPos = [r,c] if self.board.turnValid(self.board.grid,self.curPos,self.finPos,color): self.itemClicked = False self.t += 1 print (r, c) move = self.grid_to_uci(self.curPos, self.finPos) self.computer.user_move(move) #Add to missing pieces list --------------------------------------------------- if self.board.grid[self.finPos[0]][self.finPos[1]]%6 > 1 and color == 'White': self.missingPiecesBlack.append(self.board.grid[self.finPos[0]][self.finPos[1]]) if len(self.missingPiecesBlack) > 4: self.missingPiecesBlack.remove(min(self.missingPiecesBlack)) elif self.board.grid[self.finPos[0]][self.finPos[1]]%6 > 1 and color == 'Black': self.missingPiecesWhite.append(self.board.grid[self.finPos[0]][self.finPos[1]]) if len(self.missingPiecesWhite) > 4: self.missingPiecesWhite.remove(min(self.missingPiecesWhite)) # -------------------------------------------------------------- self.board.grid[self.finPos[0]][self.finPos[1]] = self.board.grid[self.curPos[0]][self.curPos[1]] self.board.grid[self.curPos[0]][self.curPos[1]] = 0 #Pawn promotion ----------------------------------------------------------- if (self.board.grid[self.finPos[0]][self.finPos[1]] == 1 and r == 7) or (self.board.grid[self.finPos[0]][self.finPos[1]] == 7 and r == 0): if color == 'White' and len(self.missingPiecesWhite) > 0: self.board.grid[self.finPos[0]][self.finPos[1]] = max(self.missingPiecesWhite) self.missingPiecesWhite.remove(max(self.missingPiecesWhite)) elif color == "Black" and len(self.missingPiecesBlack) > 0: self.board.grid[self.finPos[0]][self.finPos[1]] = max(self.missingPiecesBlack) self.missingPiecesBlack.remove(max(self.missingPiecesBlack)) # ----------------------------------------------------------- self.canvas.delete(ALL) self.displayBoard() if self.comOp == True and self.t%2 == 1: color = self.colors[self.t%2] choice = self.computer.makeMove2(self.board,color) print(choice[1],choice[2],choice[3]) # move = self.computer.computer_move() # choice = self.uci_to_grid(str(move)) # choice = [[]] + choice print (choice[1], choice[2]) #Add to missing pieces list --------------------------------------------------- if self.board.grid[choice[2][0]][choice[2][1]]%6 > 1 and color == 'Black': self.missingPiecesWhite.append(self.board.grid[choice[2][0]][choice[2][1]]) if len(self.missingPiecesWhite) > 4: self.missingPiecesWhite.remove(min(self.missingPiecesWhite)) elif self.board.grid[choice[2][0]][choice[2][1]]%6 > 1 and color == 'White': self.missingPiecesBlack.append(self.board.grid[choice[2][0]][choice[2][1]]) if len(self.missingPiecesBlack) > 4: self.missingPiecesBlack.remove(min(self.missingPiecesBlack)) # ----------------------------------------------------------- self.board.grid[choice[2][0]][choice[2][1]] = self.board.grid[choice[1][0]][choice[1][1]] self.board.grid[choice[1][0]][choice[1][1]] = 0 #Pawn promotion ----------------------------------------------------------- if (self.board.grid[choice[2][0]][choice[2][1]] == 1 and r == 7) or (self.board.grid[choice[2][0]][choice[2][1]] == 7 and r == 0): if color == 'Black' and len(self.missingPiecesBlack) > 0: self.board.grid[choice[2][0]][choice[2][1]] = max(self.missingPiecesBlack) self.missingPiecesBlack.remove(max(self.missingPiecesBlack)) elif color == "White" and len(self.missingPiecesWhite) > 0: self.board.grid[choice[2][0]][choice[2][1]] = max(self.missingPiecesWhite) self.missingPiecesWhite.remove(max(self.missingPiecesWhite)) # ----------------------------------------------------------- # print() # print(choice[1],choice[2],choice[3]) # print() self.t += 1 self.canvas.delete(ALL) self.displayBoard() self.Master.update() def uci_to_grid(self, move): move_from = [8 - (ord(move[1]) - ord('0')), ord(move[0]) - 97] move_to = [8 - (ord(move[3]) - ord('0')), ord(move[2]) - 97] return [move_from, move_to] def grid_to_uci(self, move_from, move_to): move = [] move.append(chr(move_from[1]+97)) move.append(chr(8-move_from[0]+ord('0'))) move.append(chr(move_to[1]+97)) move.append(chr(8-move_to[0]+ord('0'))) return ''.join(move)
def createSolutionBoard(self): self.mainboard = chessBoard(self.size, True) self.mainboard.createAndEvalBoard(False)
class App: board = cb.chessBoard() t = 0 itemClicked = False comOp = False colors = ['White', 'Black'] missingPiecesBlack = [] missingPiecesWhite = [] def __init__(self, master): self.Master = master master.title("Chess") self.canvas = Canvas(self.Master) self.pieces = [ tk.PhotoImage(file='SquareWhite.gif'), tk.PhotoImage(file='PawnBlack.gif'), tk.PhotoImage(file='KnightBlack.gif'), tk.PhotoImage(file='BishopBlack.gif'), tk.PhotoImage(file='RookBlack.gif'), tk.PhotoImage(file='QueenBlack.gif'), tk.PhotoImage(file='KingBlack.gif'), tk.PhotoImage(file='PawnWhite.gif'), tk.PhotoImage(file='KnightWhite.gif'), tk.PhotoImage(file='BishopWhite.gif'), tk.PhotoImage(file='RookWhite.gif'), tk.PhotoImage(file='QueenWhite.gif'), tk.PhotoImage(file='KingWhite.gif') ] self.activePieces = [ tk.PhotoImage(file='SquareWhite.gif'), tk.PhotoImage(file='PawnBlackAct.gif'), tk.PhotoImage(file='KnightBlackAct.gif'), tk.PhotoImage(file='BishopBlackAct.gif'), tk.PhotoImage(file='RookBlackAct.gif'), tk.PhotoImage(file='QueenBlackAct.gif'), tk.PhotoImage(file='KingBlackAct.gif'), tk.PhotoImage(file='PawnWhiteAct.gif'), tk.PhotoImage(file='KnightWhiteAct.gif'), tk.PhotoImage(file='BishopWhiteAct.gif'), tk.PhotoImage(file='RookWhiteAct.gif'), tk.PhotoImage(file='QueenWhiteAct.gif'), tk.PhotoImage(file='KingWhiteAct.gif') ] self.emptySpaces = [ tk.PhotoImage(file='SquareWhite.gif'), tk.PhotoImage(file='SquareGrey.gif'), tk.PhotoImage(file='SquareActive.gif'), tk.PhotoImage(file='SquareClicked.gif') ] self.frame = Frame(master) self.frame.grid() self.play = Label( self.frame, text="Single player (against computer) or two players?") self.play.grid(row=0, column=0, columnspan=6) self.YesButton = Button(self.frame, text="Single Player", command=self.Yes) root.bind( '<Return>', (lambda e, YesButton=self.YesButton: self.YesButton.invoke())) self.YesButton.grid(row=1, column=0) self.NoButton = Button(self.frame, text='Two Players', command=self.No) self.NoButton.grid(row=1, column=1) def Yes(self): self.comOp = True self.computer = com.chessCom() self.frame.destroy() self.startGame() def No(self): self.frame.destroy() self.startGame() def startGame(self): self.Master.minsize(874, 778) #Width, height self.canvas.config(height=778, width=874, bg='black') self.canvas.pack() self.Master.update() self.displayBoard() def displayBoard(self): for i in range(8): for j in range(8): lW = 10 + 96 * i lH = 10 + 96 * j self.canvas.create_image(lW, lH, image=self.emptySpaces[(i + j) % 2], anchor=NW, activeimage=self.emptySpaces[2]) for r in range(8): for c in range(8): lH = 10 + 96 * r lW = 10 + 96 * c if self.board.grid[r][c] != 0: self.canvas.create_image( lW, lH, image=self.pieces[self.board.grid[r][c]], anchor=NW, activeimage=self.activePieces[self.board.grid[r][c]]) for k in range(12): lH = 10 + 61 * k self.canvas.create_image(779, lH, image=self.emptySpaces[0], anchor=NW) for l in range(len(self.missingPiecesBlack)): lH = 10 + 96 * l self.canvas.create_image( 779, lH, image=self.pieces[self.missingPiecesBlack[l]], anchor=NW) for n in range(len(self.missingPiecesWhite)): lH = 394 + 96 * n self.canvas.create_image( 779, lH, image=self.pieces[self.missingPiecesWhite[n]], anchor=NW) self.Master.update() def callback(self, event): # print(event.x,event.y) color = self.colors[self.t % 2] if self.itemClicked == False: for r in range(len(self.board.grid)): for c in range(len(self.board.grid[r])): lH = 10 + 96 * r lW = 10 + 96 * c if event.x in range(lW, lW + 90) and event.y in range( lH, lH + 90 ) and self.board.grid[r][c] != 0 and self.board.pieces[ self.board.grid[r][c]][1] == color: self.canvas.create_image(lW, lH, image=self.emptySpaces[3], anchor=NW) self.canvas.create_image( lW, lH, image=self.pieces[self.board.grid[r][c]], anchor=NW) self.itemClicked = True self.curPos = [r, c] else: for r in range(len(self.board.grid)): for c in range(len(self.board.grid[r])): lH = 10 + 96 * r lW = 10 + 96 * c if event.x in range(lW, lW + 90) and event.y in range( lH, lH + 90): if r == self.curPos[0] and c == self.curPos[1]: self.canvas.create_image( lW, lH, image=self.emptySpaces[(r + c) % 2], anchor=NW) self.canvas.create_image( lW, lH, image=self.pieces[self.board.grid[r][c]], anchor=NW, activeimage=self.activePieces[ self.board.grid[r][c]]) self.itemClicked = False else: self.finPos = [r, c] if self.board.turnValid(self.board.grid, self.curPos, self.finPos, color): self.itemClicked = False self.t += 1 #Add to missing pieces list --------------------------------------------------- if self.board.grid[self.finPos[0]][self.finPos[ 1]] % 6 > 1 and color == 'White': self.missingPiecesBlack.append( self.board.grid[self.finPos[0]][ self.finPos[1]]) if len(self.missingPiecesBlack) > 4: self.missingPiecesBlack.remove( min(self.missingPiecesBlack)) elif self.board.grid[self.finPos[0]][ self.finPos[ 1]] % 6 > 1 and color == 'Black': self.missingPiecesWhite.append( self.board.grid[self.finPos[0]][ self.finPos[1]]) if len(self.missingPiecesWhite) > 4: self.missingPiecesWhite.remove( min(self.missingPiecesWhite)) # -------------------------------------------------------------- self.board.grid[self.finPos[0]][ self.finPos[1]] = self.board.grid[ self.curPos[0]][self.curPos[1]] self.board.grid[self.curPos[0]][ self.curPos[1]] = 0 #Pawn promotion ----------------------------------------------------------- if (self.board.grid[self.finPos[0]] [self.finPos[1]] == 1 and r == 7) or ( self.board.grid[self.finPos[0]][ self.finPos[1]] == 7 and r == 0): if color == 'White' and len( self.missingPiecesWhite) > 0: self.board.grid[self.finPos[0]][ self.finPos[1]] = max( self.missingPiecesWhite) self.missingPiecesWhite.remove( max(self.missingPiecesWhite)) elif color == "Black" and len( self.missingPiecesBlack) > 0: self.board.grid[self.finPos[0]][ self.finPos[1]] = max( self.missingPiecesBlack) self.missingPiecesBlack.remove( max(self.missingPiecesBlack)) # ----------------------------------------------------------- self.canvas.delete(ALL) self.displayBoard() if self.comOp == True and self.t % 2 == 1: color = self.colors[self.t % 2] choice = self.computer.makeMove( self.board, color) #Add to missing pieces list --------------------------------------------------- if self.board.grid[choice[2][0]][choice[2][ 1]] % 6 > 1 and color == 'Black': self.missingPiecesWhite.append( self.board.grid[choice[2][0]][choice[2] [1]]) if len(self.missingPiecesWhite) > 4: self.missingPiecesWhite.remove( min(self.missingPiecesWhite)) elif self.board.grid[choice[2][0]][choice[2][ 1]] % 6 > 1 and color == 'White': self.missingPiecesBlack.append( self.board.grid[choice[2][0]][choice[2] [1]]) if len(self.missingPiecesBlack) > 4: self.missingPiecesBlack.remove( min(self.missingPiecesBlack)) # ----------------------------------------------------------- self.board.grid[choice[2][0]][ choice[2][1]] = self.board.grid[ choice[1][0]][choice[1][1]] self.board.grid[choice[1][0]][choice[1][1]] = 0 #Pawn promotion ----------------------------------------------------------- if (self.board.grid[choice[2][0]][choice[2][1]] == 1 and r == 7) or (self.board.grid[ choice[2][0]][choice[2][1]] == 7 and r == 0): if color == 'Black' and len( self.missingPiecesBlack) > 0: self.board.grid[choice[2][0]][ choice[2][1]] = max( self.missingPiecesBlack) self.missingPiecesBlack.remove( max(self.missingPiecesBlack)) elif color == "White" and len( self.missingPiecesWhite) > 0: self.board.grid[choice[2][0]][ choice[2][1]] = max( self.missingPiecesWhite) self.missingPiecesWhite.remove( max(self.missingPiecesWhite)) # ----------------------------------------------------------- # print() # print(choice[1],choice[2],choice[3]) # print() self.t += 1 self.canvas.delete(ALL) self.displayBoard() self.Master.update()