How to create a grid for memory tile game?

7 ビュー (過去 30 日間)
Fatma Al-Sharji
Fatma Al-Sharji 2015 年 4 月 3 日
回答済み: thabo Mokomo 2016 年 11 月 17 日
How to create a grid for memory tile game in MATLAB. The grid should contain random number of rows and number of columns by the choice of the player.

採用された回答

Star Strider
Star Strider 2015 年 4 月 4 日
編集済み: Star Strider 2015 年 4 月 4 日
The grid generation is easy:
nrows = 5; % User Input: Number Of Rows
ncols = 7; % User Input: NumberOf Columns
XL = [0 nrows];
YL = [0 ncols];
rowx = repmat([0:ncols], 2, 1);
rowy = [min(XL)*ones(1, ncols+1); max(XL)*ones(1, ncols+1)];
colx = [min(YL)*ones(1, nrows+1); max(YL)*ones(1, nrows+1)];
coly = repmat([0:nrows], 2, 1);
figure(1)
plot(rowx, rowy, 'g')
hold on
plot(colx, coly, 'g')
hold off
axis equal tight

その他の回答 (2 件)

Fatma Al-Sharji
Fatma Al-Sharji 2015 年 4 月 5 日
編集済み: Star Strider 2015 年 4 月 5 日
Thank You so much!
But how can I display numbers on the tiles so that it displays two similar numbers? Here's my code but it didn't work out with me
A = randi(numRows,numCols);
nums = randperm((numRows*numCols)/2);
Could you please check what's the error with my code?
Thanks
  1 件のコメント
Star Strider
Star Strider 2015 年 4 月 5 日
編集済み: Star Strider 2015 年 4 月 5 日
My pleasure!
Using the randi function is definitely the way to go, so that is the correct choice. I took it a bit further. (Note that you have to have the strsplit function to make my code work. If you don’t have it, there is a workaround with a for loop.)
This works:
nrows = 5; % User Input: Number Of Rows
ncols = 7; % User Input: NumberOf Columns
XL = [0 nrows];
YL = [0 ncols];
rowx = repmat([0:ncols], 2, 1);
rowy = [min(XL)*ones(1, ncols+1); max(XL)*ones(1, ncols+1)];
colx = [min(YL)*ones(1, nrows+1); max(YL)*ones(1, nrows+1)];
coly = repmat([0:nrows], 2, 1);
figure(1)
plot(rowx, rowy, 'g')
hold on
plot(colx, coly, 'g')
hold off
axis equal tight
N = randi(nrows*ncols, 1, nrows*ncols);
Ns = strsplit(sprintf('%2d\n', N));
Ns = Ns(1:end-1);
xmat = repmat([1:ncols], 1, nrows)-0.5;
ymat = repmat([1:nrows], 1, ncols)-0.5;
text(xmat, ymat, Ns, 'HorizontalAlignment','center', 'VerticalAlignment','middle')

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


thabo Mokomo
thabo Mokomo 2016 年 11 月 17 日
how do you add all the pictures

カテゴリ

Help Center および File ExchangeVideo games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by