79 lines
2.0 KiB
Python
Executable File
79 lines
2.0 KiB
Python
Executable File
from os import system, name
|
|
|
|
def clear():
|
|
if name == 'nt':
|
|
_ = system('cls')
|
|
else:
|
|
_ = system('clear')
|
|
|
|
clear()
|
|
|
|
def print_board(board):
|
|
for row in board:
|
|
print(" | ".join(row))
|
|
print("-" * 9)
|
|
|
|
|
|
def check_winner(board, player):
|
|
# Überprüfen Sie Reihen
|
|
for row in board:
|
|
if all([cell == player for cell in row]):
|
|
return True
|
|
|
|
# Überprüfen Sie Spalten
|
|
for col in range(3):
|
|
if all([board[row][col] == player for row in range(3)]):
|
|
return True
|
|
|
|
# Überprüfen Sie Diagonalen
|
|
if all([board[i][i] == player for i in range(3)]) or all([board[i][2 - i] == player for i in range(3)]):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def check_draw(board):
|
|
return all([cell in ['X', 'O'] for row in board for cell in row])
|
|
|
|
|
|
def tic_tac_toe():
|
|
board = [[' ' for _ in range(3)] for _ in range(3)]
|
|
current_player = 'X'
|
|
|
|
while True:
|
|
print_board(board)
|
|
print(f"Spieler {current_player} ist am Zug.")
|
|
|
|
# Eingabe des Spielers
|
|
while True:
|
|
try:
|
|
row = int(input("Wähle eine Zeile (0, 1, 2): "))
|
|
col = int(input("Wähle eine Spalte (0, 1, 2): "))
|
|
if row in [0, 1, 2] and col in [0, 1, 2] and board[row][col] == ' ':
|
|
clear()
|
|
break
|
|
else:
|
|
print("Ungültige Eingabe, versuche es erneut.")
|
|
except ValueError:
|
|
print("Bitte geben Sie eine gültige Zahl ein.")
|
|
|
|
board[row][col] = current_player
|
|
|
|
if check_winner(board, current_player):
|
|
print_board(board)
|
|
print(f"Spieler {current_player} gewinnt!")
|
|
break
|
|
|
|
if check_draw(board):
|
|
print_board(board)
|
|
print("Das Spiel endet unentschieden!")
|
|
break
|
|
|
|
# Wechseln des Spielers
|
|
current_player = 'O' if current_player == 'X' else 'X'
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tic_tac_toe()
|
|
|