I have a program for a dice game where I need to be able to ask the user for how many players will be playing, and then based on that input, instantiate that number of player objects. How would I go about doing that? I have function written to ask the user for the input and then return the integer. Is there a way that I can call the Player class constructor a certain number of times from within that function? I have posted my code below. Thanks for the assistance!
EDIT: I updated my code here. I accidentally had a few of the functions for the Player class defined outside of the class's brackets.
// LCRDiceGame.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
class Player //player class to control chips and turns
{
public:
staticint chips;
Player() //player object constructor
{
chips = 3;
}
staticvoid SetChips()
{
//switch (dice) //figure out how to get the dice outcome from RollDice()
// case 1 : //TODO: code to subtract 1 chip from current player, and add 1 chip to player to the left
// case 2 : //TODO: code to subtract 1 chip from current player
// case 3 : //TODO: code to subtract 1 chip from current player, and add 1 chip to player to the right
// case 4 : //TODO: do no action
// case 5 : //TODO: do no action
// case 6 : //TODO: do no action
}
staticvoid CheckChips()
{
if (chips > 0)
{
}
}
};
staticint SetPlayers() //receive input for number of players, display error message if number is less than 3
{
int numOfPlayers = 0;
cout << "Please enter number of players (3 or more): " << endl;
cin >> numOfPlayers;
if (numOfPlayers <= 2)
{
cout << "This game requires 3 or more players." << endl;
SetPlayers();
}
return numOfPlayers;
}
class Dice
{
public:
staticint RollDice()
{
//TODO: some sort of loop to determine how many dice to roll
int dice = (rand() % 6 + 1);
return dice;
}
};
int main()
{
SetPlayers(); //set number of players
//TODO: implement a way of receiving number of players and instantiating that number of player objects
return 0;
}
The SetPlayers() function. And I just did a very basic layout of the rest of the code. But I am trying to work on this specific part for now because once I can instantiate the number of players needed, then I can continue to work on the CheckChips() and SetChips() functions, as well as building the Dice class.
to hold the number of players for the game. The game requires 3 or more players. Even though the end user will be playing by himself, the game should simulate a turn for each of the users, allowing them to roll the dice each turn. So the SetPlayers() function is called and asks the user how many players are playing. As long as the number of players is 3 or above, the function returns an integer of that amount.
// Example program
#include <iostream>
#include <string>
#include <vector>
//This is how you can pass a vector<int>
//We are taking it by const reference to prevent unnecessary copying
void printVector(const std::vector<int>& container) {
for (std::size_t index = 0; index != container.size(); ++index) {
std::cout << "Index [" << index << "]: " << container[index] << '\n';
}
}
int main()
{
std::size_t size;
std::cout << "Enter the size of your container: ";
std::cin >> size;
//The angle brackets mean the type
//<int> means the container will be holding integers,
//if it was <double> it would be holding doubles.
std::vector<int> container(size);
//We will be looping through the elements of the vector via a loop.
for (std::size_t index = 0; index != size; ++index) {
std::cout << "Enter in [" << index << "]: ";
std::cin >> container[index];
}
std::cout << "\nDisplaying the containers elements:\n";
printVector(container);
std::cout.flush();
std::cin >> size;
return 0;
}
// Example program
#include <iostream>
#include <string>
#include <list> //Include the list library.
//This is how you can a list to a function
//pass by const reference to prevent unecessary copying.
void printList(const std::list<int>& container) {
std::size_t count = 0;
for (std::list<int>::const_iterator iter = container.begin(); iter != container.end(); ++iter) {
std::cout << "Node (" << count << "): " << *iter << '\n';
count++;
}
//Note this can be a done a lot easier via c++ 11 range based for loop
// for (auto& iter : container) {
// std::cout << "Node (" << count << "): " << iter << '\n';
// count++;
//}
}
int main()
{
std::size_t size;
std::cout << "Enter the size of your container: ";
std::cin >> size;
std::list<int> container(size);
std::size_t count = 0;
//We will be looping through the elements of the list via iteration.
for (std::list<int>::iterator iter = container.begin(); iter != container.end(); ++iter) {
std::cout << "Node (" << count << "): ";
std::cin >> *iter; //dereference the iterator
count++;
}
//Note this can be a done a lot easier via c++ 11 range based for loop
// for (auto& iter : container) {
// std::cout << "Node (" << count << "): ";
// std::cin >> iter;
// count++;
//}
std::cout << "\nDisplaying the containers elements:\n";
printList(container);
std::cout.flush();
std::cin >> size;
return 0;
}
These are two of the more used containers in the standard library.
If you want more information and more possible containers to use you here is a link. http://www.cplusplus.com/reference/stl/
In general I'd recommend using the vector mainly since I'm not sure if you are going to be erasing and inserting a lot at runtime. If you are then a list would be better for that.
You could also use a pointer and assign to allocated memory. Although I'd recommend using the former rather than this method mainly because of the possibility of leaks.
// Example program
#include <iostream>
#include <string>
#include <vector>
//This is how you can pass a ptr to an array.
//We use size a means of looping through the array.
void printPtr(int* container, std::size_t size) {
for (std::size_t index = 0; index != size; ++index) {
std::cout << "Index [" << index << "]: " << container[index] << '\n';
}
}
int main()
{
std::size_t size;
std::cout << "Enter the size of your container: ";
std::cin >> size;
//We allocate memory on the heap. To which we allocate N ints.
//Note depending on the input a bad_alloc and/or bad_array_new_length exception could be thrown.
int* container = newint[size];
//We will be looping through the elements of the ptr via indexes and a loop.
for (std::size_t index = 0; index != size; ++index) {
std::cout << "Enter in [" << index << "]: ";
std::cin >> container[index];
}
std::cout << "\nDisplaying the containers elements:\n";
printPtr(container, size);
std::cout.flush();
std::cin >> size;
//Very important to free memory after usage otherwise we have a memory leak.
//This could be in a destructor of a class.
delete[] container;
return 0;
}
While I really appreciate this thorough answer, this assignment is for school and I think most of this is outside the scope of the class, at least at this point. However, from reviewing your post, I did come up with a way to make a simple loop to create these objects. Thank you for the help!