Skip to content

Instantly share code, notes, and snippets.

@MarkGamed7794
Last active June 23, 2019 16:42
Show Gist options
  • Save MarkGamed7794/d1bfb4236bff5a811204409bff315a86 to your computer and use it in GitHub Desktop.
Save MarkGamed7794/d1bfb4236bff5a811204409bff315a86 to your computer and use it in GitHub Desktop.
SCRIPT-8
//title:Yahtzee
var lower = 0;
var upper = 0;
var dice = [16,16,16,16,16];
var held = [false,false,false,false,false];
var rollsLeft = 3;
var hoveringOver = -1;
var combos = ["Ones","Twos","Threes","Fours","Fives","Sixes","3 of a Kind","4 of a Kind","Full House","Sm. Straight","Lg. Straight","Yahtzee","Chance","Left Bonus"];
var descriptions = [
["Sum of all","ones"],
["Sum of all","twos"],
["Sum of all","threes"],
["Sum of all","fours"],
["Sum of all","fives"],
["Sum of all","sixes"],
["Three of the","same, worth","the sum of","all dice"],
["Four of the","same, worth","the sum of","all dice"],
["A pair and 3","of a kind,","worth 25"],
["4 ascending","dice, worth","30"],
["5 ascending","dice, worth","40"],
["Five of the","same, worth","50"],
["No pattern,","worth the sum","of all dice"],
["If the left","section sums ","to 63 or over,","score 35","extra points"]
]
var rollingTimer = 0;
var mouseX = 0;
var mouseY = 0;
var scores = {
"Ones":-1,
"Twos":-1,
"Threes":-1,
"Fours":-1,
"Fives":-1,
"Sixes":-1,
"3 of a Kind":-1,
"4 of a Kind":-1,
"Full House":-1,
"Sm. Straight":-1,
"Lg. Straight":-1,
"Yahtzee":-1,
"Chance":-1,
}
function count(a,b){
var c = 0;
for(var i = 0; i < a.length; ++i){
if(a[i] == b)
c++;
}
return c;
}
function checkHover(){
var hoveringOver2 = -1;
for(var i = 0;i < 12;i++){
if(mouseX >= 10+(32*(Math.floor(i/6))) && mouseX <= 18+(32*(Math.floor(i/6))) && mouseY >= 46+(i%6)*12 && mouseY <= 54+(i%6)*12){
hoveringOver2 = i;
}
}
if(mouseX >= 10+(32*(Math.floor(1))) && mouseX <= 18+(32*(Math.floor(1))) && mouseY >= 46+(6)*12 && mouseY <= 54+(6)*12){
hoveringOver2 = 12;
}
return hoveringOver2;
}
function checkCombos(b){
var possibleCombos = [];
var a = b.sort();
if(a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4]){possibleCombos.push("Yahtzee");}
if(a[0] == a[1]-1 && a[1] == a[2]-1 && a[2] == a[3]-1 && a[3] == a[4]-1){possibleCombos.push("Lg. Straight");}
if(a=>(z=[0,1,2,3]).some(b=>z.every(c=>a.includes(c+b)))){possibleCombos.push("Sm. Straight");}
if((a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) || (a[3] == a[4] && a[1] == a[2] && a[2] == a[3])){possibleCombos.push("4 of a Kind");}
if((a[0] == a[1] && a[1] == a[2]) || (a[1] == a[2] && a[2] == a[3]) || (a[2] == a[3] && a[3] == a[4])){possibleCombos.push("3 of a Kind");}
if(a[0] == a[1] && a[1] == a[2] && a[2] != a[3] && a[3] == a[4]){possibleCombos.push("Full House");}
if(a[0] == a[1] && a[1] != a[2] && a[2] == a[3] && a[3] == a[4]){possibleCombos.push("Full House");}
if(a.includes(1)){possibleCombos.push("Ones");}
if(a.includes(2)){possibleCombos.push("Twos");}
if(a.includes(3)){possibleCombos.push("Threes");}
if(a.includes(4)){possibleCombos.push("Fours");}
if(a.includes(5)){possibleCombos.push("Fives");}
if(a.includes(6)){possibleCombos.push("Sixes");}
possibleCombos.push("Chance");
return possibleCombos;
}
update = (dummy,input) => {
if(input.left){mouseX = clamp(mouseX-1,0,127);}
if(input.right){mouseX = clamp(mouseX+1,0,127);}
if(input.up){mouseY = clamp(mouseY-1,0,127);}
if(input.down){mouseY = clamp(mouseY+1,0,127);}
if(input.aPressed){
for(var i = 0;i < 5;i++){
if(mouseX >= 20+i*20 && mouseX <= 28+i*20 && mouseY >= 27 && mouseY <= 35 && rollsLeft != 3){held[i] = !held[i];}
}
if(checkHover() >= 0 && checkHover() < 13 && scores[combos[checkHover()]] == -1 && rollsLeft != 3){
if(checkCombos(dice).includes(combos[hoveringOver])){
if(checkHover() < 6){
scores[combos[checkHover()]] = count(dice,checkHover()+1)*(checkHover()+1)
}else if(checkHover() < 8 || checkHover() == 12){
scores[combos[checkHover()]] = dice.reduce((a, b) => a + b, 0) // sums all dice
}else if(checkHover() == 8){
scores[combos[checkHover()]] = 25
}else if(checkHover() == 9){
scores[combos[checkHover()]] = 30
}else if(checkHover() == 10){
scores[combos[checkHover()]] = 40
}else if(checkHover() == 11){
scores[combos[checkHover()]] = 50
}
}else{scores[combos[checkHover()]] = 0;}
rollsLeft = 3;
dice = [16,16,16,16,16]
held = [false,false,false,false,false]
}
}
if(input.bPressed && rollsLeft > 0){
rollingTimer = 90;
rollsLeft--;
}
}
draw = () => {
clear(6)
for(var i = 0;i < 10;i++){
sprite(20+i*20,27,dice[i]-1,held[i]*-2);
}
rectFill(0,41,128,87,5);
line(0,40,128,40);
line(75,40,75,128);
hoveringOver = -1;
for(var i = 0;i < 12;i++){
sprite(10+(32*(Math.floor(i/6))),46+(i%6)*12,16+i);
if(scores[combos[i]] != -1){print(19+(32*(Math.floor(i/6))),48+(i%6)*12,scores[combos[i]])}else{print(19+(32*(Math.floor(i/6))),48+(i%6)*12,"---")}
if(mouseX >= 10+(32*(Math.floor(i/6))) && mouseX <= 18+(32*(Math.floor(i/6))) && mouseY >= 46+(i%6)*12 && mouseY <= 54+(i%6)*12){
hoveringOver = i;
}
}
sprite(10+32,46+6*12,16+12);
if(scores[combos[12]] != -1){print(19+(32*(Math.floor(1))),48+(6)*12,scores[combos[12]])}else{print(19+(32*(Math.floor(1))),48+(6)*12,"---")}
if(mouseX >= 10+(32*(Math.floor(1))) && mouseX <= 18+(32*(Math.floor(1))) && mouseY >= 46+(6)*12 && mouseY <= 54+(6)*12){
hoveringOver = 12;
}
if(mouseX >= 12 && mouseX <= 27 && mouseY >= 119 && mouseY <= 126){hoveringOver = 13;}
if(hoveringOver >= 0){
print(78,47,combos[hoveringOver]);
for(var i = 0;i < descriptions[hoveringOver].length;i++){
print(78,54+7*i,descriptions[hoveringOver][i],3);
}
}
var scoreSum = 0;
var lowerSum = 0;
for(var i = 0;i <= 12;i++){
if(scores[combos[i]] != -1){
scoreSum += scores[combos[i]]
if(i <= 5){lowerSum += scores[combos[i]]}
}
}
if(lowerSum >= 63){scoreSum += 35;}
print(78,112,"Score: " + scoreSum);
print(78,121,"Rolls: " + rollsLeft);
var invert = 0;
if(lowerSum >= 63){invert = 5;}
rectFill(12,119,15,7,5-invert)
print(18,120,lowerSum,invert);
print(13,119,'-',invert);
print(13,121,'-',invert);
if(rollingTimer > 0){
for(var i = 0;i < 5;i++){
if(!held[i]){
dice[i] = random(1,6);
}
}
rollingTimer--;
}
sprite(mouseX,mouseY,30);
}
{
"iframeVersion": "0.1.255",
"lines": [
187,
0,
0,
0,
0,
0,
0,
0
]
}
{
"0": [
" ",
"1111111 ",
"1000001 ",
"1000001 ",
"1007001 ",
"1000001 ",
"1000001 ",
"1111111 "
],
"1": [
" ",
"1111111 ",
"1000071 ",
"1000001 ",
"1000001 ",
"1000001 ",
"1700001 ",
"1111111 "
],
"2": [
" ",
"1111111 ",
"1000071 ",
"1000001 ",
"1007001 ",
"1000001 ",
"1700001 ",
"1111111 "
],
"3": [
" ",
"1111111 ",
"1700071 ",
"1000001 ",
"1000001 ",
"1000001 ",
"1700071 ",
"1111111 "
],
"4": [
" ",
"1111111 ",
"1700071 ",
"1000001 ",
"1007001 ",
"1000001 ",
"1700071 ",
"1111111 "
],
"5": [
" ",
"1111111 ",
"1700071 ",
"1000001 ",
"1700071 ",
"1000001 ",
"1700071 ",
"1111111 "
],
"6": [
" ",
"1111111 ",
"1700071 ",
"1000001 ",
"1707071 ",
"1000001 ",
"1700071 ",
"1111111 "
],
"7": [
" ",
"1111111 ",
"1707071 ",
"1000001 ",
"1700071 ",
"1000001 ",
"1707071 ",
"1111111 "
],
"8": [
" ",
"1111111 ",
"1707071 ",
"1000001 ",
"1707071 ",
"1000001 ",
"1707071 ",
"1111111 "
],
"9": [
" ",
"1111111 ",
"1707071 ",
"1070701 ",
"1000001 ",
"1070701 ",
"1707071 ",
"1111111 "
],
"10": [
" ",
"1111111 ",
"1700071 ",
"1070701 ",
"1707071 ",
"1070701 ",
"1700071 ",
"1111111 "
],
"11": [
" ",
"1111111 ",
"1707071 ",
"1070701 ",
"1700071 ",
"1070701 ",
"1707071 ",
"1111111 "
],
"12": [
" ",
"1111111 ",
"1707071 ",
"1070701 ",
"1707071 ",
"1070701 ",
"1707071 ",
"1111111 "
],
"13": [
" ",
"1111111 ",
"1070771 ",
"1707071 ",
"1070701 ",
"1707071 ",
"1770701 ",
"1111111 "
],
"14": [
" ",
"1111111 ",
"1707071 ",
"1770771 ",
"1007001 ",
"1770771 ",
"1707071 ",
"1111111 "
],
"15": [
" ",
"1111111 ",
"1777771 ",
"1000071 ",
"1007771 ",
"1000001 ",
"1070001 ",
"1111111 "
],
"16": [
" ",
"3333333 ",
"3222223 ",
"3222223 ",
"3220223 ",
"3222223 ",
"3222223 ",
"3333333 "
],
"17": [
" ",
"3333333 ",
"3222203 ",
"3222223 ",
"3222223 ",
"3222223 ",
"3022223 ",
"3333333 "
],
"18": [
" ",
"3333333 ",
"3222203 ",
"3222223 ",
"3220223 ",
"3222223 ",
"3022223 ",
"3333333 "
],
"19": [
" ",
"3333333 ",
"3022203 ",
"3222223 ",
"3222223 ",
"3222223 ",
"3022203 ",
"3333333 "
],
"20": [
" ",
"3333333 ",
"3022203 ",
"3222223 ",
"3220223 ",
"3222223 ",
"3022203 ",
"3333333 "
],
"21": [
" ",
"3333333 ",
"3022203 ",
"3222223 ",
"3022203 ",
"3222223 ",
"3022203 ",
"3333333 "
],
"22": [
" ",
"3333333 ",
"3000003 ",
"3222203 ",
"3200003 ",
"3222203 ",
"3000003 ",
"3333333 "
],
"23": [
" ",
"3333333 ",
"3022023 ",
"3022023 ",
"3022023 ",
"3000003 ",
"3222023 ",
"3333333 "
],
"24": [
" ",
"3333333 ",
"3220223 ",
"3200023 ",
"3000003 ",
"3202023 ",
"3200023 ",
"3333333 "
],
"25": [
" ",
"3333333 ",
"3222223 ",
"3222023 ",
"3000003 ",
"3222023 ",
"3222223 ",
"3333333 "
],
"26": [
" ",
"3333333 ",
"3220223 ",
"3222023 ",
"3000003 ",
"3222023 ",
"3220223 ",
"3333333 "
],
"27": [
" ",
"3333333 ",
"3022203 ",
"3022203 ",
"3000003 ",
"3220223 ",
"3220223 ",
"3333333 "
],
"28": [
" ",
"3333333 ",
"3000003 ",
"3222203 ",
"3220003 ",
"3222223 ",
"3202223 ",
"3333333 "
],
"29": [
" ",
"3333333 ",
"3002003 ",
"3022203 ",
"3002003 ",
"3002203 ",
"3002003 ",
"3333333 "
],
"30": [
"2222 ",
"20022 ",
"200022 ",
"2000022 ",
"2000002 ",
"2020022 ",
"2222002 ",
" 2222 "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment