Question

In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

In C++ please.. I only need the game.cpp. thanks.

Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction.

——————————————-

//battleship.h

#pragma once

// structure definitions and function prototypes

// for the battleship assignment

// 3/20/2019

#include

#include

#ifndef BATTLESHIP_H_

#define BATTLESHIP_H_

//

// data structures definitions

//

const int fleetSize = 6; // number of battleships

const int fieldSize = 6; // the field (ocean) is fieldSize * fieldSize

// coordinates (Location) of the ship and shots

struct Location {

int x; // 1 through fieldSize

char y; // 'a' through fieldSize

};

// contains ship's coordinates (Location) and whether is was sunk

struct Ship {

Location loc;

bool sunk;

};

//

// initialization functions

//

void initialize(Ship[]); // places every Ship in a Location where x-coordinate is -1

// and y-coordinate is '*' (a star) to signify

// that the Ship is not deployed

Location pick(); // generates a random Location

bool match(const Ship &, const Location &); // returns true if this Location matches

// the Location of the Ship

// returns false otherwise

int check(const Ship[], const 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(const 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_ */

—————————————————-

//battleship.cpp

#include

#include

#include

#include

#include "battleship.h"

using namespace std;

Location pick()

{

Location loc;

loc.x = rand() % 6 + 1;

int randNum = rand() % 6 + 1;

loc.y = 'a' + (rand() % 6);

return loc;

}

Location fire()

{

Location loc;

cout << "Input X: ";

cin >> loc.x;

cout << "Input Y: ";

cin >> loc.y;

return loc;

}

void printShip(const Ship&s)

{

cout << "Ship is at: " << s.loc.x << s.loc.y << endl;

if(s.sunk) {

cout << "This Ship is destroyed" << endl;

}

else {

cout << "This ship still alive" << endl;

}

}

bool match(const Ship &ship, const Location &myLocation)

{

return (ship.loc.x == myLocation.x && ship.loc.y == myLocation.y) ? true : false;

}

void sink(Ship&ship) {

ship.sunk = true;

}

void initialize(Ship ships[]) {

for (int i = 0; i < fleetSize; ++i) {

ships[i].loc.x = -1;

ships[i].loc.y = '*';

ships[i].sunk = false;

}

}

void printFleet(const Ship ships[]) {

for (int i = 0; i < fleetSize; ++i) {

printShip(ships[i]);

}

}

int check(const Ship ships[], const Location &myLocation) {

for (int i = 0; i < fleetSize; ++i) {

if (match(ships[i], myLocation)) {

return i;

}

}

return -1;

}

void deploy(Ship ships[]) {

int index = 0;

while (index < fleetSize) {

Location randomLoc = pick();

int pos = check(ships, randomLoc);

if (pos == -1) {

ships[index].loc = randomLoc;

ships[index].sunk = false;

index++;

}

}

}

bool operational(const Ship ships[]) {

for (int i = 0; i < fleetSize; ++i) {

if (!ships[i].sunk) {

return true;

}

}

return false;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 //Header File (battleship.h)
#include <cstdlib>
#include <ctime>


#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_

//
// data structures definitions
//

const int fleetSize = 6; // number of battleships
const int fieldSize = 6;  // the field (ocean) is fieldSize * fieldSize

                                                  // coordinates (Location) of the ship and shots
struct Location {
        int x;  // 1 through fieldSize
        char y; // 'a' through fieldSize
};

// contains ship's coordinates (Location) and whether is was sunk
struct Ship {
        Location loc;
        bool sunk;
};

//
// initialization functions
//
void initialize(Ship[]); // places every Ship in a Location where x-coordinate is -1
                                                 // and y-coordinate is '*' (a star) to signify
                                                 // that the Ship is not deployed

Location pick(); // generates a random Location
bool match(const 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(const 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_
//battleship.cpp file

#include"battleship.h";
#include <iostream>;

using std::cout; using std::cin; using std::endl;
Location pick() {
        Location loc;
        loc.x = (rand() % 6) + 1;
        switch ((rand() % 6) + 1) {
        case 1:loc.y = 'a'; break;
        case 2:loc.y = 'b'; break;
        case 3:loc.y = 'c'; break;
        case 4:loc.y = 'd'; break;
        case 5:loc.y = 'e'; break;
        case 6:loc.y = 'f'; break;
        }
        return loc;
}
Location fire() {
        Location fireLoc;
        cout << "Input x: ";
        cin >> fireLoc.x;
        cout << "Input y: ";
        cin >> fireLoc.y;
        return fireLoc;
}
void printShip(const Ship s) {
        cout << "Ship is at " << s.loc.x << ",";
        cout << s.loc.y << endl;

        if (s.sunk) {
                cout << "Ship is destroyed" << endl;
        }
        else {
                cout << "Ship is still up" << endl;
        }
}
bool match(const Ship myShip, Location myLoc) {
        return (myShip.loc.x == myLoc.x && myShip.loc.y == myLoc.y) ? true : false;
}
void sink(Ship &myShip) {
        myShip.sunk = true;
}
void initialize(Ship ships[]) {
        for (int i = 0; i < fleetSize; i++) {
                ships[i].loc.x = -1;
                ships[i].loc.y = '*';
                ships[i].sunk = false;
        }
}
void printFleet(const Ship ships[]) {
        for (int i = 0; i < fleetSize; i++) {
                printShip(ships[i]);
        }
}
void deploy(Ship ships[]) {
        int shipIndex = 0;
        Location randomLoc;
        while (shipIndex < fleetSize) {
                Location randomLoc = pick();
                if (check(ships, randomLoc) == -1) {
                        ships[shipIndex].loc.x = randomLoc.x;
                        ships[shipIndex].loc.y = randomLoc.y;
                        ships[shipIndex].sunk = false;
                        shipIndex++;
                }
        }
}
int check(const Ship ships[], Location loc) {
        for (int i = 0; i < fleetSize; i++) {
                if (match(ships[i], loc)) {
                        return i;
                }
        }
        return -1;
}

bool operational(const Ship ships[]) {
        for (int i = 0; i < fleetSize; i++) {
                if (!ships[i].sunk) {
                        return true;
                }
        }
        return false;
//game.cpp file
#include "battleship.h"
#include <iostream>

using std::cout; using std::cin; using std::endl;

int main() {
        cout << "Welcome to Battleship! There are 6 ships on the board and it is your job to sink them. Good Luck!" << endl << endl;
        cout << "The field is a 6x6 square, you will input a number (1-6) and a letter (a-f), Each ship takes up one space and once hit is sunk." << endl;
        cout << "Please input your first guess: " << endl;
        

        



}
Add a comment
Know the answer?
Add Answer to:
In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • i need this in C# please can any one help me out and write the full...

    i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks. Module 4 Programming Assignment – OO Design and implementation (50 points) Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT