This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f, player = [[0,0,0], [0,0,0], [0,0,0]], 0 # playing field; player turn indicator | |
while True: # game loop | |
inp = str.split(input("\nPlayer " + str(player+1) + " (format 'x y'; range 0-2): ").strip(), " ") # placing choice | |
if f[int(inp[0])][2-int(inp[1])] == 0: # chosen field must be empty | |
f[int(inp[0])][2-int(inp[1])] = 1 if player == 0 else -1 # player 2 gets -1 value for easier win checking | |
player = 1 - player # switch player turns for next round | |
w = [0,0,0, 0,0,0, 0,0] # if a player has won, lines add up to 3 or -3 | |
for i in range(3): # loop for printing the playing field & win checking | |
for j in range(3): | |
print(2 if f[j][i] == -1 else f[j][i], "\n" if j == 2 else "", sep=" ", end="") # field display |