check_win additional if query for name return
This commit is contained in:
parent
5f6263aa8e
commit
05b66c6514
48
main.py
48
main.py
@ -10,6 +10,7 @@ field = ["",
|
||||
|
||||
name_player1 = input("Spieler 1 gib deinen Spielernamen ein.\n")
|
||||
name_player2 = input("Spieler 2 gib deinen Spielernamen ein.\n")
|
||||
|
||||
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.
|
||||
@ -70,21 +71,45 @@ if name_players():
|
||||
|
||||
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[1] == '\x1b[1;34m' "X" '\x1b[1;0m':
|
||||
return name_player1
|
||||
elif field[1] == '\x1b[1;31m' "O" '\x1b[1;0m':
|
||||
return name_player2
|
||||
if field[4] == field[5] == field[6]:
|
||||
return field[4]
|
||||
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
|
||||
if field[7] == field[8] == field[9]:
|
||||
return field[7]
|
||||
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
|
||||
if field[1] == field[4] == field[7]:
|
||||
return field[1]
|
||||
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
|
||||
if field[2] == field[5] == field[8]:
|
||||
return field[2]
|
||||
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
|
||||
if field[3] == field[6] == field[9]:
|
||||
return field[3]
|
||||
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
|
||||
if field[1] == field[5] == field[9]:
|
||||
return field[1]
|
||||
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
|
||||
if field[3] == field[5] == field[7]:
|
||||
return field[3]
|
||||
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
|
||||
|
||||
|
||||
def draw_game(): # Diese funktion prüft das Spielfeld auf X oder O also, ob keine Zahlen mehr vorhanden sind.
|
||||
@ -101,11 +126,8 @@ while run: # Das ist die Gameloop. Die Schleife wurde hier mit run auf True ges
|
||||
# das Feld bei jedem Zug nach unten rutschen.
|
||||
field[sel_field] = player
|
||||
winner = check_win()
|
||||
if winner == "X":
|
||||
print("Spieler " + name_player1 + " hat gewonnen!")
|
||||
run = False
|
||||
elif winner == "O":
|
||||
print("Spieler " + name_player2 + " hat gewonnen!")
|
||||
if winner:
|
||||
print("Spieler " + winner + " hat gewonnen!")
|
||||
run = False
|
||||
else:
|
||||
if draw_game():
|
||||
|
Loading…
x
Reference in New Issue
Block a user