Created
November 26, 2023 21:15
-
-
Save tonitch/dc01fe18e6aac7fb42997467ccc524b4 to your computer and use it in GitHub Desktop.
This file contains 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
# Tic tac toe for logiscool | |
board = ["-", "-", "-", | |
"-", "-", "-", | |
"-", "-", "-"] | |
player = "x" # "o" | |
winned = False | |
while not winned: | |
if player == "x": | |
player = "o" | |
else: | |
player = "x" | |
# Display board | |
print(" 1 2 3 ") | |
for i in range(3): | |
print(f"{i+1} {board[i * 3]} {board[i * 3 + 1]} {board[i * 3 + 2]}") | |
print(f"C'est au tour de joueur {player}") | |
valid_place = False | |
while not valid_place: | |
row = int(input("Row:"))-1 | |
column = int(input("Column:"))-1 | |
if (row <= 2 ) and (row >= 0) and (column <= 2 ) and (column >= 0): | |
if board[row + column * 3] == "-": | |
board[row + column * 3] = player | |
valid_place = True | |
# check if winned | |
# Lines | |
for i in range(3): | |
if (board[i] == board[i+3] and board[i+3] == board[i+6] and board[i] != "-") or (board[i * 3 + 0] == board[i * 3 + 1] and board[i * 3 + 1] == board[i * 3 + 2] and board[i*3+0] != "-"): | |
winned = True | |
# Diagonales | |
if board[0] == board[4] and board[4] == board[8] or board[2] == board[4] and board[4] == board[6] and board[5] != "-": | |
winned = True | |
print(f"Joueur {player} a gagné") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment