Compare commits

..

No commits in common. "master" and "Tic-Tac-Toe-1.0" have entirely different histories.

183
main.py
View File

@ -1,73 +1,19 @@
#!/usr/bin/python3 #!/usr/bin/python3
from os import system from os import system
import random
# This is the list in which the fields for output on the console. The individual fields are marked by X or O
# replaced by the game
# 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 = ["", field = ["",
"1", "2", "3", "1", "2", "3",
"4", "5", "6", "4", "5", "6",
"7", "8", "9"] "7", "8", "9"]
player = '\x1b[1;34m' "X" '\x1b[1;0m' # The characters X and O are thus output in color. player = '\x1b[1;34m' "X" '\x1b[1;0m' # Die Zeichen X und O werden so farbig ausgegeben.
# Attention! O has a different color # Achtung! O hat eine andere Farbe
run = True # Variable set to True so that the program is terminated cleanly. run = True # Variable, die auf Wahr gesetzt ist, damit das Programm sauber beendet wird.
def player_names1(): def p_field(): # p_ = print. Damit wird das Spielfeld aus der Liste field auf der Konsole ausgegeben
pl_names = True
while pl_names:
name_player1 = input("Spieler 1 gib deinen Spielernamen ein.\n")
if name_player1 == "":
print("Ups keine gültige eingabe...")
continue
else:
return name_player1
pl_names = False
def player_names2():
pl_names = True
while pl_names:
name_player2 = input("Spieler 2 gib deinen Spielernamen ein.\n")
if name_player2 == "":
print("Ups keine gültige eingabe...")
continue
else:
return name_player2
pl_names = False
def gamers():
try:
while True:
# pl_ai player vs player or player vs ai
pl_ai = input("Gegen den Computer spielen,\ndie 1 drücken.\nFür 2 Spieler die 2 drücken.\n")
pl_ai = int(pl_ai)
if pl_ai == 1:
name_player1 = player_names1()
name_player2 = 'Computer'
return name_player1, name_player2
if pl_ai == 2:
name_player1 = player_names1()
name_player2 = player_names2()
return name_player1, name_player2
else:
print("Bitte nur 1 oder 2 wählen!")
continue
except ValueError:
print("Bitte nur 1 oder 2 wählen!")
gamers()
one_two = gamers()
name_player1 = one_two[0]
name_player2 = one_two[1]
def p_field(): # p_ = print. This outputs the field from the field list on the console
print("") print("")
print(" " + field[1] + "|" + field[2] + "|" + field[3]) print(" " + field[1] + "|" + field[2] + "|" + field[3])
print(" " + field[4] + "|" + field[5] + "|" + field[6]) print(" " + field[4] + "|" + field[5] + "|" + field[6])
@ -77,64 +23,36 @@ def p_field(): # p_ = print. This outputs the field from the field list on the
def play_now(): def play_now():
global run global run
if player == '\x1b[1;31m' "0" '\x1b[1;0m' and name_player2 == 'Computer': while True:
field_ai = [] try: # Try eingebaut damit auch alle anderen Zeichen und Buchstaben abgefangen werden.
for i in field: sel_field = input("Bitte ein Feld auswählen:\nZum beenden des Spiels q drücken.\n")
if i != '\x1b[1;34m' "X" '\x1b[1;0m' and i != '\x1b[1;31m' "0" '\x1b[1;0m' and i != '': if sel_field == "q":
# Here it is checked whether the element corresponds to what was specified here. run = False
# the momentary element is unequal X and unequal O and unequal '' thus another element, return 1
# then it is added to the liste field_ai. Info! In the field list are only strings! if sel_field == "":
field_ai += i print("Ohne eingabe kein gewinn!!!")
sel_field = int(random.choice(field_ai)) continue
# here a random element is selected from the newly created list. sel_field = int(sel_field)
# The element is also cast into an integer and assigned to the sel_field variable if sel_field >= 1 and sel_field <= 9:
return sel_field if field[sel_field] == '\x1b[1;34m' "X" '\x1b[1;0m' or field[sel_field] == '\x1b[1;31m' "O" '\x1b[1;0m':
else: print("Spielfeld wurde schon ausgewählt!\n")
while True:
try: # Try also installs all other characters and letters.
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' "0" '\x1b[1;0m'):
print("Spielfeld wurde schon ausgewählt!\n")
else:
return sel_field
else: else:
print("Bitte nur Ziffern von 1 - 9 wählen!\n") return sel_field
except ValueError: else:
print("Bitte nur Ziffern von 1 - 9 wählen!\n") 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(): # Check which player is on. (function player change) def change_player(): # Prüfung welcher Spieler dran ist. (Funktion Spielerwechsel)
global player global player
if run: if player == '\x1b[1;34m' "X" '\x1b[1;0m':
if player == '\x1b[1;34m' "X" '\x1b[1;0m': player = '\x1b[1;31m' "0" '\x1b[1;0m'
player = '\x1b[1;31m' "0" '\x1b[1;0m' else:
print("Spieler " + name_player2 + " ist dran.") player = '\x1b[1;34m' "X" '\x1b[1;0m'
else:
player = '\x1b[1;34m' "X" '\x1b[1;0m'
print("Spieler " + name_player1 + " ist dran.")
def name_players(): def check_win(): # Prüfung ob 3 Felder mit X oder O hintereinander (Horizontal, Waagerecht, Diagonal) belegt 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
if name_players():
print()
print("Spieler " + name_player1 + " ist dran.")
def check_win(): # check whether 3 fields are consistent with X or O.
if field[1] == field[2] == field[3]: if field[1] == field[2] == field[3]:
return field[1] return field[1]
if field[4] == field[5] == field[6]: if field[4] == field[5] == field[6]:
@ -153,34 +71,27 @@ def check_win(): # check whether 3 fields are consistent with X or O.
return field[3] return field[3]
def draw_game(): # This function checks the field for X or O so whether no numbers are present. 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" \ 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": and field[6] != "6" and field[7] != "7" and field[8] != "8" and field[9] != "9":
return True return True
def main(): while run: # Das ist die Gameloop. Die Schleife wurde hier mit run auf True gesetzt, damit das Spiel sauber
global run # beendet wird.
while run: # This is the Gameloop. The loop was set to True here with run so that the game is finished clean p_field()
p_field() sel_field = play_now()
sel_field = play_now() system('clear') # Hiermit wird das Spielfeld immer wieder an der gleichen Position angezeigt, ohne dies würde
system('clear') # As a result, the field is repeatedly displayed at the same position without this would # das Feld bei jedem Zug nach unten rutschen.
# slide down the field on each train. field[sel_field] = player
field[sel_field] = player winner = check_win()
winner = check_win() change_player()
if winner: if winner:
if winner == '\x1b[1;34m' "X" '\x1b[1;0m': print("Spieler " + winner + " hat gewonnen!")
print("Spieler " + name_player1 + " hat gewonnen.") run = False
run = False else:
if winner == '\x1b[1;31m' "0" '\x1b[1;0m': if draw_game():
print("Spieler " + name_player2 + " hat gewonnen.") print("Spiel ist unentschieden...")
run = False run = False
else:
if draw_game():
print("Spiel ist unentschieden...")
run = False
change_player()
input("Enter zum beenden...")
if __name__ == '__main__':
main()