Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to complete this program I dont know much about the battleship game: Background: Stepwise refinement is a low level design technique in

Please help me to complete this program I dont know much about the battleship game:

Background: Stepwise refinement is a low level design technique in which the programmer writes pseudo-code for what the program is supposed to do in steps, expanding non-obvious steps at each iteration of the process. Eventually, the stepwise refinement will be an almost step by step guideline to the implementation of the program. Objective: The main objective of this assignment is to assess students ability to apply the stepwise refinement process to develop a new algorithm and carry that through to the implementation of the program. Implementation must follow the top-down design approach, where the solution starts by describing the general functionality of a game. Next, more details are provided in successive steps to refine the implementation. Problem Description: Assume you are hired by a game developing company to write a new computer game for kids. This company has decided to create a version of battleship with a few different rules hoping that this new game will be more entertaining. If you are unfamiliar with the original game of battleship, please learn how to play. Here is a link that includes the instructions: https://www.hasbro.com/common/instruct/Battleship.PDF This modified version of battleship that you required to create is very much like the original game. The differences between the game are as follows: ? This new version of battleship is played on a 6x6 board instead of 10x10 ? There will only be two ships in this version ? The ships will only be of size 2 ? A healthy ship is represented by O a sunk portion of a ship is represented by X There are several steps that you will need to implement: First ask the player to enter the coordinates of the first ship. Make sure that this input is on the board and actually makes up a ship of length 2. Next ask the player to enter the coordinates of the second ship. Again, make sure that these inputs are on the board and make up a ship of length 2. However, this time, also check that the ships are not on top of each other. Next you need to have the computer select the coordinates for his ship. Have the computer first generate a coordinate randomly. After this have the computer pick a random direction to extend the ship. Make sure that these coordinates are valid. Use this same logic to generate the position of the computers second ship making sure the ships do not overlap again. Now the game will begin. Have the human select a coordinate the attack and check if it is a hit or miss. Next have the computer randomly select a location to attack. If it computer registers a hit, print the board to show the outcome of the attack. Once the computer or the human has sunk both ships, print the winner and end the game. You need to write the stepwise refinement prior to implementing your game so you can show the project manager what you plan to do. You can use the lecture notes to read more about stepwise refinement. General Instructions: ? You are given a code that compiles with no errors. ? Functions to update the board are already provided, you will need to update the data structures that correspond to places on the board. ? You only need to implement the playGame() function using the step-wise refinement approach. ? Your job is to ask the human player for input and check that it is valid ? You also need to complete the logic for the computer player Task: Your task is to apply the technique of stepwise refinement to design an algorithm for the playGame()function and play the game of modified battleship. Your playGame() function should not break the provided working code, and it should interact with and call other given functions. Help: This assignment requires development of the program using step-wise refinements to write the C++ program which implements the battleship game as described. The game will be played by a human and computer player. Step-wise refinement is a technique used for writing programs. The process starts with a simple statement describing the main functionality of the program. Thereafter, the programmer repeatedly and gradually expands this statement into two or more statements. Next, the new statement(s) is/are repeatedly expanded into more detailed statement(s) with the goal of moving towards the final implementation of your program. Use a text editor (e.g., the Code::Blocks editor or Notepad) to write the first pseudo-code statement of the problem. Next, add more statements that support the implementation of that first pseudo-code statement. Each time you refine a step, copy and paste the current version of the program design to the end of the text file, then make the change within that pasted version. Stick to the instructor's convention of using a different number of *s to indicate the level of expansion applicable to each statement (see example below). When the design is complete, the text file will contain a complete record of the design process used to reach the final design. The text file you submit may be quite lengthy and should show the entire process. Do not remove anything and do not just submit the final iteration. Below is a partial example of stepwise refinement (Note it is only a partial example and has nothing to do with the program for this assignment.): .(this process would be continued until every statement is adequately refined using pseudo-code). And this is the main .cpp file:

#include  #include  #include  #include  #include  using namespace std; // check if a certain position on the board is occupied by a human ship. 1 indicates health ship, 2 indicated sunk portion of ship string checkPos(int x, int y, int human[][6]) { string str = ""; if(human[x][y] == 1) str = "O"; else if(human[x][y] == 2) str = "X"; else str = " "; return str; } //print the human's side of the board. This will show if the ships placed are hit void printBoard(int human[][6], int computer[][6]) { cout << " "; cout << " ___ ___ ___ ___ ___ ___ " << endl; cout << " | | | | | | |" << endl; cout << "5 | " << checkPos(0, 5, human) << " | " << checkPos(1, 5, human) <<" | " << checkPos(2, 5, human) << " | " << checkPos(3, 5, human) << " | " << checkPos(4, 5, human) << " | " << checkPos(5, 5, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "4 | " << checkPos(0, 4, human) << " | " << checkPos(1, 4, human) << " | " << checkPos(2, 4, human) << " | " << checkPos(3, 4, human) << " | " << checkPos(4, 4, human) << " | " << checkPos(5, 4, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "3 | " << checkPos(0, 3, human) << " | " << checkPos(1, 3, human) << " | " << checkPos(2, 3, human) << " | " << checkPos(3, 3, human) << " | " << checkPos(4, 3, human) << " | " << checkPos(5, 3, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "2 | " << checkPos(0, 2, human) << " | " << checkPos(1, 2, human) << " | " << checkPos(2, 2, human) << " | " << checkPos(3, 2, human) << " | " << checkPos(4, 2, human) << " | " << checkPos(5, 2, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "1 | " << checkPos(0, 1, human) << " | " << checkPos(1, 1, human) << " | " << checkPos(2, 1, human) << " | " << checkPos(3, 1, human) << " | " << checkPos(4, 1, human) << " | " << checkPos(5, 1, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "0 | " << checkPos(0, 0, human) << " | " << checkPos(1, 0, human) << " | " << checkPos(2, 0, human) << " | " << checkPos(3, 0, human) << " | " << checkPos(4, 0, human) << " | " << checkPos(5, 0, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " 0 1 2 3 4 5 " << endl; } void initializePieces(int human[][6], int computer[][6]) { for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) { human[i][j] = 0; computer[i][j] = 0; } } void playGame(int human[][6], int computer[][6]) { ///complete this function } int main() { int human[6][6]; int computer[6][6]; initializePieces(human, computer); //playGame(human, computer); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions