Okay everyone. I have run into another bit of trouble. I have the code, and while it is working...its only doing one repetition. Even after I press one of the options R, P, or S (rock, paper, scissors) and then press ESC it will not quit. Here is what I have and underneath that is the printout of what it is doing. Any ideas would be great. I tried using a continue statement, but it's not working with that either.
/*
Rock, Paper, Scissors
Asgn4-1 Ross
2013
CISP360
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> //this is where rand is located
#include <time.h> // contains the time structure
#pragma warning (disable:4996) //stops the warnings for scanf
void main()
{
char ch1; //user input
char ch2; //puter input
char chok; //flag
time_t t; //time structure
//seed rand # generator
srand((unsigned) time(&t));
printf("Let's play Rock, Paper, Scissors!\n May the best player win!\n"); //ouputs to the screen
printf("Enter R, P, or S. Press ESC to end the game.\n\n\n"); //outputs instructions to the game
printf("Player Computer Winner\n");
printf("------------------------------------\n");
//main loop
do{
//gets player's move
ch1 = getch();
switch(ch1)
{
case 'R':
case 'r':
case 'P':
case 'p':
case 'S':
case 's':
case 27: //ESC
chok = 1;
break;
default:
chok = 0;
}
}while(!chok); //input validation loop
//convert to upper case
if(ch1 > 'Z') ch1 -= ' ';
/*
ASCII value for a space (' ') is 32.
Integer representations of lower case chars are 32 higher than those of their equivalent upper case chars.
So if you subtract 32 from a lower case char it will become an upper case char.
*/
//gets computer's move
ch2 = rand() % 3; // code in C to get a random #
switch(ch2)
{
case 0:
ch2 = 'R';
break;
case 1:
ch2 = 'P';
break;
case 2:
ch2 = 'S';
break;
}
//checks who won
if(ch1 == 'R' && ch2 == 'R')
printf("Rock Rock Draw\n");
if(ch1 == 'R' && ch2 == 'P')
printf("Rock Paper Computer\n");
if(ch1 == 'R' && ch2 == 'S')
printf("Rock Scissors Player\n");
if(ch1 == 'P' && ch2 == 'R')
printf("Paper Rock Player\n");
if(ch1 == 'P' && ch2 == 'P')
printf("Paper Paper Draw\n");
if(ch1 == 'P' && ch2 == 'S')
printf("Paper Scissors Computer\n");
if(ch1 == 'S' && ch2 == 'R')
printf("Scissors Rock Computer\n");
if(ch1 == 'S' && ch2 == 'P')
printf("Scissors Paper Player\n");
if(ch1 == 'S' && ch2 == 'S')
printf("Scissors Scissors Draw\n");
while(ch1 != 27);
}
and here is what it is doing:
Let's play Rock, Paper, Scissors! May the best player win! Enter R, P, or S. Press ESC to end the game.
Player Computer Winner ------------------------------------ Paper Paper Draw
Question
Terabojin
Okay everyone. I have run into another bit of trouble. I have the code, and while it is working...its only doing one repetition. Even after I press one of the options R, P, or S (rock, paper, scissors) and then press ESC it will not quit. Here is what I have and underneath that is the printout of what it is doing. Any ideas would be great. I tried using a continue statement, but it's not working with that either.
and here is what it is doing:
Let's play Rock, Paper, Scissors!
May the best player win!
Enter R, P, or S. Press ESC to end the game.
Player Computer Winner
------------------------------------
Paper Paper Draw
Link to comment
Share on other sites
8 answers to this question
Recommended Posts