Hello:
I need help identifying some errors in a program that has to make a dice game for one person.
However, when I am trying to use if statements to explain the user when some moves are invalid, I can't do it because it displays the message explaining why it is wrong, but it doesn't keep going, it gets stuck in the message.
EX. it has to say "......Sum" when the two dice are the same value, or "illegal value" if both dice values have been already used.
This is the code i have now:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// ||
//Display( ) this function displays the game board with column labels indicating the //point value of each position.
void Display(char board[])
{
cout<<" the current game board: "<<endl;
cout<<setw(8);
for (int k=1; k<13; k++)
cout<<setw(4)<<k;
cout<<endl<<setw(8);
for (int i=1; i<13; i++)
cout<<setw(4)<<board[i];
cout<<endl;
}
int main()
{
char choice='s', board[13]; // we make the board //size 13 so we can use 1 to 12
// (otherwise 0 to 11 for size 12)
int dice1, dice2, j, score=0, valid=0;
srand(time(NULL)); //randomize the seed // for generating random numbers
for (j=1;j<=12;j++) //initialize board
board[j]='.';
while(choice != 'q' && choice != 'Q')
{
Display(board);
dice1 = rand()%6+1;
dice2 = rand()%6+1;
cout<<endl<<endl<<"Dice roll: "<<dice1<<" "<<dice2<<endl<<endl;
//first input a choice and check for whether choice is valid.
valid=0;
while(!valid)
{
// prompt the user for a move
cout<<"Choose either D, S, or Q"<<endl;
cout<<"(D = both dice, S = Sum both, Q = quit):";
cin>>choice;
// check all of the various ways choice could be invalid, display
// the appropriate message explaining why it is invalid.
// Otherwise, if the choice is valid, set the valid flag to 1
// e.g : else valid=1;
if (board[dice1]=='X' || board[dice2]=='X' || board[dice1+dice2]=='X')
{cout<<"illegal move "<<endl;
}
else
{ if (dice1==dice2 && (choice!='S') )
{cout<<endl<<"A double "<<dice1<<" means you have to choose sum(S) "<<endl<<endl;
}
else
{valid=1;
}
}
}
//process choice
if ((choice=='s' || choice=='S') && board[dice1+dice2]!='X')
{
board[dice1+dice2]='X';
score=(dice1+dice2)+score;
}
else
{
if ((choice=='d' || choice=='D')&& board[dice1]!='X' && board[dice2]!='X')
{ board[dice1]=board[dice2]='X';
score=(dice1+dice2)+score;
}
}
}
// Tally up remaining points
//I still have to work the points!
cout<<endl<<endl<<"Final Score is "<<score<<endl<<endl;
system("pause");
}
This link takes you to an example on how the teacher wants our program to work!