add blue X and red O translate comments to english
This commit is contained in:
parent
65ba5f68ff
commit
b3ed5a748f
48
main.py
48
main.py
@ -2,17 +2,17 @@
|
|||||||
from os import system
|
from os import system
|
||||||
import random
|
import random
|
||||||
|
|
||||||
# Das ist die Liste in der die Felder für die ausgabe auf der Konsole. Die einzelnen Felder werden durch X oder O
|
# This is the list in which the fields for output on the console. The individual fields are marked by X or O
|
||||||
# beim Spiel ersetzt
|
# replaced by the game
|
||||||
|
|
||||||
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' # Die Zeichen X und O werden so farbig ausgegeben.
|
player = '\x1b[1;34m' "X" '\x1b[1;0m' # The characters X and O are thus output in color.
|
||||||
# Achtung! O hat eine andere Farbe
|
# Attention! O has a different color
|
||||||
#player = 'X'
|
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 player_names1():
|
||||||
@ -42,9 +42,8 @@ def player_names2():
|
|||||||
def gamers():
|
def gamers():
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
pl_ai = input(
|
# pl_ai player vs player or player vs ai
|
||||||
"Gegen den Computer spielen,\ndie 1 drücken.\nFür 2 Spieler die 2 drücken.\n") # pl_ai player or künstliche
|
pl_ai = input("Gegen den Computer spielen,\ndie 1 drücken.\nFür 2 Spieler die 2 drücken.\n")
|
||||||
# intelligenz
|
|
||||||
pl_ai = int(pl_ai)
|
pl_ai = int(pl_ai)
|
||||||
if pl_ai == 1:
|
if pl_ai == 1:
|
||||||
name_player1 = player_names1()
|
name_player1 = player_names1()
|
||||||
@ -59,7 +58,7 @@ def gamers():
|
|||||||
continue
|
continue
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Bitte nur 1 oder 2 wählen!")
|
print("Bitte nur 1 oder 2 wählen!")
|
||||||
main() # Hier steht nachher main() drin!
|
main()
|
||||||
|
|
||||||
|
|
||||||
one_two = gamers()
|
one_two = gamers()
|
||||||
@ -68,7 +67,7 @@ name_player1 = one_two[0]
|
|||||||
name_player2 = one_two[1]
|
name_player2 = one_two[1]
|
||||||
|
|
||||||
|
|
||||||
def p_field(): # p_ = print. Damit wird das Spielfeld aus der Liste field auf der Konsole ausgegeben
|
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])
|
||||||
@ -82,17 +81,17 @@ def play_now():
|
|||||||
field_ai = []
|
field_ai = []
|
||||||
for i in field:
|
for i in field:
|
||||||
if i != '\x1b[1;34m' "X" '\x1b[1;0m' and i != '\x1b[1;31m' "0" '\x1b[1;0m' and i != '':
|
if i != '\x1b[1;34m' "X" '\x1b[1;0m' and i != '\x1b[1;31m' "0" '\x1b[1;0m' and i != '':
|
||||||
# hier wird geprüft, ob das element dem entspricht, was hier angegeben wurde.
|
# Here it is checked whether the element corresponds to what was specified here.
|
||||||
# ist das momentane element ungleich X und ungleich O und ungleich '' also ein anderes element,
|
# the momentary element is unequal X and unequal O and unequal '' thus another element,
|
||||||
# dann wird es in die liste field_ai hinzugefügt. Info! In der Liste field sind nur Strings!
|
# then it is added to the liste field_ai. Info! In the field list are only strings!
|
||||||
field_ai += i
|
field_ai += i
|
||||||
sel_field = int(random.choice(field_ai))
|
sel_field = int(random.choice(field_ai))
|
||||||
# hier wird ein zufälliges element, aus der neu erzeugten liste
|
# here a random element is selected from the newly created list.
|
||||||
# ausgewählt dieser wird auch gleich in ein integer gecastet und der Variable sel_field zugewiesen
|
# The element is also cast into an integer and assigned to the sel_field variable
|
||||||
return sel_field
|
return sel_field
|
||||||
else:
|
else:
|
||||||
while True:
|
while True:
|
||||||
try: # Try eingebaut damit auch alle anderen Zeichen und Buchstaben abgefangen werden.
|
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")
|
sel_field = input("Bitte ein Feld auswählen:\nZum beenden des Spiels q drücken.\n")
|
||||||
if sel_field == "q":
|
if sel_field == "q":
|
||||||
run = False
|
run = False
|
||||||
@ -102,7 +101,7 @@ def play_now():
|
|||||||
continue
|
continue
|
||||||
sel_field = int(sel_field)
|
sel_field = int(sel_field)
|
||||||
if sel_field >= 1 and sel_field <= 9:
|
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':
|
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")
|
print("Spielfeld wurde schon ausgewählt!\n")
|
||||||
else:
|
else:
|
||||||
return sel_field
|
return sel_field
|
||||||
@ -112,7 +111,7 @@ def play_now():
|
|||||||
print("Bitte nur Ziffern von 1 - 9 wählen!\n")
|
print("Bitte nur Ziffern von 1 - 9 wählen!\n")
|
||||||
|
|
||||||
|
|
||||||
def change_player(): # Prüfung welcher Spieler dran ist. (Funktion Spielerwechsel)
|
def change_player(): # Check which player is on. (function player change)
|
||||||
global player
|
global player
|
||||||
if run:
|
if run:
|
||||||
if player == '\x1b[1;34m' "X" '\x1b[1;0m':
|
if player == '\x1b[1;34m' "X" '\x1b[1;0m':
|
||||||
@ -133,7 +132,7 @@ if name_players():
|
|||||||
print("Spieler " + name_player1 + " ist dran.")
|
print("Spieler " + name_player1 + " ist dran.")
|
||||||
|
|
||||||
|
|
||||||
def check_win(): # Prüfung ob 3 Felder mit X oder O hintereinander (Horizontal, Waagerecht, Diagonal) belegt sind.
|
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]:
|
||||||
@ -152,7 +151,7 @@ def check_win(): # Prüfung ob 3 Felder mit X oder O hintereinander (Horizontal
|
|||||||
return field[3]
|
return field[3]
|
||||||
|
|
||||||
|
|
||||||
def draw_game(): # Diese funktion prüft das Spielfeld auf X oder O also, ob keine Zahlen mehr vorhanden sind.
|
def draw_game(): # This function checks the field for X or O so whether no numbers are present.
|
||||||
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
|
||||||
@ -160,12 +159,11 @@ def draw_game(): # Diese funktion prüft das Spielfeld auf X oder O also, ob ke
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
global run
|
global run
|
||||||
while run: # Das ist die Gameloop. Die Schleife wurde hier mit run auf True gesetzt, damit das Spiel sauber
|
while run: # This is the Gameloop. The loop was set to True here with run so that the game is finished clean
|
||||||
# beendet wird.
|
|
||||||
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()
|
||||||
if winner:
|
if winner:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user