help with basic 2048 game function
Mostra commenti meno recenti
hi
i'm a super basic user, i barely know how to code. i tried to create a very simple function that reacreate the game '2048' with a 4x4 matrix serving as the palyground.
it's really rudimentary and so mane lines are written poorly (i would like to say creatively )because i don't know better. I also know it's written in such an uneffincent way.
but in my mind it should at least work. and it doesn't.
now, i tried to comment trough what i tried to code the machine to do. Is there anyone willing to get peek at the code and tell me what conceptual mistakes i've done?
thank you in advance
function [a] = game2048(a)
%%the user is asked to run the funcion with a random variable a, that is
%%overwritten and initialized as zeros(4,4) if it isnt a 4X4 matrix
%%(it's possible to cheat by inputting a solved problem)
%%(dunno how to make a function accept no input)
if size(a)~=[4,4]
a=zeros(4,4);
ii=1+round(9*rand);
%a random value(10% is 4, 90% is 2) is given to a random cell
if ii>1
a(1+round(3*rand),1+round(3*rand))=2
else
a(1+round(3*rand),1+round(3*rand))=4
end
end
%%the user is asked with a direction to drag the cells to
direzione=input('direzione? usa WASD','s');
if isempty(direzione)%%control over the imput
direzione=input('direzione? usa solo WASD','s');
end
a1=zeros(4,4);
while a1~=a %%an initial state of the matrix is being saved, to check is there are
%%no more passages to be done(if the last while cycle has as output the input, no more
%%work has to be done)
a1=a;
if direzione=='w'%%2 checks are made, is a cell is empty, the next one in the direction of the move is cut-pasted onto it, if it has the same value the firs one gets doubled and the second one nullified
for jj=1:3 %%2 passages of checks are made for each move, across the matrix. first going against the direction of the move(last row/column of this passage is not included), and "indented" in it going perpendicularly to it across the matrix
for ii=1:4
if a(jj,ii)==0
a(jj,ii)=a(jj,ii+1);
a(jj,ii+1)=0;
elseif a(jj,ii)==a(jj,ii+1)
a(jj,ii)=a(jj,ii)*2;
a(jj,ii+1)=0;
end
end
end
elseif direzione=='s'
for jj=flipr(2:4)
for ii=1:4
if a(jj,ii)==0
a(jj,ii)=a(jj,ii-1);
a(jj,ii-1)=0;
elseif a(jj,ii)==a(jj,ii-1)
a(jj,ii)=a(jj,ii)*2;
a(jj,ii-1)=0;
end
end
end
elseif direzione=='a'
for ii=1:3
for jj=1:4
if a(jj,ii)==0
a(jj,ii)=a(jj,ii+1);
a(jj+1,ii)=0;
elseif a(jj,ii)==a(jj,ii+1)
a(jj,ii)=a(jj,ii)*2;
a(jj,ii+1)=0;
end
end
end
elseif direzione=='d'
for ii=flipr(2:4)
for jj=1:4
if a(jj,ii)==0
a(jj,ii)=a(jj-1,ii);
a(jj-1,ii)=0;
elseif a(jj,ii)==a(jj-1,ii)
a(jj,ii)=a(jj,ii)*2;
a(jj-1,ii)=0;
end
end
end
else
direzione=input('direzione? usa SOLO WASD','s');%%control over the imput
end
end
hh=1;
kk=1;
while a(hh,kk)~=0%%a random value(10% is 4, 90% is 2) is given to a random cell that is empty
hh=1+round(3*rand);
kk=1+round(3*rand);
end
ii=1+round(9*rand);
if ii>1
a(hh,kk)=2
else
a(hh,kk)=4
end
if max(max(a))==2048%% look for the "2048" tile-> user won
input('hai vinto');
else
if min(min(a))==0%%if there is any empty tile, the game keeps going, the function calls itself with the current state as input
game2048(a);
else%%if none of the above is true, the user lost the game
c=input('hai perso, vuoi rigiocare? y/n','s');
if isempty(c)||c~='y'||c~='n'
c=input('hai perso, vuoi rigiocare? y/n','s');
end
if c=='y'
game2048(zeros);
else
clear a;a='ciao';
end
end
end
end
2 Commenti
the cyclist
il 2 Ago 2019
For testing purpose, I would suggest you put the line
rng default
at the beginning, so that your code is reproducible, giving the same error each time. Take it out later, so that the game is random again.
It would be helpful if you were to tell us specifically what you mean by "it doesn't work".
Jad Michele Samuel Musallam
il 3 Ago 2019
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Desktop in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
