Ejemplo n.º 1
0
 def test_valid_moves_clean_board(self):
     board = Board()
     board.clean()
     king = King(board, Square(4, 4), BLACK)
     expected = [
         Square(3, 3),
         Square(3, 4),
         Square(3, 5),
         Square(4, 3),
         Square(4, 5),
         Square(5, 3),
         Square(5, 4),
         Square(5, 5)
     ]
     result = king.valid_moves()
     self.assertEqual(expected, result)
Ejemplo n.º 2
0
 def test_valid_moves_with_other_pieces(self):
     board = Board()
     board.clean()
     board.matrix[1][1] = 'P'
     board.matrix[1][2] = 'P'
     board.matrix[2][3] = 'b'
     king = King(board, Square(2, 2), WHITE)
     expected = [
         Square(1, 3),
         Square(2, 1),
         Square(3, 1),
         Square(3, 2),
         Square(3, 3),
         Square(1, 1),
         Square(1, 2)
     ]
     result = king.valid_moves()
     self.assertEqual(expected, result)
Ejemplo n.º 3
0
 def create_new_piece(piece, position):
     if piece.get_piece_type() == Type.PAWN:
         from pawn import Pawn
         return Pawn(position, piece.get_piece_alliance())
     elif piece.get_piece_type() == Type.QUEEN:
         from queen import Queen
         return Queen(position, piece.get_piece_alliance())
     elif piece.get_piece_type() == Type.BISHOP:
         from bishop import Bishop
         return Bishop(position, piece.get_piece_alliance())
     elif piece.get_piece_type() == Type.KNIGHT:
         from knight import Knight
         return Knight(position, piece.get_piece_alliance())
     elif piece.get_piece_type() == Type.ROOK:
         from rook import Rook
         return Rook(position, piece.get_piece_alliance())
     elif piece.get_piece_type() == Type.KING:
         from king import King
         return King(position, piece.get_piece_alliance())
Ejemplo n.º 4
0
    def new_piece(self, char="", pos=(0, 0), side=""):
        # Returns a new instance of a pieces class based on the string given to this method

        if char.lower() == 'b':
            return Bishop(char=char, pos=pos, side=side)

        if char.lower() == 'k':
            return King(char=char, pos=pos, side=side)

        if char.lower() == 'n':
            return Knight(char=char, pos=pos, side=side)

        if char.lower() == 'p':
            return Pawn(char=char, pos=pos, side=side)

        if char.lower() == 'q':
            return Queen(char=char, pos=pos, side=side)

        if char.lower() == 'r':
            return Rook(char=char, pos=pos, side=side)

        return Piece(char=char, pos=pos, side=side)
Ejemplo n.º 5
0
 def king(self, side, coord):
     return King(side, coord)
Ejemplo n.º 6
0
b.place(Pawn('f7', 'Black'))
b.place(Pawn('g7', 'Black'))
b.place(Pawn('h7', 'Black'))
b.place(Rook('a1', 'White'))
b.place(Rook('h1', 'White'))
b.place(Rook('a8', 'Black'))
b.place(Rook('h8', 'Black'))
b.place(Knight('b1', 'White'))
b.place(Knight('g1', 'White'))
b.place(Knight('b8', 'Black'))
b.place(Knight('g8', 'Black'))
b.place(Bishop('c1', 'White'))
b.place(Bishop('f1', 'White'))
b.place(Bishop('c8', 'Black'))
b.place(Bishop('f8', 'Black'))
b.place(King('e1', 'White'))
b.place(Queen('d1', 'White'))
b.place(Queen('d8', 'Black'))
b.place(King('e8', 'Black'))

b.disp()
print("\n")

cnt = 0

while True:
    pos = input()
    pos = pos.split(" ")
    initial = pos[0]
    final = pos[1]
Ejemplo n.º 7
0
 def test_valid_moves_initial_position(self):
     king = King(Board(), Square(7, 4), WHITE)
     expected = []
     result = king.valid_moves()
     self.assertEqual(expected, result)
Ejemplo n.º 8
0
 def test_calls_super_in_init(self, init):
     king = King(Board(), Square(4, 2), WHITE)
     assert init.called
Ejemplo n.º 9
0
from piece import Colour
from pawn import Pawn
from king import King
from queen import Queen
from bishop import Bishop
from knight import Knight
from rook import Rook

STARTING_BOARD = (Pawn(Colour.WHITE, (0, 1)), Pawn(Colour.WHITE, (1, 1)),
                  Pawn(Colour.WHITE, (2, 1)), Pawn(Colour.WHITE, (3, 1)),
                  Pawn(Colour.WHITE, (4, 1)), Pawn(Colour.WHITE, (5, 1)),
                  Pawn(Colour.WHITE, (6, 1)), Pawn(Colour.WHITE, (7, 1)),
                  Rook(Colour.WHITE, (0, 0)), Rook(Colour.WHITE, (7, 0)),
                  Knight(Colour.WHITE, (1, 0)), Knight(Colour.WHITE, (6, 0)),
                  Bishop(Colour.WHITE, (2, 0)), Bishop(Colour.WHITE, (5, 0)),
                  Queen(Colour.WHITE, (3, 0)), King(Colour.WHITE, (4, 0)),
                  Pawn(Colour.BLACK, (0, 6)), Pawn(Colour.BLACK, (1, 6)),
                  Pawn(Colour.BLACK, (2, 6)), Pawn(Colour.BLACK, (3, 6)),
                  Pawn(Colour.BLACK, (4, 6)), Pawn(Colour.BLACK, (5, 6)),
                  Pawn(Colour.BLACK, (6, 6)), Pawn(Colour.BLACK, (7, 6)),
                  Rook(Colour.BLACK, (0, 7)), Rook(Colour.BLACK, (7, 7)),
                  Knight(Colour.BLACK, (1, 7)), Knight(Colour.BLACK, (6, 7)),
                  Bishop(Colour.BLACK, (2, 7)), Bishop(Colour.BLACK, (5, 7)),
                  Queen(Colour.BLACK, (3, 7)), King(Colour.BLACK, (4, 7)))


class GameTree(object):
    def __init__(self, pieces, colour):
        self.pieces = list(pieces)
        self.colour = colour
        self.children = []
Ejemplo n.º 10
0
for img in gif_images:
    screen.register_shape("../Pieces/" + img + ".gif")

mychessboard = Board()
screen.tracer(100)
mychessboard.drawBoard()
mychessboard.putWhitePawns(white_pawns)
mychessboard.putBlackPawns(black_pawns)

white_rook_king = Rook(mychessboard.chess_coord["h1"], True)
white_bishop_king = Bishop(mychessboard.chess_coord["f1"], True)
white_knight_king = Knight(mychessboard.chess_coord["g1"], True)
white_rook_queen = Rook(mychessboard.chess_coord["a1"], True)
white_bishop_queen = Bishop(mychessboard.chess_coord["c1"], True)
white_knight_queen = Knight(mychessboard.chess_coord["b1"], True)
white_king = King(mychessboard.chess_coord["e1"], True)
white_queen = Queen(mychessboard.chess_coord["d1"], True)

black_rook_king = Rook(mychessboard.chess_coord["h8"], False)
black_bishop_king = Bishop(mychessboard.chess_coord["f8"], False)
black_knight_king = Knight(mychessboard.chess_coord["g8"], False)
black_rook_queen = Rook(mychessboard.chess_coord["a8"], False)
black_bishop_queen = Bishop(mychessboard.chess_coord["c8"], False)
black_knight_queen = Knight(mychessboard.chess_coord["b8"], False)
black_king = King(mychessboard.chess_coord["e8"], False)
black_queen = Queen(mychessboard.chess_coord["d8"], False)

mychessboard.putwhitePieces(rookq=white_rook_queen,
                            knightq=white_knight_queen,
                            bishopq=white_bishop_queen,
                            queen=white_queen,
Ejemplo n.º 11
0
    def __init__(self, color, boardRoot):

        if color not in ["white", "black"]:
            raise Exception("invalid color")
        self.color = color

        self.root = boardRoot

        board = self.root["board"].getBoard()  #board[","]

        if self.color == 'white':

            self.root['pieces'].append(
                King(location=board["H,1"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Queen(location=board["G,0"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Bishop(board["H,2"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Bishop(board["F,0"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Knight(board["H,3"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Knight(board["E,0"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Rook(board["H,4"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Rook(board["D,0"], root=self.root, color=self.color))

            self.root['pieces'].append(
                Pawn(board["H,0"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["F,1"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["G,2"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["E,3"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["F,2"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["G,3"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["G,1"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["E,1"], root=self.root, color=self.color))

        else:
            self.root['pieces'].append(
                King(location=board["B,7"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Queen(location=board["A,6"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Bishop(board["A,5"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Bishop(board["C,7"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Knight(board["A,4"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Knight(board["D,7"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Rook(board["A,3"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Rook(board["E,7"], root=self.root, color=self.color))

            self.root['pieces'].append(
                Pawn(board["A,7"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["C,6"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["B,5"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["B,4"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["C,5"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["D,4"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["B,6"], root=self.root, color=self.color))
            self.root['pieces'].append(
                Pawn(board["D,6"], root=self.root, color=self.color))
Ejemplo n.º 12
0
 def test_king_attacks(self):
     king1 = King(Colour.WHITE, (4, 4))
     pawn1 = Pawn(Colour.BLACK, (4, 5))
     pawn2 = Pawn(Colour.BLACK, (4, 6))
     self.assertEquals(set(king1.list_attacks([pawn1, pawn2])),
                       set([(4, 5)]))
Ejemplo n.º 13
0
B = Board()
for i in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']:
    B.place(Pawn(i + '2', 'Black'))
    B.place(Pawn(i + '7', 'White'))
for i in ['a', 'h']:
    B.place(Rook(i + '1', 'Black'))
    B.place(Rook(i + '8', 'White'))
for i in ['b', 'g']:
    B.place(Knight(i + '1', 'Black'))
    B.place(Knight(i + '8', 'White'))
for i in ['c', 'f']:
    B.place(Bishop(i + '1', 'Black'))
    B.place(Bishop(i + '8', 'White'))
for i in ['d']:
    B.place(King(i + '1', 'Black'))
    B.place(King(i + '8', 'White'))
for i in ['e']:
    B.place(Queen(i + '1', 'Black'))
    B.place(Queen(i + '8', 'White'))

turn = True
print("\nWelcome To Ch-ISS\n\n \t\t This is a game made in python!\n\n\n")
P1 = input('Enter Name of Player 1(bottom): ')
P2 = input('Enter Name of Player 2(top): ')
board = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

os.system('cls' if os.name == 'nt' else 'clear')
B.disp()

while True:
Ejemplo n.º 14
0
    r"C:\Users\\coomb\Documents\Python Problems\Chess\board.png")
image = pygame.transform.scale(image, (400, 400))
rect = image.get_rect()

board = [[None for i in range(8)] for j in range(8)]

for i in range(len(board[6])):
    p = Pawn((6, i), "black", "P")
    board[6][i] = p

# White pieces

board[0][0] = Rook((0, 0), "white", "R")
board[0][1] = Knight((0, 1), "white", "N")
board[0][2] = Bishop((0, 2), "white", "B")
board[0][3] = King((0, 3), "white", "K")
board[0][4] = Queen((0, 4), "white", "Q")
board[0][5] = Bishop((0, 5), "white", "B")
board[0][6] = Knight((0, 6), "white", "N")
board[0][7] = Rook((0, 7), "white", "R")

for i in range(len(board[1])):
    p = Pawn((1, i), "white", "P")
    board[1][i] = p

# Black pieces

board[7][0] = Rook((7, 0), "black", "R")
board[7][1] = Knight((7, 1), "black", "N")
board[7][2] = Bishop((7, 2), "black", "B")
board[7][3] = King((7, 3), "black", "K")