Ejemplo n.º 1
0
    def setup_initial_pieces(self):
        for [row, colour] in [[0, "BLACK"], [7, "WHITE"]]:
            self.board[row][0] = pcs.Rook(colour)
            self.board[row][1] = pcs.Knight(colour)
            self.board[row][2] = pcs.Bishop(colour)
            self.board[row][5] = pcs.Bishop(colour)
            self.board[row][6] = pcs.Knight(colour)
            self.board[row][7] = pcs.Rook(colour)
            for column in range(8):
                if colour == "BLACK":
                    self.board[row + 1][column] = pcs.Pawn(colour)
                else:
                    self.board[row - 1][column] = pcs.Pawn(colour)
            #temp
            self.board[row][0].update_role(roles.BasicRole())
            self.board[row][1].update_role(roles.BasicRole())
            self.board[row][2].update_role(roles.BasicRole())
            self.board[row][5].update_role(roles.BasicRole())
            self.board[row][6].update_role(roles.BasicRole())
            self.board[row][7].update_role(roles.BasicRole())

        self.board[0][3] = pcs.Queen("BLACK")
        self.board[0][4] = pcs.King("BLACK")
        self.board[7][3] = pcs.King("WHITE")
        self.board[7][4] = pcs.Queen("WHITE")

        #temp
        self.board[0][3].update_role(roles.BasicRole())
        self.board[0][4].update_role(roles.BasicRole())
        self.board[7][3].update_role(roles.BasicRole())
        self.board[7][4].update_role(roles.BasicRole())
Ejemplo n.º 2
0
 def readyToPlay(self):
     board = [
         [
             Pieces.Rook(Color.WHITE, self),
             Pieces.Knight(Color.WHITE, self),
             Pieces.Bishop(Color.WHITE, self),
             Pieces.King(Color.WHITE, self),
             Pieces.Queen(Color.WHITE, self),
             Pieces.Bishop(Color.WHITE, self),
             Pieces.Knight(Color.WHITE, self),
             Pieces.Rook(Color.WHITE, self),
         ],
         [Pieces.Pawn(Color.WHITE, self) for i in range(8)],
         [PieceName.NONE for i in range(8)],
         [PieceName.NONE for i in range(8)],
         [PieceName.NONE for i in range(8)],
         [PieceName.NONE for i in range(8)],
         [Pieces.Pawn(Color.BLACK, self) for i in range(8)],
         [
             Pieces.Rook(Color.BLACK, self),
             Pieces.Knight(Color.BLACK, self),
             Pieces.Bishop(Color.BLACK, self),
             Pieces.King(Color.BLACK, self),
             Pieces.Queen(Color.BLACK, self),
             Pieces.Bishop(Color.BLACK, self),
             Pieces.Knight(Color.BLACK, self),
             Pieces.Rook(Color.BLACK, self),
         ],
     ]
     for i in range(8):
         for j in range(8):
             self.__playboard[i][j].piece_setter(board[i][j])
     return
Ejemplo n.º 3
0
    def __init__(self, grid, screen):
        # White
        grid[0][0] = Pieces.Rook("White", 0, 0, screen)
        grid[0][7] = Pieces.Rook("White", 0, 7, screen)

        grid[0][1] = Pieces.Knight("White", 0, 1, screen)
        grid[0][6] = Pieces.Knight("White", 0, 6, screen)

        grid[0][2] = Pieces.Bishop("White", 0, 2, screen)
        grid[0][5] = Pieces.Bishop("White", 0, 5, screen)

        grid[0][4] = Pieces.Queen("White", 0, 4, screen)
        grid[0][3] = Pieces.King("White", 0, 3, screen)
        for x in range(8):
            grid[1][x] = Pieces.Pawn("White", 1, x, screen)

        # Black changes in some comments
        grid[7][0] = Pieces.Rook("Black", 7, 0, screen)
        grid[7][7] = Pieces.Rook("Black", 7, 7, screen)

        grid[7][1] = Pieces.Knight("Black", 7, 1, screen)
        grid[7][6] = Pieces.Knight("Black", 7, 6, screen)

        grid[7][2] = Pieces.Bishop("Black", 7, 2, screen)
        grid[7][5] = Pieces.Bishop("Black", 7, 5, screen)

        grid[7][4] = Pieces.Queen("Black", 7, 4, screen)
        grid[7][3] = Pieces.King("Black", 7, 3, screen)
        for y in range(8):
            grid[6][y] = Pieces.Pawn("Black", 6, y, screen)
        self.grid = grid
        return
Ejemplo n.º 4
0
def gen_kings(board: Board, k, bk):
    kings = {"white": Pieces.King(board, board.get_square((board.size // 2, board.size - 1)), white, k),
             "black": Pieces.King(board, board.get_square((board.size // 2, 0)), black, bk)}

    board.white_pieces.add(kings["white"])
    board.black_pieces.add(kings["black"])
    return kings
Ejemplo n.º 5
0
    def __init__(self, textmode=False):
        self.textmode = textmode
        self.pieces.append(Pieces.Rook(self.BLACK))
        self.pieces.append(Pieces.Knight(self.BLACK))
        self.pieces.append(Pieces.Bishop(self.BLACK))
        self.pieces.append(Pieces.Queen(self.BLACK))
        self.pieces.append(Pieces.King(self.BLACK))
        self.pieces.append(Pieces.Bishop(self.BLACK))
        self.pieces.append(Pieces.Knight(self.BLACK))
        self.pieces.append(Pieces.Rook(self.BLACK))

        for i in range(9, 17):
            self.pieces.append(Pieces.Pawn(self.BLACK))

        for i in range(17, 65 - 16):
            self.pieces.append(None)

        for i in range(65 - 16, 65 - 8):
            self.pieces.append(Pieces.Pawn(self.WHITE))

        self.pieces.append(Pieces.Rook(self.WHITE))
        self.pieces.append(Pieces.Knight(self.WHITE))
        self.pieces.append(Pieces.Bishop(self.WHITE))
        self.pieces.append(Pieces.Queen(self.WHITE))
        self.pieces.append(Pieces.King(self.WHITE))
        self.pieces.append(Pieces.Bishop(self.WHITE))
        self.pieces.append(Pieces.Knight(self.WHITE))
        self.pieces.append(Pieces.Rook(self.WHITE))
Ejemplo n.º 6
0
def init(color):

    #intialization of board based on the color
    if color.lower() == "white":
        board[0][0] = Pieces.Rook("Black")
        board[0][1] = Pieces.Knight("Black")
        board[0][2] = Pieces.Bishop("Black")
        board[0][3] = Pieces.Queen("Black")
        board[0][4] = Pieces.King("Black")
        board[0][5] = Pieces.Bishop("Black")
        board[0][6] = Pieces.Knight("Black")
        board[0][7] = Pieces.Rook("Black")

        for x in range(8):
            board[1][x] = Pieces.Pawn("Black")

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

        for x in range(8):
            board[6][x] = Pieces.Pawn("White")

    else:
        board[0][0] = Pieces.Rook("White")
        board[0][1] = Pieces.Knight("White")
        board[0][2] = Pieces.Bishop("White")
        board[0][3] = Pieces.King("White")
        board[0][4] = Pieces.Queen("White")
        board[0][5] = Pieces.Bishop("White")
        board[0][6] = Pieces.Knight("White")
        board[0][7] = Pieces.Rook("White")

        for x in range(8):
            board[1][x] = Pieces.Pawn("White")

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

        for x in range(8):
            board[6][x] = Pieces.Pawn("Black")
 def init_board(self):
     piece_dict = {
         "P":
         lambda row, col, board, color: p.Pawn(row, col, board, color, self.
                                               special_move_mem),
         "N":
         lambda row, col, board, color: p.Knight(row, col, board, color),
         "B":
         lambda row, col, board, color: p.Bishop(row, col, board, color),
         "Q":
         lambda row, col, board, color: p.Queen(row, col, board, color),
         "K":
         lambda row, col, board, color: p.King(row, col, board, color),
         "R":
         lambda row, col, board, color: p.Rook(row, col, board, color),
     }
     for row in range(0, 8):
         for col in range(0, 8):
             piece = self.string_board[row][col]
             if piece != "..":
                 piece_color = piece[0]
                 if piece_color == 'b':
                     piece_color = "black"
                 else:
                     piece_color = "white"
                 piece_type = piece[1]
                 self.string_board[row][col] = piece_dict[piece_type](
                     row, col, self.string_board, piece_color)
                 if piece_color == "black":
                     self.black_playable_pieces.append(
                         self.string_board[row][col])
                 else:
                     self.white_playable_pieces.append(
                         self.string_board[row][col])
     return self.string_board
Ejemplo n.º 8
0
    def return_positions(self, piece, game):
        move_able = []
        cur_row, cur_col = game.get_piece_pos(piece)
        # Refer to get_direction_of for understanding of the code statement below
        for quadrant in range(self.quadrant_corners):
            y_dir = self.get_direction_of(quadrant, self.COL_INDEX)

            if piece.get_piece_name() == Pieces.Pawn().get_piece_name():
                piece_direction = game.get_piece_directions()[
                    piece.get_color()]
                if piece_direction != y_dir:
                    continue

            elif piece.get_piece_name() == Pieces.King().get_piece_name():
                if not piece.is_moved():  # Haven't Moved
                    pass

            for distance in range(1, self.max_distance + 1):
                new_row = cur_row + distance * y_dir
                if self.is_movable(game, piece, new_row, cur_col):
                    # Will King be threaten
                    move_able.append([new_row, cur_col])
                    if game.get_space(*move_able[-1]):
                        break
                else:
                    break
        return move_able
Ejemplo n.º 9
0
 def __init__(self):
     super(Chess,
           self).__init__(600,
                          600,
                          resizable=False,
                          caption='Chess',
                          config=pyglet.gl.Config(double_buffer=True),
                          vsync=False)
     self.wKing = p.King(4, 0)
     self.bKing = p.King(4, 7, False)
     self.board = [[
         p.Rook(0, 0),
         p.Knight(1, 0),
         p.Bishop(2, 0),
         p.Queen(3, 0), self.wKing,
         p.Bishop(5, 0),
         p.Knight(6, 0),
         p.Rook(7, 0)
     ], [p.Pawn(i, 1) for i in range(8)], [None for i in range(8)],
                   [None for i in range(8)], [None for i in range(8)],
                   [None for i in range(8)],
                   [p.Pawn(i, 6, False) for i in range(8)],
                   [
                       p.Rook(0, 7, False),
                       p.Knight(1, 7, False),
                       p.Bishop(2, 7, False),
                       p.Queen(3, 7, False), self.bKing,
                       p.Bishop(5, 7, False),
                       p.Knight(6, 7, False),
                       p.Rook(7, 7, False)
                   ]]
     self.validsprites = []
     for i in range(8):
         rowsprites = []
         for j in range(8):
             sprite = pyglet.sprite.Sprite(self.validImg, 75 * j, 75 * i)
             sprite.visible = False
             rowsprites.append(sprite)
         self.validsprites.append(rowsprites)
     self.wQueen = pyglet.sprite.Sprite(self.spritesheet[7], 131.25, 225)
     self.wRook = pyglet.sprite.Sprite(self.spritesheet[10], 218.75, 225)
     self.wBishop = pyglet.sprite.Sprite(self.spritesheet[8], 306.25, 225)
     self.wKnight = pyglet.sprite.Sprite(self.spritesheet[9], 393.75, 225)
     self.bQueen = pyglet.sprite.Sprite(self.spritesheet[1], 131.25, 225)
     self.bRook = pyglet.sprite.Sprite(self.spritesheet[4], 218.75, 225)
     self.bBishop = pyglet.sprite.Sprite(self.spritesheet[2], 306.25, 225)
     self.bKnight = pyglet.sprite.Sprite(self.spritesheet[3], 393.75, 225)
Ejemplo n.º 10
0
    def create_pieces(self):
        # Kings
        self.pieces['white'].append(Pieces.King(True, (4, 0)))
        self.pieces['black'].append(Pieces.King(False, (4, 7)))

        # Pawns
        for x in range(8):
            self.pieces['white'].append(Pieces.Pawn(True, (x, 1)))
            #self.pieces['black'].append(Pieces.Pawn(False, (x, 6)))

        # White Pieces
        # TODO: Move List
        # TODO: Win Screen
        # TODO: Resign and draw
        # TODO: Clock
        #self.pieces['white'].append(Pieces.Knight(True, (1, 0)))
        #self.pieces['white'].append(Pieces.Knight(True, (6, 0)))
        #self.pieces['white'].append(Pieces.Bishop(True, (2, 0)))
        self.pieces['white'].append(Pieces.Bishop(True, (5, 0)))
        self.pieces['white'].append(Pieces.Rook(True, (0, 0)))
        #self.pieces['white'].append(Pieces.Rook(True, (7, 0)))
        self.pieces['white'].append(Pieces.Queen(True, (3, 0)))

        # Black Pieces
        #self.pieces['black'].append(Pieces.Knight(False, (1, 7)))
        #self.pieces['black'].append(Pieces.Knight(False, (6, 7)))
        #self.pieces['black'].append(Pieces.Bishop(False, (2, 7)))
        self.pieces['black'].append(Pieces.Bishop(False, (5, 7)))
        #self.pieces['black'].append(Pieces.Rook(False, (0, 7)))
        self.pieces['black'].append(Pieces.Rook(False, (7, 7)))
        self.pieces['black'].append(Pieces.Queen(False, (3, 7)))

        # TODO: Move this to end of turn
        # Show pieces
        for x in self.pieces['white']:
            self.pieces_grid[x.pos] = x
            self.labels[x.pos[0]][x.pos[1]].config(text=' ' + x.display + ' ',
                                                   fg='silver')
        for x in self.pieces['black']:
            self.pieces_grid[x.pos] = x
            self.labels[x.pos[0]][x.pos[1]].config(text=' ' + x.display + ' ',
                                                   fg='brown')
Ejemplo n.º 11
0
 def fromNotation(m, colour):
     try:
         mm = [m[0:2], m[2:4]]  # e.g. e2e4
         fr = [0, 0]
         to = [0, 0]
         conv = dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7)
         fr[1] = conv[mm[0][0]]
         fr[0] = 7 - (int(mm[0][1]) - 1)
         to[1] = conv[mm[1][0]]
         to[0] = 7 - (int(mm[1][1]) - 1)
         # Promotion?
         promotionDict = dict(Q=Pieces.Queen(colour),
                              R=Pieces.Rook(colour),
                              N=Pieces.Knight(colour),
                              B=Pieces.Bishop(colour),
                              K=Pieces.King(colour))
         if len(m) > 4:
             return PromotionMove(fr, to, promotionDict[m[4]])
         else:
             return Move(fr, to)
     except Exception as e:
         print e
         raise
Ejemplo n.º 12
0
    def check(self, event):
        if event:
            opp = self.game.PM.get_next_player()
            pieces_opp = opp.get_pieces()
            user = self.game.PM.get_current_player()
            pieces_user = user.get_pieces()
            user_king = [p for p in pieces_user if p.get_piece_name() == Piece.King().get_piece_name() and p.get_color() == user.get_color()]
            king_location = self.game.get_piece_pos(*user_king)
            movable = []
            for p in pieces_opp:
                try:
                    for pat in p.patterns:
                        movable += pat.return_positions(p, self.game)
                except TypeError:
                    try:
                        movable += p.patterns.return_positions(p, self.game)
                    except AttributeError as e:
                        print(e)

            movable = sorted(list(set(map(tuple, movable))))
            print(movable)
            print(king_location)
            print(king_location in movable)
Ejemplo n.º 13
0
testCase2 = Logic.move_piece(testCase2, 2, 3, P.Move((2, 4), P.NON_CAPTURE))
testCase2 = Logic.move_piece(testCase2, 7, 5, P.Move((5, 5)))
testCase2 = Logic.move_piece(testCase2, 0, 4, P.Move((0, 5), P.NON_CAPTURE))
testCase2 = Logic.move_piece(testCase2, 5, 4, P.Move((5, 1)))

testCase3 = B.Board()
testCase3 = Logic.move_piece(testCase3, 4, 1, P.Move((4, 3), P.NON_CAPTURE))
testCase3 = Logic.move_piece(testCase3, 0, 6, P.Move((0, 4), P.NON_CAPTURE))
testCase3 = Logic.move_piece(testCase3, 3, 0, P.Move((5, 2)))
testCase3 = Logic.move_piece(testCase3, 1, 6, P.Move((1, 4), P.NON_CAPTURE))
testCase3 = Logic.move_piece(testCase3, 5, 0, P.Move((2, 3)))
testCase3 = Logic.move_piece(testCase3, 1, 4, P.Move((1, 3), P.NON_CAPTURE))
testCase3 = Logic.move_piece(testCase3, 5, 2, P.Move((5, 6)))

testCase4 = B.Board(True)
testCase4.board[0][0] = P.King(testCase4.whiteKingId, P.WHITE)
testCase4.board[1][0] = P.Rook(100, P.WHITE)
testCase4.board[2][0] = P.Rook(101, P.WHITE)
testCase4.board[2][1] = P.Queen(102, P.BLACK)
testCase4.board[7][1] = P.Pawn(103, P.WHITE)
testCase4.board[0][2] = P.Pawn(104, P.WHITE)
testCase4.board[1][2] = P.Knight(105, P.BLACK)
testCase4.board[2][3] = P.Knight(106, P.BLACK)
testCase4.board[4][5] = P.Pawn(107, P.BLACK)
testCase4.board[1][6] = P.Pawn(108, P.BLACK)
testCase4.board[6][6] = P.Pawn(109, P.BLACK)
testCase4.board[7][6] = P.Pawn(110, P.BLACK)
testCase4.board[2][7] = P.Bishop(111, P.BLACK)
testCase4.board[3][7] = P.Rook(112, P.BLACK)
testCase4.board[6][7] = P.King(testCase4.blackKingId, P.BLACK)
testCase4.unmoved = [103, 108, 109, 110, 111]
Ejemplo n.º 14
0
qCMoves = [
            P.Move((4,3)),
            P.Move((5,3)),
            P.Move((3,2)),
            P.Move((3,4)),
            P.Move((2,2)),
            P.Move((4,2)),
            P.Move((4,4)),
            P.Move((5,5))
          ]


"""------------KING TESTS----------"""
kingCenter = deepcopy(board)
kingCenter.board[3][3] = P.King(0, P.WHITE)
kingCenter.unmoved.remove(0)

kingCastle = deepcopy(board)
kingCastle.board[4][0] = P.King(0, P.WHITE)
kingCastle.board[0][0] = P.Rook(1, P.WHITE)
kingCastle.board[7][0] = P.Rook(2, P.WHITE)

kingCastleOpposed = deepcopy(kingCastle)
kingCastleOpposed.board[3][1] = P.Pawn(3, P.BLACK)
kingCastleOpposed.whiteChecked = True
kingCastleOpposed.unmoved.remove(3)

kingCastleOpposed2 = deepcopy(kingCastle)
kingCastleOpposed2.board[3][1] = P.Rook(3, P.BLACK)
kingCastleOpposed2.unmoved.remove(3)
Ejemplo n.º 15
0
import pytest
import Board as B
import Pieces as P
import Logic
from DecisionTree import aiSearch, endStateCheck, Node

stalemate = B.Board(True)

stalemate.board[3][3] = P.King(stalemate.whiteKingId, P.WHITE)
stalemate.board[0][4] = P.Rook(100, P.BLACK)
stalemate.board[2][0] = P.Queen(101, P.BLACK)
stalemate.board[5][1] = P.Knight(102, P.BLACK)
stalemate.board[7][2] = P.Rook(103, P.BLACK)
stalemate.board[7][7] = P.King(stalemate.blackKingId, P.BLACK)
stalemate.unmoved = []
stalemate.initializePosVals()

unknownError = B.Board()
unknownError = Logic.move_piece(unknownError, 4, 1,
                                P.Move((4, 3), P.NON_CAPTURE))
unknownError = Logic.move_piece(unknownError, 6, 6,
                                P.Move((6, 4), P.NON_CAPTURE))
unknownError = Logic.move_piece(unknownError, 3, 0, P.Move((5, 2)))
unknownError = Logic.move_piece(unknownError, 5, 7, P.Move((6, 6)))
unknownError = Logic.move_piece(unknownError, 5, 0, P.Move((2, 3)))
unknownError = Logic.move_piece(unknownError, 0, 6,
                                P.Move((0, 4), P.NON_CAPTURE))
unknownError = Logic.move_piece(unknownError, 5, 2, P.Move((5, 6)))
unknownError.initializePosVals()

crash = B.Board(True)
Ejemplo n.º 16
0
	def getValidMoves(self, board, piece, colour, enforceCaptures=True):
		moves = []
		fr = piece
                # First, we get a (strictly optimistic) list of plausible places this piece could move to,
                # ignoring line of sight and promotions.
                # This is faster than checking validity of all possible moves.
                destList = board.pieces[fr[0]*8+fr[1]].getPlausibleMoves(fr)
                for to in destList:
			m = Move.Move(fr, to)
			try:
				self.validate( m, board, colour, enforceCaptures )
				moves.append( m )
			except RulesViolation as e:
				pass
                # Now consider promotion moves
		promotionPieces = [Pieces.Queen(colour), Pieces.Rook(colour), Pieces.Knight(colour), Pieces.Bishop(colour), Pieces.King(colour)]
                if isinstance(board.pieces[fr[0]*8+fr[1]], Pieces.Pawn):
                        row = fr[0]
                        if colour==0 and row==1:
                                for col in [fr[1]-1, fr[1], fr[1]+1]:
                                        to = [0, col]
                                        for pp in promotionPieces:
				                m = Move.PromotionMove(fr, to, pp)
				                try:
				                	self.validate( m, board, colour, enforceCaptures )
				                	moves.append( m )
				                except RulesViolation as e:
				                	pass
                        elif colour==1 and row==6:
                                for col in [fr[1]-1, fr[1], fr[1]+1]:
                                        to = [7, col]
                                        for pp in promotionPieces:
				                m = Move.PromotionMove(fr, to, pp)
				                try:
				                	self.validate( m, board, colour, enforceCaptures )
				                	moves.append( m )
				                except RulesViolation as e:
				                	pass
		return moves
Ejemplo n.º 17
0
import Pieces as P

W_ROOKS = [P.Rook(0, P.WHITE), P.Rook(5, P.WHITE)]
W_KNIGHTS = [P.Knight(1, P.WHITE), P.Knight(6, P.WHITE)]
W_BISHOPS = [P.Bishop(2, P.WHITE), P.Bishop(7, P.WHITE)]
W_QUEEN = P.Queen(3, P.WHITE)
W_KING = P.King(4, P.WHITE)
W_PAWNS = []

B_ROOKS = [P.Rook(8, P.BLACK), P.Rook(13, P.BLACK)]
B_KNIGHTS = [P.Knight(9, P.BLACK), P.Knight(14, P.BLACK)]
B_BISHOPS = [P.Bishop(10, P.BLACK), P.Bishop(15, P.BLACK)]
B_QUEEN = P.Queen(11, P.BLACK)
B_KING = P.King(12, P.BLACK)
B_PAWNS = []

EMPTY = P.Empty()

for i in range(0, 8):
    W_PAWNS.append(P.Pawn(16 + i, P.WHITE))
    B_PAWNS.append(P.Pawn(24 + i, P.BLACK))


class Board():
    def __init__(self, blank=False):
        self.board = [
        ]  # The game's current state, represented as a 2d array of Piece and Empty objects
        self.unmoved = list(range(
            0, 32))  # The ids of all pieces that haven't moved
        self.whiteChecked = False  # True when white's king is in check
        self.blackChecked = False  # True when black's king is in check