ich habe angefangen mich mit Programmierung zu beschäftigen und frisch hier im Forum registriert. Bisher habe ich nur Kleinkram mit Powershell im täglichen Doing gemacht und vor vielen Jahren mal TurboPascal in der Schule gelernt.
Als Einsteigerthema habe ich mir ein bekanntes Würfelspiel mit 5 Würfeln ausgesucht. Die Logik funktioniert und ich kann mit einem Spieler eine Runde durchspielen. Da ich mich als nächstes mit einer passenden GUI beschäftigen möchte, ist die Ausgabe ziemlich rudimentär.
Da ich noch kompletter Neuling bin, würde ich mich sehr über ein paar Hinweise zur Verbesserung freuen. Das ganze ist noch recht sperrig leider. Ein paar Dinge sind aktuell auch nur Test, z.B. die Möglichkeit auch andere Würfel als 5W6 zu benutzen oder mehr als 5 Würfel.
Die nächsten Schritte sind
- Mehr als 1 Spieler (lokal)
- Grafische Oberfläche
- Anbindung Datenbank für die Speicherung von Ergebnissen und co.
Da ich damit bestimmt noch eine ganze Weile beschäftigt bin, würde ich gerne jetzt schon lernen etwas eleganter und strukturierter die Möglichkeiten zu nutzen und mir klassische Anfängerfehler gar nicht erst antrainieren

Vielen Dank für eure Hilfe!

VG
Chris
Code: Alles auswählen
# Define the Dice
class Dice:
#just forward the number of die sites and how many you want
def __init__(self, seiten, anzahl):
self.seiten = seiten
self.anzahl = anzahl
def getSeiten(self):
return self.seiten
def getAnzahl(self):
return self.anzahl
# first time init result list -> [0] will keep the die results [1] the number of sites (maybe there will be different dies later) [2] will track the dice that are choosen by the player
def initDie():
for rollW6 in range(W6.anzahl):
result[0].append(0)
result[1].append(W6.seiten)
result[2].append(False)
# roll the Dice and set temp entries in rollCache and countNumbers from previous rolls to Zero
def rollDice():
import random
global countNumbers
global rollCache
countNumbers = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0}
rollCache = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
for rollW6 in range(W6.anzahl):
if result[2][rollW6] == False:
result[0][rollW6] = random.randint(1,W6.seiten)
for rollW10 in range(W10.anzahl):
if result[2][rollW10] == False:
result[0][rollW10] = random.randint(1,W10.seiten)
# check possible results after max 3 rolls or player want to pickup the result
def analyzeResults():
for die in range(0,len(result[0])):
rollCache[result[0][die]] += 1
for die in range(1,W6.seiten+1):
countNumbers[die] = rollCache[die] * die
if max(rollCache.values()) >= 3 :
countNumbers[7] = sum(result[0])
if max(rollCache.values()) >= 4 :
countNumbers[8] = sum(result[0])
if 3 in rollCache.values() and 2 in rollCache.values():
countNumbers[9] = 25
streetCheck = sorted(set(result[0]))
if streetCheck == [1, 2, 3, 4] or streetCheck == [2, 3, 4, 5] or streetCheck == [3, 4, 5, 6] or streetCheck == [1, 3, 4, 5, 6] or streetCheck == [1, 2, 3, 4, 6]:
countNumbers[10] = 30
if streetCheck == [1, 2, 3, 4, 5] or streetCheck == [2, 3, 4, 5, 6]:
countNumbers[11] = 40
countNumbers[10] = 30
if max(rollCache.values()) == 5:
countNumbers[12] = 50
countNumbers[13] = countNumbers[1]+countNumbers[2]+countNumbers[3]+countNumbers[4]+countNumbers[5]+countNumbers[6]
# some debugging prints till a GUI is implemented
print(result[0])
print(rollCache)
print(countNumbers)
# player will choose which die should not be rolled again the next roll. He can also put a Die back for roll again
def checkDie():
rollAgain = False
while not rollAgain:
choose = int(input('1-5 or w >>> '))
if choose == 1:
if result[2][0] == True:
result[2][0] = False
else:
result[2][0] = True
if choose == 2:
if result[2][1] == True:
result[2][1] = False
else:
result[2][1] = True
if choose == 3:
if result[2][2] == True:
result[2][2] = False
else:
result[2][2] = True
if choose == 4:
if result[2][3] == True:
result[2][3] = False
else:
result[2][3] = True
if choose == 5:
if result[2][4] == True:
result[2][4] = False
else:
result[2][4] = True
if choose == 6:
rollAgain = True
print(result[2])
# pick up a result and write it to the final result list
def pickResult():
picked = False
nextRoll = False
x=1
notok = False
toblock = 0
#check for wrong player entries -> its not possible to override an existing result
while not notok:
toblock = int(input("Blockeintrag oder mit Eingabe 14 weiterwürfeln >>> "))
if toblock < 14:
if block[toblock] == 0:
notok = True
if toblock == 14:
notok = True
while not picked:
if toblock != 14:
if toblock == x and block[toblock] == 0:
if countNumbers[x] > 0:
block[x] = countNumbers[x]
else:
block[x] = 9999
picked = True
x += 1
nextRoll = True
else:
picked = True
print("Nextroll", nextRoll)
return(nextRoll)
# when game is finished count the "upper" part. if the result is higher 65 the player will gain a bonus of 35
def countUpper():
result = 0
for x in range(1,7):
if block[x] < 9999:
result += block[x]
if result > 65:
result += 35
return(result)
# and count the lower part
def countLower():
result = 0
for x in range(7,14):
if block[x] < 9999:
result += block[x]
return(result)
# Initialize the dice and the result list and some variables
W6 = Dice(6,5)
W10 = Dice(10,0)
countNumbers = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0}
rollCache = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
result = [[],[],[]]
block = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0}
rollCount = 0
blockFinished = False
# start the game
initDie()
while not blockFinished:
rollCount = 1
while rollCount <= 3:
if rollCount == 1:
for x in range (0, len(result[2])):
result[2][x] = False
if rollCount in range (1,4):
rollDice()
analyzeResults()
if rollCount < 3:
checkDie()
print(result[2])
alreadyPicked = pickResult()
if alreadyPicked:
print(block)
rollCount = 4
else:
rollCount += 1
while rollCount > 3 and not alreadyPicked:
print("Kein Wurf übrig. Bitte Wurf auf den Block übertragen")
alreadyPicked = pickResult()
if min(block.values()) > 0:
print(block)
oben = countUpper()
unten = countLower()
if oben > 65:
print("Oben mit Bonus +35", oben)
else:
print("Oben kein Bonus", oben)
print("Unten ", unten)
print("Gesamtpunktzahl ", oben+unten)
print("Spiel vorbei")
blockFinished = True