I am trying to make a tic-tac-toe game however when I try implement two players, the printed out result (current game state) is not as expected. for example when I try to put the letter 'x' in box 1 it will add random letters/numbers in different spots. I have already tried this with only one player and it wrks perfectly.
import random a = ' ' b = ' ' c = ' ' d = ' ' e = ' ' f = ' ' g = ' ' h = ' ' i = ' ' def gamestateboard(a, b, c, d, e, f, g, h, i): x = [a, b, c] y = [d, e, f] z = [g, h, i] print(x) print(y) print(z) gamestateboard(a, b, c, d, e, f, g, h, i) def checkwin(): if a == 0 and b == 0 and c == 0: print('Winner') if a == 0 and d == 0 and g == 0: print('Winner') if g == 0 and h == 0 and i == 0: print('Winner') if c == 0 and e == 0 and g == 0: print('Winner') if a == 0 and e == 0 and i == 0: print('Winner') if c == 0 and f == 0 and i == 0: print('Winner') for i in range(1000000): inputs = str(input('Enter a position: ')) if i%2 == 0: if inputs == '1': a = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '2': b = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '3': c = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '4': d = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '5': e = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '6': f = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '7': g = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '8': h = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '9': i = 0 gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() else: if inputs == '1': a = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '2': b = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '3': c = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '4': d = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '5': e = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '6': f = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '7': g = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '8': h = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() if inputs == '9': i = 'X' gamestateboard(a, b, c, d, e, f, g, h, i) checkwin() https://stackoverflow.com/questions/67249258/how-to-fix-tic-tac-toe-two-player-bug April 25, 2021 at 10:37AM
没有评论:
发表评论