Пример #1
0
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols

        self.board = [[0 for x in range(8)] for _ in range(rows)]
        self.board[0][0] = Rook(0, 0, 'b')
        self.board[0][1] = Knight(0, 1, 'b')
        self.board[0][2] = Bishop(0, 2, 'b')
        self.board[0][3] = Queen(0, 3, 'b')
        self.board[0][4] = King(0, 4, 'b')
        self.board[0][5] = Bishop(0, 5, 'b')
        self.board[0][6] = Knight(0, 6, 'b')
        self.board[0][7] = Rook(0, 7, 'b')
        '''self.board[1][0] = Pawn(1,0, 'b')
        self.board[1][1] = Pawn(1,1, 'b')
        self.board[1][2] = Pawn(1,2, 'b')
        self.board[1][3] = Pawn(1,3, 'b')
        self.board[1][4] = Pawn(1,4, 'b')
        self.board[1][5] = Pawn(1,5, 'b')
        self.board[1][6] = Pawn(1,6, 'b')
        self.board[1][7] = Pawn(1,7, 'b')'''

        self.board[7][0] = Rook(7, 0, 'w')
        self.board[7][1] = Knight(7, 1, 'w')
        self.board[7][2] = Bishop(7, 2, 'w')
        self.board[7][3] = Queen(7, 3, 'w')
        self.board[7][4] = King(7, 4, 'w')
        self.board[7][5] = Bishop(7, 5, 'w')
        self.board[7][6] = Knight(7, 6, 'w')
        self.board[7][7] = Rook(7, 7, 'w')
        '''self.board[6][0] = Pawn(6,0, 'w')
Пример #2
0
 def __init__(self):
     self.pieces = []
     self.selected_idx = None
     self._player_name = None
     for i in range(8):
         self.pieces += [Pawn(i, 1, 'black')]
         self.pieces += [Pawn(i, 6, 'white')]
     self.pieces += [
         Rook(0, 0, 'black'),
         Rook(7, 0, 'black'),
         Knight(1, 0, 'black'),
         Knight(6, 0, 'black'),
         Bishop(2, 0, 'black'),
         Bishop(5, 0, 'black'),
         Queen(3, 0, 'black'),
         King(4, 0, 'black'),
         Rook(0, 7, 'white'),
         Rook(7, 7, 'white'),
         Knight(1, 7, 'white'),
         Knight(6, 7, 'white'),
         Bishop(2, 7, 'white'),
         Bishop(5, 7, 'white'),
         Queen(3, 7, 'white'),
         King(4, 7, 'white'),
     ]
Пример #3
0
	def test_castling_forbidden_rook_moved(self):
		'''Test that castling is forbidden once the rook moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((0,7),(1,7))
		self.assertTrue(move in rook_l.get_moves((0,7),board))
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((7,7),(6,7))
		self.assertTrue(move in rook_r.get_moves((7,7),board))
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertFalse(Castling("right",board.white) in moves)
Пример #4
0
    def __init__(self):  #setup board and pieces
        self.blackPieces = []
        self.whitePieces = []

        self.blackPieces.append(King(4, 0, BLACK))
        self.blackPieces.append(Queen(3, 0, BLACK))
        self.blackPieces.append(Bishop(2, 0, BLACK))
        self.blackPieces.append(Bishop(5, 0, BLACK))
        self.blackPieces.append(Knight(1, 0, BLACK))
        self.blackPieces.append(Knight(6, 0, BLACK))
        self.blackPieces.append(Rook(0, 0, BLACK))
        self.blackPieces.append(Rook(7, 0, BLACK))
        for i in range(0, 8):
            self.blackPieces.append(Pawn(i, 1, BLACK))

        self.whitePieces.append(King(4, 7, WHITE))
        self.whitePieces.append(Queen(3, 7, WHITE))
        self.whitePieces.append(Bishop(2, 7, WHITE))
        self.whitePieces.append(Bishop(5, 7, WHITE))
        self.whitePieces.append(Knight(1, 7, WHITE))
        self.whitePieces.append(Knight(6, 7, WHITE))
        self.whitePieces.append(Rook(0, 7, WHITE))
        self.whitePieces.append(Rook(7, 7, WHITE))
        for i in range(0, 8):
            self.whitePieces.append(Pawn(i, 6, WHITE))
Пример #5
0
    def __init__(self):
        self.rows = common.DIMENSION
        self.cols = common.DIMENSION

        self.current_color = "white"

        self.board_inst = {}
        for row in range(self.rows):
            for col in range(self.cols):
                self.board_inst[row, col] = Empty(row, col, None)

        self.board_inst[0, 0] = Rook(0, 0, 'black')
        self.board_inst[0, 1] = Knight(0, 1, 'black')
        self.board_inst[0, 2] = Bishop(0, 2, 'black')
        self.board_inst[0, 3] = Queen(0, 3, 'black')
        self.board_inst[0, 4] = King(0, 4, 'black')
        self.board_inst[0, 5] = Bishop(0, 5, 'black')
        self.board_inst[0, 6] = Knight(0, 6, 'black')
        self.board_inst[0, 7] = Rook(0, 7, 'black')

        self.board_inst[7, 0] = Rook(7, 0, 'white')
        self.board_inst[7, 1] = Knight(7, 1, 'white')
        self.board_inst[7, 2] = Bishop(7, 2, 'white')
        self.board_inst[7, 3] = Queen(7, 3, 'white')
        self.board_inst[7, 4] = King(7, 4, 'white')
        self.board_inst[7, 5] = Bishop(7, 5, 'white')
        self.board_inst[7, 6] = Knight(7, 6, 'white')
        self.board_inst[7, 7] = Rook(7, 7, 'white')

        for i in range(common.DIMENSION):
            self.board_inst[1, i] = Pawn(1, i, 'black')
            self.board_inst[6, i] = Pawn(6, i, 'white')
Пример #6
0
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols

        self.ready = False

        self.last = None

        self.copy = True

        self.board = [[0 for x in range(8)] for _ in range(rows)]

        self.board[0][0] = Rook(0, 0, "b")
        self.board[0][1] = Knight(0, 1, "b")
        self.board[0][2] = Bishop(0, 2, "b")
        self.board[0][3] = Queen(0, 3, "b")
        self.board[0][4] = King(0, 4, "b")
        self.board[0][5] = Bishop(0, 5, "b")
        self.board[0][6] = Knight(0, 6, "b")
        self.board[0][7] = Rook(0, 7, "b")

        self.board[1][0] = Pawn(1, 0, "b")
        self.board[1][1] = Pawn(1, 1, "b")
        self.board[1][2] = Pawn(1, 2, "b")
        self.board[1][3] = Pawn(1, 3, "b")
        self.board[1][4] = Pawn(1, 4, "b")
        self.board[1][5] = Pawn(1, 5, "b")
        self.board[1][6] = Pawn(1, 6, "b")
        self.board[1][7] = Pawn(1, 7, "b")

        self.board[7][0] = Rook(7, 0, "w")
        self.board[7][1] = Knight(7, 1, "w")
        self.board[7][2] = Bishop(7, 2, "w")
        self.board[7][3] = Queen(7, 3, "w")
        self.board[7][4] = King(7, 4, "w")
        self.board[7][5] = Bishop(7, 5, "w")
        self.board[7][6] = Knight(7, 6, "w")
        self.board[7][7] = Rook(7, 7, "w")

        self.board[6][0] = Pawn(6, 0, "w")
        self.board[6][1] = Pawn(6, 1, "w")
        self.board[6][2] = Pawn(6, 2, "w")
        self.board[6][3] = Pawn(6, 3, "w")
        self.board[6][4] = Pawn(6, 4, "w")
        self.board[6][5] = Pawn(6, 5, "w")
        self.board[6][6] = Pawn(6, 6, "w")
        self.board[6][7] = Pawn(6, 7, "w")

        self.turn = "w"

        self.time1 = 900
        self.time2 = 900

        self.storedTime1 = 0
        self.storedTime2 = 0

        self.winner = None

        self.startTime = time.time()
Пример #7
0
 def test_moves_blocked_enemy(self):
     self.white.castling.append((False, False))
     king = King(self.white, (4, 4))
     knight = Knight(self.black, (3, 3))
     self.board.add_piece(king)
     self.board.add_piece(knight)
     moves = list(king.moves(self.board))
     self.assertEquals(len(moves), 8, moves)
Пример #8
0
 def test_can_reach_castle_queen(self):
     rook1 = Rook(self.white, (0, 0))
     king = King(self.white, (4, 0))
     rook2 = Rook(self.white, (7, 0))
     self.board.add_piece(king)
     self.board.add_piece(rook1)
     self.board.add_piece(rook2)
     self.assertTrue(king.can_reach(self.board, (2, 0)))
Пример #9
0
    def test_king_can_move_one_space_any_direction(self):
        king = King(color='WHITE')
        is_valid_move = king.validate_move((4,2), (3,1))
        self.assertTrue(is_valid_move)
        
        is_valid_move = king.validate_move((4,2), (3,3))
        self.assertTrue(is_valid_move)

        is_valid_move = king.validate_move((4,2), (5,1))
        self.assertTrue(is_valid_move)
Пример #10
0
 def __init__(self, player1, player2, graphic_interface, verbose = False) :
     self.board = [
         Rook(Color.BLACK),
         Knight(Color.BLACK),
         Bishop(Color.BLACK),
         Queen(Color.BLACK),
         King(Color.BLACK),
         Bishop(Color.BLACK),
         Knight(Color.BLACK),
         Rook(Color.BLACK)
     ]
     self.board += [Pawn(Color.BLACK) for _ in range(8)]
     self.board += [Null() for _ in range(32)]
     self.board += [Pawn(Color.WHITE) for _ in range(8)]
     self.board += [
         Rook(Color.WHITE),
         Knight(Color.WHITE),
         Bishop(Color.WHITE),
         Queen(Color.WHITE),
         King(Color.WHITE),
         Bishop(Color.WHITE),
         Knight(Color.WHITE),
         Rook(Color.WHITE)
     ]
     self.board_with_boundaries = [
         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
         -1,  0,  1,  2,  3,  4,  5,  6,  7, -1,
         -1,  8,  9, 10, 11, 12, 13, 14, 15, -1,
         -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
         -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
         -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
         -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
         -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
         -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
         -1, -1, -1, -1, -1, -1, -1, -1 ,-1 ,-1,
         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
     ]
     self.player1 = player1
     self.player2 = player2
     self.graphic_interface = graphic_interface
     self.selected_case = None
     self.selected_case_2 = None
     self.turn = Color.WHITE
     self.special_moves_authorization = [
         True, #White castle
         True, #White castle (queen side)
         True, #Black castle
         True, #Black castle (queen side)
         -1,   #White "en passant" capture
         -1    #Black "en passant" capture
     ]
     self.moves_played=[]
     self.end = False
     self.verbose = verbose
Пример #11
0
 def __init__(self):
     self.board = ChessBoard(self)
     self.pieces = [Pawn(self, "white", item) for item in [i + "2" for i in "abcdefgh"]]
     self.pieces.extend([Pawn(self, "black", item) for item in [i + "7" for i in "abcdefgh"]])
     self.pieces.extend([Rook(self, "white", item) for item in ("a1", "h1")])
     self.pieces.extend([Rook(self, "black", item) for item in ("a8", "h8")])
     self.pieces.extend([Bishop(self, "white", item) for item in ("c1", "f1")])
     self.pieces.extend([Bishop(self, "black", item) for item in ("c8", "f8")])
     self.pieces.extend([Knight(self, "white", item) for item in ("b1", "g1")])
     self.pieces.extend([Knight(self, "black", item) for item in ("b8", "g8")])
     self.pieces.extend([King(self, "white", "e1"), King(self, "black", "e8"), Queen(self, "white", "d1"), Queen(self, "black", "d8")])
     self.players = []
Пример #12
0
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols

        self.turn = "w"
        self.winner = None
        self.last = None

        #player time
        self.time1 = 900
        self.time2 = 900
        """Places all the pieces on 2D Array Board & generate the board"""
        self.board = [[0 for x in range(8)] for _ in range(rows)]

        self.board[0][0] = Rook(0, 0, "b")
        self.board[0][1] = Knight(0, 1, "b")
        self.board[0][2] = Bishop(0, 2, "b")
        self.board[0][3] = Queen(0, 3, "b")
        self.board[0][4] = King(0, 4, "b")
        self.board[0][5] = Bishop(0, 5, "b")
        self.board[0][6] = Knight(0, 6, "b")
        self.board[0][7] = Rook(0, 7, "b")

        self.board[7][0] = Rook(7, 0, "w")
        self.board[7][1] = Knight(7, 1, "w")
        self.board[7][2] = Bishop(7, 2, "w")
        self.board[7][3] = Queen(7, 3, "w")
        self.board[7][4] = King(7, 4, "w")
        self.board[7][5] = Bishop(7, 5, "w")
        self.board[7][6] = Knight(7, 6, "w")
        self.board[7][7] = Rook(7, 7, "w")

        #faster way of setting pawns
        for colum in range(0, 8):
            self.board[1][colum] = Pawn(1, colum, "b")
            self.board[6][colum] = Pawn(6, colum, "w")

        #keep track of pieces move
        self.moveStack = []

        self.turn = "w"

        self.time1 = 900
        self.time2 = 900

        self.storedTime1 = 0
        self.storedTime2 = 0

        self.winner = None

        self.startTime = time.time()
Пример #13
0
    def __init__(self,
                 grid,
                 newBoard=False,
                 whiteInCheck=False,
                 blackInCheck=False):

        if (newBoard == True):

            self.mobility = 0
            self.bMobility = 0
            self.grid = [[None for x in range(0, 8)] for y in range(0, 8)]
            for i in range(0, 8):
                for j in range(0, 8):
                    if (grid[i][j] == '_' or grid[i][j] == '-'):
                        self.grid[i][j] = None
                    elif (grid[i][j] == 'P'):
                        self.grid[i][j] = Pawn(i, j, 'w', 'P')
                    elif (grid[i][j] == 'p'):
                        self.grid[i][j] = Pawn(i, j, 'b', 'p')
                    elif (grid[i][j] == 'N'):
                        self.grid[i][j] = Knight(i, j, 'w', 'N')
                    elif (grid[i][j] == 'n'):
                        self.grid[i][j] = Knight(i, j, 'b', 'n')
                    elif (grid[i][j] == 'B'):
                        self.grid[i][j] = Bishop(i, j, 'w', 'B')
                    elif (grid[i][j] == 'b'):
                        self.grid[i][j] = Bishop(i, j, 'b', 'b')
                    elif (grid[i][j] == 'Q'):
                        self.grid[i][j] = Queen(i, j, 'w', 'Q')
                    elif (grid[i][j] == 'q'):
                        self.grid[i][j] = Queen(i, j, 'b', 'q')
                    elif (grid[i][j] == 'K'):
                        self.grid[i][j] = King(i, j, 'w', 'K')
                    elif (grid[i][j] == 'k'):
                        self.grid[i][j] = King(i, j, 'b', 'k')
                    elif (grid[i][j] == 'R'):
                        self.grid[i][j] = Rook(i, j, 'w', 'R')
                    elif (grid[i][j] == 'r'):
                        self.grid[i][j] = Rook(i, j, 'b', 'r')
            self.whiteInCheck = whiteInCheck  # change later maybe?
            self.blackInCheck = blackInCheck  # change later maybe?

        else:
            self.mobility = 0
            self.bMobility = 0
            self.grid = grid
            self.whiteInCheck = whiteInCheck  # change later maybe?
            self.blackInCheck = blackInCheck  # change later maybe?

        self.evalValue = self.evaluationFunction()
Пример #14
0
	def test_castling_no_moves(self):
		'''Test castling when neither the king nor the rook's have moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)

		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)
Пример #15
0
    def __init__(self):
        self.cols = COLS
        self.rows = ROWS
        self.captured_piece = None
        self.turn = "white"
        self.en_passant_possibility = None
        self.board = [[0 for col in range(COLS)] for rows in range(ROWS)]

        # Black pieces
        self.board[0][0] = Rook(0, 0, "black")
        self.board[0][1] = Knight(0, 1, "black")
        self.board[0][2] = Bishop(0, 2, "black")
        self.board[0][3] = Queen(0, 3, "black")
        self.board[0][4] = King(0, 4, "black")
        self.board[0][5] = Bishop(0, 5, "black")
        self.board[0][6] = Knight(0, 6, "black")
        self.board[0][7] = Rook(0, 7, "black")

        # Black pawns
        self.board[1][0] = Pawn(1, 0, "black")
        self.board[1][1] = Pawn(1, 1, "black")
        self.board[1][2] = Pawn(1, 2, "black")
        self.board[1][3] = Pawn(1, 3, "black")
        self.board[1][4] = Pawn(1, 4, "black")
        self.board[1][5] = Pawn(1, 5, "black")
        self.board[1][6] = Pawn(1, 6, "black")
        self.board[1][7] = Pawn(1, 7, "black")

        # White pieces
        self.board[7][0] = Rook(7, 0, "white")
        self.board[7][1] = Knight(7, 1, "white")
        self.board[7][2] = Bishop(7, 2, "white")
        self.board[7][3] = Queen(7, 3, "white")
        self.board[7][4] = King(7, 4, "white")
        self.board[7][5] = Bishop(7, 5, "white")
        self.board[7][6] = Knight(7, 6, "white")
        self.board[7][7] = Rook(7, 7, "white")

        # White pawns
        self.board[6][0] = Pawn(6, 0, "white")
        self.board[6][1] = Pawn(6, 1, "white")
        self.board[6][2] = Pawn(6, 2, "white")
        self.board[6][3] = Pawn(6, 3, "white")
        self.board[6][4] = Pawn(6, 4, "white")
        self.board[6][5] = Pawn(6, 5, "white")
        self.board[6][6] = Pawn(6, 6, "white")
        self.board[6][7] = Pawn(6, 7, "white")
Пример #16
0
def generateGrid():
    return {
        "whites": [Pawn(Vector(i, 6), "whites") for i in range(8)] +
        [Rook(Vector(0, 7), "whites"),
         Rook(Vector(7, 7), "whites")] +
        [Bishop(Vector(2, 7), "whites"),
         Bishop(Vector(5, 7), "whites")] + [Queen(Vector(3, 7), "whites")] +
        [Knight(Vector(1, 7), "whites"),
         Knight(Vector(6, 7), "whites")] + [King(Vector(4, 7), "whites")],
        "blacks": [Pawn(Vector(i, 1), "blacks") for i in range(8)] +
        [Rook(Vector(0, 0), "blacks"),
         Rook(Vector(7, 0), "blacks")] +
        [Bishop(Vector(2, 0), "blacks"),
         Bishop(Vector(5, 0), "blacks")] + [Queen(Vector(3, 0), "blacks")] +
        [Knight(Vector(1, 0), "blacks"),
         Knight(Vector(6, 0), "blacks")] + [King(Vector(4, 0), "blacks")]
    }
Пример #17
0
def colour_pieces(colour):
    pieces = [Pawn(colour) for _ in range(8)]
    pieces += [Rook(colour) for _ in range(2)]
    pieces += [Knight(colour) for _ in range(2)]
    pieces += [Bishop(colour) for _ in range(2)]
    pieces += [Queen(colour)]
    pieces += [King(colour)]
    return pieces
Пример #18
0
	def __init__(self):
		self.state = [[EMPTY_SQUARE for y in range(BOARD_SIZE)] for x in range(BOARD_SIZE)]
		self.white_pieces = ROOK + HORSE + BISHOP + QUEEN + KING + PAWN
		self.black_pieces = self.white_pieces.lower()
		# White
		self.state[0] = [Rook(0, 0), Knight(0, 1), Bishop(0, 2), Queen(0, 3), King(0, 4), Bishop(0, 5), Knight(0, 6), Rook(0, 7)]
		self.state[1] = [Pawn(1, 0), Pawn(1, 1), Pawn(1, 2), Pawn(1, 3), Pawn(1, 4), Pawn(1, 5), Pawn(1, 6), Pawn(1, 7)]
		# Black
		self.state[6] = [Pawn(7, 0, False), Pawn(7, 1, False), Pawn(7, 2, False), Pawn(7, 3, False), Pawn(7, 4, False), Pawn(7, 5, False), Pawn(7, 6, False), Pawn(7, 7, False)]
		self.state[7] = [Rook(6, 0, False), Knight(6, 1, False), Bishop(6, 2, False), Queen(6, 3, False), King(6, 4, False), Bishop(6, 5, False), Knight(6, 6, False), Rook(6, 7, False)]
Пример #19
0
 def _row_of_king(color):
     return [
         Lance(color),
         Knight(color),
         SilverGeneral(color),
         GoldGeneral(color),
         King(color),
         GoldGeneral(color),
         SilverGeneral(color),
         Knight(color),
         Lance(color)
     ]
Пример #20
0
	def test_castling_permitted_after_undo(self):
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((0,7),(1,7))
		self.assertTrue(move in rook_l.get_moves((0,7),board))
		move.perform(board)
		move.undo(board)

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)
Пример #21
0
    def __init__(self):
        self.board = [[0 for x in range(8)] for _ in range(8)]

        self.board[0][0] = Rook("a8", "b", "ROOKB1")
        self.board[0][1] = Knight("b8", "b", "KNIGHTB1")
        self.board[0][2] = Bishop("c8", "b", "BISHOPB1")
        self.board[0][3] = Queen("d8", "b", "QUEENB")
        self.board[0][4] = King("e8", "b", "KINGB")
        self.board[0][5] = Bishop("f8", "b", "BISHOPB2")
        self.board[0][6] = Knight("g8", "b", "KNIGHTB2")
        self.board[0][7] = Rook("h8", "b", "ROOKB2")

        self.board[1][0] = Pawn("a7", "b", "PAWNBA")
        self.board[1][1] = Pawn("b7", "b", "PAWNBB")
        self.board[1][2] = Pawn("c7", "b", "PAWNBC")
        self.board[1][3] = Pawn("d7", "b", "PAWNBD")
        self.board[1][4] = Pawn("e7", "b", "PAWNBE")
        self.board[1][5] = Pawn("f7", "b", "PAWNBF")
        self.board[1][6] = Pawn("g7", "b", "PAWNBG")
        self.board[1][7] = Pawn("h7", "b", "PAWNBH")

        self.board[7][0] = Rook("a1", "w", "ROOKW1")
        self.board[7][1] = Knight("b1", "w", "KNIGHTW1")
        self.board[7][2] = Bishop("c1", "w", "BISHOPW1")
        self.board[7][3] = Queen("d1", "w", "QUEENW")
        self.board[7][4] = King("e1", "w", "KINGW")
        self.board[7][5] = Bishop("f1", "w", "BISHOPW2")
        self.board[7][6] = Knight("g1", "w", "KNIGHTW2")
        self.board[7][7] = Rook("h1", "w", "ROOKW2")

        self.board[6][0] = Pawn("a2", "w", "PAWNWA")
        self.board[6][1] = Pawn("b2", "w", "PAWNWB")
        self.board[6][2] = Pawn("c2", "w", "PAWNWC")
        self.board[6][3] = Pawn("d2", "w", "PAWNWD")
        self.board[6][4] = Pawn("e2", "w", "PAWNWE")
        self.board[6][5] = Pawn("f2", "w", "PAWNWF")
        self.board[6][6] = Pawn("g2", "w", "PAWNWG")
        self.board[6][7] = Pawn("h2", "w", "PAWNWH")
Пример #22
0
    def test_king_cant_move_L_shapes(self):
        king = King('WHITE')
        is_valid_move = king.validate_move((1,3),(3,2))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((1,3),(2,5))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((2,3), (1,5))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((2,3), (3,5))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((2,3), (4,4))
        self.assertFalse(is_valid_move)
Пример #23
0
    def test_cannot_move_diagonally_more_than_one_space(self):
        king = King(color='WHITE')
        is_valid_move = king.validate_move((4,2), (6,4))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((4,2), (1,3))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((4,2), (7,1))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((4,2), (7,3))
        self.assertFalse(is_valid_move)

        is_valid_move = king.validate_move((4,2), (1,1))
        self.assertFalse(is_valid_move)
Пример #24
0
    def factory(piece, color):
        piece = piece.lower()
        color = color.lower()
        color = 'w' if color == 'white' else 'b'

        if piece == 'pawn':
            return Pawn(color)

        elif piece == 'rook':
            return Rook(color)

        elif piece == 'knight':
            return Knight(color)

        elif piece == 'bishop':
            return Bishop(color)

        elif piece == 'queen':
            return Queen(color)

        elif piece == 'king':
            return King(color)
Пример #25
0
    def piece_setup(self):
        for i in [0, 1]:
            for k in range(8):
                self.place(Pawn(0, k, 6))
                self.place(Pawn(1, k, 1))

            if i == 0:
                j = 7
            else:
                j = 0

            self.place(Rook(i, 0, j))
            self.place(Rook(i, 7, j))

            self.place(Knight(i, 1, j))
            self.place(Knight(i, 6, j))

            self.place(Bishop(i, 2, j))
            self.place(Bishop(i, 5, j))

            self.place(Queen(i, 3, j))
            self.place(King(i, 4, j))

            self.move_stack = []
Пример #26
0
    def loadGame(self):
        import json
        from piece import Piece, Pawn, Rook, Bishop, Queen, Knight, King

        with open("save.json", "r") as saveFile:
            data = json.load(saveFile)
            for key in data:
                if key == "turns":
                    self.cb.turns = data[key]
                if key == "lastMove":
                    self.cb.lastMove["pieceName"] = data[key]["pieceName"]
                    self.cb.lastMove["type"] = data[key]["type"]
                    self.cb.lastMove["color"] = data[key]["color"]
                    self.cb.lastMove["origX"] = data[key]["origX"]
                    self.cb.lastMove["origY"] = data[key]["origY"]
                    self.cb.lastMove["futureX"] = data[key]["futureX"]
                    self.cb.lastMove["futureY"] = data[key]["futureY"]
                if key != "turns" and key != "lastMove":
                    if data[key]["type"] == "Pawn":
                        piece = Pawn(data[key]["type"], data[key]["name"],
                                     data[key]["color"],
                                     data[key]["x_position"],
                                     data[key]["y_position"], self.cb,
                                     data[key]["initialMoveNotDone"],
                                     data[key]["isActive"])
                    if data[key]["type"] == "Rook":
                        piece = Rook(data[key]["type"], data[key]["name"],
                                     data[key]["color"],
                                     data[key]["x_position"],
                                     data[key]["y_position"], self.cb,
                                     data[key]["initialMoveNotDone"],
                                     data[key]["isActive"])
                    if data[key]["type"] == "Knight":
                        piece = Knight(data[key]["type"], data[key]["name"],
                                       data[key]["color"],
                                       data[key]["x_position"],
                                       data[key]["y_position"], self.cb,
                                       data[key]["initialMoveNotDone"],
                                       data[key]["isActive"])
                    if data[key]["type"] == "Bishop":
                        piece = Bishop(data[key]["type"], data[key]["name"],
                                       data[key]["color"],
                                       data[key]["x_position"],
                                       data[key]["y_position"], self.cb,
                                       data[key]["initialMoveNotDone"],
                                       data[key]["isActive"])
                    if data[key]["type"] == "Queen":
                        piece = Queen(data[key]["type"], data[key]["name"],
                                      data[key]["color"],
                                      data[key]["x_position"],
                                      data[key]["y_position"], self.cb,
                                      data[key]["initialMoveNotDone"],
                                      data[key]["isActive"])
                    if data[key]["type"] == "King":
                        piece = King(data[key]["type"], data[key]["name"],
                                     data[key]["color"],
                                     data[key]["x_position"],
                                     data[key]["y_position"], self.cb,
                                     data[key]["initialMoveNotDone"],
                                     data[key]["isActive"])

                    self.cb.addPiece(piece)
Пример #27
0
 def test_cant_move_backward_more_than_one_space(self):
     king = King(color='BLACK')
     is_valid_move = king.validate_move((4,2), (7,2))
     self.assertFalse(is_valid_move)
Пример #28
0
 def test_can_castle_not_queen(self):
     king = King(Location(8, 'e'), Player.BLACK, self.board)
     rook_king_side = Rook(Location(8, 'h'), Player.BLACK, self.board)
     self.assertTrue(king.can_castle(Side.KING))
     self.assertFalse(king.can_castle(Side.QUEEN))
Пример #29
0
 def test_checked(self):
     king = King(Location(5, 'd'), Player.BLACK, self.board)
     self.assertFalse(king.checked())
     king2 = King(Location(4, 'e'), Player.WHITE, self.board)
     self.assertTrue(king.checked())
     self.assertTrue(king2.checked())
Пример #30
0
                                          gameboard)

            #Problème dans les coordonnées
            while make_a_move["message"] == "pb":
                game = player.play()
                make_a_move = player.one_turn(game["s_row"], game["s_column"],
                                              game["t_row"], game["t_column"],
                                              gameboard)
                view(gameboard)

            #Acceptation des coordonnées : la pièce est un pion ou une dame
            if make_a_move["type"] == "Checker":
                piece = Checker(game["s_row"], game["s_column"], game["t_row"],
                                game["t_column"], player.number)
            else:
                piece = King(game["s_row"], game["s_column"], game["t_row"],
                             game["t_column"], player.number)

            #Si le joueur désire se déplacer.
            if make_a_move["message"] == "I'm on my way":
                gameboard = piece.move(gameboard)
                player_turn["status"] = "end of play"

            #Ou le joueur désire manger un pion adverse.
            else:
                gameboard = piece.move(gameboard)
                gameboard = piece.capture(gameboard, make_a_move)
                player.win_one_point()

            #Vérification si le pion ne devient pas une dame
            if make_a_move["type"] == "Checker" and piece.check_king():
                gameboard = piece.become_king(gameboard)
Пример #31
0
 def copy(self):
     return King(self.pos, self.color, self.rotate)
Пример #32
0
 def test_moves_corner(self):
     self.white.castling.append((False, False))
     king = King(self.white, (0, 0))
     self.board.add_piece(king)
     moves = list(king.moves(self.board))
     self.assertEquals(len(moves), 3, moves)
Пример #33
0
 def __init__(self, pos, color, size, rotate=False):
     img = 'king-{}.png'.format(color)
     King.__init__(self, pos, color, rotate)
     DrawablePiece.__init__(self, pos, color, img, size, rotate)
Пример #34
0
    def newGame(self):
        from piece import Piece, Pawn, Rook, Bishop, Queen, Knight, King
        # Whites
        WP1 = Pawn("Pawn", "WP1", "White", 0, 1, self.cb, True, True)
        WP2 = Pawn("Pawn", "WP2", "White", 1, 1, self.cb, True, True)
        WP3 = Pawn("Pawn", "WP3", "White", 2, 1, self.cb, True, True)
        WP4 = Pawn("Pawn", "WP4", "White", 3, 1, self.cb, True, True)
        WP5 = Pawn("Pawn", "WP5", "White", 4, 1, self.cb, True, True)
        WP6 = Pawn("Pawn", "WP6", "White", 5, 1, self.cb, True, True)
        WP7 = Pawn("Pawn", "WP7", "White", 6, 1, self.cb, True, True)
        WP8 = Pawn("Pawn", "WP8", "White", 7, 1, self.cb, True, True)

        WKK = King("King", "WKK", "White", 4, 0, self.cb, True, True)
        WQ1 = Queen("Queen", "WQ1", "White", 3, 0, self.cb, True, True)
        WR1 = Rook("Rook", "WR1", "White", 0, 0, self.cb, True, True)
        WR2 = Rook("Rook", "WR2", "White", 7, 0, self.cb, True, True)
        WK1 = Knight("Knight", "WK1", "White", 1, 0, self.cb, True, True)
        WK2 = Knight("Knight", "WK2", "White", 6, 0, self.cb, True, True)
        WB1 = Bishop("Bishop", "WB1", "White", 2, 0, self.cb, True, True)
        WB2 = Bishop("Bishop", "WB2", "White", 5, 0, self.cb, True, True)

        self.cb.addPiece(WP1)
        self.cb.addPiece(WP2)
        self.cb.addPiece(WP3)
        self.cb.addPiece(WP4)
        self.cb.addPiece(WP5)
        self.cb.addPiece(WP6)
        self.cb.addPiece(WP7)
        self.cb.addPiece(WP8)
        self.cb.addPiece(WKK)
        self.cb.addPiece(WQ1)
        self.cb.addPiece(WR1)
        self.cb.addPiece(WR2)
        self.cb.addPiece(WB1)
        self.cb.addPiece(WB2)
        self.cb.addPiece(WK1)
        self.cb.addPiece(WK2)

        # Blacks
        BP1 = Pawn("Pawn", "BP1", "Black", 0, 6, self.cb, True, True)
        BP2 = Pawn("Pawn", "BP2", "Black", 1, 6, self.cb, True, True)
        BP3 = Pawn("Pawn", "BP3", "Black", 2, 6, self.cb, True, True)
        BP4 = Pawn("Pawn", "BP4", "Black", 3, 6, self.cb, True, True)
        BP5 = Pawn("Pawn", "BP5", "Black", 4, 6, self.cb, True, True)
        BP6 = Pawn("Pawn", "BP6", "Black", 5, 6, self.cb, True, True)
        BP7 = Pawn("Pawn", "BP7", "Black", 6, 6, self.cb, True, True)
        BP8 = Pawn("Pawn", "BP8", "Black", 7, 6, self.cb, True, True)

        BKK = King("King", "BKK", "Black", 4, 7, self.cb, True, True)
        BQ1 = Queen("Queen", "BQ1", "Black", 3, 7, self.cb, True, True)
        BR1 = Rook("Rook", "BR1", "Black", 0, 7, self.cb, True, True)
        BR2 = Rook("Rook", "BR2", "Black", 7, 7, self.cb, True, True)
        BK1 = Knight("Knight", "BK1", "Black", 1, 7, self.cb, True, True)
        BK2 = Knight("Knight", "BK2", "Black", 6, 7, self.cb, True, True)
        BB1 = Bishop("Bishop", "BB1", "Black", 2, 7, self.cb, True, True)
        BB2 = Bishop("Bishop", "BB2", "Black", 5, 7, self.cb, True, True)

        self.cb.addPiece(BP1)
        self.cb.addPiece(BP2)
        self.cb.addPiece(BP3)
        self.cb.addPiece(BP4)
        self.cb.addPiece(BP5)
        self.cb.addPiece(BP6)
        self.cb.addPiece(BP7)
        self.cb.addPiece(BP8)
        self.cb.addPiece(BKK)
        self.cb.addPiece(BQ1)
        self.cb.addPiece(BR1)
        self.cb.addPiece(BR2)
        self.cb.addPiece(BB1)
        self.cb.addPiece(BB2)
        self.cb.addPiece(BK1)
        self.cb.addPiece(BK2)
Пример #35
0
 def test_can_reach_normal(self):
     king = King(self.white, (4, 4))
     self.board.add_piece(king)
     self.assertTrue(king.can_reach(self.board, (3, 3)))
Пример #36
0
 def test_attacked_locations(self):
     king = King(Location(5, 'd'), Player.BLACK, self.board)
     results = {Location(4, 'c'), Location(4, 'e'), Location(6, 'c'), Location(6, 'e'), Location(4, 'd'),
                Location(6, 'd'), Location(5, 'c'), Location(5, 'e')}
     self.assertEquals(set(king.attacked_locations()), results)
Пример #37
0
 def test_cant_reach(self):
     king = King(self.white, (4, 4))
     self.board.add_piece(king)
     self.assertFalse(king.can_reach(self.board, (2, 2)))