Reputation: 15
So this is how the program will received the data
x_data = []
def xuser_input():
while True:
x = As("Player X, Please enter a number to place: ",int)
if (x > 10) or (x < 0):
print("Input must be Bigger than 0 and Smaller than 9")
continue
try:
board[x] = "X"
x_data.append(x)
except (IndexError):
print("Invalid input")
continue
t_Board()
break
There will be another for Y as well. This is the tictactoe board
def t_Board():
print(f"| {board[0]} | {board[1]} | {board[2]} |\n_____________")
print(f"| {board[3]} | {board[4]} | {board[5]} |\n_____________")
print(f"| {board[6]} | {board[7]} | {board[8]} |")
This will stop the game if this condition is met which is the wining formula.
def stops_board():
if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
board[0] == board[4] ==
board[8]) or (board[2] == board[4] == board[6]):
return False
For now this is how i ask the data input and check if theres a winning solution
while True:
xuser_input()
stops_board()
yuser_input()
stops_board()
Upvotes: 0
Views: 440
Reputation: 13
You should first figure out the steps:
Function to initialise board, check valid postition and draw the board
check if there is a winner or it is a draw
Upvotes: 1
Reputation: 15
def stops_board():
if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
board[0] == board[4] == board[8]) or (board[2] == board[4] == board[6]):
return True
while True:
xuser_input()
if stops_board(): break
yuser_input()
if stops_board(): break
Upvotes: 0
Reputation: 1182
as i see it, stops_board()
should return True if the game should stop, and False if it should continue. correct? if so, you could use:
while True:
xuser_input()
if stops_board(): break
yuser_input()
if stops_board(): break
Upvotes: 0