-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wild.py
153 lines (138 loc) · 6.5 KB
/
Wild.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Universidade Federal de Pernambuco - UFPE
Projeto IF969 - ALGORITMOS E ESTRUTURAS DE DADOS
Curso: Sistemas de Informação
Ultima modificação: 11/12/2019
Autor: Alisson Diego Diniz D. Da Fonseca (adddf@cin.ufpe.br)
"""
import random
from Tabuleiro import *
class Wild:
def __init__(self, gameTree=None):
self.tabuleiro = Tabuleiro('Wild')
self.nomeJogo = 'Wild'
self.arvore = gameTree
self.ultimaJogada = (0, 0, 'X') # Ultima jogada serve para checar se o jogo encerrou no método fimDeJogo
def __str__(self):
string = ''
tabuleiro = self.tabuleiro.criarTabuleiro()
for linha in tabuleiro:
i = 0
for elemento in linha:
if elemento == '':
elemento = ' '
if i < 2:
string += elemento + '|'
else:
string += elemento
i += 1
string += '\n'
return string
def tutorial(self):
'''Mostra o tutorial do jogo Wild Tic-Tac-Toe'''
print('\n\n' + ' ' * 20 + ' Tutorial Wild Tic-Tac-Toe ')
print(
'\nOlá, para realizar suas jogadas siga as regras abaixo:\n\n1 - Primeiro você deve inserir um número de 1 a 9, que representam em ordem as posições do tabuleiro\n\n2 - Após inserir a posição, você deve digitar o símbolo que deseja jogar. "x" ou "o".')
print('\nExemplos: \n\nEntradas: 7 -> x')
self.tabuleiro.tabuleiro = '000000100'
print(self)
print('Entradas: 1 -> o')
self.tabuleiro.tabuleiro = '200000000'
print(self)
print('Entradas: 5 -> o')
self.tabuleiro.tabuleiro = '000020000'
print(self)
self.tabuleiro.tabuleiro = '000000000'
def posicoesDisponiveis(self):
'''Retorna uma lista com as posições disponíveis para jogar no formato de input para o usuário'''
jogadas = self.jogadasValidas()
posicoes = []
for jogada in jogadas:
if jogada[2] == 'X':
if jogada[0] == 0:
posicoes.append(str(jogada[1] + 1))
elif jogada[0] == 1:
posicoes.append(str(jogada[1] + 4))
elif jogada[0] == 2:
posicoes.append(str(jogada[1] + 7))
return posicoes
def jogadasValidas(self, blackList=[]):
'''Retorna todas as jogadas válidas possíveis no formato (linha, coluna, simbolo) excluindo as que estão no parâmetro blackList'''
tabuleiro = self.tabuleiro.criarTabuleiro()
jogadas = []
indiceL = 0
for linha in tabuleiro:
indiceC = 0
for coluna in linha:
if coluna == '':
for simbolo in ['X', 'O']:
if (indiceL, indiceC, simbolo) not in blackList:
jogadas.append((indiceL, indiceC, simbolo))
indiceC += 1
indiceL += 1
return jogadas
def jogar(self, jogada):
'''Faz a jogada e adiciona no grafo a aresta correspondente a esta jogada, somente se ainda não existir ligação entre elas'''
linha = jogada[0]
coluna = jogada[1]
simbolo = jogada[2]
anterior = self.tabuleiro.tabuleiro
i = linha * 3 + coluna
self.ultimaJogada = (int(linha), int(coluna), simbolo)
self.tabuleiro.tabuleiro = self.tabuleiro.alterarTabuleiro(i, simbolo)
if not self.arvore.existeLigacao(anterior, self.tabuleiro.tabuleiro):
self.arvore.add(anterior, self.tabuleiro.tabuleiro, 0)
def jogarAleatorio(self, blackList=[]):
'''Joga aleatoriamente, porém exclui as jogadas contidas na blacklist'''
jogadas = self.jogadasValidas(blackList)
pos = random.randint(0, len(jogadas) - 1)
self.jogar(jogadas[pos])
def fimDeJogo(self):
'''Checa se a a partida encerrou com a ultima jogada. Este é um método mais eficiente do que o tabuleiroFinalizado()'''
tabuleiro = self.tabuleiro.criarTabuleiro()
linha = self.ultimaJogada[0]
coluna = self.ultimaJogada[1]
simbolo = self.ultimaJogada[2]
if tabuleiro[linha][0] == simbolo and tabuleiro[linha][1] == simbolo and tabuleiro[linha][2] == simbolo:
return True
if tabuleiro[0][coluna] == simbolo and tabuleiro[1][coluna] == simbolo and tabuleiro[2][coluna] == simbolo:
return True
if (tabuleiro[0][0] == simbolo and tabuleiro[1][1] == simbolo and tabuleiro[2][2] == simbolo) or (
tabuleiro[2][0] == simbolo and tabuleiro[1][1] == simbolo and tabuleiro[0][2] == simbolo):
return True
return False
def tabuleiroFinalizado(self):
"""Checa se a partida encerrou em qualquer posicao do tabuleiro"""
tabuleiro = self.tabuleiro.criarTabuleiro()
for l in range(3):
if tabuleiro[l][0] == 'X' and tabuleiro[l][1] == 'X' and tabuleiro[l][2] == 'X':
return True
if tabuleiro[l][0] == 'O' and tabuleiro[l][1] == 'O' and tabuleiro[l][2] == 'O':
return True
for c in range(3):
if tabuleiro[0][c] == 'X' and tabuleiro[1][c] == 'X' and tabuleiro[2][c] == 'X':
return True
if tabuleiro[0][c] == 'O' and tabuleiro[1][c] == 'O' and tabuleiro[2][c] == 'O':
return True
if (tabuleiro[0][0] == 'O' and tabuleiro[1][1] == 'O' and tabuleiro[2][2] == 'O') or (
tabuleiro[2][0] == 'O' and tabuleiro[1][1] == 'O' and tabuleiro[0][2] == 'O'):
return True
if (tabuleiro[0][0] == 'X' and tabuleiro[1][1] == 'X' and tabuleiro[2][2] == 'X') or (
tabuleiro[2][0] == 'X' and tabuleiro[1][1] == 'X' and tabuleiro[0][2] == 'X'):
return True
return False
def buscarPosicao(self, proximo, anterior):
'''Recebe dois estados do tabuleiro, retorna a linha e a coluna que difere estes tabuleiros, ou seja, a jogada seguinte'''
i = 0
while i < 9:
if proximo[i] != anterior[i]:
simbolo = 'X' if proximo[i] == '1' else 'O'
iLinha = i
linha = 0
while iLinha > 2:
linha += 1
iLinha -= 3
coluna = iLinha
return (linha, coluna, simbolo)
i += 1
return None