In: Computer Science
My assignment is to program the movment around a monopoly board, with three players. After rolling the two dice, player 1 moves and so fourth. Any solutions for this code would be great. It doesnt matter what they land on, just the number of spaces and taking turns. the coding is C++, the requierments are very basic, there need to be no specification on rather the movements acuacly means. Just rolling a die and sneding player one to space 6 after rolling a three and another three at the same time. A monopoly board as 40 spaces, a 10x10. each player takes turns rolling, and move to where ever they roll.
Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.
Please provide your feedback
Thanks and Happy learning!
#include <iostream>
#include <string>
#include <time.h>
class Player
{
private:
int m_currentPosition;
std::string m_name;
public:
Player(std::string playerName) : m_name(playerName), m_currentPosition(0)
{
srand(time(NULL));
}
//Assuming the start position as zero(0).
//If the current position is more than the number of
//cells in the board(ie 40) then wrap around to the first position(ie 0th position)
void moveCurrentPosition(int numOfPlacesToMove)
{
std::cout << "Moving the current position of " << m_name << " by " << numOfPlacesToMove << std::endl;
m_currentPosition = (m_currentPosition + numOfPlacesToMove) % 40;
}
void play()
{
std::cout <<"-------------------------------------------" << std::endl;
std::cout << m_name << " turn : " << std::endl;
//roll the dice;
//Generate a random value between 1 and 6
int dice1Reading = std::rand() % 6 + 1;
int dice2Reading = std::rand() % 6 + 1;
std::cout << "Current position of " << m_name << " : " << std::endl;
printPlayerCurrentState();
std::cout << m_name << " dice 1 reading " << dice1Reading << std::endl;
std::cout << m_name << " dice 2 reading " << dice2Reading << std::endl;
moveCurrentPosition(dice1Reading + dice2Reading);
std::cout << "New position of " << m_name << " : " << std::endl;
printPlayerCurrentState();
std::cout << "-------------------------------------------" << std::endl;
}
void printPlayerCurrentState()
{
std::cout << "\t" << m_name << " is at position " << m_currentPosition << std::endl;
}
};
int main()
{
Player p1("Player 1");
Player p2("Player 2");
Player p3("Player 3");
//Currently each player plays only one time.
//If we want to make the game to go on until
//the user presses Ctr-C, then uncomment the
//while loop below.
//while (1)
//{
p1.play();
p2.play();
p3.play();
//}
return 0;
}