The problem with this code below is that apart from the print statement the check_win()
function is not returning anything I could fix this by ditching all the logic in the while loop and not using functions but I want this code to be crystal clear, any suggestions?
board = {
'a1': '', 'a2': '', 'a3': '',
'b1': '', 'b2': '', 'b3': '',
'c1': '', 'c2': '', 'c3': ''
}
player_one = True
player_one_choice = input("Play player one: ")
if player_one_choice in board:
board[player_one_choice] = 'x'
player_one = False
elif player_one_choice not in board:
print("Wrong input")
print(board)
checkwin = False
def check_win():
"""
'x' check win logic
"""
# Vertical check logic
if 'x' in board['a1'] and 'x' in board['a2'] and 'x' in board['a3']:
print("congrats x won")
checkwin == True
elif 'x' in board['b1'] and 'x' in board['b2'] and 'x' in board['b3']:
print("congrats x won")
checkwin == True
elif 'x' in board['c1'] and 'x' in board['c2'] and 'x' in board['c3']:
print("congrats x won")
checkwin == True
# Horizontal check logic
elif 'x' in board['a1'] and 'x' in board['b1'] and 'x' in board['c1']:
print("congrats x won")
checkwin == True
elif 'x' in board['a2'] and 'x' in board['b2'] and 'x' in board['c2']:
print("congrats x won")
checkwin == True
elif 'x' in board['a3'] and 'x' in board['b3'] and 'x' in board['c3']:
print("congrats x won")
checkwin == True
# Diagonal check logic
elif 'x' in board['a1'] and 'x' in board['b2'] and 'x' in board['c3']:
print("congrats x won")
checkwin == True
elif 'x' in board['a3'] and 'x' in board['b2'] and 'x' in board['c1']:
print("congrats x won")
checkwin == True
elif 'o' in board['a1'] and 'o' in board['a2'] and 'o' in board['a3']:
print("congrats o won")
checkwin == True
elif 'o' in board['b1'] and 'o' in board['b2'] and 'o' in board['b3']:
print("congrats o won")
checkwin == True
elif 'o' in board['c1'] and 'o' in board['c2'] and 'o' in board['c3']:
print("congrats o won")
checkwin == True
# Horizontal check logic
elif 'o' in board['a1'] and 'o' in board['b1'] and 'o' in board['c1']:
print("congrats o won")
checkwin == True
elif 'o' in board['a2'] and 'o' in board['b2'] and 'o' in board['c2']:
print("congrats o won")
checkwin == True
elif 'o' in board['a3'] and 'o' in board['b3'] and 'o' in board['c3']:
print("congrats o won")
checkwin == True
# Diagonal check logic
elif 'o' in board['a1'] and 'o' in board['b2'] and 'o' in board['c3']:
print("congrats o won")
checkwin == True
elif 'o' in board['a3'] and 'o' in board['b2'] and 'o' in board['c1']:
print("congrats o won")
checkwin == True
return check_win
while True:
check_win()
player_two_choice = input("PLay player two: ")
if player_two_choice != 'x' and player_one_choice in board:
board[player_two_choice] = 'o'
player_one = True
print(board)
if player_two_choice != 'x' and player_one_choice not in board or player_two_choice == 'a' or player_two_choice == 'b' or player_two_choice == 'c':
print("Wrong input")
check_win()
if checkwin:
break
player_one_choice = input("Play player one: ")
if player_one_choice in board:
board[player_one_choice] = 'x'
player_one = False
elif player_one_choice not in board:
print("Wrong input")
break
print(board)
This is the ```output``` I am receiving
Play player one: a1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': '', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
PLay player two: b1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
Play player one: a2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
PLay player two: b2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
Play player one: a3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
PLay player two: b3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': 'o', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
Play player one:
Wrong input
Process finished with exit code 0
CodePudding user response:
You've misspelled the checkwin variable
Change this:
return check_win
to this:
return checkwin
on line 84