Reputation: 69
Hello I am working on a project for programming II. I apologize in advance for any mistakes I make on here (this is my first post). I am working on a program will imitate the game "Farkle." My teacher has provided us with a framework and I have been hung up on implementing the roll function. My goal here is to use the rand function to randomly generate a number between 1 - 6 and placing that value inside the vector RolledDice (I dont need help with the rand function) I just need help setting the values of RolledDice equal to that of what the rand function produces.
The problem is the Roll()
function in Game.cpp
rolledDice
to an int
value.public
class.In my Game.cpp I have:
Game::Die::Die ()
{
// This function will: Initalize the value of the dice
pips = 0;
selected = false;
used = false;
}
ostream & operator << (ostream & outs, const Game::Die & D)
{
// This function will: output the score
return outs;
}
Game::Game (const string & T)
{
// This function will: initializer the random variables
title = T;
// srand (0);
srand (time (NULL));
}
Game::~Game ()
{
}
void Game::Init ()
{
gameState = GO;
whoseTurn = NONE;
humanOnBoard = false;
aiaOnBoard = false;
humanScore = 0;
aiaScore = 0;
lastTurn = false;
}
void Game::Instructions (ostream & outs)
{
outs << endl << "Welcome to " << title << endl;
outs << "The goal of this game is to accumulate 5000"
<< " points before your opponent." << endl;
outs << "In this case, this program is your opponent." << endl;
outs << "You must score 500 points in a single turn to"
<< " start accumulating points." << endl << endl;
sleep (1);
}
void Game::StartTurn ()
{
if (whoseTurn == NONE || whoseTurn == AIA)
{
whoseTurn = HUMAN;
Roll();
}
else
{
whoseTurn = AIA;
turnDone = false;
numToRoll = MAX_DICE;
savedDice.clear ();
subTotal = 0;
turnTotal = 0;
Roll();
}
}
bool Game::Enter (char selection, ostream & outs)
{
// This function will:
switch (selection)
{
case 'i': Instructions (outs);
break;
case 'r':
SaveSelected();
Roll ();
break;
case 's':
SaveSelected();
SaveScore();
break;
case 'q':
gameState = QUITTER;
turnDone = true;
break;
default:
{
int which = selection - 'a';
if (which < 0 || which >= rolledDice.size())
return false;
rolledDice[which].selected = !rolledDice[which].selected;
}
}
return true;
}
void Game::Roll () // Roll function
{
rolledDice.push_back(Game::Die.pips); // **This is my issue**
}
I tried:
void Game::Roll () // Roll function
{
int numRolled = rand()%6 + 1;
rolledDice.push_back(numRolled) // **This is my issue**
}
As well as:
void Game::Roll () // Roll function
{
int numRolled = rand()%6 + 1;
rolledDice.pips.push_back(numRolled) // **This is my issue**
}
And this doesn't work because rolledDice
cant be converted into an int
And many other things that I couldn't possibly fit in this post I just thought these were the closest ones to illustrating what I am trying to do.
In my Game.h I have:
class Game
{
public:
Game (const string & T);
~Game ();
void Init ();
void Instructions (ostream & outs);
void StartTurn ();
friend ostream & operator << (ostream & outs, const Game & G);
bool Enter (char selection, ostream & outs);
bool TurnDone ();
int AIAPlayer (ostream & outs);
bool Done ();
void Message (ostream & outs);
struct Die
{
Die ();
friend ostream & operator << (ostream & outs, const Die & D);
int pips;
bool selected;
bool used;
};
private:
void Roll ();
int CalculateScore (vector <Die> & dice);
void SaveSelected ();
void SaveScore ();
string title;
vector <Die> rolledDice; // Dice Rolled
vector <Die> remainingDice; // Will take out dice saved
vector <Die> selectedDice; // Will temporarily save the selected dice
vector <Die> savedDice; // Will save the dice
state_types gameState;
player_type whoseTurn;
bool turnDone;
int numToRoll;
int rolledScore;
int subTotal;
int turnTotal;
bool humanOnBoard;
bool aiaOnBoard;
int humanScore;
int aiaScore;
bool lastTurn;
};
If you need to post any more information will do. Any help is appreciated many thanks.
Upvotes: 1
Views: 151
Reputation: 392
Create a constructor for your inner struct:
Die(int p)
{
pips=p;
}
then it is possible to populate the rolledDice vector: void Game::Roll ()
{
int p = // some random generator
rolledDice.push_back(Game::Die(p));
}
Upvotes: 0
Reputation: 14705
By creating a constructor
Die::Die(int pips)
You are creating a constructor that will be used as an implicit type conversion. This greatly simplifies the syntax for adding to the vector. To not have this you need the keyword explicit. It sometimes makes for unintuitive semantics in your program.
A simple proof of concept:
#include <iostream>
#include <vector>
struct Die{
Die(int pips):pips(pips){}
int pips;
};
std::vector<Die> tosses;
int main(void)
{
tosses.push_back(2); // Works because of the converting constructor.
tosses.push_back(2);
tosses.push_back(1);
tosses.push_back(2);
for(auto toss:tosses){
std::cout << toss.pips;
}
}
Also consider http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution for elegant and correct dice-throwing. Most arguments I've seen against std::rand contains throwing dice.
Upvotes: 2