2024-07-13 11:56:04 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from os import system
|
|
|
|
|
|
|
|
# Das ist die Liste in der die Felder für die ausgabe auf der Konsole. Die einzelnen Felder werden durch X oder O
|
|
|
|
# beim Spiel ersetzt
|
|
|
|
field = ["",
|
|
|
|
"1", "2", "3",
|
|
|
|
"4", "5", "6",
|
|
|
|
"7", "8", "9"]
|
|
|
|
|
2024-07-13 14:13:37 +02:00
|
|
|
name_player1 = input("Spieler 1 gib deinen Spielernamen ein.\n")
|
|
|
|
name_player2 = input("Spieler 2 gib deinen Spielernamen ein.\n")
|
2024-07-14 17:26:56 +02:00
|
|
|
|
2024-07-13 11:56:04 +02:00
|
|
|
player = '\x1b[1;34m' "X" '\x1b[1;0m' # Die Zeichen X und O werden so farbig ausgegeben.
|
|
|
|
# Achtung! O hat eine andere Farbe
|
|
|
|
run = True # Variable, die auf Wahr gesetzt ist, damit das Programm sauber beendet wird.
|
|
|
|
|
|
|
|
|
|
|
|
def p_field(): # p_ = print. Damit wird das Spielfeld aus der Liste field auf der Konsole ausgegeben
|
|
|
|
print("")
|
|
|
|
print(" " + field[1] + "|" + field[2] + "|" + field[3])
|
|
|
|
print(" " + field[4] + "|" + field[5] + "|" + field[6])
|
|
|
|
print(" " + field[7] + "|" + field[8] + "|" + field[9])
|
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
|
|
def play_now():
|
|
|
|
global run
|
|
|
|
while True:
|
|
|
|
try: # Try eingebaut damit auch alle anderen Zeichen und Buchstaben abgefangen werden.
|
|
|
|
sel_field = input("Bitte ein Feld auswählen:\nZum beenden des Spiels q drücken.\n")
|
|
|
|
if sel_field == "q":
|
|
|
|
run = False
|
|
|
|
return 1
|
|
|
|
if sel_field == "":
|
|
|
|
print("Ohne eingabe kein gewinn!!!")
|
|
|
|
continue
|
|
|
|
sel_field = int(sel_field)
|
|
|
|
if sel_field >= 1 and sel_field <= 9:
|
|
|
|
if field[sel_field] == '\x1b[1;34m' "X" '\x1b[1;0m' or field[sel_field] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
print("Spielfeld wurde schon ausgewählt!\n")
|
|
|
|
else:
|
|
|
|
return sel_field
|
|
|
|
else:
|
|
|
|
print("Bitte nur Ziffern von 1 - 9 wählen!\n")
|
|
|
|
except ValueError:
|
|
|
|
print("Bitte nur Ziffern von 1 - 9 wählen!\n")
|
|
|
|
|
|
|
|
|
|
|
|
def change_player(): # Prüfung welcher Spieler dran ist. (Funktion Spielerwechsel)
|
|
|
|
global player
|
2024-07-13 14:13:37 +02:00
|
|
|
if run:
|
|
|
|
if player == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
player = '\x1b[1;31m' "0" '\x1b[1;0m'
|
2024-07-13 15:42:49 +02:00
|
|
|
print("Spieler " + name_player2 + " ist dran.")
|
2024-07-13 14:13:37 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
player = '\x1b[1;34m' "X" '\x1b[1;0m'
|
2024-07-13 15:42:49 +02:00
|
|
|
print("Spieler " + name_player1 + " ist dran.")
|
2024-07-13 14:13:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def name_players():
|
|
|
|
if field[1] == "1" and field[2] == "2" and field[3] == "3" and field[4] == "4" and field[5] == "5" \
|
|
|
|
and field[6] == "6" and field[7] == "7" and field[8] == "8" and field[9] == "9":
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
if name_players():
|
2024-07-13 15:42:49 +02:00
|
|
|
print("Spieler " + name_player1 + " ist dran.")
|
2024-07-13 11:56:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_win(): # Prüfung ob 3 Felder mit X oder O hintereinander (Horizontal, Waagerecht, Diagonal) belegt sind.
|
|
|
|
if field[1] == field[2] == field[3]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[1] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[1] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[4] == field[5] == field[6]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[4] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[4] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[7] == field[8] == field[9]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[7] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[7] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[1] == field[4] == field[7]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[1] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[1] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[2] == field[5] == field[8]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[2] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[2] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[3] == field[6] == field[9]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[3] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[3] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[1] == field[5] == field[9]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[1] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[1] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
if field[3] == field[5] == field[7]:
|
2024-07-14 17:26:56 +02:00
|
|
|
if field[3] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
|
|
|
return name_player1
|
|
|
|
elif field[3] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
|
|
|
return name_player2
|
2024-07-13 11:56:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def draw_game(): # Diese funktion prüft das Spielfeld auf X oder O also, ob keine Zahlen mehr vorhanden sind.
|
|
|
|
if field[1] != "1" and field[2] != "2" and field[3] != "3" and field[4] != "4" and field[5] != "5" \
|
|
|
|
and field[6] != "6" and field[7] != "7" and field[8] != "8" and field[9] != "9":
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
while run: # Das ist die Gameloop. Die Schleife wurde hier mit run auf True gesetzt, damit das Spiel sauber
|
|
|
|
# beendet wird.
|
|
|
|
p_field()
|
|
|
|
sel_field = play_now()
|
|
|
|
system('clear') # Hiermit wird das Spielfeld immer wieder an der gleichen Position angezeigt, ohne dies würde
|
|
|
|
# das Feld bei jedem Zug nach unten rutschen.
|
|
|
|
field[sel_field] = player
|
|
|
|
winner = check_win()
|
2024-07-14 17:26:56 +02:00
|
|
|
if winner:
|
|
|
|
print("Spieler " + winner + " hat gewonnen!")
|
2024-07-13 11:56:04 +02:00
|
|
|
run = False
|
|
|
|
else:
|
|
|
|
if draw_game():
|
|
|
|
print("Spiel ist unentschieden...")
|
|
|
|
run = False
|
2024-07-13 14:13:37 +02:00
|
|
|
change_player()
|
2024-07-13 21:09:22 +02:00
|
|
|
|
|
|
|
system('./main.py')
|