Exemple #1
0
def main():
    run = True
    clock = pygame.time.Clock()
    game = Game(WIN)

    while run:
        clock.tick(FPS)

        if game.stalemate():
            print('Stalemate!\nWhite - 1/2 | Black - 1/2\nGame over.')
            run = False
        elif game.checkmate():
            winner = 'White'
            if game.turn == WHITE:
                winner = 'Black'
            print(f'Checkmate!\n{winner} wins!\nGame over.')
            run = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                row, col = get_mouse_row_col(pos)
                game.select(row, col)

        game.update()

    pygame.quit()
Exemple #2
0
def main():
    run = True
    clock = pygame.time.Clock()
    game = Game(WIN)
    DEPTH = 3

    while run:
        clock.tick(FPS)
        if game.turn == BLACK:
            start = time.time()
            value, new_board = minimax(game.board.get_board(), DEPTH, True, game)
            game.ai_move(new_board)
            print("TIME: ", time.time() - start)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                row, col = get_row_col_from_mouse(pos)

                if col <= 7:
                    game.select((8 * row + col))

            keys = pygame.key.get_pressed()
            if keys[pygame.K_r]:
                game.reset()

        game.update()
    pygame.quit()
Exemple #3
0
def main():
    game = Game(WIN)
    running = True
    clock = pygame.time.Clock()

    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                row, col = get_row_col_from_mouse(pos)
                game.select(row, col)

        game.update()

    pygame.quit()
Exemple #4
0
class GameFlow:
    def __init__(self, skill_level):
        self.window = pygame.display.set_mode(
            (WIDTH + 2 * BOARD_EDGE, HEIGHT + 2 * BOARD_EDGE))
        pygame.display.set_caption("Chess")
        self.clock = pygame.time.Clock()
        self.game = Game(self.window, skill_level)
        self.game_over = False

    def update_display(self):
        self.game.update()

    def make_player_move(self, position):
        self.game.select(position)

    def make_engine_move(self):
        self.game.make_engine_move()

    def play(self):
        while not self.game_over:
            self.clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.game_over = True
                if self.game.turn == WHITE:
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        pos = pygame.mouse.get_pos()
                        self.game.select(pos)
                else:
                    self.game.make_engine_move()
            self.update_display()
            result = self.game.get_winner()
            if result:
                print(result)
                self.game.board.print_board_to_console()
                self.game.reset()
                # self.game_over = True
            pygame.display.flip()

        pygame.quit()