Multiplayer Dice Game:

4 views (last 30 days)
Luis Gomez
Luis Gomez on 24 Sep 2019
Commented: Rik on 24 Sep 2019
Okay so this is currently what I have so far for my project and the problem I am currently having is creating an input or variable that will allow my game/code to be dependant on the number of players that are selected at the beginning. Im confused as to how my fprintf should look like since I need to create an array/matrix for the number of players selected.
%% "FIFTY the Party Dice Game"
clear
clc
Welcome = input('Welcome to the Fifty Dice Game!');
Player = input('Enter the number of players: ');
NumberOfPlayers=round(Player,0);
if(NumberOfPlayers<=0)
NumberOfPlayers=0;
end
% Step 1:Generate initial dice roll for x amount of players
% Step 2:Generate gamescore for x amount of players
% Make an array/matrix on how many players for fprintf
GameScore = linspace(Player, ,)
% Step 3:Create if statements that added the score depending if they matched.
% Step 4:Create Reroll
% Step 5:Create statement for while statement.
% Step 6:Create if statement for Reroll.
% Step 7:Insert While Loop.
% Value that will keep the game score
GameScore = 0;
Player_1 = 1;
Player_2 = 2;
Player_3 = 3;
Player_4 = 4;
% Generates Number of rolls according to number of players
dice1 =randi(6,1,Player);
dice2 =randi(6,1,Player);
%Create a game score equal to zero, depending on x amount of players
multigamescore = linspace(0,50,GameScore);
% Variable that will count the round number
RoundNumber = 0;
% While If statement/code block to set game score(Conditions for the game).
while(GameScore ~= 50)
dice1 = randi(6,1) ; %ReRoll of dice
dice2 = randi(6,1);
if(GameScore ~= 50)
% Increment the round number by 1.
RoundNumber = RoundNumber +1;
fprintf('\nRound %.f: \n',RoundNumber)
fprintf('** Score Board ** \n')
fprintf('Player %.f Score: \n', Player_1)
fprintf('Player %.f Score: \n', Player_2)
fprintf('Player %.f Score: \n', Player_3)
fprintf('Player %.f Score: \n', Player_4)
fprintf('Current Score: %.f \n',GameScore)
fprintf('You rolled %.f and %.f!\n\n',dice1, dice2)
end
%% Section for How Points are given.
if (dice1~=dice2)
fprintf('No points.\n')
elseif (dice1 == 3) && (dice2 ==3)
fprintf('Double 3! Game score set back to 0.\n')
GameScore = 0;
elseif (dice1== 6)&&(dice2 ==6)
fprintf('Double 6! Add 25 points to score.\n')
GameScore = GameScore + 25;
elseif (dice1 == 1) && (dice2 == 1)
fprintf('Double 1! Add 5 points to score.\n')
GameScore = GameScore + 5;
elseif (dice1 == 2) && (dice2 == 2)
fprintf('Double 2! Add 5 points to score.\n')
GameScore = GameScore + 5;
elseif (dice1 == 4) && (dice2 == 4)
fprintf('Double 4! Add 5 points to score.\n')
GameScore = GameScore + 5;
elseif (dice1 == 5) && (dice2 == 5)
fprintf('Double 5! Add 5 points to score.\n')
GameScore = GameScore + 5;
else
fprintf('Double dice! Add 5 points to score.\n')
GameScore = GameScore + 5;
end
if (GameScore == 50)
fprintf('\nRound %.f: \n',RoundNumber)
fprintf('Current Score: %.f \n',GameScore)
fprintf('You rolled %.f and %.f! \n\n',dice1, dice2)
fprintf('Congratulations Player %.f!!! You WON! Your final score is 50!', Player_1)
end
end
  4 Comments
darova
darova on 24 Sep 2019
I think he can help you. But it's only my opinion
Rik
Rik on 24 Sep 2019
You didn't really ask a specific question, so I'll just provide some random comments:
multigamescore = linspace(0,50,GameScore);
%this doesn't do what the comment promisses, it creates 0 length array (because GameScore is 0)
multigamescore=zeros(1,Player);
To get the number of players I would use something like this:
Player=0;%initialize to invalid number to enter loop
while Player<=0 || Player~=round(Player)
Player = input('Enter the number of players: ');
end
And for the display:
for n=1:Player
fprintf('Player %d Score: %d\n',n,score(n))
end

Sign in to comment.

Answers (0)

Categories

Find more on Number games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!