Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use C for that Design a simple battleship game. One player plays against the computer. The computer picks at randoma number between 0 and

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please use C for that

Design a simple battleship game. One player plays against the computer. The computer picks at randoma number between 0 and 9(inclusive). The number the computer picks is the location of the battleship. The player tries to guess the location of the battleship in a maximum of 3 tries. To guess the location, the player enters a number (0-9). The computer displays the game board as a row of dots (periads). If the player finds the battleship, the computer displays the game board with an in the position where the ship was hit. If the player misses, the computer displays an 'm. All ather locations remain dats. Example run Computer Battleship (Sinplified Version) Try to find the battleship and sink her. To fire a torpedo, enter a location (nunber between e and 9) You have three torpedos. Let's see if you have any ski11 (e 9) Enter a location between e and 9:3 Missed Try again. (e 9) Enter a location between e and 9:5 BOOM! A hitt 9) You win! (Gare Over.) You may have already noticed this program looks a lot like number guessing games you've likely seen in a code example or demonstration. The main difference here is how it displays the information to the user. Arrays (From the Introduction) Recall an array is a series af variables of the same data type and same name stored in consecutive memary locations int gameBoard 10// declare array of 10 integers, called garneBoard Array Usage Access each element of an array with an index: x- gameBoard4 assign the value in the St location of gameBoard to The array index is a hard-coded, fixed number (4) in this example. However, the index could have been a variable: initialize the array (set all locations to not explared ganoBoard(x] "."; // set the xen elenont of ganoBoard toe Random Numbers C includes some functions for generating random numbers. The functions are defined in stith You will need to include the stdhb.h header if it is not already. To use the time functions, also include time.h. srand (tis(NULL)); / seed randon number generator dth current tine seconds r rand /I generate a randon number and store in The random number generator is nat realy random. If you do not "seed the generator with a unique number, the rand() function will return the same sequence of "random" numbers each time you run your program. The srand) function only needs to be called once in your program To restrict the range of numbers, you can use the modulus operator: rand() % 18; // random number range e.g Writing the Code The number guessing game from a previous class demo is included with this lab. You may modify this code to play battieship as described above. Or, you may choase to write your own program entirely. Each function should do one pecific task. Ideally, you should have anly function colls and oops (when needed) in main). Storing Battleships and Torpedoes You can use just one array to store ALL garme runtime info, or you can use mutiple arrays. Whatever your choice, decide this during the Design phase! Don't try to change something this fundamental after you've written the actual game code. It could cause you to waste a lot of time, and you could end up with a confusing mess. Exumple 1:Multiple Arrays char playerBoard| 101; // stores user info: unexploredhit."N, or miss M' char shipboard[10]; stores ship locations: water-0 ship-1 If you use multiple arrays, your displayBoard() function can simply print on screen whatever is in the playerBoardl] array. Example 2: One Array char garmeBoard 10: I/water 0; ship 1; hit-"h'; miss-"M If you use one array, you'll need to write your displayBoard() function to print the actual value in the array psition if it's an h' or wt', but print a if it's any other value. Passing an Array to a Function The game arrays) store the game runtime information. Since game info is needed throughout the program run, game array(s) should be declared in main). When a function needs access to a game array, you simply call that function with the name of the array. You will need a function that displays the game board. Therefore, you'll have to pass the garmeSoard array into that function. To pass an array, declare the function like this: void showGameBoard char theBoard) I/don't forget the asterisk you use fLe. call the function lke this: and access gameBoard's values inside the function like this (same as if the array was local to the function): printf", theBoard[xl: NOTE: "void" is shown in the above example, but your function could return a value if you wish Lke the function in the demo code, this function need only compare two simple integers and return a 1 if they match or O otherwise. You will need a function to update the gameBoard with .m'orT4",according to what the computeResult0 function returns. Your function might look like this: void updateBoard(char "theBoard, int location, char valuel f verify location is valid (0-9) //place value into theBoard at index location Notice you could also have a function called "initBoard) instead of a game board initialization loop in maln(). initBoard() called at the start af te ga e and sets all locations to unexplored tn addition, the initSoard() function could call your updateBoard) function ten times (each time with the location going from 0 to 9) and withas the vaue. You will need to make other modifications throughout the program to make it work as described. Don't forget to limit the number of allowed guesses to 3 Design, Design, Design! Remember to design your program first. You shauld work out all of the logic and details on how the game will work, including how the game will store all gameplay information (variables], how it will handle bad input, and what it will display on screen before you write actual code, work on paper first if helps, and then transfer the final pseudo-code into a c file to serve as a scaffold from which you write code Part 2: A Ship Larger than One You should have the Simple Battleship game working properly. Now, modify your code to generate ships that take up to four consecutive locations. Think about how this could work How will you store the ship's locations (now that it requires more spots)? . Does the computeResultt) function need to change? You will need to change the code that determines the game is over (won or lost). determines the game is over (won or last). HINTS Get a random number to determine the ship size (1-4) -Get a second random number to find the ship's starting location (0-9). What if the ship is too big for the start location (ie. ship wauld extend beyand the end af the board)? o The ship needs to take up consecutive spaces (left or right) of the random number. [Otherwise,you would have multiple ships.) You'l have to check to be sure ship doesn't go off of the game board. When the player makes a hit, the game is not over until the rest of the ship is hit (or the player shoots all torpedoes.) You might need additional lagic tests For example, in a game with only five maximum guesses, you can't win if you miss the first two tries and the ship is four spaces long. o

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions