From a8c0d5619ee6d518737628d6c585233d0bba23da Mon Sep 17 00:00:00 2001 From: louie Date: Sun, 20 Sep 2020 09:26:58 -0700 Subject: First commit --- free.js | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 free.js (limited to 'free.js') diff --git a/free.js b/free.js new file mode 100644 index 0000000..a0f0bc6 --- /dev/null +++ b/free.js @@ -0,0 +1,223 @@ +var dice = { + + // game ends when round = 13 (assuming yahtzee is not achieved) + scoring:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + scoring_set: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + vals:[0,0,0,0,0], + holds:[0,0,0,0,0], + roll:-1, + round:0, + fhoak:0, //for use in determining full house - keep + finish:false, //will be true when the game is over - keep + + hold_func:function(n){ //called when a dice is clicked on - keep + dice.holds[n] = !dice.holds[n]; + document.getElementById(`hold${n}`).innerHTML= (dice.holds[n]? "
HOLD
":' ') + }, + rand:function(){ //picks a random dice value - keep + return Math.ceil(Math.random()*6); + }, + + oak: function(num_of_kind, repeat = 0){ //determines x of a kind scoring (including yahtzee) -keep + let ret = 0; + let count; + for(let n = 1; n <= 6; n++){ //each possible dice value is checked + if(n !== repeat){ + count = 0; + for(let i = 0; i < 5; i++){ //how many of this val do I have? lets look at each dice... + if(dice.vals[i] == n) count++; + } + } + if(count >= num_of_kind) { + dice.fhoak = n; + for(let i = 0; i < 5; i++){ + ret += dice.vals[i]; + } + } + } + return ret; + }, + + full_house: function(){ //determines full house scoring (using x of a kind and dice.fhoak) - keep + return (dice.oak(3) ? (dice.oak(2, dice.fhoak) ? 25:0) : 0); + }, + + straight: function(size){ //determines small and large straight scoring - keep + let str = ''; + let count; + let p = 0; + for(let val in dice.vals){ + str += dice.vals[val]; + } + for(let a = size; a <= 6; a++){ + count = 0; + for(let b = 1+(a-size); b <= a; b++){ + if(str.includes(b)){ + count++; + } + } + if(count >= size){ + p = 30+(size-4)*10; + break; + } + } + return p; + }, + + chance:function(){ //determines chance scoring - keep + let sum = 0; + for(let val in dice.vals){ + sum += dice.vals[val]; + } + return sum; + }, + + final_scoring:function(){ //called when dice.finish is set to true to determine the final score values - keep, toChange + for(let d = 0; d < 6; d++){ + dice.scoring[6] += dice.scoring[d]; + } + + dice.scoring[7] = (dice.scoring[6] >= 63 ? 35 : 0); + dice.scoring[8] = dice.scoring[6] + dice.scoring[7]; + + if(dice.scoring[16] == '✓'){ + dice.scoring[17] = 100; + } + if(dice.scoring[16] == '✗'){ + dice.scoring[17] = 0; + } + + dice.scoring[18] = dice.scoring[8]; + for(let d = 9; d < 16; d++){ + dice.scoring[19] += dice.scoring[d]; + } + dice.scoring[19] += dice.scoring[17] + dice.scoring[20] = dice.scoring[18] + dice.scoring[19]; + + dice.finish = true; + }, + + score:function(element){ + dice.roll = 3; + + let calc_arr = [dice.oak(3), dice.oak(4), dice.full_house(), dice.straight(4), dice.straight(5), (dice.oak(5) ? 50:0), dice.chance(), (dice.oak(5) ? '✓':'✗')]; + + dice.scoring_set[element-2] = true; + if(element <= 7){ //determine scores each upper round + for(let d = 0; d < 5; d++){ + if(dice.vals[d] == element - 1){ + dice.scoring[element-2] += dice.vals[d]; + } + } + } + + if(element > 7){ //determines scores for each lower round + dice.scoring[element-2] = calc_arr[element-11]; + } + + if(dice.round == (12 + dice.scoring[14]/50)){ + dice.final_scoring(); + } + + for(let d = 0; d < 5; d++){ //reset holds at the start of each round + dice.holds[d] = 0; + } + }, + + render_dice:function(dont_shuffle){ //main function called each time roll or submit is clicked on -keep, toChange + + (dice.roll == 2 ? document.getElementById('roll_display_free').style.display = 'none' : document.getElementById('roll_display_free').style.display = 'unset'); + + update(); //keep + let out = ''; + for(let i = 0; i < 5; i++){ //keep + dice.vals[i] = dice.holds[i] || dont_shuffle ? dice.vals[i] : dice.rand(); + out += + ` + +
` + (dice.holds[i]? "
HOLD
":' ') + `
+
`; + } + + document.getElementById('full_scoreboard').innerHTML = render_scoreboard(); //keep + + if(!dice.finish){ //renders the main playfield - keep + document.getElementsByClassName('dice_display')[0].innerHTML = out; + document.getElementById('roll_count').innerHTML = "Roll " + (dice.roll ? `#${dice.roll}` : 'the dice!'); + document.getElementById('roll_count').style.marginLeft = (dice.roll ? '120px' : '67.5px'); + //console.log(dice.scoring); + } + else{ //renders the game over screen if dice.finish is true - keep + document.getElementById('grid_element8').insertAdjacentHTML('beforeend', `${dice.scoring[6]}`); + document.getElementById('grid_element9').insertAdjacentHTML('beforeend', `${dice.scoring[7]}`); + document.getElementById('grid_element10').insertAdjacentHTML('beforeend', `${dice.scoring[8]}`); + document.getElementById('grid_element19').insertAdjacentHTML('beforeend', `${dice.scoring[17]}`); + document.getElementById('grid_element20').insertAdjacentHTML('beforeend', `${dice.scoring[18]}`); + document.getElementById('grid_element21').insertAdjacentHTML('beforeend', `${dice.scoring[19]}`); + document.getElementById('grid_element22').insertAdjacentHTML('beforeend', `${dice.scoring[20]}`); + document.getElementsByClassName('dice_display')[0].setAttribute('style', 'display:none'); + document.getElementById('submit_line').setAttribute('style', 'display:none'); + document.getElementsByClassName('top_pad_free')[0].innerHTML = + ` +
Thanks for Playing!
+
Your Final Score: ${dice.scoring[20]}
+ + Click Here to Play Again + + + Click Here to Go Back + +
`; + } + } +} + +function make_head(header){ //creates the header - keep + return ` ${header} `; +} + +function update(){ //increments dice.roll each time in dice.render - keep, toChange + if(dice.roll < 3){ + dice.roll++ + } + else { + dice.roll = 0; + dice.round++; + } + console.log(dice.round); +} + +function render_scoreboard(){ //script to create the scoreboard layout - keep + let round_val = + ['UPPER SECTION', 'Aces', 'Twos', 'Threes', 'Fours', 'Fives', 'Sixes', 'TOTAL SCORE', 'BONUS', 'TOTAL', + '3 of a Kind', '4 of a Kind', 'Full House', 'Small Straight', 'Large Straight', 'YAHTZEE', 'Chance', + 'YAHTZEE BONUS ✓s', 'YAHTZEE BONUS scoring', 'TOTAL UPPER', 'TOTAL LOWER', 'GRAND TOTAL']; + let clickables = [2,3,4,5,6,7,11,12,13,14,15,16,17]; + if(dice.scoring[14] > 0){ + clickables.push(18); + } + let out = ''; + + for(let a=1; a <= 2; a++){ + if(a == 2){ + out += "
LOWER SECTION
\n"; + } + out += `
\n`; + + for(let i=1; i <= (8+a*2); i++){ + out += `
${round_val[(i-1)+(a-1)*10]}`; + out += (dice.scoring_set[(i + (a-1)*10) - 2] ? `${dice.scoring[(i + (a-1)*10) - 2]}` : '') + `
\n`; + } + out += '
'; + } + return out; +} + +document.getElementsByClassName('shifted_head')[0].innerHTML = make_head("Now Playing: Free Mode"); //keep +dice.render_dice(); //keep +document.getElementById('full_scoreboard').innerHTML = render_scoreboard(); //keep \ No newline at end of file -- cgit