ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 03 May 2017 My Price 9.00

Create a Program From a Written Specification

Need help to solve this, i have no idea on what is asked here...What will the finished code look like?

Complete the following assignment. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. When you complete this week’s assignment, save the document as yourLastName_GSP115_W6_Assignments.docx. Submit it to the Week 6 assignment Dropbox.

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 Fight—Version 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 algorithm—randomly pick a direction (or don’t 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 <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------

//----------------------------------

 

Answers

(11)
Status NEW Posted 03 May 2017 04:05 AM My Price 9.00

-----------

Attachments

file 1493786080-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)