You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can I plot a game board in a figure
21 views (last 30 days)
Show older comments
Im programming the game battleship, and i need create a figure 2, 6 by 6 boards (one for the user and one for the computer). I was searching up on figures in matlab but i didnt find anything on how to create a grid in the figure. So how can i do that please, also can i have each grid correspond to a number from 1 to 36.
Accepted Answer
Image Analyst
on 19 Dec 2021
You can just do
numSpaces = 6;
myBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
subplot(1, 2, 1);
pcolor(myBoard);
axis square
title('Your Board', 'FontSize', 15)
subplot(1, 2, 2);
pcolor(computersBoard);
axis square
title('Computer Board', 'FontSize', 15)
Check the File Exchange, and click the tag on the left for numerous battleship programs:
30 Comments
Tariq Hammoudeh
on 19 Dec 2021
Edited: Tariq Hammoudeh
on 19 Dec 2021
Thank you for that, but how can make it white and have the computers board on top of the player's, and is there a way to have the grids numbered, so small number inside each grid?
Image Analyst
on 19 Dec 2021
Try this:
% Demo by Image Analyst, December, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
numSpaces = 6;
myBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
%-----------------------------------------------------------------------
% YOUR BOARD.
subplot(1, 2, 1);
pcolor(myBoard);
colormap([1,1,1])
axis square
title('Your Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xtl = {'0', '1', '2', '3', '4', '5', '6'};
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
ytl = {'0', 'F', 'E', 'D', 'C', 'B', 'A'};
yticklabels(ytl)
% Put up grid locations in the cells.
for row = 1 : numSpaces
for col = 1 : numSpaces
str = sprintf('%s%s', ytl{col + 1}, xtl{row + 1});
xt = row + 0.5;
yt = col + 0.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
end
end
%-----------------------------------------------------------------------
% COMPUTER'S BOARD.
subplot(1, 2, 2);
pcolor(computersBoard);
axis square
title('Computer Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
yticklabels(ytl)
% Put up grid locations in the cells.
for row = 1 : numSpaces
for col = 1 : numSpaces
str = sprintf('%s%s', ytl{col + 1}, xtl{row + 1});
xt = row + 0.5;
yt = col + 0.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
end
end
Tariq Hammoudeh
on 19 Dec 2021
Ok thank you so much, so i cant make it numbered from 1 to 36 instead, right?
Image Analyst
on 19 Dec 2021
Edited: Image Analyst
on 19 Dec 2021
Of course you can. Please try to program it up. You learn by doing.
If the code below works, please Accept the answer. Thanks in advance 🙂
% Demo by Image Analyst, December, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
numSpaces = 6;
myBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
%-----------------------------------------------------------------------
% YOUR BOARD.
subplot(1, 2, 1);
pcolor(myBoard);
colormap([1,1,1])
axis square
title('Your Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xtl = {'0', '1', '2', '3', '4', '5', '6'};
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
ytl = {'0', 'F', 'E', 'D', 'C', 'B', 'A'};
yticklabels(ytl)
% Put up grid locations in the cells.
counter = 1;
for row = 1 : numSpaces
for col = 1 : numSpaces
str = sprintf('%d', counter);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
counter = counter + 1;
end
end
%-----------------------------------------------------------------------
% COMPUTER'S BOARD.
subplot(1, 2, 2);
pcolor(computersBoard);
axis square
title('Computer Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
yticklabels(ytl)
% Put up grid locations in the cells.
% This has the numbers going in the opposite direction.
counter = 1;
for col = 1 : numSpaces
for row = 1 : numSpaces
str = sprintf('%d', counter);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
counter = counter + 1;
end
end
Pick whichever direction for the numbers you want.
Tariq Hammoudeh
on 19 Dec 2021
Thank you this really helped, just one more thing, can i have the 2 boards on top of each other instead of next to each other, also how can i get rid of the outside tick labels.
I tried removing
xticks(0.5 : numSpaces + 0.5)
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
yticklabels(ytl)
But I just end up with numbers from 1 to 7 on the outside. And i just want to remove them completely.
Image Analyst
on 20 Dec 2021
Just get rid of the calls to those and say
axis off;
To have diagrams over and under use
subplot(2, 1, 1);
and
subplot(2, 1, 2);
Tariq Hammoudeh
on 20 Dec 2021
Thank you so much for that, i will try and carry on now by myself, but can i just get a tip on how would i start to edit this plot as the game is played, For example changing the color of a grid and setting a grid to full.
Image Analyst
on 20 Dec 2021
The board color is determined by this line
colormap([1,1,1])
The first number is red, second is green, and third is blue. They range from 0 (black) to 1 (full brightness of that color).
To change other stuff on your figure, see attached demo.
Tariq Hammoudeh
on 20 Dec 2021
Thank you but i want to know how can i change the color of a specific grid, so for example change the color of grid numbers 2 and 3 to blue
Image Analyst
on 20 Dec 2021
You have to set up the values of the image you send to pcolor
m = ones(7);
m(3, 4) = 2;
m(4, 2) = 3
m = 7×7
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 2 1 1 1
1 3 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
pcolor(m);
axis ij; % Flip chart vertically
cmap = [1,1,1;
0,0,1;
0,0,1]
cmap = 3×3
1 1 1
0 0 1
0 0 1
colormap(cmap)
Tariq Hammoudeh
on 20 Dec 2021
Edited: Tariq Hammoudeh
on 20 Dec 2021
Ok so what i need is to have my original figure,using the previous code, then use
disp(' Its time to place your ships')
disp(' You will now place your corvette (1X2) ship')
startingGrid= input(' Please choose the grid number you want your ship to start in : ');
if startingGrid == 1
change the color of grids 1 and 2 to blue(or any color)
end
if I just use code you just gave me, it gives me a whole new figure (i attached a screenshot of it), so how can i keep the existing figure and just edit the color of some grids to represent ships being placed in them.
Walter Roberson
on 21 Dec 2021
Nothing in the code that Image Analyst posted deliberately opens a new figure.
In the posted code, what could potentially open a new figure is the call
subplot(1, 2, 1);
- if there is no "traditional" figure at all, that code would cause one to be created
- if there are traditional figures, but none of them are marked internally as the "current figure", then that line of code would result in a new traditional figure being opened. Usually it is not enough to delete the current figure in order to cause the "current figure" to be set empty: in such a case, the current figure is usually selected from the children of the graphics root. However, figures would not be eligible for automatic selection if the figure handlevisibility property is set "off" or (sometimes) "callback". Also, if the graphics root CurrentFigure property is deliberately set to empty, then no other figure will be substituted when a figure is closed.
Pay attention here to where I said "traditiona figure": if you happen to be using App Designer, then that subplot() call is likely to cause a new figure to be created. If you are using App Designer then all of the graphics operations in the code must be changed to refer to the appropriate parent graphics object. For example,
hold(app.axis1, 'on')
instead of hold on
Walter Roberson
on 21 Dec 2021
Edited: Walter Roberson
on 21 Dec 2021
Updating existing graphics:
The first time through, when you are first creating the board, then instead of
pcolor(m);
you would do something like
if first_time || ~isvalid(app.user_board)
app.user_board = pcolor(m, 'Parent', app.axis1);
axis(app.axis1, 'ij');
first_time = false;
else
app.user_board.CData = m;
end
drawnow();
The assignment to the CData property updates the surf graphics object that is created by pcolor() without creating any new object.
If you are using traditional figures instead of App Designer,
if first_time || ~isvalid(handles.user_board)
handles.user_board = pcolor(m, 'Parent', handles.axis1);
axis(handles.axis1, 'ij');
first_time = false;
guidata(hObject, handles);
else
handles.user_board.CData = m;
end
drawnow();
Tariq Hammoudeh
on 21 Dec 2021
Edited: Walter Roberson
on 21 Dec 2021
Thank you for that, but i tried this instead of the other code and i would get "Unrecognized function or variable 'first_time'.".
But the other code works fine, its just that i need to write if statements, so,
startingGrid= input('please enter the number of the grid you want your ship to start in')
if startingGrid == 1
change the color of grids numbers 1 and 2
end
Is that possible in code, to choose specifc number of the grid to another color to represesnt ships, using image analyst's code:
numSpaces = 6; % to plot a gridded figure with 6 rows and 6 columns
playersBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
%Creating the player's boards
subplot(2, 1, 2);
pcolor(playersBoard);
colormap([1,1,1])
axis square % to make the plot have grids
title('Your Board', 'FontSize', 15) %setting up the title of the board with the size of the text
axis off;
% Putting the numbers in each grid, using a for loop to put a number
% in each grid and move on to the next.
count = 1;
for col = 1 : numSpaces
for row = 1 : numSpaces
str = sprintf('%d', count);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
count = count + 1;
end
end
Walter Roberson
on 21 Dec 2021
Edited: Walter Roberson
on 21 Dec 2021
numSpaces = 6; % to plot a gridded figure with 6 rows and 6 columns
playersBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
%Creating the player's boards
subplot(2, 1, 2);
user_board = pcolor(playersBoard);
colormap([1,1,1; 1 0 0; 0 0 1; 0 0 0]); %white for unknown, red for player 1, blue for player 2, black for known empty
axis square % to make the plot have grids
title('Your Board', 'FontSize', 15) %setting up the title of the board with the size of the text
axis off;
% Putting the numbers in each grid, using a for loop to put a number
% in each grid and move on to the next.
count = 1;
for col = 1 : numSpaces
for row = 1 : numSpaces
str = sprintf('%d', count);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
count = count + 1;
end
end
while true
startingGrid = input('please enter the number of the grid you want your ship to start in');
if ~isscalar(startingGrid) || ~isnumeric(startingGrid) || ...
startingGrid < 1 | startingGrid > numSpaces || ...
startingGrid ~= floor(startingGrid)
fprintf('Must be integer from 1 to %d\n', numSpaces);
else
break
end
end
orientation = randi(2);
if orientation == 1
row = randi(numSpaces);
col = randi(numSpaces-startingGrid);
playersBoard(row, col:col+startingGrid-1) = 1;
else
row = randi(numSpaces-startingGrid);
col = randi(numSpaces);
playersBoard(row:row+startingGrid-1, col) = 1;
end
user_board.CData = playersBoard;
drawnow;
Tariq Hammoudeh
on 21 Dec 2021
If there is no way to do that in code (pick a number from the grid, and change its color), how can i specify which grid to change
Walter Roberson
on 21 Dec 2021
Edited: Walter Roberson
on 21 Dec 2021
while true
aim_row = input('Please enter row to aim for: ');
if ~isscalar(aim_row) || ~isnumeric(aim_row) || ...
aim_row < 1 | aim_row > numSpaces || ...
aim_row ~= floor(aim_row)
fprintf('Must be integer from 1 to %d\n', numSpaces);
else
break
end
end
while true
aim_col = input('Please enter column to aim for: ');
if ~isscalar(aim_col) || ~isnumeric(aim_col) || ...
aim_col < 1 | aim_col > numSpaces || ...
aim_col ~= floor(aim_col)
fprintf('Must be integer from 1 to %d\n', numSpaces);
else
break
end
end
what_is_there = player2Board(aim_row, aim_col);
if what_is_there == 0
playerBoard(aim_row, aim_col) = 3;
elseif what_is_there == 1
fprintf('Your opponent has one of your pieces?!?!\n');
playerBoard(aim_row, aim_col) = 3;
elseif what_is_there == 2
playerBoard(aim_row, aim_col) = 2;
fprintf('You hit!!\n');
elseif what_is_there == 3
fprintf('You already aimed there!\n');
end
user_board.CData = playerBoard;
Tariq Hammoudeh
on 21 Dec 2021
Thank you for that, but firslty this code just removed the computer's board (when I run it, it only shows the player's board and its all in blue) and only colors 1 grid after inputting the starting grid. Also I need the player to be able to place his 5 ships using, the startingGrid then whether its vertical or horizonatal,
I had this in mind:
disp(' Its time to place your ships')
disp(' You will now place your corvette (1X2) ship')
startingGrid= input(' Please choose the grid number you want your ship to start in : ');
orientation= input( please enter v if you want your ship to be placed vertically, or h if you want your ship to be placed horizontally)
if startingGrid == 1
if orientation == v
change grids 1 and 7 to blue
elseif orientation == h
change grids 1 and 2 to blue
end
end
Because i need to be able to place 5 different ships each with different dimensions,
Also im very well aware that this way is not practical at all as ill need to have 36 if statements for each of the 5 ships, but it is the only way i can think of. So how can i make it so that the player can just enter the starting grid then orientation, then the correct number of grids change color, while keeping the computer's boards in the figure.
Tariq Hammoudeh
on 21 Dec 2021
Please note that the placing of those 5 ships is only for the user (the user will be playing against the computer), so just to be a little bit more efficient, is there a way to also a set colored grid (ship placed in grid) as full, so later when the game actually starts, i can use,
if grid is full
disp(hit)
else
disp(miss)
Walter Roberson
on 22 Dec 2021
is there a way to also a set colored grid (ship placed in grid) as full
NO, there is not.
You need two graphics objects, one showing the user's board as the user knows it (the one that is "hidden" from the opponent), and the other one showing the opponent's board as the user knows it, which should start showing no information known.
You are effectively asking whether you could get away with just having the graphics two graphics object, and checking one of them to see whether the position the user aimed at was occupied. But the graphics object that displays what the user knows about the opponent's board does not know what the opponent has at a given location unless that information has already been revealed by the user having already aimed there.
You also need an array representing the current state of the user's board (as known to the user), and another array representing the current state of the opponent's board (as known to the user), and another array representing the current state of the opponent's board (as known to the opponent), and another array representing the current state of the user's board (as known to the opponent.)
In the code I posted, I only used two arrays, one representing what the user knows, and one representing what the opponent knows. That was a mistake on my part, as it does not easily allow information about the opponents board to be represented in the same square as information about the user's pieces. For example of the user had a piece at C4 and aimed the opponent's C4 and found the opponent was empty there, then you need information for C4 that the user has a piece and that the user has checked and found the opponent's space empty at C4. Easiest to separate into two arrays. It is, though, not the only way -- you can encode information about both players, if you are willing to do a bit more work extracting the information.
For that matter, it would be possible to encode all of the information in one array:
- 0 -- not occupied by either player and neither player has fired at the other player there
- 1 -- occupied by the player only and neither player has fired at the other player there
- 2 -- occupied by the opponent only and neither player as fired at the other player there
- 3 -- occupied by the player and the opponent and neither player has fired at the other player there
- 4 -- not occupied by either player and the player has fired at the opponent there -- a miss for the player
- 5 -- occupied by the player only and the player has fired at the opponent there -- a miss for the player
- 6 - occupied by the opponent only and the player has fired at the opponent there -- a hit for the player
- 7 occupied by the player and the opponent and the player has fired at the opponent there -- a hit for the player
- 8 not occupied by either player, and the opponent has fired at the player here -- a miss for the opponent
- 9 occupied by the player only and the opponent has fired at the player here -- a hit for the opponent
- 10 occupied by the opponent only and the opponent has fired at the player here -- a miss for the opponent
- 11 occupied by the player and the opponent and the opponent has fired at the player here -- a hit for the opponent
- 12 not occupied by either player and the player has fired at the opponent here and the opponent has fired at the player here -- a miss for the player and a miss for the opponent
- 13 occupied by the player only and the player has fired at the opponent there and the opponent has fired at the player there -- a miss for the player but a hit for the opponent
- 14 occupied by the opponent only and the player has fired at the opponent there and the opponent has fired by the player there -- a hit for the player and a miss for the opponent
- 15 occupied by the player and the opponent and both have fired at each other there -- a hit for the player and a hit for the opponent
So you could encode everything into one array, and then process it to figure out what to display. If you think that is easier.
Tariq Hammoudeh
on 22 Dec 2021
Edited: Tariq Hammoudeh
on 22 Dec 2021
Thank you so much, but firstly, the computer's (opponent) ships placements are in external files that i have to create and for every game the program must choose one of those files randomly and place them for the computer ( my figure should only show the computer's board not where his ships are), but Ill get to all that later, for now all i need is to allow the user to enter a number from 1 to 36, then let them enter either v or h for the orientation of the ship, and then place the ship based on what was entered. And i want to do that while showing both boards in the figure and whenever a user places a ship the color of those specifc grids changes. ( so basically for now i just want to be able to place the ships, ill get to the gameplay later, as I really dont know how to do more than one thing at once).
Walter Roberson
on 22 Dec 2021
In that case, the answer to
is there a way to also a set colored grid (ship placed in grid) as full
is:
If you have saved the handle to the pcolor() object that was used to display the user's board as known to the user, and the opponent (the computer) is aiming at the human player, then you can
grid_is_full = graphics_object_for_user_board.CData(IndexAimedAt);
if grid_is_full
disp('Computer hit!');
graphics_object_for_user_board.CData(IndexAimedAt) = 2; %it's been hit
else
disp('Computer missed!');
end
However, there is no graphics object representing where the computer stored its pieces, so you cannot use the same technique to determine whether the user was successful in hitting the computer's pieces. Instead, you would have to consult an array of data that is not a graphics object.
Eventually, you would have something like
grid_is_full = array_for_computer_board(IndexAimedAt);
if grid_is_full
disp('Player hit!');
graphics_object_for_computer_board.CData(IndexAimedAt) = 2; %it's been hit
else
disp('Player missed!');
graphics_object_for_computer_board.CData(IndexAimedAt) = 1; %it's been missed, but it is no longer unknown
end
drawnow()
Tariq Hammoudeh
on 22 Dec 2021
Ok thank you, this is for the gameplay, but i need to place the ships before this starts, so firstly i need to write all the if statements to change the color of the grids to represent ships, so how can i use,
startingGrid= input(' Please choose the grid number you want your ship to start in : ');
orientation= input( please enter v if you want your ship to be placed vertically, or h if you want your ship to be placed horizontally)
if startingGrid == 1
if orientation == v
change colors of grids 1 and 7
elseif orientation == h
change colors of grids 1 and 2
end
end
also while i do that how can i make it so that the user cant put 2 ships in the same grid.
Image Analyst
on 22 Dec 2021
@Tariq Hammoudeh, this is becoming quite involved. If you want the fun of writing it yourself, then have fun. If all you need is one already written for you, then I'm sure there are lots of battleship games in the File Exchange already. But me writing one for you, or accompanying you on a long development process is not something I can afford to spend the time to do. Walter has been kind enough to pick up where I left off but I don't think he wants to spend hours more on your project. No offense but I think you should have enough skills so far to do it yourself, and if you don't, then that's how you learn, and that's how you have fun and a sense of your accomplishment (not ours) once it's finished. I just thought I'd let you know why I don't have time for this question anymore. But good luck and have fun. 🙂
Tariq Hammoudeh
on 22 Dec 2021
I really know this, but the reason i ask those questions, is because i cant learn if i dont have the code in the first place, so i could never know how to code if i dont know what code to use or when, so when i ask, i learn from the answers. but its fine, thanks for the help
Walter Roberson
on 23 Dec 2021
Edited: Walter Roberson
on 23 Dec 2021
the reason i ask those questions, is because i cant learn if i dont have the code in the first place
In that case, you should drop the course. Computer programming is more about writing new code to do things, not about finding some existing code and rewriting it (somehow). If you can't learn how to write code without an example of code for the same purpose, then you are not going to be able to program for the purposes fo the course.
Exception: there is big demand for people who are able to rework code that was written in the 1950's to 1970's, that now does not work because of changes to the programming language, or which people want to write in more modern programming languages. Code written in programming languages such as COBOL. In such cases, code already exists as reference, but must be repaired or must be used as a guide to write new code.
I have several long-running "remediation" projects in my career. It is often hard work. To do it well, you have to be able to study the existing code and see how each line fits with every other line, and not only see the bugs but also be able to create a mental model of the shortcomings of each previous programmer who worked on the code, to be able to figure out what they intended to write by seeing what they did write instead. You have to be able to recognize that not only did someone in the past break existing code that was probably working fine: you have to recognize their pattern of bugs to figure out what they thought they were doing, and then to figure out why they thought they had to do that, what were they really trying to do. Which might involve undoing changes they made and changing a different section of the code instead.
Tariq Hammoudeh
on 23 Dec 2021
ok but how can someone learn if they dont know what is the language capable of, for example i didnt know that subplot() existed, and then i also have no clue on how can i edit it, like i dont know what i can and cant do, and i definetly cant just "write" code when i dont know how it works in the first place. But as I said its fine, this is my last assignment and this is my only software course.
Walter Roberson
on 23 Dec 2021
There is a difference between not knowing which routines to call to accomplish tasks, compared to saying that you can't learn if you do not already have code for the same purpose in front of you.
There are lots and lots of example programs in the File Exchange, and if you ask us about specific tasks then we are happy to answer.
But you have effectively told us that you need us to write the whole program for you first before you can write your own code, and we are not willing to write the whole program for you.
I have shown you how to change specific locations in graphics by altering the CData property of the handle returned by pcolor()
I have shown you how to make tests of whether conditions are satisfied or not.
Additional hints:
- pcolor() is the wrong thing to use. Use image() instead -- but make sure the data values are uint8() so that the data values can act like images into the color map (I already showed you an example of a color map)
- input() with only a prompt always executes the line that the user types as if it were code. So if you ask the user to enter h or v and they enter those, then MATLAB would attempt to execute the v or p, by searching for variables and functions with those names. If you want the user to enter text then add the 's' option to input()
- Use strcmp() to compare text. For example strcmp(orientation, 'v')
- You will find that it is easier to have the user enter positions in terms of grid coordinates. In classic Battleships game the rows are numbered 1 to 10 and the columns are labeled 'A' to 'J' . If you have the user enter the coordinates as text you could figure out whether they had entered number-letter or letter-number and extract appropriate parts.
- The reason it is easier with grid coordinates than with numbers like 1 to 36, besides being easier for the user to understand, is that when you want to place a ship, it is easiest to have row and column of the starting point so that you can use (row, column:column+length-1) or (row:row+length-1,column) like I showed in https://www.mathworks.com/matlabcentral/answers/1614260-how-can-i-plot-a-game-board-in-a-figure#comment_1899030
Image Analyst
on 23 Dec 2021
Yes I agree that image() would be better than pcolor(). The only disadvantage is that image() doesn't give grid lines while pcolor() does. However you can use yline() and xline() to draw grid lines. If you do that then image is probably better because with pcolor you need to have one more row and column of your matrix that you basically don't use because pcolor() does not display the last row and column of the matrix.
Walter Roberson
on 23 Dec 2021
pcolor() interpolates; which is a problem for this purpose.
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)