Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tabuleiro:
- def __init__(self):
- self.casas = [1,2,3,4,5,6,7,8,9]
- def winMsg(self,j):
- print("O jogo terminou! O jogador " + str(j+1) + " ganhou!")
- def mostraTabuleiro(self):
- print("|===|===|===|")
- print("| " + str(self.casas[0]) + " | " + str(self.casas[1]) + " | " + str(self.casas[2]) + " |")
- print("|===|===|===|")
- print("| " + str(self.casas[3]) + " | " + str(self.casas[4]) + " | " + str(self.casas[5]) + " |")
- print("|===|===|===|")
- print("| " + str(self.casas[6]) + " | " + str(self.casas[7]) + " | " + str(self.casas[8]) + " |")
- print("|===|===|===|")
- def marcaPos(self,pos,j):
- pos = int(pos)
- if (j == 0):
- marca = 'X'
- if (j == 1):
- marca = 'O'
- if pos < 1 or pos > 9:
- print("Posição inválida. Digite outra.")
- userPos = input()
- marcaPos(userPos)
- elif (self.casas[pos-1] == 'X') or (self.casas[pos-1] == 'O'):
- print("\nPosição já ocupada. Digite outra.")
- userPos = input()
- marcaPos(userPos)
- elif (self.casas[pos-1] != 'X') and (self.casas[pos-1] != "O"):
- self.casas[pos-1] = marca
- def testa(self,j):
- for x in range(2):
- if self.casas[x] == self.casas[x+3]:
- if self.casas[x+3] == self.casas[x+6]:
- winMsg(j)
- return False
- if (self.casas[0] == self.casas[1]) and (self.casas[2] == self.casas[1]):
- self.winMsg(j)
- return False
- if (self.casas[3] == self.casas[4]) and (self.casas[4] == self.casas[3]):
- winMsg(j)
- return False
- if (self.casas[6] == self.casas[7]) and (self.casas[7] == self.casas[8]):
- winMsg(j)
- return False
- if (self.casas[0] == self.casas[4]) and (self.casas[4] == self.casas[8]):
- winMsg(j)
- return False
- if (self.casas[2] == self.casas[4]) and (self.casas[4] == self.casas[6]):
- winMsg(j)
- return False
- else:
- return True
- def trocaJogador(i):
- if (i == 0):
- return 1
- if (i == 1):
- return 0
- gameOn = True
- jAtual = 1
- T1 = Tabuleiro()
- while (gameOn == True):
- jAtual = trocaJogador(jAtual)
- T1.mostraTabuleiro()
- print("Jogador " + str(jAtual+1) + "] Digite a posição que você deseja marcar: ")
- pos = input()
- T1.marcaPos(pos,jAtual)
- T1.mostraTabuleiro()
- gameOn = T1.testa(jAtual)
- print(gameOn)
Advertisement
Add Comment
Please, Sign In to add comment