Esempio n. 1
0
 def take_action(self, board, piece):
     next_moves = board.find_next_moves(
         piece, ACTION.EAT) + board.find_next_moves(piece, ACTION.MOVE)
     # print(next_moves[0][0].prev)
     if (len(next_moves)):
         mv_state = board.move(next_moves[0].prev, next_moves[0].next)
         return next_moves[0] if mv_state != EXEC_STATE.FAIL else pair()
     return pair()
Esempio n. 2
0
 def play(self, board, turn):
     if turn == 1:
         m = convertToPoint(choice(firstBlackMove))
         board.move(m[0], m[1])
         r = ['y', ' ', 'x', ' ', 'y', ' ', 'x']
         r[0] = str(m[0].y)
         r[2] = str(m[0].x)
         r[4] = str(m[1].y)
         r[6] = str(m[1].x)
         r = ''.join(r)
         return r
     else:
         temp = minimaxMoveBlack(board, turn)
         r = compareBoard(board, temp, 0)
         board.chesses = temp.chesses
         board.activeChesses = temp.activeChesses
         return r
Esempio n. 3
0
pygame.display.update()
time.sleep(1)

# Pygame Main Loop
while not crashed:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        if board.player == 1:
            # Check for key events
            if event.type == pygame.KEYDOWN:
                if event.key in keyDict:
                    board.move(keyDict[event.key])

        # Prompt for minimax agent to play for player 2
        elif board.player == 2: # board.player has to be 2 then
            board.move(agent.minimax(board)[0])

        # Change the caption
        pygame.display.set_caption(f"Connect 4: {board.WIDTH} by {board.HEIGHT} Board") 

    # Draw horizontal gridlines
    for x in range(board.WIDTH + 1):
        pygame.draw.rect(display, (255, 255, 255),
                                            (0, x * board.BLOCKSIZE, (board.HEIGHT + 1) * board.BLOCKSIZE, 1))
    
    # Draw vertical gridlines
    for y in range(board.HEIGHT + 1):
Esempio n. 4
0
pygame.display.flip()
crashed = False

# Key dictionary
keyDict = {pygame.K_1:1, pygame.K_2:2, pygame.K_3:3, pygame.K_4:4, pygame.K_5:5, pygame.K_6:6, pygame.K_7:7}

# %% Pygame Main Loop
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    
        # Check for key events
        if event.type == pygame.KEYDOWN:
            if event.key in keyDict:
                board.move(keyDict[event.key])
                print(board.matrix)

    # Draw horizontal gridlines
    for x in range(board.WIDTH + 1):
        pygame.draw.rect(display, (255, 255, 255),
                                            (0, x * board.BLOCKSIZE, (board.HEIGHT + 1) * board.BLOCKSIZE, 1))
    
    # Draw vertical gridlines
    for y in range(board.HEIGHT + 1):
        pygame.draw.rect(display, (255, 255, 255),
                                            (y * board.BLOCKSIZE, 0, 1, (board.WIDTH + 1) * board.BLOCKSIZE))

    # Draw pieces
    for x in range(board.WIDTH):
        for y in range(board.HEIGHT):