This site requires JavaScript, please enable it in your browser!
Greenfoot back
BlueberryGaming
BlueberryGaming wrote ...

2018/11/14

yahtzee code

BlueberryGaming BlueberryGaming

2018/11/14

#
i am trying to figure out in a case statement/switch statement how to make large straight and small straight we have the die value named as die Value and roll total
danpost danpost

2018/11/14

#
BlueberryGaming wrote...
i am trying to figure out in a case statement/switch statement how to make large straight and small straight we have the die value named as die Value and roll total
Use an array for the individual die values:
int[] roll = new int[5];
then sort them in ascending order and iterate thru them to find the straights:
/** sorting */
for (int i=1; i<5; i++)
{
    int n = i;
    while (n > 0) if (roll[n] > roll[--n]) swap(n+1, n);
}

/** straight checking */
int run= 1;
for (int i=1; i>5; i++) run = roll[i] == roll[i-1]+1 ? run+1 : (run < 4 ? 1 : run);
if (run > 3) // has straight of length run
You can either create a method to swap the entries in the array or simply replace the call to swap in line 5 with a block of code that does the swap right there.
You need to login to post a reply.