def test_knight_legal_moves(self): """Make sure legal moves are correct for knight in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('b8'), ['a6', 'c6']) self.assertCountEqual(board.get_legal_moves('b1'), ['a3', 'c3']) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('f6'), ['d7', 'd5', 'e8', 'h5', 'e4']) self.assertCountEqual(board.get_legal_moves('f3'), ['d4', 'd2', 'e1', 'h4', 'e5'])
def test_pawn_legal_moves(self): """Make sure legal moves are correct for pawn in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('a7'), ['a6', 'a5']) self.assertCountEqual(board.get_legal_moves('a2'), ['a3', 'a4']) board = chessboard.Board(start_state=self.FEN[1]) self.assertCountEqual(board.get_legal_moves('d4'), ['c5', 'd5']) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('a6'), ['a5']) self.assertCountEqual(board.get_legal_moves('a3'), ['a4'])
def test_bishop_legal_moves(self): """Make sure legal moves are correct for bishop in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('c8'), []) self.assertCountEqual(board.get_legal_moves('c1'), []) board = chessboard.Board(start_state=self.FEN[1]) self.assertCountEqual(board.get_legal_moves('a2'), ['b3', 'c4', 'b1']) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('c5'), ['d4', 'e3', 'f2', 'b4', 'a3', 'b6', 'a7']) self.assertCountEqual(board.get_legal_moves('c4'), ['b5', 'a6', 'd5', 'e6', 'f7', 'b3', 'a2'])
def test_queen_legal_moves(self): """Make sure legal moves are correct for queen in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('d8'), []) self.assertCountEqual(board.get_legal_moves('d1'), []) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('e7'), ['e6', 'e8', 'd7', 'd8']) self.assertCountEqual(board.get_legal_moves('e2'), ['e1', 'e3', 'd1', 'd2']) board = chessboard.Board(start_state=self.FEN[3]) self.assertCountEqual( board.get_legal_moves('h7'), ['h6', 'h5', 'h4', 'g6', 'f5', 'e4', 'd3', 'c2', 'b1', 'g8'])
def test_king_legal_moves(self): """Make sure legal moves are correct for king in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('e8'), []) self.assertCountEqual(board.get_legal_moves('e1'), []) board = chessboard.Board(start_state=self.FEN[1]) self.assertCountEqual(board.get_legal_moves('c5'), ['b4', 'b5', 'b6', 'c6', 'd6', 'd5', 'd4']) self.assertCountEqual(board.get_legal_moves('e1'), ['d1', 'd2', 'e2', 'f2', 'f1']) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('g8'), ['h8']) self.assertCountEqual(board.get_legal_moves('g1'), ['h1']) board = chessboard.Board(start_state=self.FEN[3]) self.assertCountEqual(board.get_legal_moves('e7'), ['d6', 'd7', 'd8', 'e8', 'f8', 'f7', 'e6'])
def test_rook_legal_moves(self): """Make sure legal moves are correct for rook in different setups""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('a8'), []) self.assertCountEqual(board.get_legal_moves('a1'), []) board = chessboard.Board(start_state=self.FEN[2]) self.assertCountEqual(board.get_legal_moves('f8'), ['e8', 'd8', 'c8', 'b8']) self.assertCountEqual(board.get_legal_moves('f1'), ['e1', 'd1', 'c1', 'b1']) board = chessboard.Board(start_state=self.FEN[3]) self.assertCountEqual(board.get_legal_moves('a8'), [ 'g8', 'f8', 'e8', 'd8', 'c8', 'b8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1' ]) self.assertCountEqual( board.get_legal_moves('a1'), ['b1', 'c1', 'd1', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2'])
def __init__(self, parent): self.parent = parent canvas_width = self.columns * self.dim_square canvas_height = self.rows * self.dim_square self.canvas = Canvas(parent, width = canvas_width, height=canvas_height, background="grey") self.canvas.pack(padx=8, pady=8) self.draw_board() self.canvas.bind("<Button-1>", self.square_clicked) self.chessboard = chessboard.Board()
def test_board_initialisation(self): """Make sure the FEN string used to initialise the board is reproduced""" for input_str in VALID_FEN_STRINGS: board = chessboard.Board(start_state=input_str) output_str = board.get_FEN() # Sort the castling part of input str as output str always sorts input_str = re.sub( r'\s([KkQq-]+)\s', lambda m: ' ' + ''.join(sorted(m.group(1))) + ' ', input_str) self.assertEqual(input_str, output_str)
def Mousereleased(self, event): #get the location of the mouse release event self.mouseReleasedX = event.y self.mouseReleasedY = event.x #initialize a list to verify the move self.move2compare = [] #append the position of the mouse clicked and the mouse released location self.move2compare.append((self.mousePressedX, self.mousePressedY, int(self.mouseReleasedX/self.square_size), int(self.mouseReleasedY/self.square_size))) #calls the function determinePiece() from the chessboard.py chessboard.Board().determinePiece(self.move2compare) #this calls back the function drawPieces() after 100 ms self.root.after(100, self.drawPieces()) return
def test_algebraic_board_funcs(self): """ Algebraic functions should except both algebraic format and positional indexes """ board = chessboard.Board() self.assertEqual(board.is_occupied(0), board.is_occupied('a8')) self.assertEqual(board.is_occupied(56), board.is_occupied('a1')) self.assertEqual(board.is_occupied(31), board.is_occupied('h5')) self.assertEqual(board.get_piece(0), board.get_piece('a8')) self.assertEqual(board.get_piece(56), board.get_piece('a1')) self.assertEqual(board.get_piece(31), board.get_piece('h5')) self.assertEqual(board.get_piece_name(0), board.get_piece_name('a8')) self.assertEqual(board.get_piece_name(56), board.get_piece_name('a1')) self.assertEqual(board.get_piece_name(31), board.get_piece_name('h5'))
def test_algebraic_board_funcs_error(self): """ Algebraic functions should error for inputs other than algebraic format and positional indexes (int) """ board = chessboard.Board() with self.assertRaises(utils.AlgebraicFuncException): board.get_piece('0') with self.assertRaises(utils.AlgebraicFuncException): board.get_piece('a') with self.assertRaises(utils.AlgebraicFuncException): board.get_piece('k8') with self.assertRaises(utils.AlgebraicFuncException): board.get_piece(64) with self.assertRaises(utils.AlgebraicFuncException): board.get_piece(1.0) with self.assertRaises(utils.AlgebraicFuncException): board.get_piece(board) with self.assertRaises(utils.AlgebraicFuncException): board.get_piece(board.get_FEN) with self.assertRaises(utils.AlgebraicFuncException): board.get_piece(board.get_FEN())
if piece is not None: filename = "../pieces_image/%s%s.png" % ( piece.shortname.lower(), piece.color) piecename = "%s%s%s" % (piece.shortname, x, y) if (filename not in self.images): self.images[filename] = ImageTk.PhotoImage(file=filename) self.canvas.create_image(0, 0, image=self.images[filename], tags=(piecename, "occupied"), anchor="c") x0 = (y * self.dim_square) + int(self.dim_square / 2) y0 = ((7 - x) * self.dim_square) + int(self.dim_square / 2) self.canvas.coords(piecename, x0, y0) def show(chessboard): root = Tk() root.title("Chess") gui = GUI(root, chessboard) gui.draw_board() gui.draw_pieces() root.mainloop() if __name__ == "__main__": game = chessboard.Board() show(game)
def test_get_legal_moves_output(self): """Test the pos_idx keyword argument works""" board = chessboard.Board(start_state=self.FEN[0]) self.assertCountEqual(board.get_legal_moves('a7'), ['a6', 'a5']) self.assertCountEqual(board.get_legal_moves('a7', pos_idx=True), [16, 24])
import pygame import time import os import chessboard pygame.init() screen = pygame.display.set_mode((720,960), pygame.RESIZABLE) running = True clock = pygame.time.Clock() pygame.display.set_caption("Chess Lite") scale = 1 windowsize = (720,960) board = chessboard.Board() turn = chessboard.Sides.WHITE # setting the first turn to be white selectedposition = None showntext = "" gameover = False # Basic color library class COLOR: BLACK = (0,0,0) WHITE = (255,255,255) RED = (255,0,0) ORANGE = (255,165,0) YELLOW = (255,255,0) GREEN = (0,255,0) BLUE = (0,0,255)
def drawPieces(self): #all these lines set the variables to the right image path #subsample function reduces the size of the image self.w.whiteRook = PhotoImage(file="BRook.png")#.subsample(4,4) self.w.whiteKnight = PhotoImage(file="BKnight.png")#.subsample(4,4) self.w.whiteBishop = PhotoImage(file="BBishop.png")#.subsample(4,4) self.w.whiteQueen = PhotoImage(file="BQueen.png")#.subsample(4,4) self.w.whiteKing = PhotoImage(file="BKing.png")#.subsample(4,4) self.w.whitePawn = PhotoImage(file="BPawn.png")#.subsample(4,4) self.w.blackRook = PhotoImage(file="RRook.png")#.subsample(4,4) self.w.blackKnight = PhotoImage(file="RKnight.png")#.subsample(4,4) self.w.blackBishop = PhotoImage(file="RBishop.png")#.subsample(4,4) self.w.blackQueen = PhotoImage(file="RQueen.png")#.subsample(4,4) self.w.blackKing = PhotoImage(file="RKing.png")#.subsample(4,4) self.w.blackPawn = PhotoImage(file="RPawn.png")#.subsample(4,4) #iterates through all the tiles for i in range(64): ########## WHITE PIECES ########## #if the particular tile on the board has the letter "R", then a white Rook will be placed if chessboard.Board().chessBoard[int(i/8)][i%8] == "R": WRook = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whiteRook) self.w.tag_bind(WRook, "<ButtonPress-1>", self.Mousepress) #binding the rook image with the function Mousepress() self.w.tag_bind(WRook, "<ButtonRelease-1>", self.Mousereleased) #binding the rook image with the function Mousereleased() #if the particular tile on the board has the letter "N", then a white Knight will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "N": WKnight = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whiteKnight) self.w.tag_bind(WKnight, "<ButtonPress-1>", self.Mousepress) #binding the knight image with the function Mousepress() self.w.tag_bind(WKnight, "<ButtonRelease-1>", self.Mousereleased) #binding the knight image with the function Mousereleased() #if the particular tile on the board has the letter "B", then a white Bishop will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "B": WBishop = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whiteBishop) self.w.tag_bind(WBishop, "<ButtonPress-1>", self.Mousepress) #binding the bishop image with the function Mousepress() self.w.tag_bind(WBishop, "<ButtonRelease-1>", self.Mousereleased) #binding the bishop image with the function Mousereleased() #if the particular tile on the board has the letter "Q", then a white Queen will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "Q": WQueen = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whiteQueen) self.w.tag_bind(WQueen, "<ButtonPress-1>", self.Mousepress) #binding the queen image with the function Mousepress() self.w.tag_bind(WQueen, "<ButtonRelease-1>", self.Mousereleased) #binding the queen image with the function Mousereleased() #if the particular tile on the board has the letter "K", then a white King will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "K": WKing = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whiteKing) self.w.tag_bind(WKing, "<ButtonPress-1>", self.Mousepress) #binding the king image with the function Mousepress() self.w.tag_bind(WKing, "<ButtonRelease-1>", self.Mousereleased) #binding the king image with the function Mousereleased() #if the particular tile on the board has the letter "P", then a white Pawn will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "P": WPawn = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.whitePawn) self.w.tag_bind(WPawn, "<ButtonPress-1>", self.Mousepress) #binding the pawn image with the function Mousepress() self.w.tag_bind(WPawn, "<ButtonRelease-1>", self.Mousereleased) #binding the pawn image with the function Mousereleased() ########## BLACK PIECES ########## #if the particular tile on the board has the letter "r", then a black Rook will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "r": BRook = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackRook) self.w.tag_bind(BRook, "<ButtonPress-1>", self.Mousepress) #binding the rook image with the function Mousepress() self.w.tag_bind(BRook, "<ButtonRelease-1>", self.Mousereleased) #binding the rook image with the function Mousereleased() #if the particular tile on the board has the letter "n", then a white Knight will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "n": BKnight = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackKnight) self.w.tag_bind(BKnight, "<ButtonPress-1>", self.Mousepress) #binding the knight image with the function Mousepress() self.w.tag_bind(BKnight, "<ButtonRelease-1>", self.Mousereleased) #binding the knight image with the function Mousereleased() #if the particular tile on the board has the letter "b", then a white Bishop will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "b": BBishop = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackBishop) self.w.tag_bind(BBishop, "<ButtonPress-1>", self.Mousepress) #binding the bishop image with the function Mousepress() self.w.tag_bind(BBishop, "<ButtonRelease-1>", self.Mousereleased) #binding the bishop image with the function Mousereleased() #if the particular tile on the board has the letter "q", then a white Queen will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "q": BQueen = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackQueen) self.w.tag_bind(BQueen, "<ButtonPress-1>", self.Mousepress) #binding the queen image with the function Mousepress() self.w.tag_bind(BQueen, "<ButtonRelease-1>", self.Mousereleased) #binding the queen image with the function Mousereleased() #if the particular tile on the board has the letter "k", then a white King will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "k": BKing = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackKing) self.w.tag_bind(BKing, "<ButtonPress-1>", self.Mousepress) #binding the king image with the function Mousepress() self.w.tag_bind(BKing, "<ButtonRelease-1>", self.Mousereleased) #binding the king image with the function Mousereleased() #if the particular tile on the board has the letter "p", then a white Pawn will be placed elif chessboard.Board().chessBoard[int(i/8)][i%8] == "p": BPawn = self.w.create_image((i%8)*self.square_size+2, int(i/8)*self.square_size+2, anchor="nw", image=self.w.blackPawn) self.w.tag_bind(BPawn, "<ButtonPress-1>", self.Mousepress) #binding the pawn image with the function Mousepress() self.w.tag_bind(BPawn, "<ButtonRelease-1>", self.Mousereleased) #binding the pawn image with the function Mousereleased()