Created
August 19, 2024 09:23
-
-
Save horstjens/e3dac657aab68abc3e58e4e3c81c50b5 to your computer and use it in GitHub Desktop.
tictactoe
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 spiel für 2 spieler, eingabekontrolle | |
# sieg ermittlung | |
# Horst JENS, 2024 08 | |
def gewonnen(): | |
feld = spielfeld.replace("-+-+-\n","") | |
feld = feld.replace("|","") | |
feld = feld.replace("\n","") | |
print(feld) | |
# 012 | |
# 345 | |
# 678 | |
combos = ((0,1,2), (3,4,5), (6,7,8), | |
(0,3,6), (1,4,7), (2,5,8), | |
(0,4,8), (2,4,6)) | |
for a,b,c in combos: | |
if feld[a] == feld[b] == feld[c]: | |
return True | |
return False | |
spielernamen = ("spieler1","spieler2") | |
spielerzeichen = ("X","O") | |
runde = 1 | |
erlaubt = ["1","2","3","4","5","6","7","8","9"] | |
spielfeld = """ | |
1|2|3 | |
-+-+- | |
4|5|6 | |
-+-+- | |
7|8|9 | |
""" | |
while runde <= 9: | |
print("Spielrunde:", runde) | |
name = spielernamen[runde%2] | |
zeichen = spielerzeichen[runde%2] | |
print(spielfeld) | |
eingabe = input(f"{name}, welches Feld (Nummer) ? >>>") | |
if eingabe not in erlaubt: | |
print("unerlaubte Eingabe!") | |
continue | |
spielfeld = spielfeld.replace(eingabe, zeichen) | |
if gewonnen(): | |
print(f"{name} hat gewonnen!") | |
break | |
runde += 1 # runde = runde + 1 | |
print(spielfeld) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment