Advertisement
MRtecno98

Tris Game

Jun 1st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. instructions = \
  2. """
  3. \nISTRUZIONI:
  4. Questo gioco è un semplice tris
  5. Per ogni turno il giocatore deve inserire un numero
  6. di slot che sarà quello in cui sarà inserita la lettera
  7. corrispondente al giocatore(X per il player 1 e O per il player 2)
  8. Gli slot sono strutturati secondo una numerazione crescente.
  9. Questa è una tabella con ogni slot nel suo posto:
  10.                    1|2|3
  11.                    4|5|6
  12.                    7|8|9
  13. Il gioco finisce quando un giocatore riesce ad allineare tre lettere uguali
  14. Spero che ti piaccia! ;)\n
  15. """ \
  16. [1:-1]
  17.  
  18. table = [["-","-","-"],
  19.          ["-","-","-"],
  20.          ["-","-","-"]]
  21.  
  22. playerSymbols = {1:"X" , 2:"O"}
  23.  
  24. def printTable(table) :
  25.     string = []
  26.     for line in table :
  27.         strLine = "|".join(line)
  28.         string.append(strLine)
  29.     string = "\n".join(string)
  30.     print(string)
  31.  
  32. while True :
  33.         inst = input("Hai mai giocato a questo gioco?(y/n): ").lower()
  34.         if inst == "n" :
  35.             print(instructions)
  36.             break
  37.         elif inst == "y" :
  38.             break
  39.         else :
  40.             continue
  41.  
  42. input("Premi ENTER per iniziare!")
  43. player = 1
  44. while True :
  45.     printTable(table)
  46.     symbol = playerSymbols[player]
  47.     print("Player %s(%s)" % (str(player), symbol))
  48.     while True :
  49.         slot = input("Inserisci slot: ")
  50.         if slot == "" :
  51.             continue
  52.         if not slot.isdecimal() :
  53.             print("Lo slot deve essere un numero")
  54.             continue
  55.         slot = int(slot)
  56.         if not 1 <= slot <= 9 :
  57.             print("Slot non valido")
  58.             continue
  59.         else :
  60.             break
  61.          
  62.     if 1 <= slot <= 3 :
  63.         if table[0][slot - 1] != "-" :
  64.             table[0][slot - 1] = symbol
  65.     elif 4 <= slot <= 6 :
  66.         table[1][(slot - 6) - 1] = symbol
  67.     elif 7 <= slot <= 9 :
  68.         table[2][(slot - 6) - 1] = symbol
  69.    
  70.     userXWin =   ((table[0] == ["X","X","X"]) or \
  71.                  (table[1] == ["X","X","X"]) or \
  72.                  (table[2] == ["X","X","X"]) or \
  73.                  ((table[0][0] == "X") and (table[1][1] == "X") and (table[2][2] == "X")) or \
  74.                  ((table[0][2] == "X") and (table[1][1] == "X") and (table[2][0] == "X")) or \
  75.                  ((table[0][1] == "X") and (table[1][1] == "X") and (table[2][1] == "X")) or \
  76.                  ((table[0][0] == "X") and (table[1][0] == "X") and (table[2][0] == "X")) or \
  77.                  ((table[0][2] == "X") and (table[1][2] == "X") and (table[2][2] == "X")))
  78.  
  79.     userOWin =   ((table[0] == ["O","O","O"]) or \
  80.                  (table[1] == ["O","O","O"]) or \
  81.                  (table[2] == ["O","O","O"]) or \
  82.                  ((table[0][0] == "O") and (table[1][1] == "O") and (table[2][2] == "O")) or \
  83.                  ((table[0][2] == "O") and (table[1][1] == "O") and (table[2][0] == "O")) or \
  84.                  ((table[0][1] == "O") and (table[1][1] == "O") and (table[2][1] == "O")) or \
  85.                  ((table[0][0] == "O") and (table[1][0] == "O") and (table[2][0] == "O")) or \
  86.                  ((table[0][2] == "O") and (table[1][2] == "O") and (table[2][2] == "O")))
  87.  
  88.     if userOWin == True :
  89.         printTable(table)
  90.         input("Il player 2 ha vinto!")
  91.         break
  92.     elif userXWin == True :
  93.         printTable(table)
  94.         input("Il Player 1 ha vinto!")
  95.         break
  96.  
  97.     if player == 1 :
  98.           player = 2
  99.     elif player == 2 :
  100.           player = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement