Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We are writing a basic function to simulate the game 2048 and I need help with basic code for the up and down functions. Please

We are writing a basic function to simulate the game 2048 and I need help with basic code for the up and down functions. Please in SIMPLE/BASIC matlab writing. We are working with moving rows and columns. I need similar tiles above or below one another to combine and move up or down until they reach a spot in the matrix that is full with a number. Here's our code thus far, if you can incorporate the up and down code writing to fit into our existing script.

A=zeros(4,4);

%% r=randi([1,4],1); c=randi([1,4],1); while A(r,c)~=0 %if the number doesn't=0, generate a new row and column number r=randi([1,4],1); c=randi([1,4],1); end A(r,c)=2;

while A(r,c)~=0 r=randi([1,4],1); c=randi([1,4],1); end A(r,c)=2; disp(A) %% gameover=0; while gameover == 0 B=menu('Direction for movement', 'Up','Down','Left','Right','Quit'); %up=1 down=2 left=3 right=4 Quit=5 %% end

%% %Left if B==3 for row=1:4 for column=1:4 if A(row,column)>0 for sumattempt=1:4-column if A(row,(column+sumattempt))>1 if A(row,(column+sumattempt)) == A(row,column) A(row,column)=A(row,column)*2; A(row,column+sumattempt) = 0; end break end end end end end for row=1:4 Arow = A(row,:); row2 = Arow(Arow ~= 0); [V1,V2] = size(row2); if V2==0 end if V2==1 Arow = [row2(1),0,0,0,]; end if V2 == 2 Arow = [row2(1),row2(2),0,0]; end if V2 == 3 Arow = [row2(1),row2(2),row2(3),0]; end if V2 == 4 end A(row,:) = Arow; end end %% %Right if B == 4 for row = 1:4 for column = 1:4 if A(row,5-column)>0 for sumattempt = 1:4-column if A(row,(5-column)-sumattempt)>1 if A(row,(5-column)-sumattempt) == A(row,5-column) A(row,5-column)=A(row,5-column)*2; A(row,(5-column)-sumattempt) = 0; end break end end end end end for row = 1:4 Arow = A(row,:); row2 = Arow(Arow ~= 0); [V1,V2] = size(row2); if V2 == 0 end if V2 == 1 Arow = [0,0,0,row2(1)]; end if V2 == 2 Arow = [0,0,row2(1),row2(2)]; end if V2 == 3 Arow = [0,row2(1),row2(2),row2(3)]; end if V2 == 4 end A(row,:) = Arow; end end

%Down %Up

%% %Ending the game if B == 5 gameover=2; end %% disp(A) if gameover==0 if A(r,c)==2048 gameover=1; end end

%% %Adding new numbers i=randi([1,2],1,1); if i==1 while A(r,c)~=0 r=randi([1,4],1); c=randi([1,4],1); end A(r,c)=2; elseif i==2 while A(r,c)~=0 r=randi([1,4],1); c=randi([1,4],1); end A(r,c)=4; end disp(A)

%% if gameover==1 disp('You win') end if gameover==2 disp('You lose') end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

Write a note on job design.

Answered: 1 week ago

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago