Programming Help. Dice game.

Hey guys. Im new to this forum, and pretty new to the programming world. I have to write a dice game program, that the user and computer roll a di. If the user wins a green led lights up, if computer wins led red lights up, if there is a tie both light up. We need to ask a the user if they want to play (1 for yes and 0 for no), and then the program starts. This is all displayed on the serial monitor.

Here is the teachers pseudo code:

Program Steps (pseudocode):
Intro block
In the setup () function:
Initialize serial communication
Seed the random number generator
Configure 2 digital pins as output – ensure that these are the same pins that each LED is attached to (eg. Pin 9 for green LED and pin 11 for red LED).
Ensure that the LEDs are off
Explain program to user

In the loop function
Declare integer variables for two dice (one for the user and one for the computer)
Ask the user if he/she wants to play. This involves reading in a character/number from the user (and therefore a variable).
If the user says yes – you can do this by asking the user to enter ‘1’ or ‘y’:
“Roll” the dice – remember you will have to do this twice, once for the user and once for the computer
Inform the user of each dice value
If the computer dice value is greater than the user dice value, switch on the red LED. Otherwise switch on the green LED.
If it is a tie, switch on both LEDs
If the user says no – by entering ‘0’ or ‘n’
Make sure that both LEDs are turned off

Thanks for any help!!

JohnsonTyler_Lab3DiceGame_EGR106_15.ino (5.18 KB)

  int redPin = 11;                         /*Initailizing the 13th pin (LED pin), and a storage space with 4 bytes
                                             of memory available.*/
                                        
  int greenPin = 9;                       //Initailizing the 12th pin, and a storage space with 4 bytes
                                           //of memory available.


  int response = 0;




  void setup()                            //Setup for the initialized pin
  {
    Serial.begin(9600);
   
    pinMode(redPin, OUTPUT);              //Declaring pin 13 as an output
    pinMode (greenPin, OUTPUT);            //Declaring pin 12 as an input
    
    randomSeed(analogRead(0));
    
    
    Serial.println( "Lets play a game of Dice.");
    
    Serial.println("");
    

    Serial.read();
    Serial.println("Press 1 to play a round of dice or 0 to quit.");
    
  }//End of setup




  void loop()                                                   //Never ending loop, with no return value
  {
    delay(2000);
    Serial.println("");                                         //Prints 2 lines for asthetics
    Serial.println("");
    
    int computerDi;                                            //Initializing computerDie & storing it
    int userDi;                                                //Initializing userDie & storing it
   

    response = Serial.parseInt();
    
    while ( response == 1 )
    {
     digitalWrite(redPin, LOW);
     delay(1000);
     digitalWrite(greenPin, LOW);
     delay(1000);
     
      computerDi = random(1, 7);
      userDi = random(1,7);
      
      Serial.println( "You rolled a:  ");
      Serial.print(userDi);
      
      Serial.println("Computer rolled a:  ");
      Serial.print(computerDi);
  
      if ( computerDi >  userDi)
      {
         digitalWrite (redPin, HIGH);
         delay (2000);
         

         Serial.println("You lose! Enter 1 to play a round of dice or 0 to quit.");
         
         break;
         
       }//End if
       
       
  
       else if ( userDi > computerDi)
       {
         digitalWrite (greenPin, HIGH);
         delay (2000);
         

         Serial.println("You WIN!!! Enter Y 1 to play a round of dice or 0 to quit.");
         
         break;
       
       }//End if
       
       
 
       else
       {
         digitalWrite (redPin, HIGH);  digitalWrite (greenPin, HIGH);
         delay (2000);
         

         Serial.println("Tied!! Enter 1 to play a round of dice or 0 to quit.");
         
         break;
       }//End else
      response++;
      }//End if
      
      
      
      while ( response ==  0)
      {
        digitalWrite(greenPin, LOW);
        delay(1000);
        digitalWrite(redPin, LOW);
        delay(1000);
        Serial.println("You chose quit (0)");
        
       break;
      response++;
      }//End while
      
      
      
      while ( response !=  0 && response !=   1)
      {
        digitalWrite(greenPin, LOW);
        delay(1000);
        digitalWrite(redPin, LOW);
        delay(1000);
        Serial.println("Error. Please type 1 to play or 0 to quit.");
        
        break;
       response++;
      }//End while
    
     
     Serial.println("");
     Serial.println("");
     Serial.println("");

     Serial.read();
     Serial.println("Would you like to play again? 1 = YES, 0 = NO");
     delay(3000);
     response = Serial.parseInt();
     response++;
  }//End loop
[code]

[/code]

well, you have to wait for an answer then... yes?

take a look at this example you can run... resistor in series calculator:

notice the use of state variable and the if, else if statements...

long R1, R2;
float result;
boolean printed;
int state;

void setup()  
{
  Serial.begin(9600);
}
void loop()  
{
  if (state == 0)
  {
    if (!printed) Serial.println(F("What is the value of R1 in ohms?"));
    printed = true;
    if (Serial.available())
    {
      R1 = Serial.parseInt();
      Serial.print(F("R1 = "));
      Serial.print(R1);
      Serial.println(F(" Ohms"));
      printed = false;
      state = 1;
    }
  }
  else if (state == 1)
  {
    if (!printed) Serial.println(F("What is the value of R2 in ohms?"));
    printed = true;
    if (Serial.available())
    {
      R2 = Serial.parseInt();
      Serial.print(F("R2 = "));
      Serial.print(R2);
      Serial.println(F(" Ohms"));
      printed = false;
      state = 2;
    }
  }
  else if (state == 2)
  {
    result =  (float) R1 * R2 / ( R1 + R2 );
    Serial.print(F("Resistor Value = "));
    Serial.print(round(result));
    Serial.println(" Ohms");
    state = 0;
  }
}

Im using the arduino uno board btw. I'm not to concerned about the wiring part. Just the code as it's not running right.

Thanks guys. Your help is much appreciated

Thanks bulldogLowell.
I am not sure if I can use Serial.print(F()); We haven't covered it in class yet. And when my program suppose to ask for 1 or 0 to continue, it's not. Ive tried manipulating the code tons of ways. Im not sure what to do. Thanks

Remove all the unnecessary white space that is a major distraction, and re-post your code.

tjohnson10:
Thanks bulldogLowell.
I am not sure if I can use Serial.print(F()); We haven't covered it in class yet. And when my program suppose to ask for 1 or 0 to continue, it's not. Ive tried manipulating the code tons of ways. Im not sure what to do. Thanks

hopefully you won't get punished for learning on your own!!!

you can easily just take out the F() macro

this

if (!printed) Serial.println(F("What is the value of R1 in ohms?"));

will instead be this:

if (!printed) Serial.println("What is the value of R1 in ohms?");
  int redPin = 11;                         /*Initailizing the 13th pin (LED pin), and a storage space with 4 bytes
                                             of memory available.*/

That code does NOTHING like what the comment says.

Im sorry about the comments. It was from my past assignment and I just copied my last code for a start and didn't change the comments

Hello,

Now add a cheat so that the user automatically wins after 3 straight losses.

I have to add this into my program but can not figure out where to even begin. If anyone can help that would be excellent

Do you have an arduino to upload the sketch to and two different color LEDs? If so, run the sketch and use the serial monitor as the interface.

Here is what I have so far, but this is what i have to add to the program.
Code enhancement: Make your code a tad unethical! So, if the user loses three rounds in a row, force the code
to ensure that the user wins the next round. You will need to use loops for this.

int LEDRED = 11;
int LEDGREEN = 9;
void setup ()
{
Serial.begin(9600);
randomSeed(analogRead(0)); //Seed the random number generator
pinMode(LEDGREEN, OUTPUT); // Configure green LED
pinMode(LEDRED,OUTPUT); // Configure red LED
digitalWrite(9, LOW); // Making sure the green LED is off
digitalWrite(11, LOW); // Making sure the red LED is off
Serial.println("Welcome to the dice game!");
}
void loop()
{
int answer;
int user;
int computer;

Serial.print('\n');
Serial.print("Want to play? (1 = yes, 2 = no)");
while(Serial.available() <=0); //Waiting while the user inputs vaule
answer = Serial.parseInt(); //Recieves the vaule from the user

if(answer == 1) //If the user inputs 1
{
user = random(1,7);
computer = random(1,7);
Serial.print('\n');
Serial.println("The user rolls a");
Serial.println(user);
Serial.println("The computer rolls a");
Serial.println(computer);

if(user > computer)
{

Serial.println("You win!!!!");
digitalWrite(LEDGREEN, HIGH);
delay(3000);
}

else if(user < computer)
{
Serial.println("You loose, Computer wins!");
digitalWrite(LEDRED, HIGH);
delay(3000);
}
else if(user ==computer)
{
Serial.println("It's a tie!");
digitalWrite(LEDRED, HIGH);
digitalWrite(LEDGREEN, HIGH);
delay(3000);
}
}
else if(answer == 2)
{
Serial.print('\n');
Serial.print("You've hurt my feelings, I really wanted to play :/");
}

digitalWrite(9, LOW); // Making sure the green LED is off
digitalWrite(11, LOW); // Making sure the red LED is off

}

Please look at number 6, bullet point 6 of first post in this thread. Read this before posting a programming question

Where is the counter that keeps track of the number of times the user has lost?

I do not have a counter. I am not that familiar with those. How would I use one?

(face palm)
Do you know how to increment a variable (byte) by 1, and set it back to zero? If so you can use this to make a simple counter.

if( /condition/) // what condition would you put in here to see if the user has lost?
{
//conditions is true
counter++;
}
else // user has won, reset the counter to zero
{
//condition is false
counter = 0;
}

Now when I place that in my code where would I place it? I would want it before the void loop, so I am thinking in the void setup?

eisenbr:
Now when I place that in my code where would I place it? I would want it before the void loop, so I am thinking in the void setup?

It's practically already in your code, you just need to figure out where that is. (Hint: When the computer wins, counter++, when the user wins counter = 0.)

All you need to do after is have an if statement that sees if the user has lost 3 times and then on the next game, the user wins.

so I am thinking in the void setup?

A whole lot of good that will do for you. :wink:

Okay I think I get where your coming from, at least it gives me something to think about rather than sitting here stuck. I am very new to programming starting only 3 weeks ago. I appreciate all the help if i have any more questions ill be sure to post them.