ren811

Jogo da Velha

Jun 1st, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. class Tabuleiro:
  2.         def __init__(self):
  3.                 self.casas = [1,2,3,4,5,6,7,8,9]
  4.                
  5.         def winMsg(self,j):
  6.                 print("O jogo terminou! O jogador " + str(j+1) + " ganhou!")
  7.        
  8.         def mostraTabuleiro(self):
  9.                 print("|===|===|===|")
  10.                 print("| " + str(self.casas[0]) + " | " + str(self.casas[1]) + " | " + str(self.casas[2]) + " |")
  11.                 print("|===|===|===|")
  12.                 print("| " + str(self.casas[3]) + " | " + str(self.casas[4]) + " | " + str(self.casas[5]) + " |")
  13.                 print("|===|===|===|")
  14.                 print("| " + str(self.casas[6]) + " | " + str(self.casas[7]) + " | " + str(self.casas[8]) + " |")
  15.                 print("|===|===|===|")
  16.                
  17.         def marcaPos(self,pos,j):
  18.                 pos = int(pos)
  19.                 if (j == 0):
  20.                         marca = 'X'
  21.                 if (j == 1):
  22.                         marca = 'O'
  23.                 if pos < 1 or pos > 9:
  24.                         print("Posição inválida. Digite outra.")
  25.                         userPos = input()
  26.                         marcaPos(userPos)
  27.                 elif (self.casas[pos-1] == 'X') or (self.casas[pos-1] == 'O'):
  28.                         print("\nPosição já ocupada. Digite outra.")
  29.                         userPos = input()
  30.                         marcaPos(userPos)
  31.                 elif (self.casas[pos-1] != 'X') and (self.casas[pos-1] != "O"):
  32.                         self.casas[pos-1] = marca
  33.        
  34.         def testa(self,j):
  35.                 for x in range(2):
  36.                         if self.casas[x] == self.casas[x+3]:
  37.                                 if self.casas[x+3] == self.casas[x+6]:
  38.                                         winMsg(j)
  39.                                         return False
  40.                 if (self.casas[0] == self.casas[1]) and (self.casas[2] == self.casas[1]):
  41.                         self.winMsg(j)
  42.                         return False
  43.                 if (self.casas[3] == self.casas[4]) and (self.casas[4] == self.casas[3]):
  44.                         winMsg(j)
  45.                         return False
  46.                 if (self.casas[6] == self.casas[7]) and (self.casas[7] == self.casas[8]):
  47.                         winMsg(j)
  48.                         return False
  49.                        
  50.                 if (self.casas[0] == self.casas[4]) and (self.casas[4] == self.casas[8]):
  51.                         winMsg(j)
  52.                         return False
  53.                 if (self.casas[2] == self.casas[4]) and (self.casas[4] == self.casas[6]):
  54.                         winMsg(j)
  55.                         return False
  56.                 else:
  57.                     return True
  58.                        
  59. def trocaJogador(i):
  60.         if (i == 0):
  61.                 return 1
  62.         if (i == 1):
  63.                 return 0
  64.  
  65. gameOn = True
  66. jAtual = 1
  67. T1 = Tabuleiro()
  68. while (gameOn == True):
  69.         jAtual = trocaJogador(jAtual)
  70.         T1.mostraTabuleiro()
  71.         print("Jogador " + str(jAtual+1) + "] Digite a posição que você deseja marcar: ")
  72.         pos = input()
  73.         T1.marcaPos(pos,jAtual)
  74.         T1.mostraTabuleiro()
  75.         gameOn = T1.testa(jAtual)
  76.         print(gameOn)
Advertisement
Add Comment
Please, Sign In to add comment