General game in Java

Asked

Viewed 114 times

0

Good afternoon guys, I’m doing the General’s game (java dice). I am in doubt on how to compare the data between them.

Sequel Possible sequences: 1-2-3-4-5 / 2-3-4-5-6 / 3-4-5-6-1 Number of points: 20

Full-Hand The number x in 3 data and the number y in the remaining two (for x different from y) Number of points: ( 3 * x + 2 * y ) + 30

Court The number x in four different data Number of points: ( 4 * x ) + 40

General The number x in the five dice Number of points: ( 5 * x ) + 50

Simple Combinations For each number from 1 to 6 the player can score points as in the example: Ex: If the player has the following sequence of numbers: 2 3 2 2 5

So far I’ve been able to take the test to see if you’re a general:

 public int VerificarDados()
{
    int aux=1;
    int q=0,u;
    for(q=0;q<5;q++)
    {
        if(dados[q]==dados[q+1])
        {
            aux++;
        }
    }
    numRept=dados[1];
    if(aux==5)
    {
        return 5; //general
    }

Could someone give me some hint how to make the other comparisons? Thanks in advance!

1 answer

1


Store in an array of 6 elements the number of occurrences of each given (example: if three numbers 1 came out, store in the first house of the array the value 3; if four numbers 6 came out, store in the last house of the array the value 4).

Then you go through the array in search of each condition you want (look for the occurrence of 5 numbers, if you find it is a General; look for the occurrence of 3 numbers and the occurrence of 2 numbers, if you find both is Full-hand, otherwise it is a simple combination; and so on.

  • Very good tip! Thanks @Piovezan

  • Problem is that this giving exceeded limit on the vector, I put the code there in the chat if you can help me. java.lang.Arrayindexoutofboundsexception

  • Remember that an array that has for example 5 elements will have indexes from 0 to 4. In the code you posted for example you should not do what is up to q < 5 but up to q < 4 because the data[q+1] will exceed the size of the array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.