Guest User

Untitled

a guest
May 8th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. initialState = input("Enter cells: ")
  2. # initialState = "_XXOO_OX_"
  3.  
  4. #Setting everything up for the first time
  5. Matrix = [[0 for x in range(3)] for y in range(3)]
  6. for i in range(3):
  7.     for j in range(3):
  8.         if (initialState[i * 3 + j] == "_"):
  9.             Matrix[i][j] = " "
  10.         else:
  11.             Matrix[i][j] = initialState[i * 3 + j]
  12.  
  13. # Function to draw onto the screen
  14. def draw():
  15.     print("---------")
  16.     for i in range(3):
  17.         for j in range(3):
  18.             if j == 0:
  19.                 print("|", end=" ")
  20.  
  21.             print(Matrix[i][j], end=" ")
  22.  
  23.             if j == 2:
  24.                 print("|")
  25.     print("---------")
  26.  
  27. #Function which keeps asking the user for input until its legal
  28. def ask():
  29.     incorrect = True
  30.     while (incorrect):
  31.  
  32.         inputNumbers = input("Enter cells: ").split(" ")
  33.         try:
  34.             if (len(inputNumbers)==2):
  35.                 row,col = int(inputNumbers[0]),int(inputNumbers[1])
  36.             elif (len(inputNumbers)!=2):
  37.                 print("Only input 2 Numbers")
  38.  
  39.             elif(row>3 or col>3 or row== 0 or col == 0):
  40.                 print("Coordinates should be from 1 to 3!")
  41.             elif Matrix[row-1][col-1] == "X" or Matrix[row-1][col-1] == "O":
  42.                 print("This cell is occupied! Choose another one!")
  43.  
  44.             else:
  45.                 incorrect = False
  46.         except:
  47.             print("You should enter numbers!")
  48.  
  49.     return row-1,col-1
  50.  
  51. #function which defines the state of the game at the moment
  52. def state():
  53.  
  54.     Won = False
  55.     Player = ""
  56.     for i in range(3):
  57.         if(Matrix[i][0] == Matrix[i][1] == Matrix[i][2]):
  58.             Won = True
  59.             Player = Matrix[i][0]
  60.         elif(Matrix[0][i] == Matrix[1][i] == Matrix[2][i]):
  61.             Won = True
  62.             Player = Matrix[0][i]
  63.         elif(Matrix[0][0] == Matrix[1][1] == Matrix[2][2]):
  64.             Won = True
  65.             Player = Matrix[1][1]
  66.         elif(Matrix[0][2] == Matrix[1][1] == Matrix[2][0]):
  67.             Won = True
  68.             Player = Matrix[1][1]
  69.  
  70.     if(Won):
  71.         print(Player + " Wins!")
  72.         return False
  73.     else:
  74.         Complete = True
  75.         for i in range(3):
  76.             for j in range(3):
  77.                 if Matrix[i][j] == " ":
  78.                     Complete = False
  79.         if(Complete):
  80.             print("Draw!")
  81.             return False
  82.         else:
  83.             print("Game not finished!")
  84.             return True
  85.  
  86.  
  87. # Initi Setup
  88. Playing = True
  89. draw()
  90. #Number is used to alternate between X and O by incrementing and using Mod
  91. number = 0
  92. while (Playing):
  93.     row,col = ask()
  94.     if(number%2==0):
  95.         Matrix[row][col] = "X"
  96.     else:
  97.         Matrix[row][col] = "O"
  98.     number+=1
  99.     draw()
  100.  
  101.     Playing = state()
Add Comment
Please, Sign In to add comment