I'm trying to make a Yahtzee game but I'm stuck on displaying the dice and showing which dice the player wants to keep. I'm using classes to create this part but I'm not used to how they work well enough and I need some help as to how I should fix this.
This is the code in question with the classes I'm trying to build.
const int HAND_SIZE = 5;
class Hand {
public:
//Display the value of the five dice
void show();
void play();
Dice* getDice(int diceNum);
//selection is the string of dice numbers that the player wants to keep.
//For example, "125" means that player wants to keep the first, second, and fifth dice, and roll the rest.
void setSelection(string selection);
Hand();
private:
Dice* dice_array[HAND_SIZE];
};
Hand::Hand(){
static Dice dice1;
dice_array[0] = &dice1;
dice1.roll();
static Dice dice2;
dice_array[1] = &dice2;
dice2.roll();
static Dice dice3;
dice_array[2] = &dice3;
dice3.roll();
static Dice dice4;
dice_array[3] = &dice4;
dice4.roll();
static Dice dice5;
dice_array[4] = &dice5;
dice5.roll();
}
void Hand::show(){
//cout << "Hand:" < ;
for (int i = 0; i < HAND_SIZE; i ){
cout << dice_array[i]->reveal()<<" ";
}
}
void Hand::setSelection(string selection){
int num;
for(int i = 0; i < selection.size(); i ){
num = selection.at(i)-'0';
dice_array[num - 1]->setcheckDice(true);
}
}
void Hand::play(){
for (int i = 0; i < HAND_SIZE; i ){
if(dice_array[i]->getcheckDice() == false){
dice_array[i]->roll();
}
}
}
Dice* Hand::getDice(int diceNum){
return dice_array[diceNum];
}
//######################################################################
//List of rows in the board
const int ONES = 0;
const int TWOS = 1;
const int THREES = 2;
const int FOURS = 3;
const int FIVES = 4;
const int SIXES = 5;
const int THREE_OF_KIND = 6;
const int FOUR_OF_KIND = 7;
const int FULL_HOUSE = 8;
const int SMALL_STRAIGHT = 9;
const int LARGE_STRAIGHT = 10;
const int CHANCE = 11;
const int YAHTZEE = 12;
const int BOARD_SIZE=13;
class Game {
public:
//calcScore returns a score of a hand (5 dice) for given row in the board.
//Note the rows are indexed from zero.
//For example if the hand is 2 1 1 5 1 then calcScore(hand, ONES) returns 3 and calcScore(hand, TWOS) returns 2.
int calcScore(Hand* hand, int row);
//Display the board
void show();
//Returns the score of the upper part of the board
int getUpperScore();
//Returns the score of the lower part of the board
int getLowerScore();
//Returns the bonus points
int getBonusScore();
//Returns the total score
int getTotalScore();
//Play a hand based on the selected row
void play(Hand* hand, int row);
//Check to see if a row has been played or not (returns true if a row has been played)
bool isPlayed(int row);
//Check to see if all rows haven been played or not (returns true if all rows have been played)
bool isFinished();
Game();
private:
int game_score[BOARD_SIZE];
};
Game::Game(){
for(int i = 0; i < BOARD_SIZE; i ){
game_score[i] = -1;
}
}
int Game::calcScore(Hand *hand, int row){
}
int Game::getUpperScore(){
int add_score = 0;
for(int i = 0; i < THREE_OF_KIND; i ){
if(game_score[i]==-1){
add_score =0;
}
else{
add_score =game_score[i];
}
}
return add_score;
}
CodePudding user response:
Don't use pointers. If you want a Hand to have five Dice, then keep it simple
class Hand {
public:
Hand();
...
private:
Dice dice_array[HAND_SIZE];
};
Hand::Hand(){
for (int i = 0; i < HAND_SIZE; i)
dice_array[i].roll();
}
That's all you need, no pointers, no statics.
CodePudding user response:
The date June 10, 1960 is special because when we write it in the following format, the month times the day equals the year. 6/10/60 Write a program that asks the user to enter a month (in numeric form), a day, and a two-digit year. The program should then determine whether the month times the day is equal to the year. If so, it should display a message saying the date is magic. Otherwise, it should display a message saying the date is not magic.