Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write functions to perform many of the tasks that the main function performs. Us

ID: 3631295 • Letter: W

Question

Write functions to perform many of the tasks that the main function performs. Use prototypes
Replace the code in the main with calls to your functions
Your program and the original program must, given the same input, produce the same output.



/*
Pick Up Sticks Design
pile = read(input_file_stream)
max_to_pick_up = read(input_file_stream)
player_name_1 = read(input_file_stream)
player_name_2 = “computer”
player_id = generateRandom(1, 2)
repeat
now_max_to_pickup = min(max_to_pick_up, pile)
player_id = player_id mod 2 + 1
if player_id = 1 then
repeat
write “Enter number of sticks to pickup”
to_pick_up = read(input_stream)
until input_stream.good and
1 <= to_pickup <= now_max_to_pickup
else
if pile mod (max_to_pickup + 1) = 1 then
to_pick_up = generateRandom(1, now_max_to_pickup)
else
to_pick_up = now_max_to_pick_up
while (pile – to_pick_up) mod (max_to_pickup + 1) ¹ 1
to_pick_up = to_pick_up – 1
end while
end if
end if
pile = pile – to_pick_up
until pile = 0
if player_id = 1 then
write player_name_2 + “wins”
else
write player_name_1 + “wins”
end if
*/
#include <cassert>
#include <cmath>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;

string trim(const string& original);

int main()
{
//read configuration file name
const string config_file_name_prompt = "Please enter your configuration file name: ";
string config_file_name;
cout << config_file_name_prompt;
getline(cin, config_file_name, ' ');
config_file_name = trim(config_file_name);
assert(config_file_name.length() > 0);

//open configuration file
ifstream config_file;
config_file.open(config_file_name.c_str());
assert(config_file.good());

//read initial number of sticks on pile
unsigned initial_sticks_on_pile;
config_file >> initial_sticks_on_pile;
assert(config_file.good() && initial_sticks_on_pile > 0);

//read maximum number of sticks to pick up
unsigned max_sticks_to_pick_up;
config_file >> max_sticks_to_pick_up;
assert(config_file.good() && 0 < max_sticks_to_pick_up && max_sticks_to_pick_up <= initial_sticks_on_pile);

//read human player's name
string human_player_name;
config_file >> human_player_name;
human_player_name = trim(human_player_name);
assert(human_player_name.length());

config_file.close();

string computer_player_name = "The Computer";

srand(unsigned(time(NULL)));
unsigned sticks_on_pile = initial_sticks_on_pile;
unsigned player_id = rand() % 2 + 1;
do
{
//compute maximum sticks that can be pickup
unsigned now_max_sticks_to_pickup = min(max_sticks_to_pick_up, sticks_on_pile);
unsigned sticks_to_pickup;
player_id = player_id % 2 + 1;
cout << "There are " << sticks_on_pile << " sticks on the file" << endl;
if(player_id == 1)
{
//human picks up sticks
cout << "It is " << human_player_name << "'s turn" << endl;
do
{
if(!cin.good())
{
cin.clear();
cin.ignore(1024, ' ');
}
cout << "Enter the number of sticks to pickup: ";
cin >> sticks_to_pickup;
}
while(!cin.good() || sticks_to_pickup <= 0 || now_max_sticks_to_pickup < sticks_to_pickup);
cout << human_player_name << " picks up " << sticks_to_pickup << " sticks" << endl;
}
else
{
//computer picks up sticks
cout << "It is " << computer_player_name << "'s turn" << endl;
//computer will lose if player makes no mistakes
if(sticks_on_pile % (max_sticks_to_pick_up + 1) == 1)
{
sticks_to_pickup = rand() % now_max_sticks_to_pickup + 1;
}
else
{
//compute winning number of sticks to pickup
sticks_to_pickup = now_max_sticks_to_pickup + 1;
do
{
sticks_to_pickup = sticks_to_pickup - 1;
}
while((sticks_on_pile - sticks_to_pickup) % (max_sticks_to_pick_up + 1) != 1);
}
cout << computer_player_name << " picks up " << sticks_to_pickup << " sticks " << endl;
}
sticks_on_pile = sticks_on_pile - sticks_to_pickup;
}
while(sticks_on_pile > 0);
if(player_id == 1)
cout << computer_player_name << " wins" << endl;
else
cout << human_player_name << " wins" << endl;
system("pause");
return 0;
}
string trim(const string& original)
{
int left = 0;
int right = original.length() - 1;
while(left <= right && isspace(original[left]))
left++;
while(left <= right && isspace(original[right]))
right--;
string edited = original.substr(left, right - left + 1);
return edited;
}

Explanation / Answer

Ok rate this as lifesaver first. I am sick and tired of people taking advantage of me. I spend 30 minutes to one hour posting a solution and then they just take the answer and dont rate it. Inbox me once you have rated it and I will send you the solution.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote