Question

Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules...

Develop a game using Matlab. Work in groups of two.

A Game of Sticks

The rules are as follows:

Players: 2

Sticks: 20

Players take turns sequentially to pick 1~3 sticks.

Loser picks up the last stick!

This project has the following deliverables:

(1) Your project implements a 2-player version of the game of sticks in the command

window.

Required display in command window:

‘Welcome to the game!’ ‘Please enter player 1 name:’ ‘Please enter player 1 name:’

‘Player 1

Required inputs in command window:

beginning: players’ names; in each step: number of sticks to remove

Greetings to the winner.

(2) You will have a bar-graph to show (and track) how many sticks there are after each

step.

(3) Your will submit a report. It will include the coding, command window records of

one trial of you and your teammate playing the game including the printouts and

inputs, bar-graph of game status records.

PLEASE do in Matlab. Thank you so much :)

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

% Matlab script to simulate the game of sticks between 2 players and plot
% the bar graph to track the sticks after each step

sticks_left = 20; % initialize the number of sticks at the stack of the game
step = 1; % initialize the step number
% display the header
fprintf('Welcome to the game!\n');
% input of player names
player1 = input('Please enter player 1 name: ','s');
player2 = input('Please enter player 2 name: ','s');
loser = 0; % variable to track the loser
% loop that continues till number of sticks > 0
while sticks_left(step) > 0
% display the step number and number of sticks left
fprintf('Step %d : \n',step);
% input of sticks picked by player 1
fprintf('Sticks left : %d\n',sticks_left(step));
fprintf('%s , enter the sticks to pick : ', player1);
player1_sticks = input('');
% validate player1 input and re-prompt until valid
while(player1_sticks < 1 || player1_sticks > 3 || player1_sticks > sticks_left(step))
fprintf('Invalid number of sticks picked .\n%s , enter the sticks to pick : ', player1);
player1_sticks = input('');
end
  
% update the sticks left
sticks_left(step) = sticks_left(step) - player1_sticks;
% if player1 has picked the last stick, set loser to player 1 and exit
% the loop
if(sticks_left(step) == 0)
loser = 1;
break;
end
  
fprintf('Sticks left : %d\n',sticks_left(step));
% input of sticks picked by player 2
fprintf('%s , enter the sticks to pick : ', player2);
player2_sticks = input('');
% validate player2 input and re-prompt until valid
while(player2_sticks < 1 || player2_sticks > 3 || player2_sticks > sticks_left(step))
fprintf('Invalid number of sticks picked .\n%s , enter the sticks to pick : ', player2);
player2_sticks = input('');
end
% update the sticks left
sticks_left(step) = sticks_left(step) - player2_sticks;
% if player2 has picked the last stick, set loser to player 1 and exit
% the loop
if(sticks_left(step) == 0)
loser = 2;
break;
end
% copy the sticks left for the next step
sticks_left(step+1) = sticks_left(step);
step = step + 1; % increment number of steps
  
end
% display the winner
if loser == 1
fprintf('Congratulations %s !! You won the game\n',player2);
else
fprintf('Congratulations %s !! You won the game\n',player1);
end
% display the bar graph
bar(sticks_left);
xlabel('Step');
ylabel('Sticks left');
title('Sticks left after each step');
%end of script

Output:

Add a comment
Know the answer?
Add Answer to:
Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules...
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
  • 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,...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remo...

    Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap/pile. The goal of the game is to avoid taking the last object.” (Wikipedia) In your implementation of the game, there will be 3 piles of objects. At the start...

  • I am creating a MATLAB game for my school project. The goal of the game is...

    I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x...

    MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x's, y's, and v's to popup in the command window so I can put in any value and it will find the value for me using the equation I put in. function project_9_sjl() % PROJECT_9_SJL project_9_sjl() is the driver function for the program. % %    Name: Scott Lawrence %   Date: 3/27/2019 %   Class: CMPSC 200 %   Description: Determine the optimal...

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