I have been working on a Tic-Tac-Toe game and part of the winning logic is I decided to store conditional statements for winning (ie. three in a row, three in column, or three diagonal) in constant variables. The problem I am facing is that boolean False
is the result of all the statements I did. I have proof with print
s I inserted into my program.
Code:
# Create game cells
list = [' ' for n in range(10)]
# Coniditions for winning
O_ROW = (
(list[1] == 'O' and list[2] == 'O' and list[3] == 'O') or
(list[4] == 'O' and list[5] == 'O' and list[6] == 'O') or
(list[7] == 'O' and list[8] == 'O' and list[9] == 'O')
)
O_COL = (
(list[1] == 'O' and list[4] == 'O' and list[7] == 'O') or
(list[2] == 'O' and list[5] == 'O' and list[8] == 'O') or
(list[3] == 'O' and list[6] == 'O' and list[9] == 'O')
)
X_ROW = (
(list[1] == 'X' and list[2] == 'X' and list[3] == 'X') or
(list[4] == 'X' and list[5] == 'X' and list[6] == 'X') or
(list[7] == 'X' and list[8] == 'X' and list[9] == 'X')
)
X_COL = (
(list[1] == 'X' and list[4] == 'X' and list[7] == 'X') or
(list[2] == 'X' and list[5] == 'X' and list[8] == 'X') or
(list[3] == 'X' and list[6] == 'X' and list[9] == 'X')
)
O_DIAG = (
(list[1] == 'O' and list[5] == 'O' and list[9] == 'O') or
(list[3] == 'O' and list[5] == 'O' and list[7] == 'O')
)
X_DIAG = (
(list[1] == 'X' and list[5] == 'X' and list[9] == 'X') or
(list[3] == 'X' and list[5] == 'X' and list[7] == 'X')
)
def displayBoard():
# Printing the game board
print()
print(f'\t {list[1]} | {list[2]} | {list[3]}')
print('\t-----------')
print(f'\t {list[4]} | {list[5]} | {list[6]}')
print('\t-----------')
print(f'\t {list[7]} | {list[8]} | {list[9]}')
print()
def playBoard(pos, pl):
# Position the play in the cell and check for already-placed cells
if list[pos] != 'X' and list[pos] != 'O':
list[pos] = pl.upper()
else:
print("Already played! Try again")
# Determine winning condition or tie
if O_ROW and O_COL and O_DIAG:
print("O Wins!")
sys.exit()
elif X_ROW and X_COL and X_DIAG:
print("X Wins!")
sys.exit()
print('''Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin
''')
# Print for debugging
print(X_ROW)
print(X_COL)
print(X_DIAG)
print(O_ROW)
print(O_COL)
print(O_DIAG)
Output:
Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin
False
False
False
False
False
False
Play X or O?
What is the problem with the logic that they all evaluate to False
?
If the reason for this logic error is encapsulating conditional statements into variables/constants, then pardon my ignorance. I tried to do this because the literal conditional statements all within the if
statements later in the code looked too long and hard to read.
You are not upadting the values, so it's false everytime. You can do someting like this.
import sys
# Create game cells
List = ['-' for n in range(10)]
# Coniditions for winning
def runGame():
O_ROW = (
not(List[1] == 'O' and List[2] == 'O' and List[3] == 'O') or
not(List[4] == 'O' and List[5] == 'O' and List[6] == 'O') or
not(List[7] == 'O' and List[8] == 'O' and List[9] == 'O')
)
O_COL = (
not(List[1] == 'O' and List[4] == 'O' and List[7] == 'O') or
not(List[2] == 'O' and List[5] == 'O' and List[8] == 'O') or
not(List[3] == 'O' and List[6] == 'O' and List[9] == 'O')
)
O_DIAG = (
not(List[1] == 'O' and List[5] == 'O' and List[9] == 'O') or
not(List[3] == 'O' and List[5] == 'O' and List[7] == 'O')
)
X_ROW = (
not(List[1] == 'X' and List[2] == 'X' and List[3] == 'X') or
not(List[4] == 'X' and List[5] == 'X' and List[6] == 'X') or
not(List[7] == 'X' and List[8] == 'X' and List[9] == 'X')
)
X_COL = (
not(List[1] == 'X' and List[4] == 'X' and List[7] == 'X') or
not(List[2] == 'X' and List[5] == 'X' and List[8] == 'X') or
not(List[3] == 'X' and List[6] == 'X' and List[9] == 'X')
)
X_DIAG = (
not(List[1] == 'X' and List[5] == 'X' and List[9] == 'X') or
not(List[3] == 'X' and List[5] == 'X' and List[7] == 'X')
)
return(O_DIAG,O_ROW,O_COL,X_ROW,X_DIAG,X_COL)
def displayBoard():
# Printing the game board
print()
print(f'\t {List[1]} | {List[2]} | {List[3]}')
print('\t-----------')
print(f'\t {List[4]} | {List[5]} | {List[6]}')
print('\t-----------')
print(f'\t {List[7]} | {List[8]} | {List[9]}')
print()
def playBoard(pos, pl):
# Position the play in the cell and check for already-placed cells
if List[pos] != 'X' and List[pos] != 'O':
List[pos] = pl.upper()
else:
print("Already played! Try again")
# Determine winning condition or tie
if not runGame()[0] or not runGame()[1] or not runGame()[2] :
displayBoard()
print("O Wins!")
sys.exit()
elif not runGame()[3] or not runGame()[4] or not runGame()[5]:
displayBoard()
print("X Wins!")
sys.exit()
elif "-" not in List:
print("It's a draw")
sys.exit()
print('''Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin
''')
while (all(runGame())):
displayBoard()
pos=int(input("pos"))
p1=input("X/O")
playBoard(pos,p1)
playBoard(pos,p1)