Question
*********************************** C++ ***********************************hw92 Modify the table and table driven simple race game so that a player who loses half his points and his turn can
*********************************** C++ ***********************************hw92
Modify the table and table driven simple race game so that a player who loses half his points and his turn can recover his turn but not his points if his new points equal the points of some other player or players. If this is the case then the player does not lose his turn but the other players lose all their points but not their next turn.
You must solve this problem by modifying the table.
permits | pts | ds? | 6 x permits + 2 x points + 1 x double | ? |
0 | 0 | 0 | 0 | + points |
0 | 0 | 1 | 1 | + points, extra turn |
0 | 1 | 0 | 2 | + points, wins |
0 | 1 | 1 | 3 | + points, wins |
0 | 2 | 0 | 4 | permits -> 1, - points |
0 | 2 | 1 | 5 | - points |
1 | 0 | 0 | 6 | permits -> 2 |
1 | 0 | 1 | 7 | permits -> 0 |
1 | 1 | 0 | 8 | permits -> 2 |
1 | 1 | 1 | 9 | permits -> 0 |
1 | 2 | 0 | 10 | permits -> 2 |
1 | 2 | 1 | 11 | permits -> 0 |
2 | 0 | 0 | 12 | do nothing |
2 | 0 | 1 | 13 | permits -> 0 |
2 | 1 | 0 | 14 | do nothing |
2 | 1 | 1 | 15 | permits -> 0 |
permits
0 roll dice, accumulate points
1 no roll dice, no accumulate points
2 roll dice, no accumulate points
============================================================ Bee.cpp
#include "Bee.h"
//operators
bool Bee::operator==(const Bee& any_bee) const
{
return _number == any_bee._number;
}
bool Bee::operator!=(const Bee& any_bee) const
{
return _number != any_bee._number;
}
//constructors
Bee::Bee(int new_number)
{
number(new_number);
permits(0);
points(0);
}
//accessors
int Bee::number() const
{
return _number;
}
int Bee::points() const
{
return _points;
}
int Bee::permits() const
{
return _permits;
}
//mutators
void Bee::number(int new_number)
{
if (new_number < 0) new_number = 0;
_number = new_number;
}
void Bee::points(int new_points)
{
if (new_points < 0) new_points = 0;
_points = new_points;
}
void Bee::permits(int new_permits)
{
new_permits = new_permits % 3;
if (new_permits < 0) new_permits += 3;
_permits = new_permits;
}
//custom methods
void Bee::add(int more_points)
{
_points += more_points;
}
====================================================== Bee.h
#pragma once
class Bee
{
public:
//operators
bool operator==(const Bee& any_bee) const;
bool operator!=(const Bee& any_bee) const;
//constructors
Bee(int new_number = 0);
//accessors
int number() const;
int points() const;
int permits() const;
//mutators
void number(int new_number);
void permits(int new_permit);
void points(int new_points);
//custom methods
void add(int more_points);
private:
int _number;
int _points;
int _permits;
};
=========================================== source.cpp
/*
A few bees take a little time away from their hive maintenance jobs to play a simple game.The rules are :
the bees take turns tossing two dice. A bee adds the number of spots on the upper most face of the dice to its points.
If the bee^s points equal the maximum points then the game ends and the bee wins.
If the bee^s points are greater than the maximum points then the bee loses one-half of its points,
its next turn, and must roll doubles before it can accumulate points.
It is possible that several bees win.
Read the number of bees and the maximum points from a file.
*/
#include
#include
#include
#include
#include
#include
#include
#include "Bee.h"
using namespace std;
int main()
{
//int seed = 0;
int seed = (int)time(nullptr);
default_random_engine e(seed);
uniform_int_distribution
int N, M;
string file_name = "parameters.txt";
ifstream inps;
inps.open(file_name);
if (inps.good())
inps >> M >> N;
else
{
M = 29;
N = 3;
}
vector
for (int i = 1; i <= N; ++i)
{
Bee bee(i);
bees.push_back(bee);
}
for (vector
cout << "bee " << bit->number() << ": " << setw(2) << 0 << " points" << endl;
cout << endl;
bool winners = false;
while (!winners)
{
vector
while(bit != bees.end())
{
int die1 = u(e);
int die2 = u(e);
int sum = die1 + die2;
int points = bit->points();
int new_points = points + sum;
cout << "bee " << bit->number() << ": " << setw(2) << new_points
<< " points, rolls " << sum << " = " << die1 << " + " << die2 << endl;
int points_category;
if (new_points < M) points_category = 0;
else if (new_points == M) points_category = 1;
else points_category = 2;
int state = 6 * bit->permits() + 2 * points_category + 1 * (die1 == die2);
switch (state)
{
case 0:
bit->add(sum);
++bit;
break;
case 1:
bit->add(sum);
break;
case 2:
case 3:
bit->add(sum);
winners = true;
++bit;
break;
case 4:
points = points / 2;
bit->points(points);
bit->permits(1);
++bit;
break;
case 5:
points = points / 2;
bit->points(points);
bit->permits(0);
++bit;
break;
case 6:
bit->permits(2);
++bit;
break;
case 7:
bit->permits(0);
++bit;
break;
case 8:
case 10:
bit->permits(2);
++bit;
break;
case 9:
case 11:
case 13:
case 15:
bit->permits(0);
++bit;
break;
case 12:
case 14:
++bit;
break;
default:
cout << "state: " << state << endl;
}
}
cout << endl;
}
for (vector
if (bit->points() == M)
cout << "bee " << bit->number() << " wins" << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started