Is it possible to manipulate arrays like this?

I have to make an Arduino program, that simulates throwing with 5 dices. (for a yahtzee game).

int five_dices[5]; 

void setup() 
{
Serial.begin (9600);
randomSeed(analogRead(0));

Serial.println ("Throw 5 dices");

for(int i = 0; i < 5; i++)
{
five_dices *= random (1,7);*
<em> Serial.println (five_dices *);*</em>
_* }*_
_*delay (2500);*_
_*Serial.println ("Choose dice(s) to hold");*_
_*}

*_
This is what i understand, i simply cannot understand how i would "hold" som of the dices, or in other words hold some of the arrays variables, and rethrow (randomize) the rest.
Hope some one can guide me, in the rigth direction with this. :slight_smile:

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup (for example, the italics in your code above), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar, then you can manually add the code tags:

```
// your code is here
```

Using code tags and other important information is explained in the How to use this forum post. Please read it.

1 Like

Each element of an array is indexed, so to store something in the first element at index 0, write

five_dices[0]=some_value;

or in the "i"th element (i < 5)

five_dices[i]=some_value;
1 Like

Yes, but it's a bit complicated...

int
    HeldDice[5];
int 
    five_die[5]; 

void setup() 
{
    Serial.begin (9600);
    while(!Serial);
    
    randomSeed(analogRead(0));
    memset( HeldDice, 0, sizeof( HeldDice ) );
    
}//setup

void loop()
{
    int
        cnt,
        i;

    cnt=0;
    for( i=0; i<5; i++ )
        if( HeldDice[i] == 0 )
            cnt++;
            
    Serial.print( "\nThrowing " ); Serial.print( cnt ); Serial.println( " die..." );
    
    for( int i=0; i<5; i++ )
    {
        if( HeldDice[i] == 0 )
            five_die[i] = random (1,7);     
    }//for

    //print roll
    Serial.print( "\nDie roll: " );
    for( i=0; i<5; i++ )
    {
        Serial.print( five_die[i] );
        if( i<4 ) 
            Serial.print( " - " );    
    }//for
    
    ChooseDiceToHold();

    Serial.println( "\nPress any key to roll again..." );
    
    //wait for a keypress, then flush...
    while( Serial.available() == 0 );
    while( Serial.available() > 0 )
        Serial.read();

}//loop

#define MAX_CHARS   10

void ChooseDiceToHold( void )
{
    long
        die_num;
    bool
        die_done = false,
        str_done = false;
    byte
        str_index,
        die_index;
    char
        stringRxed[MAX_CHARS];
    int
        char_rxd;

    memset( HeldDice, 0, sizeof( HeldDice ) );
    Serial.println( "\nEnter dice to hold. Press enter on a blank line to finish.\n" );
    
    die_index = 0;
    do
    {
        memset( stringRxed, 0, MAX_CHARS );
        str_index = 0;
        str_done = false;
        do
        {
            if( Serial.available() > 0 )
            {
                char_rxd = Serial.read();
                if( char_rxd == 0x0a )
                {
                    stringRxed[str_index] = 0;  //NULL terminator
                    str_done = true;
                }
                else if( str_index < MAX_CHARS-1 )
                {
                    stringRxed[str_index++] = char_rxd;
                    
                }//else
                             
            }//if
                
        }while( str_done == false );

        die_num = atoi(stringRxed);
        if( die_num == 0 )
        {
            die_done = true;
        }//if
        else if( die_num <= 5 )
        {
            Serial.print( "\nHolding " ); Serial.print( die_num );
            HeldDice[die_num-1] = die_num;
            
        }//elif
        else
        {
            Serial.println( "\n  *** Error. Choose a value between 1 and 5 or 0 to finish. ***" );
        
        }//else

        //flush serial buffer
        while( Serial.available() > 0 )
            Serial.read();
    
    }while( die_done == false );

}//ChooseDiceToHold

A simpler system for user input might be to get the user to enter a string with all the dice that s/he wants to hold. For example "145". Then that can be parsed by iterating over the received array and subtracting '0' from each value to get the index of the HOLD array. Just ignore values that are out of range or values after the 6th array element - for example if the user entered 183444455 the 8 and the 5s would have no effect (foolish user).

...R

Robin2:
A simpler system for user input might be to get the user to enter a string with all the dice that s/he wants to hold. For example "145". Then that can be parsed by iterating over the received array and subtracting '0' from each value to get the index of the HOLD array. Just ignore values that are out of range or values after the 6th array element - for example if the user entered 183444455 the 8 and the 5s would have no effect (foolish user).

...R

For sure. My way is kinda agricultural. Would be a good exercise for the OP to make it better using a method like yours.