Create matlab code for memory game.
4 views (last 30 days)
Show older comments
I want to create matlab code for following memory game.
Request input from the player to select two cards by entering row and column numbers.
Decision: Are the two selected/newly discovered letters the same?
clear
clc
disp('Welcome to the Memory Game Buddy!')
disp('Here is the board so far, buddy!!!')
% initialize the board
board = cell(4,8);
% prompt user
prompt = "Please enter first card (row col):";
x = input(prompt);
prompt2 = "Please enter second card (row col):";
y = input(prompt2);
4 Comments
Walter Roberson
on 16 Aug 2022
cell(4,8);
That would generate a 4 x 8 cell array, and then throw it away.
cell{1,8} = '+';
That would generate a 1 x 8 cell array named cell and assign '+' to the last column of the row. After you did this, the name cell would be a variable and it would no longer be possible to explicitly allocate cell arrays.
You should be assigning values to variables
clear cell
board = cell(4,8);
board(:) = {'+'}
Then
isnumeric(x) && isnumeric(y) && numel(x) == 2 && numel(y) == 2
You are calculating a logical result and displaying it, but you should instead be using it to make a decision with an if statement.
Answers (1)
Walter Roberson
on 16 Aug 2022
Techniques for filling in the hidden board
gorp = repmat('27=@/',1,3)
gorp = gorp(randperm(numel(gorp)))
gorp = reshape(gorp, 3, 5)
2 Comments
Walter Roberson
on 17 Aug 2022
gorp = repmat('27=@/',1,3)
That takes 5 different characters and repeats each one 3 times
gorp = gorp(randperm(numel(gorp)))
and that scrambles into a random order
gorp = reshape(gorp, 3, 5)
and that turns it into a 3 x 5 array in which each of the 5 characters is repeated 3 times.
You should be able to use the same kinds of techniques to repeat 8 different characters 4 times and create a 4 x 8 array from the results.
isnumeric(x) && isnumeric(y) && numel(x) == 2 && numel(y) == 2;
Well, before you used to be computing the logical result and displaying it instead of using it to make a decision. Now you have progressed to computing the logical result and discarding it, without using it to make a decision.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!