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)?
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Check if the position is occupied in your board.
aVar++ 14 Posting Whiz
How would i do that? The code i have is this.
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():
DisplayGrid()
while Won == False:
YourGo = input ("Your go\nEnter Number - ")
if YourGo == '7':
Grid[0][0] = 'X'
elif YourGo == '8':
Grid[0][1] = 'X'
#Player one's turn
elif YourGo == '9':
Grid[0][2] = 'X'
elif YourGo == '4':
Grid[1][0] = 'X'
elif YourGo == '5':
Grid[1][1] = 'X'
elif YourGo == '6':
Grid[1][2] = 'X'
elif YourGo == '1':
Grid[2][0] = 'X'
elif YourGo == '2':
Grid[2][1] = 'X'
elif YourGo == '3':
Grid[2][2] = 'X'
DisplayGrid()
if check() == True: break
YourGo2 = input("Play 2's turn\nEnter number - ")
if YourGo2 == '7':
Grid[0][0] = 'O'
elif YourGo2 == '8': #player Two's turn
Grid[0][1] = 'O'
elif YourGo2 == '9':
Grid[0][2] = 'O'
elif YourGo2 == '4':
Grid[1][0] = 'O'
elif YourGo2 == '5':
Grid[1][1] = 'O'
elif YourGo2 == '6':
Grid[1][2] = 'O'
elif YourGo2 == '1':
Grid[2][0] = 'O'
elif YourGo2 == '2':
Grid[2][1] = 'O'
elif YourGo2 == '3':
Grid[2][2] = 'O'
if check() == True: break
DisplayGrid()
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()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.