how to "convert" user inputted letter and number combination to a row,col position on a plot?

5 visualizaciones (últimos 30 días)
I am trying to program a game of Battleship, where a human plays against a computer. The user is to input a number, for example 'A', and a number, for example, '7'. I have already plotted the positions of both the human and computer ships. How do I correspond the user input A7 to a position on the plot, for example row=1, col=7? Also, how can I randomly determine a letter-number combo for the computer and correspond it to a row,col position? These should both loop until either the player or computer hits all ships
thanks

Respuestas (3)

Adam Danz
Adam Danz el 16 de Oct. de 2020
Editada: Adam Danz el 16 de Oct. de 2020
"The user is to input a number..."
How is the user providing these inputs? If you're using the input() function, don't. It's highly unconstrined allowing the user to enter virtually anything (more info: link1, link2).
"How do I correspond the user input A7 to a position on the plot, for example row=1, col=7?"
The numeric part already corresponds to the column number. To convert the letter A:Z to row numbers,
% letter is a single character A:Z (or a:z).
% char() is there in case user enter a string "A" intead of a char 'A'
letter = 'B';
rowNumber = double(upper(char(letter)))-64
rowNumber = 2
"how can I randomly determine a letter-number combo for the computer and correspond it to a row,col position"
M is the size of your matrix.
n is the number of random samples you want
M = ones(15,20);
n = 5;
randLetters = char(randi([65, size(M,1)+64],n,1))
randLetters = 5x1 char array
'G' 'H' 'I' 'L' 'E'
randNumbers = randi([1,size(M,2)],n,1)
randNumbers = 5×1
15 18 15 20 9
If you want to combine the letters and numbers, into the form 'A1', the numbers need to be converted to characters.
LN = strcat(randLetters, compose('%d',randNumbers))
LN = 5x1 cell array
{'G15'} {'H18'} {'I15'} {'L20'} {'E9' }

Ameer Hamza
Ameer Hamza el 16 de Oct. de 2020
Editada: Ameer Hamza el 16 de Oct. de 2020
For case when you have a string like 'A7'
str = 'A7';
nums = str - ('A1'-1);
Result
>> nums
nums =
1 7
For randomly generating such a string
str = char([randi([65 97]) randi([48 57])]); % ascii ranges of alpha numeric values
Result
>> str
str =
'I6'

J. Alex Lee
J. Alex Lee el 16 de Oct. de 2020
In case you want to make battlship on an insanely large grid, here's a more general solution to convert "Excel column" style labels into a number (case insensitive), assuming character arrays and cellstr's (strings not supported, but i think pretty simple to extend)
function d = alpha2dec(s)
% convert A-Z to 1-26, AA=27, AB=28, etc.
%
% d = alpha2dec(s)
%
% returns the decimal d of string s which represents d in base 26
% expressed as 'A'..'Z', 'AA','AB'...'AZ'
%
% Examples
% alpha2dec('A') returns 1
% alpha2dec('Z') returns 26
% alpha2dec('IV') returns 256
% alpha2dec('esa') returns 3875
%
% notes:
% 1. In ASCII, 'a' does not equal 'A', so we ensure the conversion works by
% transforming s to upper
%
if ~iscell(s)
s = {s};
end
d = nan(size(s));
for j = 1:length(s)
n = length(s{j}); % number of letters in the string
p = 26.^((n-1):-1:0); % powers of the base
a = upper(s{j}).' - 'A' + 1; % convert each "digit" into integer 1-26
d(j) = p * a; % output is the power series in b26
end
end % alpha2dec

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by