I'm creating a multiplayer game in which all players have one roll of the dice, and I want the rolls to be stored in a vector and the player with the smallest roll to win. I would like the game to reset after each time, allowing multiple plays without saving previous die rolls.
I have the vector coded for inserting player names, but I am not sure how to save a roll to each player, choose which goes first, and output scores.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;
int diceRoll();
char playerName[10];
vector <string> playerNames();
int main()
{
string inputBuffer;
int numPlayers;
diceGame gameManager;
/// get the number of players
do
{
cout << "How many players:";
getline(cin, inputBuffer);
numPlayers = atoi( inputBuffer.c_str() );
}
while (numPlayers < 1 || numPlayers > 9);
/// play the game over and over
do
{
gameManager.initializeGame(numPlayers);
gameManager.playGame();
cout << endl << "Play Again? [y/n]";
getline(cin, inputBuffer);
}
while ( tolower(inputBuffer[0]) != 'n' );
return 0;
}'''
void diceGame::initializeGame(int numPlayers)
{
/// clear the vector or robots (if necessary) and put in one robot per
player
/// ask for the names of each player, and assign that name to each robot
cout << "Enter the name of each player" << endl << endl;
vector<string> playerNames(numPlayers);
for (int i = 0; i < numPlayers; i++)
cin >> playerNames[i];
playGame();
// I think I can use this rand function to decide which player goes first, otherwise I'm not sure how to play in numerical order (ex: Player 1: Steve goes first)
cout << playerNames[rand() % (1 + numPlayers)];
}
void diceGame::playGame()
{
/// do
/// determine which player goes
/// I am unable to get this to work, since my vector is not global, and I'm not sure how to do that exactly
cout << playerNames[rand() % (1 + numPlayers)] << "'s Turn" << endl;
/// roll the dice
diceRoll();
}
int diceRoll () {
int Roll;
int min = 1;
int max = 8;
Roll = rand() % (1 + max - min) + min ;
return Roll;
}
I am able to roll the dice but not save it to a vector and eventually output a winner (lowest roll). Any help would be appreciated!
Aucun commentaire:
Enregistrer un commentaire