フィルターのクリア

How can I create a battleship game board with a variable size as a char array so that I can edit certain elements of it?

1 回表示 (過去 30 日間)
So I have the user input the size of the board they want to play on and I figured out how to do it using for loops and fprintf statements, but I need what this code produces but in an array of type char, can anyone help me out?
%Create the visible board
%for loop creating first row of the board
fprintf ('\n- ')
for i = 1:Size
fprintf (' %d ', i);
end
fprintf ('\n')
%for loop creating the rest of the rows of the board
for j = 1:Size
fprintf ('%d', j)
for j = 1:Size
fprintf (' .')
end
fprintf ('\n')
end

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 11 月 29 日
編集済み: Walter Roberson 2015 年 11 月 29 日
To get you started
ndig = ceil( log10(Size*(1+2*eps)) );
n_fmt_element = sprintf('%%%dd', ndig+1 );
c_fmt_element = sprintf('%%%ds', ndig+1 );
n_fmt = repmat(n_fmt_element, 1, Size);
c_fmt = repmat(c_fmt_element, 1, Size);
rawline{1} = num2cell(1:Size);
lines{1} = sprintf(n_fmt, rawline{1}{:});
rawline{2} = repmat({'.'}, 1, Size);
lines{2} = sprintf(c_fmt, rawline{2}{:});
board = char(lines);
  2 件のコメント
Shayne Albertson
Shayne Albertson 2015 年 11 月 29 日
Could you explain this more? I'm not sure how what you did works.
Walter Roberson
Walter Roberson 2015 年 11 月 29 日
To construct a visual board you need to print every element out with the same size. The way you do that is to use sprintf() with a format that tells the size to print out as. For example a format element of %6s says to use 6 characters for a string (right justifying and using leading blanks if the string is shorter than 6), and a format element of %6d says to use 6 characters for a decimal number (right justifying and using leading blanks if the number would require fewer than 6 digits)
But you did not define a fixed size of board. So the first step is to calculate how many characters are going to be needed to align everything nicely in a table. A board size up to 9 x 9 needs 1 digit for each number. A board size 10 x 10 up to 99 x 99 needs 2 digits for each number. A board size 100 to 999 needs 3 digits per number plus 1 space between columns. If you relate this to log10 of the size you see that log10 up to but excluding 1 exactly needs 1 digit, log10 from 1 exactly up to but excluding 2 exactly needs 2 digits, and so on. The "excluding" is because 10 exactly has a log10 of 1 exactly but needs 2 digits, and 100 exactly has a log of 2 exactly but needs 3 digits. Multiplying the Size by 1+2*eps is enough to nudge 10 exactly to something whose log10 is greater than 1, nudge 100 exactly to something whose log10 is greater than 2. So the calculation of ndig is the calculation of the number of digits needed to print out a value as large as Size for the numbering of the columns.
The two sprintf() after that are for building the format string like %6d and %6s for later use. ndig+1 is used instead of ndig to give a space between columns.
The repmat() expand the individual element formats from the single %6s (for example) to %6s%6s%6s%6s....%6s with as many copies as needed for Size. These are serving to construct sprintf formats for use a line at a time.
Beyond that... read about comma separated lists and cell arrays

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by