how to "convert" user inputted letter and number combination to a row,col position on a plot?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
Adam Danz
2020-10-16
编辑:Adam Danz
2020-10-16
"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
"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))
randNumbers = randi([1,size(M,2)],n,1)
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))
0 个评论
Ameer Hamza
2020-10-16
编辑:Ameer Hamza
2020-10-16
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'
0 个评论
J. Alex Lee
2020-10-16
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!