Note- Comments are italicized.
Main Code
%BATTLESHIP
clc
clear
%Initialize sprite variables and Shipbuilder values
my_scene = simpleGameEngine(‘Battleship.png’,84,84);
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;
play1_sprite = 11;
play2_sprite = 12;
play3_sprite = 13;
play4_sprite = 14;
diffboxleft = 19;
diffboxstar = 15;
diffboxright = 20;
msgbox1 = 16;
msgbox2 = 17;
msgbox3 = 18;
msgbox4 = 21;
msgbox5 = 22;
msgbox6 = 23;
car = 100;
bat = 99;
cru = 98;
sub = 97;
des = 96;
right = 56;
down = 58;
%Generate user board and AI board
boardU = water_sprite * ones(21,15);
%Remove spaces for Info Board and Menu
boardU(11,:) = blank_sprite;
boardU(:,1:5) = blank_sprite;
%Add Info Board and Start button
boardA = boardU;
boardU(12,1) = msgbox1;
boardU(12,2:4) = msgbox2;
boardU(12,5) = msgbox3;
boardU(13,1) = msgbox4;
boardU(13,2:4) = msgbox5;
boardU(13,5) = msgbox6;
boardU(11,9) = play1_sprite;
boardU(11,10) = play2_sprite;
boardU(11,11) = play3_sprite;
boardU(11,12) = play4_sprite;
boardU(14:16,1) = diffboxleft;
boardU(14,2:4) = diffboxstar;
boardU(15,2:3) = diffboxstar;
boardU(16,2) = diffboxstar;
boardU(14,5) = diffboxright;
boardU(15,4) = diffboxright;
boardU(16,3) = diffboxright;
% Initialize ships in info board, to be selected and placed.
[carrier] = ShipBuilder(3,1,car,down);
[battleship] = ShipBuilder(3,2,bat,down);
[cruiser] = ShipBuilder(3,3,cru,down);
[submarine] = ShipBuilder(3,4,sub,down);
[destroyer] = ShipBuilder(3,5,des,down);
boardU(carrier ~= 0) = 0;
boardU(battleship ~= 0) = 0;
boardU(cruiser ~= 0) = 0;
boardU(submarine ~= 0) = 0;
boardU(destroyer ~= 0) = 0;
boardU = boardU + carrier + battleship + cruiser + submarine + destroyer;
%These are basically switches, they stop the for loops from repeating after
%a ship is placed down
swit1 = 0;
swit2 = swit1;
swit3 = swit1;
swit4 = swit1;
swit5 = swit1;
drawScene(my_scene,boardU)
% Making the board for our placements
while sum(boardU(3,1:5) > 5) % While the ships are still in selection
[row,col,button] = getMouseInput(my_scene);
while ((col == 1) && (swit1 == 0))
if ((row >= 3) && (row <= 7))
% Insert a message saying that you will place the top-leftmost
% part of the ship. Left click to place pointing right, Right
% click to place pointing left
boardU(3:7,1) = 1;
drawScene(my_scene,boardU)
[trow,tcol,tbutton] = getMouseInput(my_scene);
[carrier] = ShipBuilder(trow,tcol,car,tbutton+55);
% Restricts placeable squares to within the play area
if ((trow <= 11) || (tcol <= 5) || ((tcol >= 12) && (tbutton ~= 3)) || ((trow >= 18) && (tbutton ~= 1)))
msgbox(‘Invalid ship placement, try again.’)
elseif (length(find(boardU(find(carrier ~= 0)) ~= 2)) > 0)
msgbox(‘Invalid ship placement, try again.’)
else
boardU(carrier ~= 0) = 0;
boardU = boardU + carrier;
drawScene(my_scene,boardU)
swit1 = 1;
end
break
else msgbox(‘PLACEHOLDER’)
break
end
end
while ((col == 2) && (swit2 == 0))
if ((row >= 3) && (row <= 6))
% Insert a message saying that you will place the top-leftmost
% part of the ship. Left click to place pointing right, Right
% click to place pointing left
boardU(3:7,2) = 1;
drawScene(my_scene,boardU)
[trow,tcol,tbutton] = getMouseInput(my_scene);
[battleship] = ShipBuilder(trow,tcol,bat,tbutton+55);
% Restricts placeable squares to within the play area
if ((trow <= 11) || (tcol <= 5) || ((tcol >= 13) && (tbutton ~= 3)) || ((trow >= 19) && (tbutton ~= 1)))
msgbox(‘Invalid ship placement, try again.’)
elseif (length(find(boardU(find(battleship ~= 0)) ~= 2)) > 0)
msgbox(‘Invalid ship placement, try again.’)
else
boardU(battleship ~= 0) = 0;
boardU = boardU + battleship;
drawScene(my_scene,boardU)
swit2 = 1;
end
break
else msgbox(‘PLACEHOLDER’)
break
end
end
while ((col == 3) && (swit3 == 0))
if ((row >= 3) && (row <= 5))
% Insert a message saying that you will place the top-leftmost
% part of the ship. Left click to place pointing right, Right
% click to place pointing left
boardU(3:7,3) = 1;
drawScene(my_scene,boardU)
[trow,tcol,tbutton] = getMouseInput(my_scene);
[cruiser] = ShipBuilder(trow,tcol,cru,tbutton+55);
% Restricts placeable squares to within the play area
if ((trow <= 11) || (tcol <= 5) || ((tcol >= 14) && (tbutton ~= 3)) || ((trow >= 20) && (tbutton ~= 1)))
msgbox(‘Invalid ship placement, try again.’)
elseif (length(find(boardU(find(cruiser ~= 0)) ~= 2)) > 0)
msgbox(‘Invalid ship placement, try again.’)
else
boardU(cruiser ~= 0) = 0;
boardU = boardU + cruiser;
drawScene(my_scene,boardU)
swit3 = 1;
end
break
else msgbox(‘PLACEHOLDER’)
break
end
end
while ((col == 4) && (swit4 == 0))
if ((row >= 3) && (row <= 5))
% Insert a message saying that you will place the top-leftmost
% part of the ship. Left click to place pointing right, Right
% click to place pointing left
boardU(3:7,4) = 1;
drawScene(my_scene,boardU)
[trow,tcol,tbutton] = getMouseInput(my_scene);
[submarine] = ShipBuilder(trow,tcol,sub,tbutton+55);
% Restricts placeable squares to within the play area
if ((trow <= 11) || (tcol <= 5) || ((tcol >= 14) && (tbutton ~= 3)) || ((trow >= 20) && (tbutton ~= 1)))
msgbox(‘Invalid ship placement, try again.’)
elseif (length(find(boardU(find(submarine ~= 0)) ~= 2)) > 0)
msgbox(‘Invalid ship placement, try again.’)
else
boardU(submarine ~= 0) = 0;
boardU = boardU + submarine;
drawScene(my_scene,boardU)
swit4 = 1;
end
break
else msgbox(‘PLACEHOLDER’)
break
end
end
while ((col == 5) && (swit5 == 0))
if ((row >= 3) && (row <= 4))
% Insert a message saying that you will place the top-leftmost
% part of the ship. Left click to place pointing right, Right
% click to place pointing left
boardU(3:7,5) = 1;
drawScene(my_scene,boardU)
[trow,tcol,tbutton] = getMouseInput(my_scene);
[destroyer] = ShipBuilder(trow,tcol,des,tbutton+55);
% Restricts placeable squares to within the play area
if ((trow <= 11) || (tcol <= 5) || ((tcol >= 15) && (tbutton ~= 3)) || ((trow >= 21) && (tbutton ~= 1)))
msgbox(‘Invalid ship placement, try again.’)
elseif (length(find(boardU(find(destroyer ~= 0)) ~= 2)) > 0)
msgbox(‘Invalid ship placement, try again.’)
else
boardU(destroyer ~= 0) = 0;
boardU = boardU + destroyer;
drawScene(my_scene,boardU)
swit5 = 1;
end
break
else msgbox(‘PLACEHOLDER’)
break
end
end
end
% Difficulty selection
diff = 0;
while (diff == 0)
[row,col] = getMouseInput(my_scene);
if ((row == 14) && ((col >= 1) && (col < 5)))
diff = 5;
elseif((row == 15) && ((col >= 1) && (col < 4)))
diff = 4;
elseif((row == 16) && ((col >= 1) && (col < 3)))
diff = 3;
else
msgbox(‘Invalid difficulty selection, please try again.’)
end
end
boardU(14:16,1:5) = blank_sprite
drawScene(my_scene,boardU)
% Wait for Start Game
while (diff > 0)
[row,col] = getMouseInput(my_scene);
if ((row == 11) && ((col >= 9) && (col < 13)))
break
end
end
boardU(11,1:15) = blank_sprite
drawScene(my_scene,boardU)
if (diff == 5)
gameover = false;
shipLengths = [5, 4, 3, 3, 2];
opponentBoard = generateAIBattleshipBoard(10, shipLengths); %this generates the gameboard(note, this does not create the board in SGE it just creates a 10×10 made of zeros and ones)
PCboard = zeros(10);
for i = 12:21
for j = 6:15
if boardU(i,j) ~= 2
PCBoard(i-11, j-5) = 1;
else
PCBoard(i-11, j-5) = 0;
end
end
end
lastHitRow = -1;
lastHitCol = -1;
while ~gameover
% Players turn first
[row,col] = getMouseInput(my_scene);
isHit = opponentBoard(row, col-5) == 1;
if isHit
disp(‘Hit!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = hit_sprite;
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = miss_sprite;
end
drawScene(my_scene,boardU)
if (length(find(boardU(1:10,6:15) == 9)) == 17)
disp(‘You Win!’)
gameover = true;
break
end
% then PC’s turn
[PCrow, PCcol, PCisHit, PCBoard] = aiTargetOpponent(PCBoard,lastHitRow,lastHitCol);
if PCisHit
lastHitRow = PCrow;
lastHitCol = PCcol;
disp(‘Hit!’);
boardU(PCrow+11, PCcol+5) = hit_sprite;
%this chunk is will display ‘miss’, the row and column and will change
%the board display to now display the white ‘miss’ sprite
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(PCrow+11, PCcol+5) = miss_sprite;
end
fprintf(‘LastRow %i and LastCol %i’, lastHitRow, lastHitCol)
%once it has checked if it is a hit or miss it will draw the scene
drawScene(my_scene, boardU);
% This makes you hit the spacebar in order for the AI to guess again
disp(‘Press the spacebar to continue…’);
if (length(find(boardU(11:21,6:15) == 9)) == 17)
disp(‘You Lose!’)
gameover = true;
break
end
end
elseif (diff == 4)
gameover = false;
shipLengths = [5, 4, 3, 3, 2];
opponentBoard = generateAIBattleshipBoard(10, shipLengths); %this generates the gameboard(note, this does not create the board in SGE it just creates a 10×10 made of zeros and ones)
PCboard = zeros(10);
for i = 12:21
for j = 6:15
if boardU(i,j) ~= 2
PCBoard(i-11, j-5) = 1;
else
PCBoard(i-11, j-5) = 0;
end
end
end
lastHitRow = -1;
lastHitCol = -1;
while ~gameover
% Players turn first
[row,col] = getMouseInput(my_scene);
isHit = opponentBoard(row, col-5) == 1;
if isHit
disp(‘Hit!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = hit_sprite;
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = miss_sprite;
end
drawScene(my_scene,boardU)
if (length(find(boardU(1:10,6:15) == 9)) == 17)
disp(‘You Win!’)
gameover = true;
break
end
% then PC’s turn
[PCrow, PCcol, PCisHit, PCBoard] = randTargetOpponent(PCBoard);
if PCisHit
lastHitRow = PCrow;
lastHitCol = PCcol;
disp(‘Hit!’);
boardU(PCrow+11, PCcol+5) = hit_sprite;
%this chunk is will display ‘miss’, the row and column and will change
%the board display to now display the white ‘miss’ sprite
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(PCrow+11, PCcol+5) = miss_sprite;
end
fprintf(‘LastRow %i and LastCol %i’, lastHitRow, lastHitCol)
%once it has checked if it is a hit or miss it will draw the scene
drawScene(my_scene, boardU);
% This makes you hit the spacebar in order for the AI to guess again
disp(‘Press the spacebar to continue…’);
if (length(find(boardU(11:21,6:15) == 9)) == 17)
disp(‘You Lose!’)
gameover = true;
break
end
end
elseif (diff == 3)
gameover = false;
shipLengths = [5, 4, 3, 3, 2];
opponentBoard = zeros(10)
opponentBoard(1:5,1) = 1;
opponentBoard(4,5:8) = 1;
opponentBoard(7:9,3) = 1;
opponentBoard(6,8:10) = 1;
opponentBoard(9:10,8) = 1;
PCboard = zeros(10);
for i = 12:21
for j = 6:15
if boardU(i,j) ~= 2
PCBoard(i-11, j-5) = 1;
else
PCBoard(i-11, j-5) = 0;
end
end
end
lastHitRow = -1;
lastHitCol = -1;
while ~gameover
% Players turn first
[row,col] = getMouseInput(my_scene);
isHit = opponentBoard(row, col-5) == 1;
if isHit
disp(‘Hit!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = hit_sprite;
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(row, col) = miss_sprite;
end
drawScene(my_scene,boardU)
if (length(find(boardU(1:10,6:15) == 9)) == 17)
disp(‘You Win!’)
gameover = true;
break
end
% then PC’s turn
[PCrow, PCcol, PCisHit, PCBoard] = randTargetOpponent(PCBoard);
if PCisHit
lastHitRow = PCrow;
lastHitCol = PCcol;
disp(‘Hit!’);
boardU(PCrow+11, PCcol+5) = hit_sprite;
%this chunk is will display ‘miss’, the row and column and will change
%the board display to now display the white ‘miss’ sprite
else
disp(‘Miss!’);
fprintf(‘%d,%d\n’, row, col)
boardU(PCrow+11, PCcol+5) = miss_sprite;
end
fprintf(‘LastRow %i and LastCol %i’, lastHitRow, lastHitCol)
%once it has checked if it is a hit or miss it will draw the scene
drawScene(my_scene, boardU);
% This makes you hit the spacebar in order for the AI to guess again
disp(‘Press the spacebar to continue…’);
if (length(find(boardU(11:21,6:15) == 9)) == 17)
disp(‘You Lose!’)
gameover = true;
break
end
end
end
Function: aiTargetOpponent
% As of 11/29/2023 11:37PM – edited by Caleb
function [row, col, isHit, opponentBoard] = aiTargetOpponent(opponentBoard, lastHitRow, lastHitCol)%defines the function. The input is the opponent’s board.
%in my main code i have the opponent board set to be the AI randomly
%generated board that i created. however, ***this needs to be changed to the
%actual player’s board when it gets implemented.***
[rows, cols] = size(opponentBoard);
% This gets the size of the board (number of rows and columns) and assigns them to variables.
% If the computer doesn’t know where any ships are it will guess a
% completely random spot on the board. This chunk is essentially
% checking if the computer knows anything yet or not. If all the
% elements in the opponent’s board are equal to 0 then it guesses
% randomly.
if all(opponentBoard(:) == 0)
row = randi(rows);
col = randi(cols);
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the first’)
return;
end
if lastHitRow ~= -1
row = lastHitRow – 1;
col = lastHitCol;
if row > 0 && row <= 10 && col > 0 && col <= 10
if opponentBoard(row, col) ~= 2
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the 1 last hit’)
return;
end
end
row = row + 1;
col = col + 1;
if row > 0 && row <= 10 && col > 0 && col <= 10
if opponentBoard(row, col) ~= 2
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the 2 last hit’)
return;
end
end
row = row + 1;
col = col – 1;
if row > 0 && row <= 10 && col > 0 && col <= 10
if opponentBoard(row, col) ~= 2
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the 3 last hit’)
return;
end
end
row = row – 1;
col = col – 1;
if row > 0 && row <= 10 && col > 0 && col <= 10
if opponentBoard(row, col) ~= 2
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the 4 last hit’)
return;
end
end
end
% This chunk checks to see if there are any unexplored positions on the
% board. If there are spots on the board that haven’t been guessed yet
% (they will appear as a -1) it will randomly guess one of those -1s.
[unexploredRows, unexploredCols] = find(opponentBoard == -1);
if ~isempty(unexploredRows)
idx = randi(length(unexploredRows));%This generates a random integer between 1 and the length of unexploredRows. The result is stored in idx.
%idx is essentially used to randomly pick an index, and then the values at that index from unexploredRows and unexploredCols are assigned to row and col, respectively.
% This process helps the computer player make a random but valid guess on the opponent’s board.
row = unexploredRows(idx);%this guesses a random x coordinate
col = unexploredCols(idx);%this guesses a random y coordinate
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2; %If there are unexplored positions, it randomly selects one, sets isHit to false,
% marks the guessed position as 2 on the opponentBoard, and exits the function.
fprintf(‘huhhhh’)
return;
end
% If the computer no longer has any more spaces to guess but has not
% found any ships, it will continue to shoot. (this chunk may not be
% needed but i kept it for redundancy).
[guessedRows, guessedCols] = find(opponentBoard == 2);
allPositions = 1:rows*cols;
unguessedPositions = setdiff(allPositions, sub2ind([rows, cols], guessedRows, guessedCols));
idx = randi(length(unguessedPositions));
[row, col] = ind2sub([rows, cols], unguessedPositions(idx));
isHit = opponentBoard(row, col) == 1; % Check if it’s a hit
opponentBoard(row, col) = 2; % Mark the guessed position
return;
end
Function: ShipBuilder
% As of 11/29/2023 11:37PM – edited by Caleb
function [a] = ShipBuilder(y,x,s,d)
% Create a function that takes in x, y, s, d ((x,y), pos. of boat, and boat type)
% And outputs the rest of the boat to be put onto board
%Initialize a simlar battleship board so the two may be meshed
my_scene = simpleGameEngine(‘Battleship.png’,84,84);
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;
%Create board such that only a ship will appear on it.
Aboard_display = zeros(21,15)
%Check the direction of the boat
if (d == 56) % facing right
%Check the type of boat
if (s == 100) % carrier
Aboard_display(y,x) = left_ship_sprite;
Aboard_display(y,x+1:x+3) = horiz_ship_sprite;
Aboard_display(y,x+4) = right_ship_sprite;
a = Aboard_display;
elseif (s == 99) % battleship
Aboard_display(y,x) = left_ship_sprite;
Aboard_display(y,x+1:x+2) = horiz_ship_sprite;
Aboard_display(y,x+3) = right_ship_sprite;
a = Aboard_display;
elseif ((s == 98) || (s == 97)) % cruiser or submarine
Aboard_display(y,x) = left_ship_sprite;
Aboard_display(y,x+1) = horiz_ship_sprite;
Aboard_display(y,x+2) = right_ship_sprite;
a = Aboard_display;
elseif (s == 96) % destroyer
Aboard_display(y,x) = left_ship_sprite;
Aboard_display(y,x+1) = right_ship_sprite;
a = Aboard_display;
end
elseif (d == 58) % facing down
if (s == 100) % carrier
Aboard_display(y,x) = top_ship_sprite;
Aboard_display(y+1:y+3,x) = vert_ship_sprite;
Aboard_display(y+4,x) = bot_ship_sprite;
a = Aboard_display;
elseif (s == 99) %Battleship
Aboard_display(y,x) = top_ship_sprite;
Aboard_display(y+1:y+2,x) = vert_ship_sprite;
Aboard_display(y+3,x) = bot_ship_sprite;
a = Aboard_display;
elseif ((s == 98) || (s == 97)) % cruiser or submarine
Aboard_display(y,x) = top_ship_sprite;
Aboard_display(y+1,x) = vert_ship_sprite;
Aboard_display(y+2,x) = bot_ship_sprite;
a = Aboard_display;
elseif (s == 96) % destroyer
Aboard_display(y,x) = top_ship_sprite;
Aboard_display(y+1,x) = bot_ship_sprite;
a = Aboard_display;
end
end
End
Function: generateAIBattleshipBoard
% As of 11/29/2023 11:37PM – edited by Caleb
function battleshipBoard = generateAIBattleshipBoard(boardSize, shipLengths)
% Initialize a board with zeros
battleshipBoard = zeros(boardSize); %creates a matrix of zeros
% Place ships on the board without overlapping
for lengthIdx = 1:length(shipLengths) %this code goes through each ship length inputted. lengtIdx is the index of ship lengths.
shipLength = shipLengths(lengthIdx);
placed = false;%sets if it is placed or not = false
%this loop continues until the ship of the current length is placed without overlapping.
while ~placed%when it is not placed
% Randomly choose whether the ship should be placed horizontally or vertically
orientation = randi([0, 1]); % randomly 0 for horizontal or 1 for vertical
%if the choice is 0, it enters the following chunk
if orientation == 0 % Place horizontally
startCol = randi(boardSize – shipLength + 1);%randomly chosen placement where the ship starts
startRow = randi(boardSize);
if ~any(battleshipBoard(startRow, startCol:startCol + shipLength – 1))%this checks if the specified column for that row is empty
%startRow:startRow + shipLength – 1 defines a range of rows corresponding to the vertical placement of a ship.
%”any…” checks if any element in the submatrix is
%nonzero if any of the elements are non-zero that means
%there is a ship there.
%essentially what is being done below is i’m going into the
%battleshipBoard matrix, specifying a row, then telling it how long
%to go based on the ship length. The -1 is there to account for
%Matlab starting the indices at 1 and it ensures that all the
%required elements are included.
battleshipBoard(startRow, startCol:startCol + shipLength – 1) = 1; % extracts a submatrix from the battleshipBoard that corresponds to the cells where the ship would be placed vertically
placed = true;%when the ship is finally placed the loop will exit because the condition is no longer met.
%it will run through this loop, searching for an
%unoccupied spot until placed=true. The same thing
%this essentially applies to the vertical chunk as well.
end
%if the choice is 1, it enters this chunk
else % Place vertically
startRow = randi(boardSize – shipLength + 1);
startCol = randi(boardSize);
if ~any(battleshipBoard(startRow:startRow + shipLength – 1, startCol))
battleshipBoard(startRow:startRow + shipLength – 1, startCol) = 1;
placed = true;
end
end
end
end
% Display the battleship board
disp(‘AI Battleship Board:’)
disp(battleshipBoard);
end
Function: randTargetOpponent
% As of 11/29/2023 11:37PM – edited by Caleb
function [row, col, isHit, opponentBoard] = randTargetOpponent(opponentBoard)%defines the function. The input is the opponent’s board.
%in my main code i have the opponent board set to be the AI randomly
%generated board that i created. however, ***this needs to be changed to the
%actual player’s board when it gets implemented.***
[rows, cols] = size(opponentBoard);
% This gets the size of the board (number of rows and columns) and assigns them to variables.
% If the computer doesn’t know where any ships are it will guess a
% completely random spot on the board. This chunk is essentially
% checking if the computer knows anything yet or not. If all the
% elements in the opponent’s board are equal to 0 then it guesses
% randomly.
if all(opponentBoard(:) == 0)
row = randi(rows);
col = randi(cols);
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2;
fprintf(‘the first’)
return;
end
% This chunk checks to see if there are any unexplored positions on the
% board. If there are spots on the board that haven’t been guessed yet
% (they will appear as a -1) it will randomly guess one of those -1s.
[unexploredRows, unexploredCols] = find(opponentBoard == -1);
if ~isempty(unexploredRows)
idx = randi(length(unexploredRows));%This generates a random integer between 1 and the length of unexploredRows. The result is stored in idx.
%idx is essentially used to randomly pick an index, and then the values at that index from unexploredRows and unexploredCols are assigned to row and col, respectively.
% This process helps the computer player make a random but valid guess on the opponent’s board.
row = unexploredRows(idx);%this guesses a random x coordinate
col = unexploredCols(idx);%this guesses a random y coordinate
isHit = opponentBoard(row, col) == 1;
opponentBoard(row, col) = 2; %If there are unexplored positions, it randomly selects one, sets isHit to false,
% marks the guessed position as 2 on the opponentBoard, and exits the function.
fprintf(‘huhhhh’)
return;
end
% If the computer no longer has any more spaces to guess but has not
% found any ships, it will continue to shoot. (this chunk may not be
% needed but i kept it for redundancy).
[guessedRows, guessedCols] = find(opponentBoard == 2);
allPositions = 1:rows*cols;
unguessedPositions = setdiff(allPositions, sub2ind([rows, cols], guessedRows, guessedCols));
idx = randi(length(unguessedPositions));
[row, col] = ind2sub([rows, cols], unguessedPositions(idx));
isHit = opponentBoard(row, col) == 1; % Check if it’s a hit
opponentBoard(row, col) = 2; % Mark the guessed position
return;
end