Claro! Aqui está um exemplo de programa em Python que determina a próxima jogada em um Jogo da Velha, com base em uma matriz de 3x3 representando o tabuleiro: ```python def proxima_jogada(tabuleiro): # Verifica se é possível vencer na próxima jogada for i in range(3): if tabuleiro[i][0] == tabuleiro[i][1] == -1 and tabuleiro[i][2] == 0: return (i, 2) elif tabuleiro[i][0] == tabuleiro[i][2] == -1 and tabuleiro[i][1] == 0: return (i, 1) elif tabuleiro[i][1] == tabuleiro[i][2] == -1 and tabuleiro[i][0] == 0: return (i, 0) elif tabuleiro[0][i] == tabuleiro[1][i] == -1 and tabuleiro[2][i] == 0: return (2, i) elif tabuleiro[0][i] == tabuleiro[2][i] == -1 and tabuleiro[1][i] == 0: return (1, i) elif tabuleiro[1][i] == tabuleiro[2][i] == -1 and tabuleiro[0][i] == 0: return (0, i) elif tabuleiro[i][0] == tabuleiro[i][1] == 1 and tabuleiro[i][2] == 0: return (i, 2) elif tabuleiro[i][0] == tabuleiro[i][2] == 1 and tabuleiro[i][1] == 0: return (i, 1) elif tabuleiro[i][1] == tabuleiro[i][2] == 1 and tabuleiro[i][0] == 0: return (i, 0) elif tabuleiro[0][i] == tabuleiro[1][i] == 1 and tabuleiro[2][i] == 0: return (2, i) elif tabuleiro[0][i] == tabuleiro[2][i] == 1 and tabuleiro[1][i] == 0: return (1, i) elif tabuleiro[1][i] == tabuleiro[2][i] == 1 and tabuleiro[0][i] == 0: return (0, i) # Verifica se é possível bloquear o oponente de vencer na próxima jogada for i in range(3): if tabuleiro[i][0] == tabuleiro[i][1] == 1 and tabuleiro[i][2] == 0: return (i, 2) elif tabuleiro[i][0] == tabuleiro[i][2] == 1 and tabuleiro[i][1] == 0: return (i, 1) elif tabuleiro[i][1] == tabuleiro[i][2] == 1 and tabuleiro[i][0] == 0: return (i, 0) elif tabuleiro[0][i] == tabuleiro[1][i] == 1 and tabuleiro[2][i] == 0: return (2, i) elif tabuleiro[0][i] == tabuleiro[2][i] == 1 and tabuleiro[1][i] == 0: return (1, i) elif tabuleiro[1][i] == tabuleiro[2][i] == 1 and tabuleiro[0][i] == 0: return (0, i) # Caso não seja possível vencer ou bloquear, joga em uma posição vazia aleatória import random vazias = [] for i in range(3): for j in range(3): if tabuleiro[i][j] == 0: vazias.append((i, j)) return random.choice(vazias) # Exemplo de uso tabuleiro = [[-1, 1, 1], [-1, -1, 0], [0, 1, 0]] proxima_jogada(tabuleiro) ``` Esse programa verifica se é possível vencer na próxima jogada, bloquear o oponente de vencer ou, caso contrário, joga em uma posição vazia aleatória. No exemplo acima, a próxima jogada seria na posição (2, 2).
Para escrever sua resposta aqui, entre ou crie uma conta
Compartilhar