Last active
November 25, 2023 09:19
-
-
Save horstjens/8a10a1d7fac1058aaee62fe74b1ba74e to your computer and use it in GitHub Desktop.
vier gewinnt
This file contains hidden or 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
import random | |
spieler = 1 | |
summe = 0 | |
rows = 6 # max. 25 | |
cols = 17 # max. 25 | |
gewinnt = 5 | |
spielzug = 0 | |
#feld = [ | |
# [0,0,0,0,0,0,0], | |
# [0,0,0,0,0,0,0], | |
# [0,0,0,0,0,0,0], | |
# [0,0,0,0,0,0,0], | |
# [0,0,0,0,0,0,0], | |
# [0,0,0,0,0,0,0], | |
# ] | |
feld = [[0 for x in range(cols)] for y in range(rows)] | |
# alphabet: A is chr(65), Z is chr(90) | |
alphabet = [chr(c) for c in range(65,91)] | |
def paint(): | |
for line in feld: | |
print(line) | |
paint() | |
while (summe < gewinnt) and (spielzug < rows*cols): | |
# spieler fragen und eingabe prüfen | |
#print(" A B C D E F G") | |
print(" " + " ".join(alphabet[:cols])) | |
print("Spieler", spieler, "Bitte Spalte eingeben:") | |
command = input(">>>").upper().strip() | |
if command == "": | |
continue | |
if command[0] not in alphabet[:cols]: | |
continue | |
#print("brav") | |
spalte = "".join(alphabet[:cols]).find(command[0]) | |
print("du spielst Spalte", spalte) | |
# spalte voll ? | |
for zeile in range(rows-1,-1,-1): | |
if feld[zeile][spalte] == 0: | |
feld[zeile][spalte] = spieler | |
break | |
else: | |
print("nicht möglich, Spalte ist voll!") | |
continue | |
print("Spieler" ,spieler, "setzt Stein auf Zeile", zeile, "Spalte", spalte) | |
# gewonnen ? | |
for richtung in ((1,0), (0,1), (1,1), (-1,1)): | |
#(-1,0),(0,-1),(-1,-1),(1,-1)): | |
# in Richtung | |
x, y = spalte, zeile | |
summe = 1 | |
while True: | |
x += richtung[0] | |
y += richtung[1] | |
if not (0 <= x < cols): | |
break | |
if not (0 <= y < rows): | |
break | |
if feld[y][x] != spieler: | |
break | |
summe += 1 | |
# in Gegenrichtung | |
x, y = spalte, zeile | |
while True: | |
x += richtung[0] * -1 | |
y += richtung[1] * -1 | |
if not (0 <= x < cols): | |
break | |
if not (0 <= y < rows): | |
break | |
if feld[y][x] != spieler: | |
break | |
summe += 1 | |
#print("summe for (",richtung,") is:", summe) | |
if summe >= gewinnt: | |
print("Spieler", spieler, "hat gewonnen!") | |
break | |
# ---- paint new level | |
paint() | |
# --- nächster Spieler ---- | |
if spieler == 1: | |
spieler = 2 | |
elif spieler == 2: | |
spieler = 1 | |
print("--- bye bye! ----") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment