Пример #1
0
 def instantiateAll(self, color):
     piecesAlive = []
     if color == ChessConstants.COLOR[0]:
         for x in range(8):
             newPawn = Pawn(color, ChessConstants.WHITE_PAWN_POS[x])
             piecesAlive.append(newPawn)
         for x in range(2):
             newKnight = Knight(color, ChessConstants.WHITE_KNIGHT_POS[x])
             piecesAlive.append(newKnight)
             newBishop = Bishop(color, ChessConstants.WHITE_BISHOP_POS[x])
             piecesAlive.append((newBishop))
             newRook = Rook(color, ChessConstants.WHITE_ROOK_POS[x])
             piecesAlive.append(newRook)
         newQueen = Queen(color, ChessConstants.WHITE_QUEEN_POS)
         piecesAlive.append(newQueen)
         newKing = King(color, ChessConstants.WHITE_KING_POS)
         piecesAlive.append(newKing)
     elif color == ChessConstants.COLOR[1]:
         for x in range(8):
             newPawn = Pawn(color, ChessConstants.BLACK_PAWN_POS[x])
             piecesAlive.append(newPawn)
         for x in range(2):
             newKnight = Knight(color, ChessConstants.BLACK_KNIGHT_POS[x])
             piecesAlive.append(newKnight)
             newBishop = Bishop(color, ChessConstants.BLACK_BISHOP_POS[x])
             piecesAlive.append(newBishop)
             newRook = Rook(color, ChessConstants.BLACK_ROOK_POS[x])
             piecesAlive.append(newRook)
         newQueen = Queen(color, ChessConstants.BLACK_QUEEN_POS)
         piecesAlive.append(newQueen)
         newKing = King(color, ChessConstants.BLACK_KING_POS)
         piecesAlive.append(newKing)
     return piecesAlive
Пример #2
0
    def loadMajorPieces(self):
        self.matrix[0][0] = Rook('BLACK', 0, 0)
        self.matrix[7][0] = Rook('BLACK', 7, 0)

        self.matrix[1][0] = Knight('BLACK', 1, 0)
        self.matrix[6][0] = Knight('BLACK', 6, 0)

        self.matrix[2][0] = Bishop('BLACK', 2, 0)
        self.matrix[5][0] = Bishop('BLACK', 5, 0)

        self.matrix[3][0] = Queen('BLACK', 3, 0)
        self.matrix[4][0] = King('BLACK', 4, 0)

        self.matrix[0][7] = Rook('WHITE', 0, 7)
        self.matrix[7][7] = Rook('WHITE', 7, 7)

        self.matrix[1][7] = Knight('WHITE', 1, 7)
        self.matrix[6][7] = Knight('WHITE', 6, 7)

        self.matrix[2][7] = Bishop('WHITE', 2, 7)
        self.matrix[5][7] = Bishop('WHITE', 5, 7)

        self.matrix[3][7] = Queen('WHITE', 3, 7)
        self.matrix[4][7] = King('WHITE', 4, 7)

        self.black_king = self.matrix[4][0]
        self.white_king = self.matrix[4][7]

        for i in range(8):
            black_piece = self.matrix[i][0]
            white_piece = self.matrix[i][7]

            self.black_pieces.append(black_piece)
            self.white_pieces.append(white_piece)
Пример #3
0
class TestKing(unittest.TestCase):
    def setUp(self):
        self.piece = King()

    def test_symbol(self):
        self.assertEqual(self.piece.symbol, chr(0x2654))

    def test_terminal_string(self):
        self.assertEqual(self.piece.get_terminal_string(), chr(0x2654))

    def test_piece_type(self):
        self.assertEqual(self.piece.type, PieceType.KING)

    def test_is_king(self):
        self.assertTrue(self.piece.is_king())

    def test_location(self):
        self.piece.location = Location(x=5, y=10)
        self.assertEqual(self.piece.location, Location(x=5, y=10))

    def test_is_enemy_true(self):
        self.assertTrue(self.piece.is_enemy(Attacker()))

    def test_is_enemy_false(self):
        self.assertFalse(self.piece.is_enemy(Defender()))
Пример #4
0
 def reset(self):
     self.pieces = []
     for x in range(0, 8):
         self.pieces.append(Pawn.Pawn((x, 6), True))
         self.pieces.append(Pawn.Pawn((x, 1), False))
     # White Pieces
     self.pieces.append(Rook.Rook((7, 7), True))
     self.pieces.append(Rook.Rook((0, 7), True))
     self.pieces.append(Knight.Knight((1, 7), True))
     self.pieces.append(Knight.Knight((6, 7), True))
     self.pieces.append(Bishop.Bishop((2, 7), True))
     self.pieces.append(Bishop.Bishop((5, 7), True))
     self.pieces.append(Queen.Queen((3, 7), True))
     self.pieces.append(King.King((4, 7), True))
     # Black Pieces
     self.pieces.append(Rook.Rook((7, 0), False))
     self.pieces.append(Rook.Rook((0, 0), False))
     self.pieces.append(Knight.Knight((1, 0), False))
     self.pieces.append(Knight.Knight((6, 0), False))
     self.pieces.append(Bishop.Bishop((2, 0), False))
     self.pieces.append(Bishop.Bishop((5, 0), False))
     self.pieces.append(Queen.Queen((3, 0), False))
     self.pieces.append(King.King((4, 0), False))
     # Set turn
     self.whitesTurn = True
Пример #5
0
    def preset1(self):
        preset1 = []

        preset1.append(King.King((0, 0), False))
        preset1.append(Rook.Rook((7, 1), True))
        preset1.append(King.King((7, 7), True))
        preset1.append(Rook.Rook((4, 4), True))
        return preset1
Пример #6
0
def test_king_two_moves():
    b = Board()
    k = King("BLACK")
    k.set_position(("F", 7))
    b.pieces.append(k)
    k.find_available_moves(b)
    k.set_position(("G", 7))
    k.find_available_moves(b)
    moves = k.available_moves
    assert (len(moves) == 8)
    assert (("E", 7) not in moves)
Пример #7
0
def test_snapshot():
    b = Board()
    k = King("WHITE")
    k.current_position = ("E", 1)
    b.pieces.append(k)
    b.save_snapshot()
    b.pieces[0].current_position = ("F", 1)
    assert (b.is_empty(("E", 1)))
    assert (not b.is_empty(("F", 1)))
    b.load_snapshot()
    assert (b.is_empty(("F", 1)))
    assert (not b.is_empty(("E", 1)))
Пример #8
0
def test_get_piece():
    b = Board()
    k = King("WHITE")
    k.current_position = ("E", 1)
    b.pieces.append(k)
    for col in COLNAMES:
        for row in range(1, 9):
            if not (col, row) == ("E", 1):
                assert (b.is_empty((col, row)))
            else:
                p = b.piece_at((col, row))
                assert (p.colour == "WHITE")
                assert (p.piece_type == "King")
Пример #9
0
 def draw(self, screen):
     #Fill in with "None", for the reset button. if you remove this part, things become interesting
     #for i in range (0, 8):
     #    for j in range (0, 8):
     #        if self.pieces[i][j] != None:
     #            del self.pieces[i][j]
     #            self.pieces[i][j] = None
     
     self.pieces[0][0] = Rook('b', [0,0])
     self.pieces[0][1] = Knight('b', [0, 1])
     self.pieces[0][2] = Bishop('b', [0,2])
     self.pieces[0][3] = Queen('b')
     self.pieces[0][4] = King('b')
     self.pieces[0][5] = Bishop('b', [0,5])
     self.pieces[0][6] = Knight('b', [0,6])
     self.pieces[0][7] = Rook('b', [0,7])
     self.pieces[1][0] = Pawn('b', [1,0])
     self.pieces[1][1] = Pawn('b', [1,1])
     self.pieces[1][2] = Pawn('b', [1,2])
     self.pieces[1][3] = Pawn('b', [1,3])
     self.pieces[1][4] = Pawn('b', [1,4])
     self.pieces[1][5] = Pawn('b', [1,5])
     self.pieces[1][6] = Pawn('b', [1,6])
     self.pieces[1][7] = Pawn('b', [1,7])
     self.pieces[7][0] = Rook('w', [7,0])
     self.pieces[7][1] = Knight('w', [7,1])
     self.pieces[7][2] = Bishop('w', [7,2])
     self.pieces[7][3] = Queen('w')
     self.pieces[7][4] = King('w')
     self.pieces[7][5] = Bishop('w', [7,5])
     self.pieces[7][6] = Knight('w', [7,6])
     self.pieces[7][7] = Rook('w', [7,7])
     self.pieces[6][0] = Pawn('w', [6,0])
     self.pieces[6][1] = Pawn('w', [6,1])
     self.pieces[6][2] = Pawn('w', [6,2])
     self.pieces[6][3] = Pawn('w', [6,3])
     self.pieces[6][4] = Pawn('w', [6,4])
     self.pieces[6][5] = Pawn('w', [6,5])
     self.pieces[6][6] = Pawn('w', [6,6])
     self.pieces[6][7] = Pawn('w', [6,7])        
     
     
     for r in self.board: #Draw board
         for s in r:
             screen.fill(s.color, s.pygameSquare)
     
     for c in self.pieces: #Fill in pieces
         for p in c:
             if p != None:
                 screen.blit(p.symbol, (self.squareSize*c.index(p), self.squareSize*self.pieces.index(c)))
Пример #10
0
    def setup_starting_positions(self):
        self.button_identities[0][0].config(image=self.pictures["Rook"])
        self.button_identities[0][1].config(image=self.pictures["Knight"])
        self.button_identities[0][2].config(image=self.pictures["Bishop"])
        self.button_identities[0][3].config(image=self.pictures["Queen"])
        self.button_identities[0][4].config(image=self.pictures["King"])
        self.button_identities[0][5].config(image=self.pictures["Bishop"])
        self.button_identities[0][6].config(image=self.pictures["Knight"])
        self.button_identities[0][7].config(image=self.pictures["Rook"])
        


        for x in range(8):
            self.button_identities[1][x].config(image=self.pictures["Pawn"])
            self.button_identities[6][x].config(image=self.pictures["PawnFilled"])

        self.button_identities[7][0].config(image=self.pictures["RookFilled"])
        self.button_identities[7][1].config(image=self.pictures["KnightFilled"])
        self.button_identities[7][2].config(image=self.pictures["BishopFilled"])
        self.button_identities[7][3].config(image=self.pictures["KingFilled"])
        self.button_identities[7][4].config(image=self.pictures["QueenFilled"])
        self.button_identities[7][5].config(image=self.pictures["BishopFilled"])
        self.button_identities[7][6].config(image=self.pictures["KnightFilled"])
        self.button_identities[7][7].config(image=self.pictures["RookFilled"])

        for x in range(2,6):
            for y in range(8):
                self.button_identities[x][y].config(image=self.pictures["Empty"])

        self.piece_array[0][0] = Rook(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][1] = Knight(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][2] = Bishop(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][3] = Queen(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][4] = King(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][5] = Bishop(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][6] = Knight(PieceColour.WHITE,self.gameMaster)
        self.piece_array[0][7] = Rook(PieceColour.WHITE,self.gameMaster)
        for x in range(8):
            self.piece_array[1][x] = Pawn(PieceColour.WHITE,self.gameMaster)
        self.piece_array[7][0] = Rook(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][1] = Knight(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][2] = Bishop(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][3] = King(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][4] = Queen(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][5] = Bishop(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][6] = Knight(PieceColour.BLACK,self.gameMaster)
        self.piece_array[7][7] = Rook(PieceColour.BLACK,self.gameMaster)
        for x in range(8):
            self.piece_array[6][x] = Pawn(PieceColour.BLACK,self.gameMaster)
        self.manual_assign()
Пример #11
0
def test_promotion():
    g = Game()
    g.clear()
    g.add_piece(Pawn("WHITE"), ("B", 7))
    g.add_piece(King("WHITE"), ("E", 1))
    g.add_piece(Pawn("BLACK"), ("G", 2))
    g.add_piece(King("BLACK"), ("E", 7))
    g.update_all_pieces()
    assert (g.is_legal_move("WHITE", ("B", 7), ("B", 8)))
    g.move(("B", 7), ("B", 8))
    assert (g.board.piece_at(("B", 8)).piece_type == "Queen")
    g.update_all_pieces()
    assert (g.is_legal_move("BLACK", ("G", 2), ("G", 1)))
    g.move(("G", 2), ("G", 1))
    assert (g.board.piece_at(("G", 1)).piece_type == "Queen")
Пример #12
0
    def __init__(self, color, pieces, board, enemy=None):
        self.color = color
        self.board = board
        self.enemy = enemy

        self.king = None
        self.pieces = []
        for piece in pieces:
            match = re.match(notation, piece)
            title = match.group('title')
            pos = match.group('pos')
            if title is None:
                self.pieces.append(Pawn(self, pos, board))
            else:
                if title in 'Rr':
                    self.pieces.append(Rook(self, pos, board))
                elif title in 'Nn':
                    self.pieces.append(Knight(self, pos, board))
                elif title in 'Bb':
                    self.pieces.append(Bishop(self, pos, board))
                elif title in 'Qq':
                    self.pieces.append(Queen(self, pos, board))
                elif title in 'Kk':
                    self.pieces.append(King(self, pos, board))
                    self.king = self.pieces[-1]
Пример #13
0
 def __init__(self, present, color='None', type='None'):
     self.present = present
     self.pos_x = 0
     self.pos_y = 0
     self.board_x = 0
     self.board_y = 0
     self.name = 'None'
     self.color = color
     if self.present == False:
         return
     if type == 'B':
         self.name = 'Bishop'
         self.type = Bishop.Bishop(self.color)
     elif type == 'R':
         self.name = 'Rook'
         self.type = Rook.Rook(self.color)
     elif type == 'K':
         self.name = 'King'
         self.type = King.King(self.color)
     elif type == 'N':
         self.name = 'Knight'
         self.type = Knight.Knight(self.color)
     elif type == 'P':
         self.name = 'Pawn'
         self.type = Pawn.Pawn(self.color)
     else:
         self.name = 'Queen'
         self.type = Queen.Queen(self.color)
Пример #14
0
def test_no_checkmate_king_takes():
    """
    Problem seen when game declared checkmate
    even though king could have taken the piece 
    threatening it - check this is fixed.
    """
    g = Game()
    g.clear()
    g.add_piece(King("BLACK"), ("H", 8))
    g.add_piece(King("WHITE"), ("E", 1))
    g.add_piece(Pawn("WHITE"), ("E", 2))
    g.add_piece(Pawn("WHITE"), ("F", 2))
    g.add_piece(Queen("BLACK"), ("D", 5))
    g.update_all_pieces()
    g.next_player_turn()
    g.move(("D", 5), ("D", 1))
    assert (not g.is_checkmate("WHITE"))
Пример #15
0
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols

        self.board = [[0 for x in range(cols)] for y 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.p1Name = "Player 1"
        self.p2Name = "Player 2"

        self.turn = "w"
Пример #16
0
def test_checkmate():
    g = Game()
    g.clear()
    g.add_piece(King("BLACK"), ("H", 8))
    g.add_piece(Queen("WHITE"), ("G", 7))
    g.add_piece(Rook("WHITE"), ("G", 6))
    assert (len(g.board.pieces) == 3)
    g.update_all_pieces()
    g.next_player_turn()
    assert (g.is_check("BLACK"))
    assert (g.is_checkmate("BLACK"))
Пример #17
0
def first_row(color: Color) -> list:
    return [
        Rook(color, 0),
        Knight(color, 1),
        Bishop(color, 2),
        Queen(color, 3),
        King(color, 4),
        Bishop(color, 5),
        Knight(color, 6),
        Rook(color, 7)
    ]
Пример #18
0
 def set_board(self):
     # Create blank board
     self.board = [[Blank()] * 8 for i in range(8)]
     # set board by updating self.board
     self.board[0][0] = Rook('black')
     self.board[7][0] = Rook('black')
     self.board[1][0] = Knight('black')
     self.board[6][0] = Knight('black')
     self.board[2][0] = Bishop('black')
     self.board[5][0] = Bishop('black')
     self.board[3][0] = Queen('black')
     self.board[4][0] = King('black')
     self.board[0][1] = Pawn('black')
     self.board[1][1] = Pawn('black')
     self.board[2][1] = Pawn('black')
     self.board[3][1] = Pawn('black')
     self.board[4][1] = Pawn('black')
     self.board[5][1] = Pawn('black')
     self.board[6][1] = Pawn('black')
     self.board[7][1] = Pawn('black')
     self.board[0][7] = Rook('white')
     self.board[7][7] = Rook('white')
     self.board[1][7] = Knight('white')
     self.board[6][7] = Knight('white')
     self.board[2][7] = Bishop('white')
     self.board[5][7] = Bishop('white')
     self.board[3][7] = Queen('white')
     self.board[4][7] = King('white')
     self.board[0][6] = Pawn('white')
     self.board[1][6] = Pawn('white')
     self.board[2][6] = Pawn('white')
     self.board[3][6] = Pawn('white')
     self.board[4][6] = Pawn('white')
     self.board[5][6] = Pawn('white')
     self.board[6][6] = Pawn('white')
     self.board[7][6] = Pawn('white')
Пример #19
0
def test_king_in_corner():
    b = Board()
    for colour in ["WHITE", "BLACK"]:
        k = King(colour)
        k.set_position(("A", 1))
        k.find_available_moves(b)
        moves = k.available_moves
        assert (len(moves) == 3)
        assert (("B", 1) in moves)
        assert (("B", 2) in moves)
        assert (("A", 2) in moves)
Пример #20
0
def test_king_on_edge():
    b = Board()
    for colour in ["WHITE", "BLACK"]:
        k = King(colour)
        k.set_position(("F", 1))
        k.find_available_moves(b)
        moves = k.available_moves
        assert (len(moves) == 5)
        assert (("E", 1) in moves)
        assert (("E", 2) in moves)
        assert (("F", 2) in moves)
        assert (("G", 1) in moves)
        assert (("G", 2) in moves)
Пример #21
0
    def from_binary(self, boardBits):
        kingX = int(boardBits[0:3], 2)
        kingY = int(boardBits[3:6], 2)
        self.pieces.add_piece(King(x=kingX, y=kingY))

        locIndex = 0
        strIndex = 6
        while locIndex < 44:
            if boardBits[strIndex] == '1':  # piece found here
                newLoc = BoardLocations.gen_from_index(locIndex)
                if boardBits[strIndex + 1] == '1':
                    self.pieces.add_piece(Attacker(location=newLoc))
                else:
                    self.pieces.add_piece(Defender(location=newLoc))
                strIndex += 1  # advance one for the data payload
            locIndex += 1
            strIndex += 1

        self.whoseTurn = "D"
        if boardBits[-2] == "1":
            self.whoseTurn = "A"
Пример #22
0
 def reset(self):
     """
     Start a new game
     """
     self.board.pieces = []
     for i in range(8):
         self.add_piece(Pawn("WHITE"), (COLNAMES[i], 2))
         self.add_piece(Pawn("BLACK"), (COLNAMES[i], 7))
     row_dict = {"WHITE": 1, "BLACK": 8}
     for colour, row in row_dict.items():
         self.add_piece(King(colour), ("E", row))
         self.add_piece(Queen(colour), ("D", row))
         for col in ["A", "H"]:
             self.add_piece(Rook(colour), (col, row))
         for col in ["B", "G"]:
             self.add_piece(Knight(colour), (col, row))
         for col in ["C", "F"]:
             self.add_piece(Bishop(colour), (col, row))
     self.next_to_play = "WHITE"
     self.history = []
     self.update_all_pieces()
Пример #23
0
def test_king_no_obstacle():
    b = Board()
    for colour in ["WHITE", "BLACK"]:
        k = King(colour)
        k.set_position(("F", 4))
        k.find_available_moves(b)
        moves = k.available_moves
        assert (len(moves) == 8)
        assert (("E", 3) in moves)
        assert (("E", 4) in moves)
        assert (("E", 5) in moves)
        assert (("F", 3) in moves)
        assert (("F", 5) in moves)
        assert (("G", 3) in moves)
        assert (("G", 4) in moves)
        assert (("G", 5) in moves)
Пример #24
0
def test_king_blocked():
    b = Board()
    for pos in [("C", 8), ("C", 7), ("E", 7), ("D", 6), ("E", 6)]:
        p = Pawn("BLACK")
        p.set_position(pos)
        b.pieces.append(p)
    k = King("BLACK")
    k.set_position(("D", 7))
    b.pieces.append(k)
    k.find_available_moves(b)
    moves = k.available_moves
    assert (len(moves) == 3)
    assert (("D", 7) not in moves)
    assert (("D", 8) in moves)
    assert (("E", 8) in moves)
    assert (("C", 6) in moves)
Пример #25
0
 def setUp(self):
     self.piece = King()
Пример #26
0
 def test_is_enemy_false_king(self):
     self.assertFalse(self.piece.is_enemy(King()))
Пример #27
0
 def __init__(self):
     self.white_checked = False
     self.black_checked = False
     self.board = [[0 for j in range(8)] for i in range(8)]
     self.white_pieces = []
     self.black_pieces = []
     self.white_king_position = ()
     self.black_king_position = ()
     # making a chess board with 8x8(64) total squares
     for index in range(1, 9):
         for letter in range(1, 9):
             self.board[index - 1][letter - 1] = Square((letter, index))
             # making a board with black and white chess pieces
             if index == 1:
                 if letter in [1, 8]:
                     self.board[index - 1][letter - 1].set_figure(
                         Rook(True, (letter, index)))
                     self.white_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter in [2, 7]:
                     self.board[index - 1][letter - 1].set_figure(
                         Knight(True, (letter, index)))
                     self.white_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter in [3, 6]:
                     self.board[index - 1][letter - 1].set_figure(
                         Bishop(True, (letter, index)))
                     self.white_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter == 4:
                     self.board[index - 1][letter - 1].set_figure(
                         Queen(True, (letter, index)))
                     self.white_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter == 5:
                     self.board[index - 1][letter - 1].set_figure(
                         King(True, (letter, index)))
                     self.white_king_position = (index, letter)
                     self.white_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
             elif index == 8:
                 if letter in [1, 8]:
                     self.board[index - 1][letter - 1].set_figure(
                         Rook(False, (letter, index)))
                     self.black_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter in [2, 7]:
                     self.board[index - 1][letter - 1].set_figure(
                         Knight(False, (letter, index)))
                     self.black_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter in [3, 6]:
                     self.board[index - 1][letter - 1].set_figure(
                         Bishop(False, (letter, index)))
                     self.black_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter == 4:
                     self.board[index - 1][letter - 1].set_figure(
                         Queen(False, (letter, index)))
                     self.black_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
                 elif letter == 5:
                     self.board[index - 1][letter - 1].set_figure(
                         King(False, (letter, index)))
                     self.black_king_position = (index, letter)
                     self.black_pieces.append(
                         self.board[index - 1][letter - 1].get_figure())
             elif index == 2:
                 self.board[index - 1][letter - 1].set_figure(
                     Pawn(True, (letter, index)))
                 self.white_pieces.append(
                     self.board[index - 1][letter - 1].get_figure())
             elif index == 7:
                 self.board[index - 1][letter - 1].set_figure(
                     Pawn(False, (letter, index)))
                 self.black_pieces.append(
                     self.board[index - 1][letter - 1].get_figure())
Пример #28
0
 def test_is_enemy_true_king(self):
     self.assertTrue(self.piece.is_enemy(King()))
Пример #29
0
def castle(y, x, oldx, piece, board):
    if x - oldx == 2:
        board[y][x - 1] = 2 * (piece // abs(piece))
        board[y][x - 4] = 0

    elif oldx - x == 2:
        board[y][x + 1] = 2 * (piece // abs(piece))
        board[y][x + 3] = 0


# Gameloop

rookcheck = Rook()
bishopcheck = Bishop()
knightcheck = Knight()
kingcheck = King()
pawncheck = Pawn()


def main():
    crashed = False
    clock = pygame.time.Clock()
    selected = None
    pos1, pos2 = None, None
    curr_piece = 0
    incheck = False

    global gameboard, counter, white_king, black_king

    while not crashed:
        displayBoard(gameboard)
Пример #30
0
        print(blackPieces)
    except EOFError:
        print()  # corrects missing endline when no pieces are input
        whitePieces = [
            f'{choice(titles)}{choice(files)}{choice(ranks)}'
            for _ in range(numPieces)
        ] + [f'k{choice(files)}{choice(ranks)}']
        blackPieces = [
            f'{choice(titles)}{choice(files)}{choice(ranks)}'
            for _ in range(numPieces)
        ] + [f'k{choice(files)}{choice(ranks)}']

    white = Player('White', whitePieces, chess)
    if white.king is None:
        print("White king missing, creating one at default starting position")
        white.king = King('White', 'e1', white.board)
        white.pieces.append(white.king)

    black = Player('Black', blackPieces, chess, white)
    if black.king is None:
        print("Black king missing, creating one at default starting position")
        black.king = King('Black', 'e8', black.board)
        black.pieces.append(black.king)

    #manually set white's enemy since black didn't exist when white was created
    white.enemy = black

    players = (white, black)
    for player in players:
        for piece_ in player.pieces:
            chess.add(piece_)