First match in Python
This commit is contained in:
commit
62835eb43c
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
7
.idea/misc.xml
generated
Normal file
7
.idea/misc.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.11 (tic-tac-toe)" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (tic-tac-toe)" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/tic-tac-toe.iml" filepath="$PROJECT_DIR$/.idea/tic-tac-toe.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
10
.idea/tic-tac-toe.iml
generated
Normal file
10
.idea/tic-tac-toe.iml
generated
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
97
main.py
Executable file
97
main.py
Executable file
@ -0,0 +1,97 @@
|
|||||||
|
#!/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"]
|
||||||
|
|
||||||
|
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
|
||||||
|
if player == '\x1b[1;34m' "X" '\x1b[1;0m':
|
||||||
|
player = '\x1b[1;31m' "0" '\x1b[1;0m'
|
||||||
|
else:
|
||||||
|
player = '\x1b[1;34m' "X" '\x1b[1;0m'
|
||||||
|
|
||||||
|
|
||||||
|
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]:
|
||||||
|
return field[1]
|
||||||
|
if field[4] == field[5] == field[6]:
|
||||||
|
return field[4]
|
||||||
|
if field[7] == field[8] == field[9]:
|
||||||
|
return field[7]
|
||||||
|
if field[1] == field[4] == field[7]:
|
||||||
|
return field[1]
|
||||||
|
if field[2] == field[5] == field[8]:
|
||||||
|
return field[2]
|
||||||
|
if field[3] == field[6] == field[9]:
|
||||||
|
return field[3]
|
||||||
|
if field[1] == field[5] == field[9]:
|
||||||
|
return field[1]
|
||||||
|
if field[3] == field[5] == field[7]:
|
||||||
|
return field[3]
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
change_player()
|
||||||
|
if winner:
|
||||||
|
print("Spieler " + winner + " hat gewonnen!")
|
||||||
|
run = False
|
||||||
|
else:
|
||||||
|
if draw_game():
|
||||||
|
print("Spiel ist unentschieden...")
|
||||||
|
run = False
|
||||||
|
|
||||||
|
input("Enter zum beenden...")
|
Loading…
x
Reference in New Issue
Block a user