Example #1
0
def main():
    """
    Main method
    """
    print("Creating a new game...")

    new_game = Game(Human(color.white), Human(color.black))
    result = new_game.play()

    print("Result is ", result)
Example #2
0
def test_points_queen_escape():
    g = Game()
    g.clear()
    g.add_piece(Pawn("BLACK"), ("C", 3))
    g.add_piece(Queen("WHITE"), ("B", 2))
    assert (len(g.board.pieces) == 2)
    g.update_all_pieces()
    player = BestNextPointsPlayer("WHITE")
    points = player.potential_points_for_move(g, "WHITE", (("B", 2), ("B", 3)))
    assert (points == 9)
Example #3
0
def test_points_rook_threaten():
    g = Game()
    g.clear()
    g.add_piece(Pawn("WHITE"), ("A", 3))
    g.add_piece(Rook("BLACK"), ("B", 8))
    assert (len(g.board.pieces) == 2)
    g.next_to_play = "BLACK"
    g.update_all_pieces()
    player = BestNextPointsPlayer("BLACK")
    points = player.potential_points_for_move(g, "BLACK", (("B", 8), ("B", 4)))
    assert (points == -5)
Example #4
0
def test_player_legal_move():
    g = Game()
    pw = Player("WHITE", is_AI=False)
    moved = pw.move(g, ("A", 2), ("A", 3))
    assert (moved)
    assert (g.board.is_empty(("A", 2)))
    assert (g.board.piece_at(("A", 3)).piece_type == "Pawn")
    assert (g.board.piece_at(("A", 3)).colour == "WHITE")
    assert (g.next_to_play == "BLACK")
Example #5
0
def test_player_illegal_move():
    g = Game()
    pw = Player("WHITE", is_AI=False)
    moved = pw.move(g, ("A", 1), ("F", 3))
    assert (not moved)
    assert (g.board.is_empty(("F", 3)))
    assert (g.board.piece_at(("A", 1)).colour == "WHITE")
    assert (g.board.piece_at(("A", 1)).piece_type == "Rook")
    assert (g.next_to_play == "WHITE")
Example #6
0
def startGame(game: Game):
    playerMoveWhite = True
    while (True):
        if playerMoveWhite:
            print(game.board)
        if not playerMoveWhite:
            engine = Engine()
            startTime = time.time()
            move = engine.selectmove(game.board, False)
            if move is None:
                print("Black resigns")
                break
            game.board.movePiece(move.startPosition, move.endPosition,
                                 move.piece)
            print("Black moves", move, "taking",
                  time.time() - startTime, "seconds")
        else:
            userMove = input()
            if userMove == "Resign" or userMove == "resign":
                break
            try:
                start, end = userMove.split()
                start = parsePositon(start)
                end = parsePositon(end)
                piece = game.board.getPiece(*start)
            except:
                print("Invalid command")
                continue
            if piece is None:
                print("No piece exists there")
                continue
            if piece.Color != WHITE:
                print("That piece is not yours")
                continue
            if not piece.isValid(start, end, piece, game.board):
                print("Invalid move")
                continue
            game.board.movePiece(start, end, piece)

        gs = game.gameStatus(playerMoveWhite)
        if not Engine().kingOnBoard(game.board,
                                    BLACK if playerMoveWhite else WHITE):
            print(game.board)
            print("King captured, ", "WHITE" if playerMoveWhite else "BLACK",
                  "wins")
            break
        if gs == GAME:
            playerMoveWhite = not playerMoveWhite
            print()
        elif gs == CHECKMATE:
            print("CHECKMATE", "WHITE" if playerMoveWhite else "BLACK", "wins")
            break
        else:
            print("STALEMATE game draw")
            break
Example #7
0

def endGame():
    font = pygame.font.Font('freesansbold.ttf', 32)
    text = font.render(f'{game.winner} win!!!!!', True, (255, 255, 255),
                       (0, 0, 0))
    textRect = text.get_rect()
    textRect.center = (600 // 2, 600 // 2)
    screen.blit(text, textRect)


if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption('Chess')
    game = Game.Game()
    game.setUp()
    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                # if the game is done: do not allow clicking
                if game.status:
                    screen_pos = pygame.mouse.get_pos()
                    game.select(screen_pos)
        screen.fill((0, 0, 0))
        game.board.draw(screen)
        game.board.drawSquare(screen, pygame.mouse.get_pos())
Example #8
0
def test_castling_queen_side():
    g = Game()
    g.move(("B", 1), ("A", 3))
    g.move(("B", 8), ("A", 6))
    g.move(("D", 2), ("D", 3))
    g.move(("D", 7), ("D", 6))
    g.move(("C", 2), ("C", 3))
    g.move(("C", 7), ("C", 6))
    g.move(("C", 1), ("D", 2))
    g.move(("C", 8), ("D", 7))
    g.move(("D", 1), ("C", 2))
    g.move(("D", 8), ("C", 7))
    g.update_all_pieces()
    assert (g.is_legal_move("WHITE", ("E", 1), ("C", 1)))
    g.move(("E", 1), ("C", 1))
    assert (not g.board.is_empty(("C", 1)))
    assert (g.board.piece_at(("C", 1)).piece_type == "King")
    assert (not g.board.is_empty(("D", 1)))
    assert (g.board.piece_at(("D", 1)).piece_type == "Rook")
    ## Now have black do the same
    assert (g.is_legal_move("BLACK", ("E", 8), ("C", 8)))
    g.move(("E", 8), ("C", 8))
    assert (not g.board.is_empty(("C", 8)))
    assert (g.board.piece_at(("C", 8)).piece_type == "King")
    assert (not g.board.is_empty(("D", 8)))
    assert (g.board.piece_at(("D", 8)).piece_type == "Rook")
Example #9
0
def test_cant_castle_after_moving():
    g = Game()
    g.move(("G", 1), ("H", 3))
    g.move(("G", 8), ("H", 6))
    g.move(("E", 2), ("D", 3))
    g.move(("E", 7), ("D", 6))
    g.move(("F", 1), ("E", 2))
    g.move(("F", 8), ("E", 7))
    ## move white king
    g.move(("E", 1), ("F", 1))
    ## and black rook
    g.move(("H", 8), ("G", 8))
    ## and move both back again
    g.move(("F", 1), ("E", 1))
    g.move(("G", 8), ("H", 8))
    g.update_all_pieces()
    assert (not g.is_legal_move("WHITE", ("E", 1), ("G", 1)))
    ## Now have black do the same
    assert (not g.is_legal_move("BLACK", ("E", 8), ("G", 8)))
Example #10
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"))
Example #11
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"))
Example #12
0
def test_check():
    g = Game()
    g.move(("A", 2), ("D", 7))
    assert (g.is_check("BLACK"))
Example #13
0
def test_take():
    g = Game()
    g.move(("A", 2), ("A", 7))
    assert (len(g.board.pieces) == 31)
    assert (g.board.piece_at(("A", 7)).colour == "WHITE")
Example #14
0
def test_black_pawn_move():
    g = Game()
    assert (not g.is_legal_move("BLACK", ("A", 7), ("A", 5)))
Example #15
0
def test_white_pawn_move():
    g = Game()
    g.move(("A", 2), ("A", 4))
    assert (g.board.is_empty(("A", 2)))
    assert (g.board.piece_at(("A", 4)).has_moved == True)
    assert (g.next_to_play == "BLACK")
Example #16
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")
Example #17
0
def main():
    game = Game()
    startGame(game)
Example #18
0
def test_initial_setup():
    g = Game()
    assert (len(g.board.pieces) == 32)
    for p in g.board.pieces:
        assert (p.has_moved == False)