Reputation: 1
Im making a Tic Tac Toe game but I have met a couple of problems while doing it.
The firs problem, is that when I had the board like:
X | - | X
or like:
- | X | X
It counts as a Win and I don't know why. If I put:
X | X | -
there's no problem with that.
The second problem, is that I want to prevent the user to select a position that is already in use, so my idea is to add to a list the position that has been selected and compare if it is already in it or not. The problem is that when I call the function for the second time it doesn't have the previus list that contain the information of the previus position (it makes a new one), therefore it never detects that the position that the user is giving is already in use.
board = ["-","-","-",
"-","-","-",
"-","-","-"]
def display():
print(board[0] + " | " + board[1] + " | " + board[2])
print(board[3] + " | " + board[4] + " | " + board[5])
print(board[6] + " | " + board[7] + " | " + board[8])
def turn():
exist=[]
pos = int(input("Choose your position"))
while True:
if pos not in exist:
exist.append(pos)
print(exist)
print("Correct position")
break
else:
print("That position has been selected, choose another one")
board[pos-1] = "X"
display()
def checkwin():
#row
if (board[0] and board[1] and board[2] == "X") or (board[3] and board[4] and board[5] == "X") or (board[6] and board[7] and board[8] == "X"):
print("Victory")
return False
else:
return True
display()
while checkwin():
turn()
checkwin()
print(board)
print("Game has ended")
NOTE: Right now the game only can have the X player, I need still to add the O player. Also, it only can detect the win for the rows. Im still working on this.
Upvotes: 0
Views: 267
Reputation: 3518
sin tribu nailed it as to why your turn
function is behaving the way it is.
As to how to make it work right... perhaps I'm missing something, but aren't you looking to make sure you can't select a space that's already occupied? For that, you could just check the board directly.
def turn():
pos = int(input("Choose your position"))
while board[pos-1] == "X":
pos = int(input("That position has been selected, choose another one"))
print("Correct position")
board[pos-1] = "X"
display()
This will keep asking until you specify an empty square.
Upvotes: 1
Reputation: 1180
To your second question, you are declaring your list inside the function turn
, which is called every time a player makes a choice. When a function returns, all of its local variables are no longer accessible. Compare this with your code.
def addToList( item ):
items = []
items.append( item )
addToList( 'item1' )
addToList( 'item2' )
Each time the function is called, items = []
is called as well. Edit to clarify my first statement, functions create their own scope. The scope of a variable is where it is available in your program. For example
def addItem( item ):
items = []
items.append( item )
addItem( "item1" )
print( items )
ERROR: items is not defined
Upvotes: 3
Reputation: 1937
The logic of (board[0] and board[1] and board[2] == "X")
won't work because you are only checking is board[2] == 'X'
, the others are just truthy, thus '-'
will also evaluate to True
You should rather check like this (board[0] == board[1] == board[2] == "X")
.
This ensures that all values are equal to 'X'
Upvotes: 2