javascript - Small Straight (Yahtzee) Algorithm -
i have created working javascript function check array of 5 numbers small straight, in yahtzee game i'm making. i've tested no end , i'm confident works 100% of time, worst algorithm of time in terms of being efficient. here looks like:
function calcsmstraight() { var sum = 0; var r = new array(); var r2 = new array(); var counter = 0; var temp; var bool = false; var bool2 = false; r[0] = document.getelementbyid('setkeep1').value; r[1] = document.getelementbyid('setkeep2').value; r[2] = document.getelementbyid('setkeep3').value; r[3] = document.getelementbyid('setkeep4').value; r[4] = document.getelementbyid('setkeep5').value; // move non-duplicates new array r2[0] = r[0]; for(var i=0; i<r.length; i++) { for(var j=0; j<r2.length; j++) { if(r[i] == r2[j]) { bool2 = true; // in new list } } // add new list if not in if(!bool2) { r2.push(r[i]); } bool2 = false; } // make sure list has @ least 4 different numbers if(r2.length >= 4) { // sort dice least greatest while(counter < r2.length) { if(r2[counter] > r2[counter+1]) { temp = r2[counter]; r2[counter] = r2[counter+1]; r2[counter+1] = temp; counter = 0; } else { counter++; } } // check if dice in order if(((r2[0] == (r2[1]-1)) && (r2[1] == (r2[2]-1)) && (r2[2] == (r2[3]-1))) || ((r2[1] == (r2[2]-1)) && (r2[2] == (r2[3]-1)) && (r2[3] == (r2[4]-1)))) { bool = true; } } if(bool) { // if small straight give 30 points sum = 30; } return sum; }
my strategy to:
1) remove duplicates adding numbers new array occur 2) make sure new array @ least 4 in length (4 different numbers) 3) sort array least greatest 4) check if first 4 or last 4 (if 5 in length) numbers in order
my question:
does know way can improve method? seems ridiculously terrible me can't think of better way , @ least works.
given you're implementing yahtzee game presumably need test other patterns beyond small straights, better create array of values before calling function can use them in tests, rather getting values dom elements inside small straight test.
anyway, here's first way came mind test small straight within array representing values of 5 six-sided dice:
// assume r array values dice r.sort(); if (/1234|2345|3456/.test(r.join("").replace(/(.)\1/,"$1") { // small straight }
note can sort array of numbers using code:
r2.sort(function(a,b){return a-b;});
...but in case values in array strings because came .value
attribute of dom elements, default string sort work r2.sort()
. either way don't need own sort routine, because javascript provides one.
edit: if assume can put 5 values string above can implement tests possible combinations big if/else this:
r.sort(); r = r.join(""); if (/(.)\1{4}/.test(r)) { alert("five of kind"); } else if (/(.)\1{3}/.test(r)) { alert("four of kind"); } else if (/(.)\1{2}(.)\2|(.)\3(.)\4{2}/.test(r)) { alert("full house"); } else if (/(.)\1{2}/.test(r)) { alert("three of kind"); } else if (/1234|2345|3456/.test( r.replace(/(.)\1/,"$1") ) { alert("small straight"); } // etc.
Comments
Post a Comment