Associating random points values to pins in a game

I am using a mega processor and infra red break beam sensors. I have developed a game based on the carnival clown moving head game. The player drops a ball down a head that moves side to side. The ball drops onto a board that has eight slots. Each slot has a break beam sensor. When a ball moves past the sensor it updates the score on a display. A standard game is where the points values of the slots does not change. I have done this and the game works well. I want to now give the player the option of playing the game where the values of the slots are given random values within a certain range for example the points values are 100, 5, 20, 10, 200. So for example slot one could be 5 or 20 in game 1 but when played again slot one could be 100.

int gameScore = 0;//players score
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29};

Do I use the randomseed generator and some sort of array that holds the points values?

[quote author=roadshark link=msg=4331148 date=1570618334Do I use the randomseed generator and some sort of array that holds the points values?
[/quote]

Sounds like a reasonable way to do it.

Ok so is this on the right track?

int mySensVals[] = {100, 5, 200, 20, 10, 100, 5, 20};//Values for the eight pins
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29};// The pins the sensors are attaced to.
long randNumber;


void setup() {
  // put your setup code here, to run once:
 randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO 
}

void loop() {
  // put your main code here, to run repeatedly:
randNumber = random(10, 20);//Generates a number between 10 and 19
}

Now when the a random number is generated this is what needs to happen.
If 10 is generated then pin 22 when activated will return a value of 100.
If 12 is generated then pin 22 when activated will return a value of 10.
etc.

Any guidance appreciated :slight_smile:

I would be inclined to use the Random function to populate the mySensVals array. Either one time in setup so that all games since power on would use the same scores, or once per game.

Looks like you need an array called something like possibleValues[] with your set values 100, 5, 200, 20, 10, 100, 5, 20.

For a regular game, use that array to populate mySensVals[]

Then for the "random game" use something like

for( int i = 0; i < 8; i++){
  mySensVals[i] = possibleValues[random(0, 7)];
}

Keep in mind, just using this as it is can POSSIBLY cause all scores to be the same. It's unlikely, but possible.

Wildbill types faster than I do!

Thx for the advice. Here is what I have come up with your assistance:)
In this scenario a player has pressed the button associated with pin 28. I have put the random number generator in case three as I am assuming if it was in case four it would continually change the values of the numbers each time through the loop. So when pressed the player score would be updated with the value from the array. Is my thinking correct here?

int mySensVals[] = {100, 5, 200, 20, 10, 100, 5, 20};//Values for the eight pins normal game.
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29};// The pins the sensors are attaced to.
int sensorPinsCount = 11;
int possibleValues[] = {100, 5, 200, 20, 10, 100, 5, 20};//Values for a random game.
long randNumber;
int x = 0;
int state = 0;
int gameScore = 0;


void setup() {
  // put your setup code here, to run once:
  for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
}

void loop() {
  switch (state)

  {
    case 0:

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        state = 1; //Move to state 1 Game has no sound effects and does not show the score (kids have to add manually)but keeps track of score so as to increase motor speed when score reaches 100
      }
      if (digitalRead (23) == LOW) {
        gameScore = 0;
        state = 2;//Move to state 2 Game has full function with set points for sensors.
      }
      if (digitalRead (24) == LOW) {
        gameScore = 0;
        state = 3; //Move to state 3 Game has full function but points for sensors set randomly.
      }
      break;
    case 3:

      randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
      for ( int i = 0; i < 8; i++) {
        mySensVals[i] = possibleValues[random(0, 7)];
        state = 4;
      }
      break;
    case 4:
      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        x = mySensVals[4];
        gameScore += x;

      }
  }
}