Example #1
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // 8

    player = 1
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

        start_time = time.time()

        while True:
            new_board = ai.next_move(board if player ==
                                     1 else board.inverted())
            if new_board is None:
                break
            else:
                board = new_board if player == 1 else new_board.inverted()
                if new_board.in_the_process_of_taking == False:
                    player = player % 2 + 1
                    break

        if time.time() - start_time > 30.0:
            print('TL')
            break

        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()
Example #2
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // 8

    for i, j in product(range(8), range(8)):
        board.board[i][j] = 0
    board.board[6][5] = 1
    board.board[6][3] = 1
    board.board[5][2] = 1
    board.board[4][3] = 1
    board.board[4][5] = 1
    board.board[3][0] = -2
    board.board[1][2] = -1
    board.board[1][4] = -1
    board.board[2][3] = -1

    player = 1
    while True:        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    while True:
                        new_board = ai.next_move(board if player == 1 else board.inverted())
                        if new_board is None:
                            break
                        else:
                            board = new_board if player == 1 else new_board.inverted()
                            if new_board.in_the_process_of_taking == False:
                                player = player % 2 + 1
                        break
                                    
        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()
Example #3
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // 8

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_click_position = event.pos

            if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                new_x, new_y = [p // grid_size for p in event.pos]
                old_x, old_y = [p // grid_size for p in mouse_click_position]
                new_board = board.do_move(old_x, old_y, new_x, new_y)                
                if new_board is not None:
                    board = new_board

            if event.type == pygame.MOUSEBUTTONUP and event.button == 3:
                x, y = [p // grid_size for p in event.pos]
                board.board[y, x] = (board.board[y, x] + 1 + 2) % 5 - 2  # change figure

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    board = board.inverted()

                if event.key == pygame.K_s:
                    board.save_board("checkers.txt")
                  
                if event.key == pygame.K_l:
                    board.open_saved_board("checkers.txt")

                if event.key == pygame.K_SPACE:
                    new_board = board.inverted()
                    while True:
                        new_board = ai.next_move(new_board)
                        if new_board is None or new_board.in_the_process_of_taking == False:
                            break
                    if new_board is not None:
                        board = new_board.inverted()
        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()
Example #4
0
def game_loop(screen: Surface, board: BoardState, ai: AI):
    grid_size = screen.get_size()[0] // 8  # размер одной клетки
    count = -1

    while True:
        # print(pygame.event.get())
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_click_position = event.pos
                # print(f'event.pos = {event.pos}')

            if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                new_x, new_y = [p // grid_size for p in event.pos]
                old_x, old_y = [p // grid_size for p in mouse_click_position]

                try:
                    new_board = board.do_move(old_x, old_y, new_x, new_y)
                except EndGame:
                    return

                if new_board is not None:
                    board = new_board

            if event.type == pygame.MOUSEBUTTONUP and event.button == 3:  # ????????????????????????
                x, y = [p // grid_size for p in event.pos]
                board.board[
                    y,
                    x] = (board.board[y, x] + 1 + 2) % 5 - 2  # change figure

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    board = board.inverted()

                elif event.key == pygame.K_SPACE:
                    new_board = ai.next_move(board)
                    if new_board is not None:
                        board = new_board

                elif event.key == pygame.K_s:
                    count += 1
                    count %= 5
                    name = f'game{count}'

                    # name = input_name()
                    db = shelve.open(name)
                    db[name] = board
                    db.close()
                    # todo

                elif event.key == pygame.K_l:
                    load = True

                elif event.key in (pygame.K_0, pygame.K_1, pygame.K_2,
                                   pygame.K_3, pygame.K_4) and load:
                    name = 'game' + (chr(event.key))
                    if os.path.exists(os.path.join(os.getcwd(), name + '.db')):
                        db = shelve.open(name)
                        board = db[name]
                        db.close()
                        load = False
                    else:
                        print('No such file:(')

        draw_board(screen, 0, 0, grid_size, board)
        pygame.display.flip()