hey again guys. Another quick question. I cant seem to figure out why this is printing out my else statement under the do while statement in main when 1 is selected. If 2 is selected it does not print. Any pointers?!
Code:
#include<stdio.h>
#include<stdlib.h>
int rollDiceOne(int scoreOne)
{
int roll=0;
roll=rand()%6+1;
printf("You rolled a %d\n", roll);
if (roll==1)
{
scoreOne=0;
printf("You lose all your points! Better luck next roll.\n");
}
else
{
scoreOne=roll+scoreOne;
}
return scoreOne;
}
int rollDiceTwo(int scoreTwo)
{
int roll=0;
roll=rand()%6+1;
printf("You rolled a %d\n", roll);
if (roll==1)
{
scoreTwo=0;
printf("You lose all your points! Better luck next roll.\n");
}
else
{
scoreTwo=roll+scoreTwo;
}
return scoreTwo;
}
int main (void)
{
int answer=0;
int roll=0;
int scoreOne=0;
int scoreTwo=0;
int choice=0;
do
{
printf("\n\n\nWhat would you like to do?\n");
printf("Lets play the Game Of Pig!!..........1\n");
printf("What are the rules?..................2\n");
printf("Exit the programs....................3\n\n");
scanf("%d", &answer);
switch(answer)
{
case 1:
printf("Lets get started!");
do
{
printf("\n\n\n_________Player 1_________\nWhat would you like to do?\n");
printf("Select 1 to skip turn and add one point.\nSelcet 2 to roll dice.\n");
scanf("%d", &choice);
if (choice==1)
{
scoreOne=scoreOne+1;
printf("Player one's score is %d\n", scoreOne);
}
if (choice==2)
{
scoreOne=rollDiceOne(scoreOne);
printf("Player one's score is %d\n", scoreOne);
}
else
{
printf("That's not a valid option. Please select 1 or 2.\n");
}
if (scoreOne>50)
{
scoreOne=scoreOne-15;
printf("Sorry! You went over 50! You lose 15 points!\n");
}
if (scoreOne==50)
{
printf("Congratulations!!!! PLAYER 1 WINS!!!\n");
exit(0);
}
printf("\n\n\n_________Player 2_________\nWhat would you like to do?\n");
printf("Select 1 to skip turn and add one point.\nSelcet 2 to roll dice.\n");
scanf("%d", &choice);
if (choice==1)
{
scoreTwo=scoreTwo+1;
printf("Player two's score is %d\n", scoreTwo);
}
if (choice==2)
{
scoreTwo=rollDiceTwo(scoreTwo);
printf("Player two's score is %d\n", scoreTwo);
}
else
{
printf("That's not a valid option. Please select 1 or 2.");
}
if (scoreTwo>50)
{
scoreTwo=scoreTwo-15;
printf("Sorry! You went over 50! You lose 15 points!\n");
}
if (scoreOne==50)
{
printf("Congratulations!!!! PLAYER 2 WINS!!!\n");
exit(0);
}
}while(scoreOne!=50, scoreTwo!=50);
break;
case 2:
printf("\n\n\nThe rules are simple. The goal of the game of pig is to get a score of 50!\n");
printf("Each player will have 2 choices upon their turn.\n");
printf("Choice 1: Skip their turn and automatically gain 1 point\n");
printf("Choice 2: Roll the dice. If you roll a 1 then all of your points are forfit.\n");
printf("\tIf you roll anything else, what ever you roll, that number will be added\n\tto your score\n");
printf("If your score goes over 50, the you lose 15 points and must attempt to get 50 again!\n");
break;
case 3:
exit(0);
break;
default:
answer=-1;
printf("Thats not a valid option! Try again!\n");
break;
}
}
while(answer=-1);
return 0;
}