Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

simulate the game \"Threes!\" in MATLAB. The \"game board\" should take place on

ID: 3902596 • Letter: S

Question

simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4 matrix. Simulate empty matrix cells with zeros. o a zero isan empaty space and a non-empty sG-) An example 0 2 2 1 Gameboard vw1 230 Thejndividual custom function should be able to take any ONE number within the gameboard id axci tme i funcon should uillixe branches io accout for all pssibl scenarios (the number is already against the wall, it is blocked by a number, it is not blocked by any numbers, it combines with the number adjacent to it, or the number is a zero so it doesn't do anything.) The function should have 4 input arguments: the current score, the current gameboard (matrix), a single row index, and a single column index. The function should have 2 output arguments: the updated score and the updated gameboard matrix. The row/column indices will be the individual game element your function should attempt to move. Example: if you are doing the DOWN function with the sample gameboard seen on the previous page, if the input is: row-2, column-3, then your function should output a new matrix with a value of 6 in index (3,3), a value of 0 in index (2,3) and a score that has increased by 6. If instead the input was row-4, column-3 then both the matrix and the score would remain unchanged since the element in index (4,3) is against the wall and cannot move down. Evaluate number movement/combination resulting from an UP movement

Explanation / Answer

Find the required code for the simulation of 'up' movement as asked in the following. The code is self explanatory with sufficient interactive text. The function for the simulation is named as game. Store the two scripts in separate files and remember to name the function script with the name of the function i.e 'game' only.

Hope this helps!

%====================== The main script ====================

clear all;
m=zeros(4);
score=0;

% Asking user for non-empty entries in Matrix
fprintf(' Please enter the initial entries in matrix! ');
for i=1:4
    for j=1:4
        resp=input(strcat('Do you want to enter at row-',num2str(i),' and column-',num2str(j),' [y/n]? :'),'s');
        if(resp=='y')
            m(i,j)=input('Please enter the value: ');      
        end
    end
end
fprintf('The entered matrix is: ');
disp(m);
resp='y';
while(1);
    resp=input('Do you want to play [y/n]? : ','s');
    if(resp=='y')
        display('Please enter the location on the board matrix you want to start with!');
        row=input('Please enter the row value: ');
        col=input('Please enter the column value: ');
        [score,m]=game(score,m,row,col);
        fprintf('The updated matrix is: ');
        disp(m);
        fprintf('The updated score is: ');
        disp(score);
    else
        break;
    end

end
fprintf('The final matrix is: ');
disp(m);
fprintf('The final score is: ');
disp(score);

%===================================================================

%====================== The game function ====================

function [ score,m ] = game( score,m,row,col )
% We are simulating an 'UP' movement only as asked
if(row>1 && m(row,col)~=0)
    m(row-1,col)=m(row,col)+m(row-1,col);
    score=score+m(row,col);
    m(row,col)=0;
end
end


%======================================================