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

MATLAB-- need a solution matlab 1. Game Designing : BATTLESHIP Battleship is a g

ID: 3709397 • Letter: M

Question

MATLAB-- need a solution matlab

1. Game Designing : BATTLESHIP

Battleship is a guessing game for two players. It is played on ruled grids (paper or board) on which the players’ fleets of ships (including battleships) are marked. The locations of the fleet are concealed from the other player. Players alternate turns calling ”shots” at the other player’s ships, and the objective of the game is to destroy the opposing player’s fleet.

In this project you are going to design one to play against computer. Your program should create a battle field and user needs to sink all the ships. For ease your program might create a single ship with minimum 3 cells.

1.1. Instructions for designing the Board :

(1) The Game Board : The board is of fixed size 9 x 9. Each square is called cell

(2) Four (4) ships are placed at various locations on the board. For ease use can choose the number of ships you want to place in the field (minimum of 3 cells need for the ship)

(3) 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.

(4) 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.

(5) 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.

1.2. How the game is played:

If the user 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 user 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, Medium and Hard (you could implement a menu, but its 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:

– Hard : the computer has 29 missiles available
– Medium : the computer has 39 missiles available – Easy : the computer has 49 missiles available

The player can play the game until he is out of missiles or he has destroyed all the ships in the field.

Extra Credit : If you implement this in multi player version you would receive an extra credit.

Sample output :

>> battleship

Choose Number of Ships : 1 Choose Number of Players : 1 Choose difficulty : Easy

*** GAME STARTED *** You have 49 Missiles

Enter coordinates to hit : 1 1 Miss
you have 48 Missiles

Enter coordinates to hit : 1 2 hit
you have 47 Missiles

Enter coordinates to hit : 1 3 hit
You destroyed a CRUISER you have 46 Missiles

There are no ships left. You WON the battle

Stats:
you have 46 Missiles left you won the game in 3 tries

>>

HAPPY PROGRAMMING !!!

Explanation / Answer

function battleship

clear readPort

figure('Units','normalized',...

'Position',[0.25 0.1 0.5 0.8],...

'Menubar','none',...

'Name','Battleship',...

'NumberTitle','off',...

'Resize','off',...

'CloseRequestFcn', 'bsclose')

atb = atboard;

locb = locboard;

f = uimenu('Label','Connection');

uimenu(f,'Label','Connect','Callback',{@bsconnect,locb,atb});

f = uimenu('Label','Talk');

uimenu(f,'Label','You sunk my battleship!','Callback',@sunk);

f = uimenu('Label','Sound');

uimenu(f,'Label','Sound On','Checked','on','Callback',@soundOff);

pt = [0.1 0.2];

destroyer = [0.1 0.3];

sub = [0.1 0.3];

battleship = [0.1 0.4];

carrier = [0.1 0.5];

k = round(rand(1));

if k == 1

orientation = 'horizontal';

else

orientation = 'vertical';

end

pt = rot90(pt,2*k);

s(1) = ship([rand(1) rand(1) pt],'pt',orientation, locb.axhandle);

k = round(rand(1));

if k == 1

orientation = 'horizontal';

else

orientation = 'vertical';

end

destroyer = rot90(destroyer,2*k);

s(2) = ship([rand(1) rand(1) destroyer],'destroyer',orientation, locb.axhandle);

k = round(rand(1));

if k == 1

orientation = 'horizontal';

else

orientation = 'vertical';

end

sub = rot90(sub,2*k);

s(3) = ship([rand(1) rand(1) sub],'sub',orientation, locb.axhandle);

k = round(rand(1));

if k == 1

orientation = 'horizontal';

else

orientation = 'vertical';

end

battleship = rot90(battleship,2*k);

s(4) = ship([rand(1) rand(1) battleship],'battleship',orientation, locb.axhandle);

k = round(rand(1));

if k == 1

orientation = 'horizontal';

else

orientation = 'vertical';

end

carrier = rot90(carrier,2*k);

s(5) = ship([rand(1) rand(1) carrier],'carrier',orientation, locb.axhandle);