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
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]
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
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?");
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);
(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;
}
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 winscounter = 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.
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.