Hello I'm currently taking C++, and I'm a beginner. We had a choice to choose a dice game for our program. I chose a game that has the following rules:
Choose a number and roll the dice. You score a point every time you roll that number. When you roll that number, you get another turn. When that number is not rolled, the turn is over. Mark the tally for each time you roll the number. First one to a certain score of 10 points wins.
I've been working on this for the past 2 days, and I'm so frustrated. Any help is greatly appreciated. We're using classes and constructors. My main issue is being able to go back and forth between the two players. I tried using a do while loop, but it didn't really help. Here's my code:
//Dice game
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Die
{
private:
int num;
public:
Die(); //Default constructor because it doesn't require arguments
void roll();
int getNum();
void gameRules();
};
class Players
{
private:
int player1Num;
int player2Num;
public:
void playerTurn();
};
void Die::gameRules()
{
cout << " ****Welocome to Madawi's Dice Game ****\n\n ";
cout << "Here are the rules:\n\n ";
cout << "This is a two player game, so grab a buddy!\n\n ";
cout << "\t1.)Please choose a number from 1-6\n\n ";
cout << "\t2.)Then roll the dice, if it lands on the number";
cout << "\tyou chose, you get a point and go again\n\n ";
cout << "\t3.)If you choose a number and it doesn't land on";
cout << "\tthe number you chose,you don't recieve a point, ";
cout << "\tand the second player goes\n\n ";
cout << "\t4.)The first person to get to 5 points first wins!\n\n ";
cout << "ENJOY!!! Let's begin:\n\n ";
}
Die::Die()
{
num = 1; //Initialize so that the values start at one
srand((unsigned)time(NULL));
}
void Die::roll()
{
num = rand()%6 + 1;
}
int Die::getNum()
{
return num;
}
void Players::playerTurn()
{
Die die1;
cout << "Hello player 1! Please choose a number from 1-6:\n";
cin >> player1Num;
cout << "You've chosen the number " << player1Num << endl;
die1.roll(); //rolls the dice
cout << die1.getNum() << endl; //displays number rolled
if (player1Num == die1.getNum())
{
cout << "Good job player 1! You got the same number\n ";
player1Points++; //Keeps track of player 1's score
if(player1Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
else
{
cout << "Sorry the numbers didn't match up\n ";
cout << "Player 2 its your turn\n ";
cout << "Player 2 please choose a number ";
cin >> player2Num;
cout << "You've chosen the number " << player2Num << endl;
die1.roll();
cout << die1.getNum() << endl;
if(player2Num == die1.getNum())
{
cout << "Good job player 2! You got the same number\n ";
player2Points++; //Keeps track of player 2's points
cout << "Player 2 its your turn again, please choose a number:\n ";
cin >> player2Num;
die1.roll();
cout << die1.getNum() << endl;
}
if(player2Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
}
int main()
{
Die dice1;
Players player1;
Players player2;
dice1.gameRules(); //Says the game rules
player1.playerTurn(); //Player makes a selection
return 0;
}
Aucun commentaire:
Enregistrer un commentaire