Skip to content

Instantly share code, notes, and snippets.

@Greenscreener
Last active September 2, 2018 11:27
Show Gist options
  • Save Greenscreener/642d46aa180e308e26689071cba2388a to your computer and use it in GitHub Desktop.
Save Greenscreener/642d46aa180e308e26689071cba2388a to your computer and use it in GitHub Desktop.
import random;
class Dice:
number = 0;
def roll(self):
self.number = random.randrange(1,7);
stash = [];
cup = [];
table = [];
air = [];
score = 0;
turns = 0;
rolls = 0;
stashed = False;
message = "";
def start():
global stash;
global cup;
global table;
global air;
global rolls;
global stashed;
stash = [];
cup = [];
table = [];
air = [];
rolls = 0;
stashed = True;
for _ in range(6):
cup.append(Dice());
def roll():
global cup;
global rolls;
rolls+=1;
for i in cup:
i.roll();
air.append(i);
cup = [];
def show():
global message;
print("Score: " + str(score) + " Turns: " + str(turns));
print("\nCup:");
for i in cup:
print(i.number);
print("\nStash:");
for i in stash:
print(i.display());
print("\nTable:");
for i in table:
print(i.display());
print(message);
message = "";
def stashDice(index):
global stashed;
stashed = True;
stash.append(table[index]);
del table[index];
class DiceGroup:
def __init__(self, groupedDice, value):
self.dice = groupedDice;
self.value = value;
def display(self):
return "Dice: " + ", ".join(map(lambda x: str(x.number),self.dice)) + " Value: " + str(self.value);
def group():
global air;
global table;
global stash;
global message;
zeroValued = 0;
if list(map(lambda x: x.number, air)).sort() is [1,2,3,4,5,6]:
table = [DiceGroup(air,2000)];
air = [];
else:
for i in range(1,7):
if sum(x.number == i for x in air) > 2:
table.append(DiceGroup(list(filter(lambda x: x.number == i, air)),i*100*(2**(sum(x.number == i for x in air) - 3)) if i != 1 else 1000*(2**(sum(x.number == i for x in air) - 3))));
air = list(filter(lambda x: x.number != i, air));
for i in air:
if i.number is 1:
table.append(DiceGroup([i],100));
elif i.number is 5:
table.append(DiceGroup([i],50));
else:
table.append(DiceGroup([i],0));
zeroValued+=1;
air = [];
totalValue = 0;
for i in table:
totalValue+=i.value;
if totalValue == 0:
endTurn();
turnScore = totalValue;
for i in stash:
turnScore+=i.value;
if turnScore < 250 and rolls > 2:
turns+=1;
message+="After three rolls, you must have at least 250 points.";
start();
if zeroValued == 0:
message+="You must roll again after a full hand."
stash = [DiceGroup([],totalValue)];
table = [DiceGroup([],0)];
for _ in range(6):
table[0].append(Dice());
def endTurn():
global score;
global turns;
global message;
turnScore = 0;
for i in table:
turnScore+=i.value;
if turnScore != 0 or stashed == True:
for i in stash:
turnScore+=i.value;
if turnScore >= 250:
score+=turnScore;
turns+=1;
start();
else:
message+="Minimal score per turn is 250.";
else:
show();
message+="All rolled dice have no value. Next turn.";
turns+=1;
start();
def putInCup():
global table;
for i in table:
for _ in i.dice:
cup.append(Dice());
table = [];
def getCommand():
global stashed;
try:
command = input("> ");
except KeyboardInterrupt:
print("\nExiting...");
exit(0);
commandSplit = command.split(" ");
if commandSplit[0] == "take":
try:
if table[int(commandSplit[1]) - 1].value != 0:
stashDice(int(commandSplit[1]) - 1);
show();
else:
print("Cannot stash dice with no value.");
except IndexError:
print("No such dice.");
except ValueError:
print("Dice are indexed with numbers.");
elif commandSplit[0] == "end":
endTurn();
show()
elif commandSplit[0] == "roll":
if stashed is True:
stashed = False;
putInCup();
roll();
group();
show();
else:
print("You must stash at least once to roll.");
elif commandSplit[0] == "exit":
print("Final score: " + str(score) + " Turns: " + str(turns));
print("Exiting...");
exit(0);
else:
print(command + ": Unrecognised Command.");
start();
show();
while True:
getCommand();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment