Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Battleship Matlab. These are the instructions : The Game Board: The board is of

ID: 3689462 • Letter: B

Question

Battleship Matlab. These are the instructions : The Game Board: The board is of fixed, square size 9 x 9. Each square of the board is called a cell. Four (4) ships are placed at various locations on the board. Each ship occupies a number of adjacent cells on the board, arranged either horizontally or vertically. Each ship has a different size (number of cells), which is defined by its type: battleship = 4, submarine = 3, cruiser = 2, destroyer = 1. The ships cannot overlap (i.e., any given square/cell on the board can be occupied by at most one ship). To place the ships on the board, ask the user to input a starting location/cell for each ship. Using that starting location, and taking into consideration the length of the ship and the constraints of the board, your program should decide (at random) whether to place the ship horizontally or vertically, and place the ship accordingly on the board. Note: if your program evaluates the scenario and comes to the conclusion that the ship cannot be placed on the board at the given starting location, the program should ask the user for a new starting location. How the game is played: Your program should repeatedly generate random locations on the board (row, column), which will become the computer’s guesses. This is an automatic operation, no user input is allowed for the guesses. If the computer guesses a location occupied by a ship, the program should indicate a ship has been hit. Otherwise, the location should be marked as a miss. A ship is sunk when the computer fired (guessed) on ALL the cells occupied by that ship. Difficulty levels: At the beginning of the game, ask the user to choose between the following difficulty levels: Easy-­as-­Pie, Medium-­Sauce and Watery-­Death (you could implement a menu, but it’s not mandatory). To make the game more interesting, the computer will have a limited number of missiles to sink all of the ships, according to the difficulty level: A. Easy-­as-­Pie: the computer has 29 missiles available, and every 7 guesses should automatically be a hit. B. Medium-­Sauce: the computer has 39 missiles available, and every 6 guesses should automatically be a hit. C. Watery-­Death: the computer has 49 missiles available, and every 5 guesses should automatically be a hit. Until when? The game ends when: 1. The computer has run out of missiles. If the computer has run out of missiles, record a win for you (the user), but give the computer the choice to play again on the same board setup (do NOT re-­initialize the board). 2. The computer has sunk all your ships. At the end of the game, when the last location of the last ship is discovered, record a win for the computer. A congratulatory message should include the number of moves the computer took to solve the board. P/S: can you please explain to me how to do this and i am thinking of making a figure for the game board and everytime the computer hits the user's battleship the figure will change colour to indicate that it has been hit.

Explanation / Answer

Hi below i have written the sample code for Battleship on MATLAB for your reference, %Board Size global SIZE = 9; %init board with zeros Board = zeros(SIZE); plotBoard = Board; plotBoard(SIZE + 1,:) = 0; %boost rows by one and assign as zeros plotBoard(:,SIZE + 1) = 1; %boost cols by one and assign as ones (pin high) Board = plotBoard; %return new boosted size to Blank main board figure(1); %work with figure 1 if useSounds [Y,fs,Nbits]=wavread('jp.wav'); sound(Y,fs,Nbits); end %------------Place Ship-------------% %Pick initial spot i = 1+SIZE*rand; j = 1+SIZE*rand; %Pick dir loop = 1; while (loop) %dir is done explicitly to maintain use of doubles for math functions temp = rand * 3; dx = 0; if (temp > 2) dx = 1; elseif (temp < 1) dx = -1; end temp = rand * 3; dy = 0; if (temp > 2) dy = 1; elseif (temp < 1) dy = -1; end i0 = dx * 4 + i; j0 = dy * 4 + j; if (i0 > 0 & i0 0 & j0 0 & targetsOpen > 0) %=======Disp showBoard titleMsg = sprintf('You have %d bombs left, please make your selection.',bombs); pcolor(0:SIZE,0:SIZE,plotBoard); %Make the plot itself axis off title(titleMsg) %=======Get mouse input & error check loop = 1; while loop [x,y] = ginput(1); jDrop = int32(x+1); iDrop = int32(y+1); if (jDrop > 0 & jDrop 0 & iDrop
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote