I have got a simple naughts and crosses game working, can someone tell me a simple def blah(): .. command to check if the player has already moved to a place on the board already taken.. E.G Player's first go = top left Player's second go = top left.. (message comes up saying please have another go)?

Check if the position is occupied in your board.

## All 2 Replies

Check if the position is occupied in your board.

How would i do that? The code i have is this.

``````Row1 = [" "," "," "]
Row2 = [" "," "," "]
Row3 = [" "," "," "]
Grid = [Row1,Row2,Row3]

Won = False

def check():
if Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
return True

if Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
return True

def NAC():
DisplayGrid()
while Won == False:

YourGo = input ("Your go\nEnter Number -  ")

if YourGo == '7':
Grid = 'X'

elif YourGo == '8':
Grid = 'X'

#Player one's turn
elif YourGo == '9':
Grid = 'X'

elif YourGo == '4':
Grid = 'X'

elif YourGo == '5':
Grid = 'X'

elif YourGo == '6':
Grid = 'X'

elif YourGo == '1':
Grid = 'X'

elif YourGo == '2':
Grid = 'X'

elif YourGo == '3':
Grid = 'X'

DisplayGrid()

if check() == True: break

YourGo2 = input("Play 2's turn\nEnter number - ")
if YourGo2 == '7':
Grid = 'O'

elif YourGo2 == '8':            #player Two's turn
Grid = 'O'

elif YourGo2 == '9':
Grid = 'O'

elif YourGo2 == '4':
Grid = 'O'

elif YourGo2 == '5':
Grid = 'O'

elif YourGo2 == '6':
Grid = 'O'

elif YourGo2 == '1':
Grid = 'O'

elif YourGo2 == '2':
Grid = 'O'

elif YourGo2 == '3':
Grid = 'O'

if check() == True: break

DisplayGrid()
check()

def DisplayGrid():
print("  -----------")
DisplayRow(Grid,Grid,Grid)
print("  -----------")
DisplayRow(Grid,Grid,Grid)
print("  -----------")
DisplayRow(Grid,Grid,Grid)
print("  -----------")

def DisplayRow (a,b,c):
print(" | ",end="")
print(a,end="")
print(" | ",end="")
print(b,end="")
print(" | ",end="")
print(c,end="")
print(" |")

NAC()
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.