Hello, i have my naughts and crosses code as pasted below. Can someone help me with code to recognise if the game has come to a draw and recognising any invalid moves.
Row1 = [" "," "," "]
Row2 = [" "," "," "]
Row3 = [" "," "," "]
Grid = [Row1,Row2,Row3]
Won = False
def check():
if Grid[0][0] == 'X' and Grid[0][1] == 'X' and Grid[0][2] == 'X':
print("player 1 wins!")
return True
elif Grid[1][0] == 'X' and Grid[1][1] == 'X' and Grid[1][2] == 'X':
print("player 1 wins!")
return True
elif Grid[2][0] == 'X' and Grid[2][1] == 'X' and Grid[2][2] == 'X':
print("player 1 wins!")
return True
elif Grid[0][0] == 'X' and Grid[1][0] == 'X' and Grid[2][0] == 'X':
print("player 1 wins!")
return True
elif Grid[0][1] == 'X' and Grid[1][1] == 'X' and Grid[2][1] == 'X':
print("player 1 wins!")
return True
elif Grid[0][2] == 'X' and Grid[1][2] == 'X' and Grid[2][2] == 'X':
print("player 1 wins!")
return True
elif Grid[0][0] == 'X' and Grid[1][1] == 'X' and Grid[2][2] == 'X':
print("player 1 wins!")
return True
elif Grid[2][0] == 'X' and Grid[1][1] == 'X' and Grid[0][2] == 'X':
print("player 1 wins!")
return True
if Grid[0][0] == 'O' and Grid[0][1] == 'O' and Grid[0][2] == 'O':
print("player 2 wins!")
return True
elif Grid[1][0] == 'O' and Grid[1][1] == 'O' and Grid[1][2] == 'O':
print("player 2 wins!")
return True
elif Grid[2][0] == 'O' and Grid[2][1] == 'O' and Grid[2][2] == 'O':
print("player 2 wins!")
return True
elif Grid[0][0] == 'O' and Grid[1][0] == 'O' and Grid[2][0] == 'O':
print("player 2 wins!")
return True
elif Grid[0][1] == 'O' and Grid[1][1] == 'O' and Grid[2][1] == 'O':
print("player 2 wins!")
return True
elif Grid[0][2] == 'O' and Grid[1][2] == 'O' and Grid[2][2] == 'O':
print("player 2 wins!")
return True
elif Grid[0][0] == 'O' and Grid[1][1] == 'O' and Grid[2][2] == 'O':
print("player 2 wins!")
return True
elif Grid[2][0] == 'O' and Grid[1][1] == 'O' and Grid[0][2] == 'O':
print("player 2 wins!")
return True
def NAC():
#Pasting in the grid
DisplayGrid()
while Won == False:
YourGo = input ("Your go\nEnter Number - ")
this_row = ['7','8','9'] # Possible inputs from this row
if YourGo in this_row:
row = 0 # Tells python which row it is in
col = this_row.index(YourGo) # This_row.index selects your choice from this_row
Grid[row][col] = 'X' # Places X in the Grid
this_row = ['4','5','6']
if YourGo in this_row:
row = 1
col = this_row.index(YourGo)
Grid[row][col] = 'X'
this_row = ['1','2','3']
if YourGo in this_row:
row = 2
col = this_row.index(YourGo)
Grid[row][col] = 'X'
DisplayGrid()
#checking if won
if check() == True:
break
YourGo2 = input("Play 2's turn\nEnter number - ")
this_row = ['7', '8', '9'] # Possible inputs from this row
if YourGo2 in this_row:
row = 0 # Tells python which row it is in
col = this_row.index(YourGo2) # This_row.index selects your choice from this_row
Grid[row][col] = 'O' # Places X in the Grid
this_row = ['4','5','6']
if YourGo2 in this_row:
row = 1
col = this_row.index(YourGo2)
Grid[row][col] = 'O'
this_row = ['1','2','3']
if YourGo2 in this_row:
row = 2
col = this_row.index(YourGo2)
Grid[row][col] = 'O'
DisplayGrid()
if check() == True:
break
check()
def DisplayGrid():
print(" -----------")
DisplayRow(Grid[0][0],Grid[0][1],Grid[0][2])
print(" -----------")
DisplayRow(Grid[1][0],Grid[1][1],Grid[1][2])
print(" -----------")
DisplayRow(Grid[2][0],Grid[2][1],Grid[2][2])
print(" -----------")
def DisplayRow (a,b,c):
print(" | ",end="")
print(a,end="")
print(" | ",end="")
print(b,end="")
print(" | ",end="")
print(c,end="")
print(" |")
NAC()