Пример #1
0
    def _place_pieces(self):
        self.tiles[0][0].piece = Rook(Color.BLACK)
        self.tiles[0][1].piece = Knight(Color.BLACK)
        self.tiles[0][2].piece = Bishop(Color.BLACK)
        self.tiles[0][3].piece = Queen(Color.BLACK)
        self.tiles[0][4].piece = King(Color.BLACK)
        self.tiles[0][5].piece = Bishop(Color.BLACK)
        self.tiles[0][6].piece = Knight(Color.BLACK)
        self.tiles[0][7].piece = Rook(Color.BLACK)

        for i in range(self.width):
            self.tiles[1][i].piece = Pawn(Color.BLACK)
            self.tiles[6][i].piece = Pawn(Color.WHITE)

        self.tiles[7][0].piece = Rook(Color.WHITE)
        self.tiles[7][1].piece = Knight(Color.WHITE)
        self.tiles[7][2].piece = Bishop(Color.WHITE)
        self.tiles[7][3].piece = Queen(Color.WHITE)
        self.tiles[7][4].piece = King(Color.WHITE)
        self.tiles[7][5].piece = Bishop(Color.WHITE)
        self.tiles[7][6].piece = Knight(Color.WHITE)
        self.tiles[7][7].piece = Rook(Color.WHITE)
Пример #2
0
 def test_it_can_validate_moves(self):
     queen = Queen(Color.BLACK)
     self.assertTrue(queen.is_valid_move('a1', 'h8'))
     self.assertTrue(queen.is_valid_move('a1', 'a8'))
     self.assertTrue(queen.is_valid_move('a1', 'h1'))
     self.assertFalse(queen.is_valid_move('a1', 'd5'))
     self.assertFalse(queen.is_valid_move('a1', 'b8'))
     self.assertFalse(queen.is_valid_move('a1', 'h2'))
     self.assertFalse(queen.is_valid_move('a1', 'c2'))
Пример #3
0
    def aiMove(self):
        self.setWindowTitle("Chess: AI")

        # parse
        move = ""
        for p in self.lastMove:
            move += chr(p['x'] + ord('a')) + str(8 - p['y'])
        print(f"player move: {move}")
        self.ai.hist.append(self.ai.hist[-1].move(
            (self.ai.parse(move[:2]), self.ai.parse(move[2:]))))
        self.ai.print_pos(self.ai.hist[-1].rotate())

        # search
        start = time.time()
        for _, move, _ in self.ai.searcher.search(self.ai.hist[-1],
                                                  self.ai.hist):
            if time.time() - start > self.ai.time:
                break

        # move
        self.ai.hist.append(self.ai.hist[-1].move(move))
        move = self.ai.render(119 - move[0]) + self.ai.render(119 - move[1])
        print(f"ai move: {move}")
        x1, y1, x2, y2 = ord(move[0]) - ord('a'), 8 - int(move[1]), ord(
            move[2]) - ord('a'), 8 - int(move[3])

        self.ai.print_pos(self.ai.hist[-1])

        if self.chessBoard[y1][x1].getType() == "Pawn":
            piece = self.chessBoard[y1][x1]
            _, promotion = piece.move(Position(x2, y2), self.chessBoard)
            if promotion:
                self.chessBoard[y2][x2] = Queen(piece.pos, piece.team)
        elif self.chessBoard[y1][x1].getType() == "King":
            self.chessBoard[y1][x1].move(Position(x2, y2), self.chessBoard,
                                         self.blackCheckBoard)
        else:
            self.chessBoard[y1][x1].move(Position(x2, y2), self.chessBoard)
        # refresh CheckBoard
        self.whiteCheckBoard, self.whiteCheck = fillCheckBoard(
            self.chessBoard, Team.WHITE)
        self.blackCheckBoard, self.BlackCheck = fillCheckBoard(
            self.chessBoard, Team.BLACK)
        self.turn = Team.WHITE
Пример #4
0
 def test_it_can_be_instantiated(self):
     queen = Queen(Color.BLACK)
     self.assertIsInstance(queen, Queen)
Пример #5
0
 def test_it_has_the_proper_strong_representation_for_its_color(self):
     b_queen = Queen(Color.BLACK)
     self.assertEqual(str(b_queen), '\u265B')
     w_queen = Queen(Color.WHITE)
     self.assertEqual(str(w_queen), '\u2655')
Пример #6
0
 def __init__(self, team: Team, chessboard, pos: Position):
     super().__init__()
     self.chessboard = chessboard
     self.pos = pos
     self.pieces = [Tile(Queen(pos, team)), Tile(Rook(pos, team)), Tile(Bishop(pos, team)),Tile(Knight(pos, team))]
     self.initUI()
Пример #7
0
from chess.pieces.bishop import Bishop
from chess.pieces.knight import Knight
from chess.pieces.rook import Rook
from chess.pieces.queen import Queen
from chess.pieces.king import King

from chess.position import Position
from chess.team import Team
from chess.check import *




chessBoard_init = [
        [
            Rook(Position(0, 0), Team.BLACK), Knight(Position(1, 0), Team.BLACK), Bishop(Position(2, 0), Team.BLACK), Queen(Position(3, 0), Team.BLACK), King(Position(4, 0), Team.BLACK), Bishop(Position(5, 0), Team.BLACK), Knight(Position(6, 0), Team.BLACK), Rook(Position(7, 0), Team.BLACK)
        ],
        [
            Pawn(Position(x, 1), Team.BLACK) for x in range(8)
        ],
        [None for _ in range(8)],
        [None for _ in range(8)],
        [None for _ in range(8)],
        [None for _ in range(8)],
        [
            Pawn(Position(x, 6), Team.WHITE) for x in range(8)
        ],
        [
            Rook(Position(0, 7), Team.WHITE), Knight(Position(1, 7), Team.WHITE), Bishop(Position(2, 7), Team.WHITE), Queen(Position(3, 7), Team.WHITE), King(Position(4, 7), Team.WHITE), Bishop(Position(5, 7), Team.WHITE), Knight(Position(6, 7), Team.WHITE), Rook(Position(7, 7), Team.WHITE)
        ],
        
Пример #8
0
 def promote_to_queen(self):
     if self.color == WHITE:
         return Queen(self.row, self.col, WHITE, WHITE_QUEEN)
     else:
         return Queen(self.row, self.col, BLACK, BLACK_QUEEN)