Create matlab code for memory game.

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

You should be checking
isnumeric(x) && isnumeric(y) && numel(x) == 2 && numel(y) == 2
and you should also be checking that any(x ~= y) or else you would be permitting the user to select the same location twice, and of course the values would be the same in that case...
Thank you. Firstly, I need to change all the values in the cell to display '+' as this signifies an unturned card. How could I do this?
Secondly, how do I go about randomly allocating 32 cards consisting of the letters A-H appearing four times. Any pairing of the same letters should be considered a successful find.
clear
clc
disp('Welcome to the Memory Game Buddy!')
disp('Here is the board so far, buddy!!!')
% initialize the board
cell(4,8);
cell{1,8} = '+';
% prompt user
prompt = "Please enter first card (row col):";
x = input(prompt);
prompt2 = "Please enter second card (row col):";
y = input(prompt2);
isnumeric(x) && isnumeric(y) && numel(x) == 2 && numel(y) == 2
any(x ~= y)
% Decision: Are the two selected/newly discovered letters the same?
% If the same, congratulate the player and keep the cards face-up (keep displaying the letters)
% If not the same, display board with the letters, then remove them after a key press
% Decision: Has the entire game board been discovered yet?
% If not, clear the screen after key press and go back to step 2.
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(:) = {'+'}
board = 4×8 cell array
{'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'} {'+'}
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 16일

0 개 추천

Techniques for filling in the hidden board
gorp = repmat('27=@/',1,3)
gorp = '27=@/27=@/27=@/'
gorp = gorp(randperm(numel(gorp)))
gorp = '/77=/@22/2=7=@@'
gorp = reshape(gorp, 3, 5)
gorp = 3×5 char array
'/=22=' '7/2=@' '7@/7@'

댓글 수: 2

Thank you for the help. I'm having trouble knowing how to randomly allocate 32 cards consisting of the letters A-H appearing four times within the cell array.
clear
clc
disp('Welcome to the Memory Game Buddy!')
disp('Here is the board so far, buddy!!!')
% initialize the board
clear cell
board = cell(4,8);
board(:) = {'+'}
% prompt user
prompt = "Please enter first card (row col):";
x = input(prompt);
prompt2 = "Please enter second card (row col):";
y = input(prompt2);
isnumeric(x) && isnumeric(y) && numel(x) == 2 && numel(y) == 2;
gorp = repmat('27=@/',1,3)
gorp = gorp(randperm(numel(gorp)))
gorp = reshape(gorp, 3, 5)
% Decision: Are the two selected/newly discovered letters the same?
% If the same, congratulate the player and keep the cards face-up (keep displaying the letters)
% If not the same, display board with the letters, then remove them after a key press
% Decision: Has the entire game board been discovered yet?
% If not, clear the screen after key press and go back to step 2.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Card games에 대해 자세히 알아보기

질문:

2022년 8월 16일

편집:

2022년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by