Question

MATLABsimulate 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 doesnt 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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('\n Please enter the initial entries in matrix! \n');
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: \n');
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: \n');
        disp(m);
        fprintf('The updated score is: \n');
        disp(score);
    else
        break;
    end

end
fprintf('The final matrix is: \n');
disp(m);
fprintf('The final score is: \n');
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


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

Add a comment
Know the answer?
Add Answer to:
MATLAB simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a MATLAB function that takes a matrix, a row number and a scalar as...

    1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...

  • C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an...

    C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an elimination dice game. It can be played by any number of players, but it is ideal to have three or more players. Wild, Wild, West! Requires the use of two dice. At the start of the game, each player receives a paper that will track the number of lives that player has. Each player starts the game with 6 lives. In the first round,...

  • Code in JAVA You are asked to implement “Connect 4” which is a two player game....

    Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • 1. Write a MATLAB function in a separate file and name it mat_function, this function should...

    1. Write a MATLAB function in a separate file and name it mat_function, this function should accept the matrix mat_in as its first input and the real number num_in as the second input, and return the following three outputs: I. Replace each element in mat_in that is greater than num_in by 1 and each element that is less than num_in by zero to get the new matrix mat1_out. II. Replace the diagonal of mat1_out by the diagonal of mat_in while...

  • answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace...

    answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace all elements divisible by 3 with 100. b) Replace all elements divisible by 2 with 50. c) Replace the first element of each row with 10. d) Display the all elements of the 2D matrix as a 10 array (9 elements in 1 row) 8) Write a function file that determines the maximum element of an input matrix A using nested loops and counts...

  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

  • In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...

    In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT