Question

In this homework, you will implement a simple version of the game Battleship, in which the player tries to destroy ships hidd

I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code with comments:

-------------------------------------------------------------------------------

#include <iostream>

#include <fstream>

using namespace std;

void battleship(bool map[4][4], string coordinates){

  //finding indices from the coordinates using some ascii math

  int x1 = coordinates[0] - 'A';

  int y1 = coordinates[1] - '1';

  int x2 = coordinates[3] - 'A';

  int y2 = coordinates[4] - '1';

  int x3 = coordinates[6] - 'A';

  int y3 = coordinates[7] - '1';

  //checking for valid coordinates

  bool validCoord = true;

  if(x1 < 0 || x1 > 3)validCoord = false;

  if(x2 < 0 || x2 > 3)validCoord = false;

  if(x3 < 0 || x3 > 3)validCoord = false;

  if(y1 < 0 || y1 > 3)validCoord = false;

  if(y2 < 0 || y2 > 3)validCoord = false;

  if(y3 < 0 || y3 > 3)validCoord = false;

  if(!validCoord) {

      cout << "Invalid coordinates entered. Game Over!\n";

      return;

  }

  else {

      //count how many targets remaining

      int count = 3;

      if(map[y1][x1])count--;

      if(map[y2][x2])count--;

      if(map[y3][x3])count--;

      if(count > 0){

          cout << count << " targets remaining. You lost!" << endl;

          return;

      }

      else {

          cout << "You won the battle!" << endl;

      }

  }

}

int main(){

  string filename;

  cin >> filename;

  //taking input from given file

  ifstream in(filename);

  bool map[4][4];

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

      for(int j = 0; j < 4; ++j){

          in >> map[i][j];

      }

  }

  //taking input of coordinates

  string coordinates;

  cin.ignore();//ignoring the newline character.

  getline(cin, coordinates);

  //calling required function

  battleship(map, coordinates);

}


-------------------------------------------------------------------------------

Sample IO:

E mapfile.txt x main.cpp 1 010 0 2 100 O 3 000 0 1000 5 Terminal File Edit View Search Terminal Help [unseen@pc]01:35 PM_$ ~/

Add a comment
Know the answer?
Add Answer to:
I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In...
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
  • I need help finishing my C++ coding assignment! My teacher has provided a template but I...

    I need help finishing my C++ coding assignment! My teacher has provided a template but I need help finishing it! Details are provided below and instructions are provided in the //YOU: comments of the template... My teacher has assigned a game that is similar to battleship but it is single player with an optional multiplayer AI... In the single player game you place down a destroyer, sub, battleship, and aircraft carrier onto the grid.... Then you attack the ships you...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • This is for my c++ class and I would really appreciate the help, Thank you! Complete...

    This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

  • I have most of the code for this assignment but need some help getting the rest....

    I have most of the code for this assignment but need some help getting the rest. Go to bottom to see what I have. Please help me figure out the foreach loops and the put() statement. In this assignment, you will be using a new data structure, HashMaps, to repeat Assignment 8.3. Main Method (5 points) Declare and initialize an HashMap with values of type of type Employee, and keys of type integer. In a while loop, keep initializing objects...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good...

    I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good to me but it will not run. Please help... due in 24 hr. Problem As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. Currently, there is a test...

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
ADVERTISEMENT