I need some help with my code in C The problem is listed bel
I need some help with my code in C++. The problem is listed below along with tips I was given as well as my attempt at the code and the neccesary files.
Objective: Develop a C++ program to play a text-based, computerized-version of BATTLESHIP™ using an instructor-provided library (battleship.o) and header file (battleship.h).
Discussion: I have provided you this file to get you started ,battleship_main.cpp , so copy it to the directory where you usually write your programs.1 By the way, you’ll also want to copy battleship.h and battleship.o. Notice that the file battleship_main.cpp has comments to give you the general idea on what order you need to do things for the game. Use those comments and the descriptions for the functions below, to figure out how to use the functions to build the main program for the game.2 The functions available to you are listed in the header file battleship.h (and also below).
They are:
void welcome(): This function clears the screen and writes the message“Welcome to BATTLESHIP” to the screen several times. Example: welcome();
void clearTheLine(int lineNum): This function erases all characters on linelineNum, where lineNum is the line number, counting from the top, usuallybetween 0 and 23. Example: clearTheLine(5);
void clearTheScreen(): This function erases all the characters on your terminalscreen leaving you with a blank screen. Example: clearTheScreen();
void pauseForEnter(): This function writes the message “Press enter tocontinue” on line 23 and waits for you to just that (actually, it’ll let you proceedafter typing any character). Example: pauseForEnter();
string getResponse(int lineNum, int colNum, string prompt): This functionlets you write a user prompt, prompt, to the user beginning at line lineNum andcolumn colNum, and then get the user’s response as a string value. Use thisfunction to get the human player’s move.
Example: userMove = getResponse(16, 0, “Please enter your move”);
1 You can copy a file with the command: cp source_file destination_directory, where source_file is the name of the file you want to copy and destination_directory is the name of the directory where you want to put the copied file. For example, to copy the file /home/faculty/ggreve/CS1210/public/battleship_main.cpp to your current directory, type the command: cp /home/faculty/ggreve/CS1210/public/battleship_main.cpp . (note the trailing “.” In the line above, this means “current directory”).
2 For example, the second comment (after the header comments) in battkeship_main.cpp says to “Welcome the player to the game.” After looking at functions available for you to use and their descriptions, you decide that the function welcome() would do this task, so you edit battleship_main.cpp to call welcome() immediately after the comment.
void writeMessage(int lineNum, int colNum, string message): This function lets you write message to the screen at line lineNum, column colNum. Use this function to give feedback on the game.
Example: writeMessage(18, 0, “The move you just typed is invalid”);
void initializeBoard(Board &gameBoard): This function creates a new game board (you will need two of these in the game, one for the human player and one for the computer player). NOTE: this function uses a new variable type called Board that you’ve not seen yet. This variable keeps track of each player’s game situation. You can declare a Board variable like any other variable (e.g., Board computerBoard;). Example: initializeBoard(computerBoard);
void displayBoard(int lineNum, int colNum, int playerType, Board gameBoard): Assuming you have declared and initialized a Board variable, displayBoard() will show it’s current configuration. The parameter playerType is either the constant HUMAN or COMPUTER to let the function know whose board is being displayed. The parameters lineNum and colNum give the upper left line and column position of the board.
Example: displayBoard(1, 1, HUMAN, humanBoard);
int checkMove(string move, Board gameBoard, int &row, int &col): This function validates a player’s move. If the move is acceptable, it returns the constant VALID_MOVE and the variables row and col can be used in the call to playMove(). If the move string is badly formed (e.g., the user typed “10 A” instead of “A 10”) or move has already been used, then checkMove() returns the constants ILLEGAL_FORMAT or REUSED_MOVE, respectively. In this case, row and col contain the unusable values -1 and -1, respectively. NOTE: you need to keep checking the move (and asking for a new one, if necessary) until you get a VALID_MOVE.
Example: result = checkMove(move, humanBoard, row, col);
int playMove(int row, int col, Board &gameBoard): This function is how you execute a move with the returned value being the result (e.g., hit or miss) of that move. Row and col are the move you want to make (you got these from checkMove()) and gameBoard is the board on which you want to make the move. Your program should call playMove() after using checkMove() to make sure the user (or computer) picked a valid move and set row and col to valid numbers. Example: result = playMove(row, col, humanBoard);
void writeResult(int lineNum, int colNum, int result, int playerType): This function uses the value returned from playMove() and writes a text message on line lineNum, column colNum telling you whether the last move was a “hit” or a “miss” (and if the result was a “hit”, what ship was hit). The parameter playerType is either the constant HUMAN or COMPUTER to let the function know who made the move. For example, suppose your program had the code: result = playMove(row, col, humanBoard);. You then take the variable result and pass it to writeResult() to report the outcome of the move.
Example: writeResult(20, 0, result, COMPUTER);
bool isASunk(int result): This function returns a true or false indicating whether or not the result of the last playMove() sunk a ship. For example, suppose your program had the code: result = playMove(row, col, humanBoard);. You then take the variable result and pass it to isASunk() to determine if you just sunk a ship.
Example: if (isASunk(result)) { /* do something */ };
string randomMove(): This function allows the computer to pick a random move. Note the computer’s move must be validated, because it may pick a move that’s already been used. Example: computerMove = randomMove();
Additional Details:
a. Submit your source code by the beginning of class on due date. Turn in a listing of your program in class that same day.
b. All work must be done on john.
c. Compile your main program with the command: g++ -c battleship_main.cpp
This assumes your main program file is called battleship_main.cpp, if you called it something different, use that name instead. This will create an object file battleship_main.o which you will use as directed below.
d. Link your program with: g++ -o battle battleship_main.o battleship.o -lcurses
The above command combines the code you’ve written (i.e., battleship_main.o with the code your instructor has written, battleship.o, with some additional system libraries for screen control and colored text, -lcurses, to produce an executable file called battle which you can run. Note: you must have already copied battleship.o to the same location as your program as directed above.
Hints:
a. Solve this problem step-by-step by implementing the directions provided by each comment in battleship_main.cpp one comment at a time. This way you can learn what each function does little-by-little.
b. You will need to use a while statement to validate the move for both the computer and the user (these while statements, but not their bodies, are provided for you).
c. The while statement for the overall game is also already provided for you. You just need to figure out how we know when we’re done. The comment in the if statement (within the while loop) which I’ve provided gives you a major clue. Also, you’ll need to declare two variables to keep track of how many ships the user and computer have sunk.
Below is the battleship.h file.
#ifndef BATTLESHIP_H
#define BATTLESHIP_H
// include files for implementing battleship
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <curses.h>
#include <kasbs.h>
using namespace std;
// use these constants to indicate if the player is a human or a computer
// battleship board size is 10x10 grid
const int BOARDSIZE = 10;
// data structure for position
struct Position {
int startRow; // ship\'s initial row
int startCol; // ship\'s initial column
int orient; // indicates whether the ship is running across
// or up and down
};
// data structure for ship
struct Ship {
Position pos; // where the ship is on the board
int size; // number of hits required to sink the ship
int hitsToSink; // number of hits remaining before the ship is sunk
char marker; // the ASCII marker used to denote the ship on the
// board
};
// a game board is made up of a 10x10 playing grid and the ships
struct Board {
char grid[BOARDSIZE][BOARDSIZE];
Ship s[6]; // NOTE: the first (zeroth) position is left empty
};
// use these constants for designating to which player we are referring
const int HUMAN = 0;
const int COMPUTER = 1;
// use these constants for deciding whether or not the user gave a proper move
const int VALID_MOVE = 0;
const int ILLEGAL_FORMAT = 1;
const int REUSED_MOVE = 2;
// functions for screen control and I/O
void welcome(bool debug = false, bool pf = false);
void clearTheLine(int x);
void clearTheScreen(void);
void pauseForEnter(void);
string getResponse(int x, int y, string prompt);
void writeMessage(int x, int y, string message);
void writeResult(int x, int y, int result, int playerType);
void displayBoard(int x, int y, int playerType, const Board &gameBoard);
// functions to control the board situation
void initializeBoard(Board &gameBoard, bool file = false);
int playMove(int row, int col, Board &gameBoard);
// function to tell what happened in the last play_move() command
bool isAMiss(int playMoveResult);
bool isAHit (int playMoveResult);
bool isASunk(int playMoveResult); // formerly named isItSunk()
int isShip (int playMoveResult);
// misc game functions
string randomMove(void);
int checkMove(string move, const Board &gameBoard, int &row, int &col);
void debug(string s, int x = 22, int y = 1);
string numToString(int x);
#ifdef BATTLESHIP_BACKWARD_COMPATIBILITY
// former function signatures
void debug(int x, int y, string s);
bool isItSunk(int playMoveResult);
// a debug macro
#ifdef DEBUG
#define DEBUGOUT(str) debug (22, 1, (str));
#else
#define DEBUGOUT(str)
#endif // DEBUG
#endif // BATTLESHIP_BACKWARD_COMPATABILITY
#endif // BATTLESHIP_H
Finally below this is battleship_main.cpp. This also is my start at the code.
#include \"battleship.h\"
int main() {
// variable declarations (you\'ll need others, of course)
// Note: a good idea is the declare them as you need them to limit scope
bool done = false;
string move;
// Welcome the player to the game
void welcome();
// Initialize the game boards
int compBoard, humanBoard;
void initializeBoard (Board compBoard);
void initializeBoard (Board humanBoard);
// Play the game until one player has sunk the other\'s ships
while(!done) {
// Clear the screen to prepare show the game situation before the moves
// Display the game board situation
void clearTheScreen();
void displayBoard(int lineNum, int colNum, int playerType, Board gameBoard);
// Get and validate the human player\'s move
int checkMove(string move, Board humanBoard, int &row, int &col);
// BTW, in the following while loop (and the if statements also), I have
// put a \"0\" in with the comments. This is because in order for the
// code to compile, you need to have something in between the parentheses
while(0/* need to make sure that the human player\'s move is valid*/) {
;
}
// Get and validate the computer\'s move
int checkMove(string move, Board compBoard, int &row, int &col);
while(0/* need to make sure that the computer\'s move is valid*/) {
string randomMove()
;
}
// Execute both moves
int playMove(int row, int col, Board &humanBoard);
int playMove(int row, int col, Board &compBoard);
// Clear the screen to show the new game situation after the moves
void clearTheScreen();
// Display the new game board situation
void displayBoard(int lineNum, int colNum, int playerType, Board gameBoard);
// Display the move choices each player made
// Show the results of the moves
void writeResult(int lineNum, int colNum, int result, int playerType);
// Take note if there are any sunken ships
bool isASunk(int result);
// determine if we have a winner
if(0/* has either player sunk five ships? */) {
// if one of the player\'s has sunk five ships the game is over
done = true;
} else {
// pause to let the user assess the situation
pauseForEnter();
}
}
// Announce the winner
if(0/* the human was the winner */) {
cout<<\"You won!!!\"<<endl;
/* You won!!! */;
} else if (0/* the computer was the winner */) {
cout<<\"The computer won :(\"<<endl;
/* The computer won :( */;
} else {
cout<<\"The game was a tie\"<<endl;
/* The game was a tie */;
}
// pause to let the result of the game sink in
return 0;
}
So there it is Before I changed it all the program had were comments. I know this is not finished and doesn\'t compile. I\'m not sure if my implementation is correct or if I\'m using everything correctly.
Solution
Answer:
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void displaygamerwindow();
void shiplocation();
void systemlocation();
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken);
void systemwindow();
void locationselect(int capacity, int left, int up, int word_input);
char shiplabel[10], systemlabel[10];
int x,locatevertical, locatehorizontal,locateupwards,locateleftwards,computevertical, computehorizontal,computeupwards,computeleftwards;
char gamerboard [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char hiddensystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
char displayedsystem [10][10]=
{
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00},
};
main()
{
HANDLE result;
result = GetStdHandle(STD_OUTPUT_HANDLE);
cout<<\"Loading Please Wait : \"<<endl;
Sleep(1000);
system(\"CLS\");
cout<<\"Loading Please Wait :\"<<endl;
Sleep(1000);
system(\"CLS\");
cout<<\"Loading Please Wait :\"<<endl;
Sleep(1000);
system(\"CLS\");
cout<<\"Loading Please Wait :\"<<endl;
Sleep(1000);
system(\"CLS\");
SetConsoleTextAttribute(result, FOREGROUND_BLUE|BACKGROUND_CYAN);
cout<<\"Lets Play BattleShip Game :\"<<endl;
SetConsoleTextAttribute(result, FOREGROUND_CYAN|FOREGROUND_GREEN|FOREGROUND_BLUE);
systemlocation();
systemwindow();
return 0;
}
void systemwindow()
{
computehorizontal=0;
computevertical=0;
cout << \"\" << endl;
cout << \" System Board \" << endl;
cout << \".........................................................\" << endl;
cout << \" |0|1|2|3|4|5|6|7|8|9|\" ;
cout<<\'\ \';
cout << computehorizontal << \"|\";
do
{
if(computehorizontal == 9 && computevertical == 10)
{break;}
if(computevertical > 9 && computehorizontal < 9)
{
cout << \"|\"<< endl;
computehorizontal = computehorizontal + 1;
cout << (computehorizontal) << \"|\";
computevertical = 0;
}
if(computehorizontal < 10)
{
if(computevertical < 9)
{
cout << hiddensystem[computehorizontal] [computevertical] << \" \";
}
if(computevertical > 8)
{
cout << hiddensystem[computehorizontal] [computevertical];
}
}
computevertical = computevertical + 1;
}
while(computehorizontal < 10);
computehorizontal = 0;
computevertical = 0;
cout << \"|\" << endl;
cout << \"................................................................\" << endl;
cout << \"\" << endl;
}
void systemlocation()
{
srand(time(NULL));
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(systemlabel,\"Carrier\");
break;
case 4:
strcpy(systemlabel,\"Battleship\");
break;
case 3:
strcpy(systemlabel,\"Kruiser\");
break;
case 2:
strcpy(systemlabel,\"Destroyer\");
break;
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=int(systemlabel[0]);
systemoption(x,computeleftwards,computeupwards, (int(systemlabel[0])));
}
do
{
computeleftwards=rand()%(10);
computeupwards=rand()%(10);
}
while((hiddensystem[computeleftwards][computeupwards]!=00));
hiddensystem[computeleftwards][computeupwards]=00;
systemoption(3,computeleftwards,computeupwards, 00);
}
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken)
{
srand(time(NULL));
int select,circle;
select=1+rand()%(4);
switch (select)
{
case 1:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection-circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection+circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(leftsidedirection-(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection+circle][upwards]=word_taken;}
systemwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(leftsidedirection-(capacity1-1))>(9))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards+(capacity1-1)) <= (9) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards+ circle]=word_taken;}
systemwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity1;circle++)
{
if(hiddensystem[leftsidedirection-circle][upwards]!=00 || int(upwards-(capacity1-1))<(0))
{
hiddensystem[leftsidedirection][upwards]=00;
x=x+1;
break;
}
if(int(upwards-(capacity1-1)) >= (0) && x==capacity1)
{
for(circle=1;circle<capacity1;circle++)
{hiddensystem[leftsidedirection][upwards-circle]=word_taken;}
systemwindow();
}
}
break;
}
}
void displaygamerwindow()
{
locatehorizontal=0;
locatevertical=0;
cout << \"\" << endl;
cout << \" Gamer \'s Board\" << endl;
cout << \".................................................................\" << endl;
cout << \" |0|1|2|3|4|5|6|7|8|9|\" ;
cout<<\'\ \';
cout << locatehorizontal << \"|\";
do
{
if(locatehorizontal == 9 && locatevertical == 10)
{break;}
if(locatevertical > 9 && locatehorizontal < 9)
{
cout << \"|\"<< endl;
locatehorizontal = locatehorizontal + 1;
cout << (locatehorizontal) << \"|\";
locatevertical = 0;
}
if(locatehorizontal < 10)
{
if(locatevertical < 9)
{
cout << gamerboard[locatehorizontal] [locatevertical] << \" \";
}
if(locatevertical > 8)
{
cout << gamerboard[locatehorizontal] [locatevertical]<<flush;
}
}
locatevertical = locatevertical + 1;
}
while(locatehorizontal < 10);
locatehorizontal = 0;
locatevertical = 0;
cout << \"|\" << endl;
cout << \"................................................................................\" << endl;
cout << \"\" << endl;
}
void locationselect(int capacity,int left,int up,int word_input)
{
int select,circle;
cout<<\"Do you need any ship to face: \"<< \'\ \' << \"1. UP 2.DOWN 3.RIGHT 4.LEFT\"<<\'\ \';
cin>>select;
switch (select)
{
case 1:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<\"Don\'t place ships illegally...\"<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left-circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 2:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left+circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<\"Please check ships place correctly.\"<<endl;
x=x+1;
break;
}
if(int(left-(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left+circle][up]=word_input;}
displaygamerwindow();
}
}
break;
case 3:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(left-(capacity-1))>(9))
{
gamerboard[left][up]=00;
cout<<\"Please check ships place correctly\"<<endl;
x=x+1;
break;
}
if(int(up+(capacity-1)) <= (9) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up+ circle]=word_input;}
displaygamerwindow();
}
}
break;
case 4:
for(circle=1;circle<capacity;circle++)
{
if(gamerboard[left-circle][up]!=00 || int(up-(capacity-1))<(0))
{
gamerboard[left][up]=00;
cout<<\"Please check ships place correctly\"<<endl;
x=x+1;
break;
}
if(int(up-(capacity-1)) >= (0) && x==capacity)
{
for(circle=1;circle<capacity;circle++)
{gamerboard[left][up-circle]=word_input;}
displaygamerwindow();
}
}
break;
}
}
void shiplocation()
{
for (x=5;x>=2;x--)
{
switch(x)
{
case 5:
strcpy(shiplabel,\"Carrier\");
break;
case 4:
strcpy(shiplabel,\"Battleship\");
break;
case 3:
strcpy(shiplabel,\"Kruiser\");
break;
case 2:
strcpy(shiplabel,\"Destroyer\");
break;
}
do
{
cout<<\"Locating one end of the \"<<shiplabel<<\".\"<<\'\ \';
cout<<\"Place left side coordinate followed by top :\"<< \'\ \';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=int(shiplabel[0]);
displaygamerwindow();
locationselect(x,locateleftwards,locateupwards, (int(shiplabel[0])));
}
do
{
cout<<\"Locate one end of the Submarine.\"<<\'\ \';
cout<<\"Place left side coordinate followed by top \"<< \'\ \';
cin>>locateleftwards;
cin>>locateupwards;
}
while(locateleftwards<0||locateleftwards>9||locateupwards<0||locateupwards>9||(gamerboard[locateleftwards][locateupwards]!=00));
gamerboard[locateleftwards][locateupwards]=00;
displaygamerwindow();
locationselect(3,locateleftwards,locateupwards, 00);
}










