Create matlab code for memory game.

9 visualizzazioni (ultimi 30 giorni)
Bozza
Bozza il 16 Ago 2022
Modificato: Bozza il 7 Ott 2022
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 Commenti
Bozza
Bozza il 16 Ago 2022
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.
Walter Roberson
Walter Roberson il 16 Ago 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(:) = {'+'}
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.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 16 Ago 2022
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 Commenti
Bozza
Bozza il 16 Ago 2022
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.
Walter Roberson
Walter Roberson il 17 Ago 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.

Accedi per commentare.

Categorie

Scopri di più su Card games in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by