How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Scott Your Own Question

Scott
Scott, MIT Graduate
Category: Homework
Satisfied Customers: 3040
Experience:  MIT Graduate (Math, Programming, Science, and Music)
3546829
Type Your Homework Question Here...
Scott is online now
A new question is answered every 9 seconds

N part 1 of this exercise, you will be working as

Customer Question

n part 1 of this exercise, you will be working as individuals in creating one function to interface with an electronic Battle Ship game:
lastNameAttack(int mode, int opponent) is your main driver function used to attack the opponent's ships. This is where much of the strategy for your program goes. (The other function you implement will be described later.) Depending on the mode, you may be starting a new game (BS_MODE_NEW_GAME), or continuing an existing one (BS_MODE_CONTINUE_GAME). It calls the opponent's incomingFire() function just as the opponent will calls yours (initially you will be using an internally defined function, until you complete part 2 of this assignment). Begin writing your attack function using the following code.
// Every student will create their own file to contain the Battle Ship
// functions. A basic skeleton of this file follows.
// NOTE: if you wish to use the rand() function, do NOT call srand()--it is
// already being called by the main() program driver.
#include
#include "battleship.h"
// Sample skeleton for attack function
// Change the name to reflect your own name
int
lastNameAttack(int mode, int opponent)
{
int column;
char row;
int ret;
// You will probably need static variables here to keep track of status,
// Such as where you fired upon already, what ships were hit, etc.
switch(mode) {
case BS_MODE_NEW_GAME:
break;
case BS_MODE_CONTINUE_GAME:
// Student must create some algorithm to decide what row and column to fire
// on, then call exactly as follows.
ret = fire[opponent](row, column);
// If you hit the opponent, you must look at the integer return code
// here to help decide where to attack next!
break;
}
return 0;
}
// Rename the attack function by replacing lastName above with yours.
// Make sure you replace the function name in the line below as well.
// Then insert your name in the last line.
int (*battleship[MaxPlayerCount])(int, int) = {NULL, lastNameAttack};
int (*fire[MaxPlayerCount])(char, int)= {NULL, incomingStub};
char const *playerName[MaxPlayerCount] = {"", "Your full name"};
The fire() function you will be calling accepts two arguments:
A capital letter (char) in the range of 'A' .. 'A'+BS_GRID_ROWS-1 that indicates the row in your grid to attack.
An integer (int) in the range of 1 .. BS_GRID_COLS that indicates the column of your grid to attack.
The return code will be:
0 if there is only open water.
The bit BS_SHIP_HIT will be set, or both BS_SHIP_HIT and BS_SHIP_SANK will be set. In addition, the ship that was hit will be indicated in the lowest four bits of the return code. You may use BS_SHIP_MASK to help extract the number for the ship type.
In order to interface properly with Battle Ship, you will need create the file battleship.h and store it in the same folder as your .cpp file created above. Do not copy this code into your .cpp file.
#ifndef BATTLESHIP
#define BATTLESHIP
const int BS_SHIP_HIT = 0x10; // Ship is hit, or
const int BS_SHIP_SANK = 0x20; // sank (must also | BS_SHIP_HIT)
const int BS_CARRIER = 1;
const int BS_BATTLESHIP= 2;
const int BS_CRUISER = 3;
const int BS_DESTROYER = 4;
const int BS_SUBMARINE = 5;
const int BS_SHIP_COUNT = 5;
const int BS_SHIP_MASK = 0x0F;
const int BS_CARRIER_SIZE = 5;
const int BS_BATTLESHIP_SIZE= 4;
const int BS_CRUISER_SIZE = 3;
const int BS_DESTROYER_SIZE = 2;
const int BS_SUBMARINE_SIZE = 3;
const int BS_MODE_NEW_GAME = 1;
const int BS_MODE_CONTINUE_GAME = 2;
const int BS_GRID_ROWS = 10; // letters A to J
const int BS_GRID_COLS = 10; // numbers 1 to 10
const int MaxPlayerCount = 65; // Maximum size for following arrays
// Pre-defined interactive functions
extern int userIncoming(char, int); //Student may reference these for testing
extern int userBattle(int, int);
extern int incomingStub(char, int);
extern int battleStub(int, int);
// Student must define and initialize these three using MaxPlayerCount
extern int (*fire[])(char, int);
extern int (*battleship[])(int, int);
extern char const *playerName[];
#endif
Everywhere in your program code, you must use the symbolic constants defined in battleship.h file where possible. Do not "hard-code" any literal values—they might change, e.g the number of rows or columns for this exercise may be adjusted before recompiling and testing your program. Your implementation should work for any reasonable number of rows or columns.
Milestone submission (part 1)
Create the .cpp file above replacing text with your name where required. Implement your attack() function so that no critical errors occur. Non-critical errors are allowed at this time.
Final submission (part 1)
Implement your attack() function so that no critical, nor non-critical errors occur. You must also implement some intelligence to your program that observes the return code of the opponents fire() function and acts accordingly. You will find you will experience two modes of play:
Searching for any ship (unbounded search): you are unaware of any un-sunk ships
Searching for a specific ship (focused search): you have already hit a ship somewhere, it is not sunk yet, and you need to finish sinking it.
Furthermore, challenge yourself to implement a version that does the best against the professor's code that you are linking with. There will also be an opportunity during lab to compete with other student versions of Battle Ship prior to the part 2 submission date.
Battle Ship part 2
This exercise is to be completed as an individual.
In part 1, you wrote a function to attack ships. It worked by calling an opponent's incoming() function. Now, you need to implement your own incoming function. Your objective is to perform the ship placement and hit-reporting portion of the game to play a legal game against an opponent. To build this function and integrate it with the Battle Ship driver, you will need to write:
lastNameIncoming(char row, int column) that accepts a coordinate that your opponent is firing upon. The first argument is the row, a letter from 'A' up to the number of rows defined by BS_GRID_ROWS. The second argument is the column, a number from 1 to BS_GRID_COLS. This function has three modes of operation:
If row == '\0' and col == 0, then you are being called to initialize your two-dimensional array which defines your sea map for your fleet of ships. Later, your incomingFire function needs to report on hits made on your fleet. To remember this information between function calls, define a static array for this purpose. (Remember by default, a function's variables use the auto storage class, which means they are automatically created when a function is called, and released when the function returns.) During initialization your code must place your ships referring to the constants BS_CARRIER_SIZE, BS_BATTLESHIP_SIZE, BS_CRUISER_SIZE, BS_DESTROYER_SIZE, and BS_SUBMARINE_SIZE to know the number of cells each ship needs. Ships may only be placed vertically or horizontally, and must only take up linearly contiguous cells in the matrix.
If row is 'A' up to 'A' + BS_GRID_ROWS-1, and col is 1 through BS_GRID_COLS, then you must report whether the attacker has missed, or made a hit. If a hit was made, you must also report which type of ship, and if the ship was sunk. Multiple values may be returned by combining the pre-defined flags together using the bitwise OR operator.
Validation: If row or col do not fall in either of the two categories above, then return 0 without doing any processing.
Insert the following code immediately before or after your attack function. It absolutely needs to be inserted before the definition of the fire[] array at the bottom of your .cpp file.
// Sample skeleton for incoming function
// MAKE SURE to replace incomingStub listed in the fire[] array
// with your function name!
int
lastNameIncoming(char row, int col)
{
// Define your static data structures here such as your grid and any status
// variables you need.
int returnCode = 0; // You definitely need this
// Starting new game
if (row == '\0' && col == 0) {
// need to initialize your grid, ship placements, etc
// otherwise, look at row, col for HIT coordinates, and report
// back to caller what is there.
} else {
// Check: if it is a hit, update the return code
// with the proper values
// If open water, then leave the return code as 0
}
return returnCode;
}
Milestone submission (part 2)
Add the above function definition to your existing Battle Ship part 1. Replace incomingStub defined for fire[] with your lastNameIncoming function name. Define and initialize the static two-dimensional array you need to record your opponent's attempts on your ships and/or to record the placement of your own ships.
If you were unsuccessful in completing Battle Ship part 1, you may still do Battle Ship part 2: in the battleship[] array at the end of the file, use the predefined function battleStub so that you can complete this exercise.
Final submission (part 2)
Implement your lastNameIncoming function so that no critical, nor non-critical errors occur. For each new game, your function must also re-locate your ships, and perform all necessary initializations.
Submitted: 1 year ago.
Category: Homework
Customer: replied 1 year ago.
Posted by JustAnswer at customer's request) Hello. I would like to request the following Expert Service(s) from you: Live Phone Call. Let me know if you need more information, or send me the service offer(s) so we can proceed.
Customer: replied 1 year ago.
here is the code#include
#include "battleship.h"
// Sample skeleton for attack function
// Change the name to reflect your own name
int
lastNameAttack(int mode, int opponent)
{
int column;
char row;
int ret;
// You will probably need static variables here to keep track of status,
// Such as where you fired upon already, what ships were hit, etc.switch(mode) {
case BS_MODE_NEW_GAME:
break;
case BS_MODE_CONTINUE_GAME:
// Student must create some algorithm to decide what row and column to fire
// on, then call exactly as follows.
ret = fire[opponent](row, column);// If you hit the opponent, you must look at the integer return code
// here to help decide where to attack next!break;
}
return 0;
}// Rename the attack function by replacing lastName above with yours.
// Make sure you replace the function name in the line below as well.
// Then insert your name in the last line.
int (*battleship[MaxPlayerCount])(int, int) = {NULL, lastNameAttack};
int (*fire[MaxPlayerCount])(char, int)= {NULL, incomingStub};
char const *playerName[MaxPlayerCount] = {"", "Your full name"};this code into your .cpp file.#ifndef BATTLESHIP
#define BATTLESHIP
const int BS_SHIP_HIT = 0x10; // Ship is hit, or
const int BS_SHIP_SANK = 0x20; // sank (must also | BS_SHIP_HIT)
const int BS_CARRIER = 1;
const int BS_BATTLESHIP= 2;
const int BS_CRUISER = 3;
const int BS_DESTROYER = 4;
const int BS_SUBMARINE = 5;
const int BS_SHIP_COUNT = 5;
const int BS_SHIP_MASK = 0x0F;
const int BS_CARRIER_SIZE = 5;
const int BS_BATTLESHIP_SIZE= 4;
const int BS_CRUISER_SIZE = 3;
const int BS_DESTROYER_SIZE = 2;
const int BS_SUBMARINE_SIZE = 3;
const int BS_MODE_NEW_GAME = 1;
const int BS_MODE_CONTINUE_GAME = 2;
const int BS_GRID_ROWS = 10; // letters A to J
const int BS_GRID_COLS = 10; // numbers 1 to 10
const int MaxPlayerCount = 65; // Maximum size for following arrays
// Pre-defined interactive functions
extern int userIncoming(char, int); //Student may reference these for testing
extern int userBattle(int, int);
extern int incomingStub(char, int);
extern int battleStub(int, int);
// Student must define and initialize these three using MaxPlayerCount
extern int (*fire[])(char, int);
extern int (*battleship[])(int, int);
extern char const *playerName[];
#endif// Sample skeleton for incoming function
// MAKE SURE to replace incomingStub listed in the fire[] array
// with your function name!
int
lastNameIncoming(char row, int col)
{
// Define your static data structures here such as your grid and any status
// variables you need.
int returnCode = 0; // You definitely need this// Starting new game
if (row == '\0' && col == 0) {
// need to initialize your grid, ship placements, etc// otherwise, look at row, col for HIT coordinates, and report
// back to caller what is there.
} else {// Check: if it is a hit, update the return code
// with the proper values// If open water, then leave the return code as 0
}
return returnCode;
}
Customer: replied 1 year ago.
Posted by JustAnswer at customer's request) Hello. I would like to request the following Expert Service(s) from you: Live Phone Call. Let me know if you need more information, or send me the service offer(s) so we can proceed.
Expert:  GLENN replied 1 year ago.

Hi

Are these math questions?

Customer: replied 1 year ago.
No this is a C++ programming question computer program CISP360, not math
Expert:  GLENN replied 1 year ago.

ah ok, I will just opt out so that computer experts can help you

Customer: replied 1 year ago.
Thanks I just submit my work by myself anyway thanks for trying.

Related Homework Questions