clc
clear
% Initialize scene
my_scene = simpleGameEngine(‘Battleship.png’,84,84);
% Set up variables to name the various sprites
blank_sprite = 1;
water_sprite = 2;
left_ship_sprite = 3;
horiz_ship_sprite = 4;
right_ship_sprite = 5;
top_ship_sprite = 6;
vert_ship_sprite = 7;
bot_ship_sprite = 8;
hit_sprite = 9;
miss_sprite = 10;
% Display empty board
Board_Display = water_sprite * ones(10,21);
Board_Display(:,11) = blank_sprite;
drawScene(my_scene,Board_Display)
fprintf(‘\n*****************************************\n’)
fprintf(‘Hello! Welcome to the game Battleship! \n’)
fprintf(‘In this game, you will need to place your ships first and guess the places computer puts\n’)
fprintf(‘The total units of Ships are 17\n’)
fprintf(‘There are five types of ships, carrier, battleship, submarine, cruiser, PT boat\n’)
fprintf(‘Carrier, the first ship, has length 5\n’)
fprintf(‘Battleship, the second ship, has length 4\n’)
fprintf(‘Submarine, the third ship, has length 3\n’)
fprintf(‘Cruiser, the fourth ship, has length 2\n’)
fprintf(‘PT boat, the fifth ship has length 1\n’)
fprintf(‘You can put your ships horizontally or vertically\n’)
fprintf(‘Then you and the computer will guess the places of ships and shot the other\n’)
fprintf(‘The first one who shot all units of the orthers will win\n’)
fprintf(‘Good luck!!!\n’)
fprintf(‘\n*****************************************\n’)
warning(‘off’,‘all’);
%% Choose the hardness of the Battleship game
fprintf(‘Difficulty will increase from Lv1 to Lv3.\n’)
fprintf(‘Lv1 = Easy\n’)
fprintf(‘Lv2 = Hard\n’)
fprintf(‘Lv3 = Hell\n’)
% Ask plaer to choose his difficulty level
Level = input(‘Please set the difficulty Level:’);
%% Prompt player to place his ships:
Player_Ships = zeros(10,10);
Ship_Length = [5,4,3,3,2];
Ship_Name = [“CARRIER”, “BATTLESHIP”, “SUBMARINE”, “CRUISER”, “PT BOAT”];
% Loop over each ship(placing)
for Ship_ID = 1:5
fprintf(‘Decide the location of your ships: %s. \n’, Ship_Name(Ship_ID))
Ship_Placed = false;
while ~Ship_Placed
Horizontal = input(‘Choose to place your ships vertically or horizontally, 1 symbolizes horizontal and 0 symbolizes vertical ‘);
% Place ships horizontally
if Horizontal
fprintf(‘The range of row is between 1 ~ 10, ‘);
Row = input(‘Please enter the row of the ship.’);
fprintf(‘The range of column is between 1 ~ %i, ‘, 11 – Ship_Length(Ship_ID));
Col = input(‘Please enter the column of the ship.’);
% Prevent player from making ships overlapping
if sum(Player_Ships(Row,Col:(Col+Ship_Length(Ship_ID)-1))) == 0
Player_Ships(Row,Col:(Col+Ship_Length(Ship_ID)-1)) = Ship_ID;
Ship_Placed = true;
Board_Display(Row,Col) = left_ship_sprite;
for pos = Col+1:(Col+Ship_Length(Ship_ID)-2)
Board_Display(Row,pos) = horiz_ship_sprite;
end
Board_Display(Row,(Col+Ship_Length(Ship_ID)-1)) = right_ship_sprite;
else
fprintf(‘The position is invalid, please choose a valid position again!’);
end
% Place ships Vertically
else
fprintf(‘The range of row is between 1 ~ %i, ‘, 11 – Ship_Length(Ship_ID));
Row = input(‘Enter the row of your ship.’);
fprintf(‘The range of column is between 1 ~ 10, ‘);
Col = input(‘Enter the column of the ship.’);
% Prevent player from making ships overlapping
if sum(Player_Ships(Row:(Row+Ship_Length(Ship_ID)-1),Col)) == 0
Player_Ships(Row:(Row+Ship_Length(Ship_ID)-1),Col) = Ship_ID;
Ship_Placed = true;
Board_Display(Row,Col) = top_ship_sprite;
for pos = Row+1:(Row+Ship_Length(Ship_ID)-2)
Board_Display(pos,Col) = vert_ship_sprite;
end
Board_Display((Row+Ship_Length(Ship_ID)-1),Col) = bot_ship_sprite;
else
fprintf(‘The position is invalid, please choose a valid position again!’);
end
end
end
drawScene(my_scene,Board_Display)
end
%% Computer randomly place ships;
% Set variables first
Cpu_Ships = zeros(10,10);
Ship_Length = [5,4,3,3,2];
Ship_Row_Co = [0,0,0,0,0];
Ship_Col_Co = [0,0,0,0,0];
Ship_Hor_Ver = [0,0,0,0,0];
for Ship_ID = 1:5
Ship_Placed = false;
while ~Ship_Placed
Horizontal = randi([0 1]);
if Horizontal
Row = randi([1 10]);
Col = randi([1 (11-Ship_Length(Ship_ID))]);
% Prevent computer from making ships overlapping
if sum(Cpu_Ships(Row,Col:(Col+Ship_Length(Ship_ID)-1))) == 0
Cpu_Ships(Row,Col:(Col+Ship_Length(Ship_ID)-1)) = Ship_ID;
Ship_Placed = true;
Ship_Row_Co(Ship_ID) = Row;
Ship_Col_Co(Ship_ID) = Col;
Ship_Hor_Ver(Ship_ID) = 1;
end
else
Row = randi([1 (11-Ship_Length(Ship_ID))]);
Col = randi([1 10]);
% Prevent computer from making ships overlapping
if sum(Cpu_Ships(Row:(Row+Ship_Length(Ship_ID)-1),Col)) == 0
Cpu_Ships(Row:(Row+Ship_Length(Ship_ID)-1),Col) = Ship_ID;
Ship_Placed = true;
Ship_Row_Co(Ship_ID) = Row;
Ship_Col_Co(Ship_ID) = Col;
Ship_Hor_Ver(Ship_ID) = 0;
end
end
end
end
%% Shooting!
Hitmiss_Display = blank_sprite * ones(10,21);
Player_Score_Count = 0;
Computer_Score_Count = 0;
while Player_Score_Count ~= 17 && Computer_Score_Count ~= 17
% Turn for the Player
PlayerShotRow = input(‘Enter the row coordinate you want to shot, the range of row is between 1 ~ 10 : ‘);
PlayerShotCol = input(‘Enter the column coordinate you want to shot, the range of column is between 1 ~ 10 : ‘);
% Prevent Player from shoting out of range
while PlayerShotRow < 1 || PlayerShotRow > 10
fprintf(‘Do not shot outside the range. \n’);
PlayerShotRow = input(‘Enter a different row coordinate which is in range: ‘);
end
while PlayerShotCol < 1 || PlayerShotCol > 10
fprintf(‘Do not shot outside the range. \n’);
PlayerShotCol = input(‘Enter a different column coordinate which is in range: ‘);
end
% Prevent player from shoting same location
while Cpu_Ships(PlayerShotRow, PlayerShotCol) == -1
fprintf(‘Do not shot a same location again. \n’);
PlayerShotRow = input(‘Enter a different row coordinate: ‘);
PlayerShotCol = input(‘Enter a different column coordinate: ‘);
end
% Check whether hit or miss for player
if Cpu_Ships(PlayerShotRow, PlayerShotCol) > 0
Player_Score_Count = Player_Score_Count + 1;
Hitmiss_Display(PlayerShotRow,PlayerShotCol + 11) = hit_sprite;
fprintf(‘Nice Hit!!! \n’);
Cpu_Ships(PlayerShotRow, PlayerShotCol) = -1;
else
Hitmiss_Display(PlayerShotRow,PlayerShotCol + 11) = miss_sprite;
fprintf(‘Sorry, you missed… \n’);
end
drawScene(my_scene,Board_Display,Hitmiss_Display)
% Turn for the Computer
CpuShotRow = randi([1 10]);
CpuShotCol = randi([1 10]);
% To increase the diffculty level
Expected_Hardness = randi([1 10]);
if Expected_Hardness <= Level
for sniper_Row = randi([1 5]):randi([6 10])
for sniper_Col = randi([1 5]):randi([6 10])
if(Player_Ships(sniper_Row, sniper_Col) ~= 0)
CpuShotRow = sniper_Row;
CpuShotCol = sniper_Col;
end
end
end
while Player_Ships(CpuShotRow, CpuShotCol) == -1
if CpuShotRow == 1
CpuShotRow = randi([CpuShotRow+1 CpuShotRow+2]);
elseif CpuShotRow == 10
CpuShotRow = randi([CpuShotRow-2 CpuShotRow-1]);
else
CpuShotRow = randi([CpuShotRow-1 CpuShotRow+1]);
end
if CpuShotCol == 1
CpuShotCol = randi([CpuShotCol+1 CpuShotCol+2]);
elseif CpuShotCol == 10
CpuShotCol = randi([CpuShotCol-2 CpuShotCol-1]);
else
CpuShotCol = randi([CpuShotCol-1 CpuShotCol+1]);
end
end
end
% Prevent computer from shoting same location
while Player_Ships(CpuShotRow, CpuShotCol) == -1
CpuShotRow = randi([1 10]);
CpuShotCol = randi([1 10]);
end
% Check whether hit or miss for computer
if Player_Ships(CpuShotRow, CpuShotCol) ~= 0
Computer_Score_Count = Computer_Score_Count + 1;
Hitmiss_Display(CpuShotRow,CpuShotCol) = hit_sprite;
fprintf(‘Being attacked. \n’);
Player_Ships(PlayerShotRow, PlayerShotCol) = -1;
else
Hitmiss_Display(CpuShotRow,CpuShotCol) = miss_sprite;
fprintf(‘Safe. \n’);
end
drawScene(my_scene,Board_Display,Hitmiss_Display)
end
%% Display Computer Ships;
for Ship_ID = 1:5
if Ship_Hor_Ver(Ship_ID) == 1
Board_Display(Ship_Row_Co(Ship_ID),Ship_Col_Co(Ship_ID) + 11) = left_ship_sprite;
for pos = Ship_Col_Co(Ship_ID)+1:(Ship_Col_Co(Ship_ID)+Ship_Length(Ship_ID)-2)
Board_Display(Ship_Row_Co(Ship_ID),pos + 11) = horiz_ship_sprite;
end
Board_Display(Ship_Row_Co(Ship_ID),(Ship_Col_Co(Ship_ID)+Ship_Length(Ship_ID)-1) + 11) = right_ship_sprite;
else
Board_Display(Ship_Row_Co(Ship_ID),Ship_Col_Co(Ship_ID) + 11) = top_ship_sprite;
for pos = Ship_Row_Co(Ship_ID)+1:(Ship_Row_Co(Ship_ID)+Ship_Length(Ship_ID)-2)
Board_Display(pos,Ship_Col_Co(Ship_ID) + 11) = vert_ship_sprite;
end
Board_Display((Ship_Row_Co(Ship_ID)+Ship_Length(Ship_ID)-1),Ship_Col_Co(Ship_ID) + 11) = bot_ship_sprite;
end
end
%% Declare the Winner!
if Player_Score_Count == 17
fprintf(‘Congratulations! You won the game ‘);
drawScene(my_scene,Board_Display,Hitmiss_Display);
elseif Computer_Score_Count == 17
fprintf(‘Unfortunately, you lost the game.. ‘);
drawScene(my_scene,Board_Display,Hitmiss_Display);
end
The file of the Battleship: