Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initialState = input("Enter cells: ")
- # initialState = "_XXOO_OX_"
- #Setting everything up for the first time
- Matrix = [[0 for x in range(3)] for y in range(3)]
- for i in range(3):
- for j in range(3):
- if (initialState[i * 3 + j] == "_"):
- Matrix[i][j] = " "
- else:
- Matrix[i][j] = initialState[i * 3 + j]
- # Function to draw onto the screen
- def draw():
- print("---------")
- for i in range(3):
- for j in range(3):
- if j == 0:
- print("|", end=" ")
- print(Matrix[i][j], end=" ")
- if j == 2:
- print("|")
- print("---------")
- #Function which keeps asking the user for input until its legal
- def ask():
- incorrect = True
- while (incorrect):
- inputNumbers = input("Enter cells: ").split(" ")
- try:
- if (len(inputNumbers)==2):
- row,col = int(inputNumbers[0]),int(inputNumbers[1])
- elif (len(inputNumbers)!=2):
- print("Only input 2 Numbers")
- elif(row>3 or col>3 or row== 0 or col == 0):
- print("Coordinates should be from 1 to 3!")
- elif Matrix[row-1][col-1] == "X" or Matrix[row-1][col-1] == "O":
- print("This cell is occupied! Choose another one!")
- else:
- incorrect = False
- except:
- print("You should enter numbers!")
- return row-1,col-1
- #function which defines the state of the game at the moment
- def state():
- Won = False
- Player = ""
- for i in range(3):
- if(Matrix[i][0] == Matrix[i][1] == Matrix[i][2]):
- Won = True
- Player = Matrix[i][0]
- elif(Matrix[0][i] == Matrix[1][i] == Matrix[2][i]):
- Won = True
- Player = Matrix[0][i]
- elif(Matrix[0][0] == Matrix[1][1] == Matrix[2][2]):
- Won = True
- Player = Matrix[1][1]
- elif(Matrix[0][2] == Matrix[1][1] == Matrix[2][0]):
- Won = True
- Player = Matrix[1][1]
- if(Won):
- print(Player + " Wins!")
- return False
- else:
- Complete = True
- for i in range(3):
- for j in range(3):
- if Matrix[i][j] == " ":
- Complete = False
- if(Complete):
- print("Draw!")
- return False
- else:
- print("Game not finished!")
- return True
- # Initi Setup
- Playing = True
- draw()
- #Number is used to alternate between X and O by incrementing and using Mod
- number = 0
- while (Playing):
- row,col = ask()
- if(number%2==0):
- Matrix[row][col] = "X"
- else:
- Matrix[row][col] = "O"
- number+=1
- draw()
- Playing = state()
Add Comment
Please, Sign In to add comment