With SF_ver3, we give the targets a chance to fight back. For each game turn, af
ID: 3813867 • Letter: W
Question
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 <iostream> // provides access to cin and cout
#include <array> // provides access to std::array
#include <time.h> // provides access to time() for srand()
//--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 <MyStruct, targetCount> Targets;
// 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------
//----------------------------------
Explanation / Answer
private static category BinOpNode extends ExpNode quantity.
ExpNode right; // The expression for its right quantity.
BinOpNode(char op, ExpNode left, ExpNode right) the required information.
assert op == '+' || op == '-' || op == '*' || op == '/';
assert left != null && right != null;
this.op = op;
this.left = left;
this.right = right;
}
double value() the worth is obtained by evaluating the left and right
// operands and mixing the values with the operator.
double x = left.value();
double y = right.value();
switch (op) come x + y;
case '-':
come back x - y;
case '*':
come back x * y;
case '/':
come back x / y;
default:
come back Double.NaN; // dangerous operator! mustn't be potential.
}
}
void printStackCommands() to guage the expression on a stack machine, first do
// no matter is important to guage the left quantity, leaving
// the solution on the stack. Then do constant issue for the
// second quantity. Then apply the operator (which suggests that sound
// the operands, applying the operator, and pushing the result).
left.printStackCommands();
right.printStackCommands();
System.out.println(" Operator " + op);
}
}
It's also attention-grabbing to appear at the new parsing subroutines. rather than computing a worth, every subprogram builds AN expression tree. as an example, the subprogram like the rule for <expression> becomes
static ExpNode expressionTree() throws ParseError browse AN expression from the present line of input and
// come back AN expression tree representing the expression.
TextIO.skipBlanks();
mathematician negative; // True if there's a number one sign.
negative = false;
if (TextIO.peek() == '-')
ExpNode exp; // The expression tree for the expression.
exp = termTree(); // begin with a tree for 1st term.
if (negative) single minus operator to the term we've
// simply scan.
exp = new UnaryMinusNode(exp);
}
TextIO.skipBlanks();
whereas ( TextIO.peek() == '+' || TextIO.peek() == '-' ) {
// scan successive term and mix it with the
// previous terms into an even bigger expression tree.
char op = TextIO.getAnyChar();
ExpNode nextTerm = termTree();
// produce a tree that applies the binary operator
// to the previous tree and also the term we tend to simply scan.
exp = new BinOpNode(op, exp, nextTerm);
TextIO.skipBlanks();
}
come back exp;
} // finish expressionTree()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.