reines Konsolenspiel. Allerdings bin ich an einem Win7 Gerät im
Moment und müsste wissen ob das löschen der Terminalanzeige
auch unter Unix/Linux Distributionen funktioniert.
Falls jemand einen Moment Zeit hätte, wäre ich sehr verbunden
(Skript ist noch nicht fertig, also bitte nicht wundern bzgl unnützer Imports
oder nicht eingebundener Funktionen >_<)
Code: Alles auswählen
#!/usr/bin/env python
import os,sys,platform
# Vier gewinnt Spiel auf Textkonsolen-Basis
class main():
def __init__(self):
g_os=platform.system()
befehl=""
if g_os =='Windows':
befehl='cls'
else:
befehl='clear'
self.befehl=befehl
# Initialisieren der Spielfelder
# 8x8 Spielfeld
small_game_field={
"A8":"-","B8":"-","C8":"-","D8":"-","E8":"-","F8":"-","G8":"-","H8":"-",
"A7":"-","B7":"-","C7":"-","D7":"-","E7":"-","F7":"-","G7":"-","H7":"-",
"A6":"-","B6":"-","C6":"-","D6":"-","E6":"-","F6":"-","G6":"-","H6":"-",
"A5":"-","B5":"-","C5":"-","D5":"-","E5":"-","F5":"-","G5":"-","H5":"-",
"A4":"-","B4":"-","C4":"-","D4":"-","E4":"-","F4":"-","G4":"-","H4":"-",
"A3":"-","B3":"-","C3":"-","D3":"-","E3":"-","F3":"-","G3":"-","H3":"-",
"A2":"-","B2":"-","C2":"-","D2":"-","E2":"-","F2":"-","G2":"-","H2":"-",
"A1":"-","B1":"-","C1":"-","D1":"-","E1":"-","F1":"-","G1":"-","H1":"-"
}
self.small_game_field=small_game_field
def fail(self, message):
os.system(self.befehl)
b=raw_input("[Fehler] %s | <Enter> fuer Weiter" %(message))
os.system(self.befehl)
self.menu()
def start_game(self, s_game_field=None,s_settings=None, field_range=None):
if s_settings!=None:
settings=s_settings
game_field=s_game_field
else:
settings=[]
os.system(self.befehl)
print(" <Spieleinstellungen>\n")
settings.append(raw_input("Name Spieler 1 (X - Stein): "))
settings.append("X")
settings.append(raw_input("Name Spieler 2 (O - Stein): "))
settings.append("O")
print("\n <Spielfeldgroesse>\n")
field_size=int(raw_input("[1] 8x8\n[2] 16x16 (nicht implementiert)\nAuswahl: "))
field_range=0
if field_size == 1:
game_field=self.small_game_field
field_range=9
elif field_size == 2:
fail("Option is not implemented yet")
os.system(self.befehl)
end=False
player=settings[0]
symbol=settings[1]
if field_range<10:
while end==False:
os.system(self.befehl)
for t in range(1,field_range):
print(game_field["A%s"%(t)]+"|"+game_field["B%s"%(t)]+"|"+game_field["C%s"%(t)]+"|"+game_field["D%s"%(t)]+"|"+game_field["E%s"%(t)]+"|"+game_field["F%s"%(t)]+"|"+game_field["G%s"%(t)]+"|"+game_field["H%s"%(t)]+"|")
print("A|B|C|D|E|F|G|H|\n")
print("\nUm einen Stein zu setzen, brauchen Sie nur\nden Buchstaben einzusetzen und <Enter> druecken\n\'exit\' zum verlassen eingeben.")
inp=raw_input("Zug von %s: " %(player))
if inp == "exit":
sys.exit(0)
if inp == "save":
self.save_game(game_field,settings,field_range)
continue
try:
cnt=8
v_set=False
inp=inp.upper()
while v_set==False:
if game_field["%s%s"%(inp,cnt)]=="-":
game_field["%s%s"%(inp,cnt)]=symbol
# Pruefung (ob ein Spieler gewonnen hat)
p_map={"A":0,"B":1,"C":2,"D":3,"E":4,"F":5,"G":6,"H":7}
rp_map={0:"A",1:"B",2:"C",3:"D",4:"E",5:"F",6:"G",7:"H"}
last_char=p_map[inp]
# Horizontale Prueflisten
if last_char+3<=7:
if game_field["%s%s"%(inp,cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+3],cnt)]==symbol:
f=raw_input("[%s] hat gewonnen! Herzlichen Glueckwunsch. <Enter>"%(player))
end=True
if last_char-2>=0:
if last_char+1<=7:
if game_field["%s%s"%(inp,cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char-1],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-1>=0:
if last_char+2<=7:
if game_field["%s%s"%(inp,cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char-1],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-3>=0:
if game_field["%s%s"%(rp_map[last_char-1],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt)]==symbol:
if game_field["%s%s"%(rp_map[last_char-3],cnt)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
# Vertikale Prueflisten
if cnt+3<=7:
if game_field["%s%s"%(inp,cnt)]==symbol:
if game_field["%s%s"%(inp,cnt+1)]==symbol:
if game_field["%s%s"%(inp,cnt+2)]==symbol:
if game_field["%s%s"%(inp,cnt+3)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
# Diagonale Prueflisten
if last_char+3<=7:
if cnt+3<=7:
if game_field["%s%s"%(rp_map[last_char+1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt+2)]==symbol:
if game_field["%s%s"%(rp_map[last_char+3],cnt+3)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-1>=0:
if cnt-1>=0:
if last_char+2<=7:
if cnt+2<=7:
if game_field["%s%s"%(rp_map[last_char-1],cnt-1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt+2)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-2>=0:
if cnt-2>=0:
if last_char+1<=7:
if cnt+1<=7:
if game_field["%s%s"%(rp_map[last_char+1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char-1],cnt-1)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt-2)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-3>=0:
if cnt-3>=0:
if game_field["%s%s"%(rp_map[last_char-1],cnt-1)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt-2)]==symbol:
if game_field["%s%s"%(rp_map[last_char-3],cnt-3)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
#-----------------------------------------
if last_char+3<=7:
if cnt-3>=0:
if game_field["%s%s"%(rp_map[last_char+1],cnt-1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt-2)]==symbol:
if game_field["%s%s"%(rp_map[last_char+3],cnt-3)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char+2<=7:
if cnt-2>=0:
if last_char-1>=0:
if cnt+1<=7:
if game_field["%s%s"%(rp_map[last_char-1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt-1)]==symbol:
if game_field["%s%s"%(rp_map[last_char+2],cnt-2)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char+1<=7:
if cnt-1>=0:
if last_char-2>=0:
if cnt+2<=7:
if game_field["%s%s"%(rp_map[last_char-1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt+2)]==symbol:
if game_field["%s%s"%(rp_map[last_char+1],cnt-1)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
if last_char-3>=0:
if cnt+3<=7:
if game_field["%s%s"%(rp_map[last_char-1],cnt+1)]==symbol:
if game_field["%s%s"%(rp_map[last_char-2],cnt+2)]==symbol:
if game_field["%s%s"%(rp_map[last_char-3],cnt+3)]==symbol:
f=raw_input("[%s] hat gewonnen!! Herzlichen Glueckwunsch. <Enter> fuer Hauptmenue..."%(player))
end=True
#------------------------
v_set=True
if player==settings[0]:
player=settings[2]
symbol=settings[3]
else:
player=settings[0]
symbol=settings[1]
else:
cnt-=1
except:
f=raw_input("[Fehler] Eingabe nicht gueltig [%s]<Enter>"%(inp))
del f
else:
pass
self.__init__()
self.menu()
def save_game(self, mapping, settings, field_range):
s1="s_settings=%s"%(settings)
s2="s_game_field=%s"%(mapping)
s3="field_range=%s"%(field_range)
f_outp=open("save.svf","wb")
f_outp.write(s1)
f_outp.write("\n")
f_outp.write(s2)
f_outp.write("\n")
f_outp.write(s3)
f_outp.close()
def load_game(self):
f_in=open("save.svf","rb")
c_s1=f_in.readline()
c_s2=f_in.readline()
c_s3=f_in.readline()
exec(c_s1)
exec(c_s2)
exec(c_s3)
f_in.close()
self.start_game(s_game_field,s_settings,field_range)
def scores(self):
self.fail("Noch nicht implementiert")
def g_help(self):
print(""" Hilfe zu \'Vier gewinnt\' v1.2\nHauptmenue>\n Hier werden alle bisher implementierten/geplanten\n Menuepunkte angezeigt die per Zahleneingabe angewaehlt\n werden koennen.\nSpiel starten>\n Spielen Sie gegen einen Freund das klassische\n \'Vier gewinnt\'. Stellen Sie die Namen ein und\n los gehts! Beim spielen brauchen Sie nur den\n Buchstaben der jeweiligen Spalte einzugeben und\n mit <Enter> zu bestaetigen. Zum speichern des\n aktuellen Spiels, geben Sie einfach \'save\'\n ein und spielen sie spaeter weiter.\n Mit \'exit\' verlassen Sie das Spiel.\nSpiel laden>\n Hier wird das zu letzt von Ihnen gespeicherte\n Spiel geladen und Sie koennen einfach weiterspielen.\nScores>\n <noch nicht implementiert>\n""")
f=raw_input("Weiter mit <Enter>")
self.menu()
def menu(self):
os.system(self.befehl)
print """Herzlich Willkommen zu \'Vier gewinnt\'!\nUm einen Menuepunkt zu verwenden, geben Sie\neinfach die entsprechende Zahl und und druecken\n<Enter>. Viel Vergnuegen...\n\n[1] Spiel starten\n[2] Spiel laden\n[3] Highscores\n[4] Hilfe\n[5] Exit\n"""
inp=int(raw_input("Auswahl ---> "))
if inp>5:
self.fail("Falsche Auswahl")
elif inp == 1:
self.start_game()
elif inp == 2:
self.load_game()
elif inp == 3:
self.scores() #Wird noch implementiert
elif inp == 4:
self.g_help()
elif inp == 5:
sys.exit(0)
else:
pass
if __name__=='__main__':
m=main()
m.menu()
MfG The Freak