Question
1. Create a Program From a Written Specification We will only have one assignment this week. This will be challenging but will also give you
1. Create a Program From a Written Specification
We will only have one assignment this week. This will be challenging but will also give you the time you need to finish your Course Project for this week and prepare for the final assignment for your Course Project. This exercise will be to use a written specification to write a program that expands on our Snowball FightVersion 2 code from Week 5 Assignment 2. You will need to generate the pseudocode and the code for this assignment.
The previous versions of Snowball Fight have been one sided. SF_ver1 involved the player throwing a snowball at one target and getting the distance between the snowball hit and the target as feedback. The target moved using a simple algorithmrandomly pick a direction (or dont move) and move if possible. The target cannot leave the grid or wrap around.
SF_ver2 added three targets, slightly changed the feedback to make it a little more interesting, and also let the player know the targets directions when they moved. A target is removed from the grid when hit, and the game ends when the player has thrown all the available snowballs or all the targets have been hit. The target movement algorithm was changed to make the target more mobile. The target still chooses a direction, one of which can be not to move, but if the target cannot move in the chosen direction, the direction is rotated in the following manner {N -> E -> S -> W -> N} until an open direction is found. Because more than one target can be in a location, at least two directions will always be available for movement. As before, the target can only move one position per turn.
With SF_ver3, we give the targets a chance to fight back. For each game turn, after the player throws a snowball, any remaining targets get to throw a snowball back. So, if you have three targets, the player has three snowballs thrown back at him or her. As before, once a target is hit, it is removed from the grid. This also means that target cannot throw a snowball. The player can choose to stay where he or she is or move at the end of each turn. Here are the victory conditions.
If all the targets have been hit and the player has not been hit at the end of a turn, the player wins.
If at least one target remains on the grid and the player has been hit, the player loses.
If all the targets and the player have been hit at the end of a turn, it is a tie.
If all the snowballs are thrown and none of the three conditions above has occurred, the game is a tie.
The targets choose where to throw the snowball at random. The targets and the player are on different grids, but the grids have the same number of rows and columns.
Here is the base code.
// Week 6 Assignment-1
// Description: Snowball Fight - version 3
//----------------------------------
//**begin #include files************
#include
#include
#include
//--end of #include files-----------
//----------------------------------
using namespace std;
//----------------------------------
//**begin global constants**********
// define coordinate structure
struct Coords
{
int x;
int y;
};
// define a struct of the target
struct MyStruct
{
int ID; // -- Identification number of the target
Coords position; // -- position of target
int dist; // -- distance between target and snowball hit
bool hit; // -- flag indicating target has been hit
};
const int gridSize = 5; // const grid size (i.e. 5x5 grid constant is 5)
const int turns = 20; // const number of turns
const int targetCount = 3; // number of targets
//--end of global constants---------
//----------------------------------
//**begin function prototypes*******
int throwSnowball(Coords p, MyStruct &Target);
void moveTarget( MyStruct &Target);
//--end of function prototypes------
//----------------------------------
//**begin main program**************
int main()
{
// initialization
srand(time(NULL));
bool allHit = false;
int hitCount = 0; // number of hits.
int dist = 0; // distance of miss
Coords snowballPos; // position of snowball hit
array
// Initialize targets
int idNum = 0;
for (auto &T: Targets) //**Error 1: add the "&"
{
T.ID = idNum++; // set identification number
// set target at random location
T.position.x = rand()%gridSize;
T.position.y = rand()%gridSize;
T.hit = false; // set target hit flag to default: false
}
// loop for the specified number of turns
for (int i = 0; i < turns; i++)
{
// get x and y position for the snowball from player
cout << "column? ";
cin >> snowballPos.x;
cout << "row? ";
cin >> snowballPos.y;
// throw snow ball (see instructions for details)
for(auto &T: Targets)
{
if (!T.hit)
{
// check for hit or miss
dist = throwSnowball(snowballPos, T);
// report results
switch (dist)
{
case 0:
cout << "***SPLAT*** You hit target " << T.ID << "!" << endl;
hitCount++;
break;
case 1:
cout << "target " << T.ID << ": Way too close!" << endl;
break;
case 2:
cout << "target " << T.ID << ": I heard it hit." << endl;
break;
default:
cout << "target " << T.ID << ": Missed by a mile." << endl;
break;
}
// target moves (see instruction for details
if (!T.hit) moveTarget(T);
}
}
if (hitCount == 3)
{
allHit = true;
break;
}
cout << "---Next Turn---" << endl;
}
// end of loop
// report score (number of hits vs turns)
if (allHit) cout << "All targets have been hit! Great job!" << endl;
else cout << "You had " << hitCount << " hits out of " << turns << " throws." << endl;
cin.get();
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
//**begin function definitions******
// Determine hit or distance
int throwSnowball(Coords p, MyStruct &Target)
{
int aDistance;
// compare to the target's position
if ( p.x == Target.position.x)
{
if (p.y == Target.position.y)
{
Target.hit = true;
return 0;
}
else
{
return abs(p.y - Target.position.y);
}
}
else
{
aDistance = abs(p.x -Target.position.x);
if (aDistance < abs(p.y - Target.position.y)) aDistance = abs(p.y -Target.position.y);
return aDistance;
}
}
// Move the target
void moveTarget( MyStruct &Target)
{
enum MyEnum
{
North, East, South, West, Stay
};
bool moveNotFound = true;
MyEnum ranNum = MyEnum(rand()%5);
if (ranNum == Stay) return;
while (moveNotFound)
{
switch (ranNum)
{
case North:
if (Target.position.y == 0) break; // can't move North
Target.position.y--;
cout << Target.ID << " moving North." << endl;
return;
case East:
if (Target.position.x == gridSize-1) break; // can't move East
Target.position.x++;
cout << Target.ID << " moving East." << endl;
return;
case South:
if (Target.position.y == gridSize -1) break; // can't move South
Target.position.y++;
cout << Target.ID << " moving South." << endl;
return;
case West:
if (Target.position.x == 0) break; // can't move West
Target.position.x--;
cout << Target.ID << " moving West." << endl;
return;
default:
break;
}
ranNum = MyEnum(int(ranNum+1)%4);
}
}
//--end of function definitions------
//----------------------------------
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