can someone wirte code for me? C++ 2013 visual studio Structures Introduction St
ID: 3679297 • Letter: C
Question
can someone wirte code for me? C++ 2013 visual studio
Structures
Introduction
Study Floating and Docking Windows of MSVS described here. As you work on the below project, demonstrate to the instructor the usage of this feature.
You are to program a simplified version of the Battleship guessing game. The assignment is broken into two parts. The below game and header file description applies to both parts.
Battleship game description. The field (ocean) is a square 5x5 grid. One of the coordinates of the grid is a number (from 1 to 5) and the other -- a letter (from 'a' to 'e'). Your program should randomly place a fleet of five ships in the ocean. Each ship takes up exactly one location in the ocean. Multiple ships cannot be placed in the same location. The ships, however, can be placed in adjacent locations. The user fires on the ships by specifying the coordinates of the shot. The program reports whether each shot was a hit or a miss. If the shot was a hit, the ship is sunk. The game continues until all ships are sunk. The program does not keep track of the locations of the previously fired shots.
Data structures and function description. The data structures and functions needed to implement the game are declared in this header file. The header file defines two structures:
Location stores the number and letter coordinates of a ship or a shot;
Ship stores the coordinates of the ship in Location substructure and a boolean variable signifying whether the ship was sunk. The fleet of the deployed ships should stored in the array where each element is a structure variable of Ship. This array is first initialized and then used in the game.
The functions are separated into three groups:
Initialization functions that place the fleet of battleships in the ocean. The major function among the initialization functions is deploy() that accepts an array of ships by reference and initializes their locations. It uses the other two initialization functions: pick() and check() The pseudocode for deploy()is as follows:
Hint: To randomly assign a character coordinate (from 'a' to 'e') for the location in pick(), randomly select a number from 1 to 5 and then use a switch to select the appropriate letter.
Hint 2: The logic of initialize(), check() and deploy() is very similar to the logic of lottery number selection functions in one of the previous labs.
Functions that display the location of the fleet. After the fleet is deployed, the user is prompted if he would like to see the location of the ships (Hint: use this option for debugging). If the user selects this option then the locations of the ship (and their status: sunk or not) is printed after every shot
The printout is done using the following functions.
printShip() prints the location and status of a single ship
printFleet() prints the location and the status (sunk or not) of the whole fleet of ships. This function uses printShip(). The output of this function might look like:
b5 sunk, c3 up, a2 up, e1 sunk, e4 up
Battle functions that control the game play. After the fleet of ships is deployed, and its location is printed if desired, the game starts. The pseudocode of the game algorithm (to be implemented in main() or a dedicated function) is as follows.
Assignments
Test. Create a project titled Lab8_Test. Add the header file battleship.h described above to your project. Add this file testShips.cpp to your project as well. Implement the functions prototyped in battleship.h file and invoked in testShips.cpp and place these function definitions inbattleship.cpp
Note that presently portions of the file are commented out. This is to encourage incremental program development. You need to implement the functions that are not commented first. Then, uncomment the second portion of code and implement those functions. Once your project works correctly with all code of testShips.cpp uncommented, submit your project.
Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp from the test assignment above; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction.
Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.
Explanation / Answer
testShips.java
#include "battleship.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main(){
// checking location functions
// create location
location mySpot;
mySpot.x=5;
mySpot.y='e';
// print location
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
mySpot = pick(); // assign randomly generated location to mySpot
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
mySpot = fire(); // assign user input location to mySpot
cout << "mySpot.x = " << mySpot.x << " mySpot.y = " << mySpot.y << endl;
// checking ship functions
ship myShip;
myShip.loc = pick(); // assign random location to myShip
myShip.sunk = false; // myShip is not sunk
printShip(myShip); // print myShip information
if(match(myShip,mySpot))
cout << "myShip is at mySpot location ";
else
cout << "myShip is not at mySpot location ";
sink(myShip); // sinking myShip
printShip(myShip); // print sunk ship
// checking fleet functions
ship myFleet[FLEET_SIZE];
initialize(myFleet); //assigning -1 to all ship's locations in myFleet
printFleet(myFleet);
deploy(myFleet); // deploying ships at random locations
printFleet(myFleet);
if(check(myFleet,mySpot) != -1)
cout << "myFleet has a ship at mySpot ";
else
cout << "myFleet does not have a ship at mySpot ";
if(operational(myFleet))
cout << "at least one ship is not sunk ";
else
cout << "all ships are sunk ";
}
battleship.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using std::cout; using std::cin; using std::endl;
bool match(ship myShip, location mySpot){
// returns true if this location matches
// the location of the ship
// returns false otherwise
if (myShip.loc.x == mySpot.x && myShip.loc.y == mySpot.y)
return true;
else
return false;
}
int check(const ship myFleet[], location mySpot){
// returns the index of element of the array
// that matches the location
// returns -1 if none do
for (int i = 0; i < FLEET_SIZE; ++i)
if (match(myFleet[i], mySpot)) {
return i;
}
return -1;
}
void initialize(ship myFleet[]) {
// places all ships in -1 X location to signify
// that the ship is not deployed
for (int i=0; i < FLEET_SIZE; ++i) {
myFleet[i].loc.x = -1;
myFleet[i].loc.y = 'X';
}
}
location pick() {
// generates a random location
location mySpot;
srand(int(time(0)));
mySpot.x = rand() % 5 + 1;
int y = rand() % 5 + 1;
switch (y) {
case 1:
mySpot.y = 'a';
break;
case 2:
mySpot.y = 'b';
break;
case 3:
mySpot.y = 'c';
break;
case 4:
mySpot.y = 'd';
break;
case 5:
mySpot.y = 'e';
break;
}
return mySpot;
}
void deploy(ship myFleet[]) {
// places an array of battleships in
// random locations in the ocean
int i = 0;
while (i < FLEET_SIZE) {
location mySpot = pick();
int tmp = check(myFleet, mySpot);
if (tmp == -1) {
myFleet[i].loc = mySpot;
myFleet[i].sunk = false;
++i;
}
}
}
// DISPLAY FUNCTIONS
void printShip(ship myShip) {
// prints the location and status (sunk or not) of a single ship
cout << "The ship is at: " << myShip.loc.x << myShip.loc.y << endl;
cout << "Status of ship: ";
if (myShip.sunk == false)
cout << "The ship is still alive!" << endl;
else
cout << "The ship was sunk!" << endl;
}
void printFleet(ship const myFleet[]) {
// prints the locations of all the ships and whether they are sunk
for (int i = 0; i < FLEET_SIZE; ++i) {
printShip(myFleet[i]);
}
}
// BATTLE FUNCTIONS
bool operational(ship const myFleet[]) {
// returns true if at least one ship in the array is not sunk
for (int i = 0; i < FLEET_SIZE; ++i) {
if (myFleet[i].loc.x != -1 && myFleet[i].loc.y != -1)
return true;
}
return false;
}
location fire() {
// asks the user to input the coordinates of the next shot
location mySpot;
cout << "Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): ";
cin >> mySpot.x >> mySpot.y;
return mySpot;
}
void sink(ship& myShip) {
// sets "sunk" member variable of the ship to true
myShip.sunk = true;
}
battleship.h
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// data structures definitions
const int FLEET_SIZE=5; // number of battleships
const int FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE
// coordinates (location) of the ship and shots
struct location{
int x; // 1 through FIELD_SIZE
char y; // 'a' through FIELD_SIZE
};
// contains ship's coordinates (location) and whether is was sunk
struct ship{
location loc;
bool sunk;
};
// initialization functions
void initialize(ship[]); // places all ships in -1 X location to signify
// that the ship is not deployed
location pick(); // generates a random location
bool match(ship, location); // returns true if this location matches
// the location of the ship
// returns false otherwise
int check(const ship[], location); // returns the index of element of the array
// that matches the location
// returns -1 if none do
// uses match()
void deploy(ship[]); // places an array of battleships in
// random locations in the ocean
// display functions
void printShip(ship); // prints the location and status (sunk or not)
// of a single ship
void printFleet(const ship[]); // prints the locations of all the ships and
// whether they are sunk
// battle functions
bool operational(const ship[]); // returns true if at least one ship in the array
// is not sunk
location fire(); // asks the user to input the coordinates of the next
// shot
// note that check() is also used in the battle
void sink(ship&); // sets "sunk" member variable of the ship to true
#endif /* BATTLESHIP_H_ */
sample output
mySpot.x = 2 mySpot.y = a
Where will you take your next shot? (Coordinates 1-5 a-e separated by a space): 2
a
mySpot.x = 2 mySpot.y = a
The ship is at: 4d
Status of ship: The ship is still alive!
myShip is not at mySpot location
The ship is at: 4d
Status of ship: The ship was sunk!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: -1X
Status of ship: The ship is still alive!
The ship is at: 4d
Status of ship: The ship is still alive!
The ship is at: 5d
Status of ship: The ship is still alive!
The ship is at: 5b
Status of ship: The ship is still alive!
The ship is at: 4a
Status of ship: The ship is still alive!
The ship is at: 2c
Status of ship: The ship is still alive!
myFleet does not have a ship at mySpot
at least one ship is not sunk
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.