Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arth3mis
arth3mis / tic_short.py
Created May 24, 2021 13:28
Tic Tac Toe in Python, shortest code (17 lines), 2-player console game
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