Skip to content

Instantly share code, notes, and snippets.

@JumpIn-Git
Created August 9, 2024 19:55
Show Gist options
  • Save JumpIn-Git/1b131058c69b55f10ea8d37eead41573 to your computer and use it in GitHub Desktop.
Save JumpIn-Git/1b131058c69b55f10ea8d37eead41573 to your computer and use it in GitHub Desktop.
a short and simple tic tac toe game inspired by https://gist.github.com/qianguigui1104/edb3b11b33c78e5894aad7908c773353
def init():
print("Welcome to tic tac toe!\nWe will start with choosing teams.")
one = input("Player 1, do you want to be O or X: ").upper()
one = one if one in ("O", "X") else "O"
two = "O" if one == "X" else "X"
board = [[" " for _ in range(3)] for _ in range(3)]
return one, two, board
def show(matrix, columns):
print(" 1 2 3 \n ---+---+---")
for column, row in zip(columns, matrix):
print(f"{column} {" | ".join(row)} \n ---+---+---")
def winning(matrix):
# Rows
for row in matrix: # Row by row
if all(c == row[0] and c != " " for c in row):
return True
# Columns
rotated = [list(row) for row in zip(*matrix)] # Columns as rows
rotated = [list(reversed(row)) for row in rotated] # Reverse to finish rotation
for row in rotated: # Row by row
if all(c == row[0] and c != " " for c in row):
return True
# Diagonals
for x, y in zip((0, 2), (2, 0)): # Left/right diagonals
if matrix[0][x] == matrix[1][1] == matrix[2][y] != " ":
return True
def draw(matrix):
if all(c != " " for row in matrix for c in row):
return True
return False
def main():
p1, p2, board = init()
player = p1
columns = 'a', 'b', 'c'
while True:
show(board, columns)
other_player = p1 if player == p2 else p2
if winning(board):
print(f'Player {1 if other_player == p2 else 2}, you won!')
break
if draw(board):
print('Its a draw!')
break
while True:
cords = input(f"Player {1 if player == p1 else 2}, please enter the cords to fill (eg a2): ").lower()
if len(cords) == 2 and cords[0] in columns and cords[1].isdigit() and 1 <= int(cords[1]) <= 3 and board[columns.index(cords[0])][int(cords[1]) - 1] == " ":
board[columns.index(cords[0])][int(cords[1]) - 1] = player
break
print('Invalid cords.')
player = other_player
if __name__ == "__main__":
main()
@2ndMessiah
Copy link

good!!

@JumpIn-Git
Copy link
Author

good!!

thx

@Murolas
Copy link

Murolas commented Jan 15, 2025

there is a problem I tried testing it but when player1 won it said that player 2 won instead
all it took was switching them around

@JumpIn-Git
Copy link
Author

there is a problem I tried testing it but when player1 won it said that player 2 won instead all it took was switching them around

wierd i didnt have that issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment