Chicago

clc
clear

fprintf(‘****************************************Instructions:****************************************\n’)
fprintf(‘* -Determine the number of players, then take turns rolling two dice each turn. *\n’)
fprintf(‘* -Starting with round 1, roll both dice, add them up, and try to get a total of two points. *\n’)
fprintf(‘* -If your total is 2, then you add 2 points to your total score. *\n’)
fprintf(‘* -Each round, 1-11, the target score will be 1 more than the number of the round. *\n’)
fprintf(‘*- Every time you roll the target number, you get that many points added to your total score.*\n’)
fprintf(‘* -Once everyone rolls once for a target score, move on to the next round. *\n’)
fprintf(‘* -They player at the end of the game with the most points, wins! *\n’)
fprintf(‘**********************************************************************************************\n’)

readiness = input(‘Press 1 if ready, press 2 if not.\n’);

%assign the number of players in the game

if readiness == 1
numPlayers = input(‘Please input number of players.\n’);
else
fprintf(‘Just learn as you go!\n’)
numPlayers = input(‘Please input number of players.\n’);
end

while (numPlayers <= 0) || (numPlayers >= 7)
numPlayers = input(‘Please input a valid number of players.\n’);
end

%creates a vector to keep track of whos turn it is
playerVector = (1:numPlayers);

%Round 1
fprintf(‘\n\nBegin round 1! Try to score 2 points.\n\n’)

for i = 1:numPlayers
%creates an initial score vector, only in round 1
scoreVector(i) = 0;
fprintf(‘Player %i roles the dice.\n\n’, i)
%Generate a random roll for two die between 1 and 6
diceVector = randi(6,[1,2]);
%Pauses just implemented so that the entire game doesnt run at once
pause(1)
%Adds up the two dice
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
%check the sum of the dice to check if player i gets points
if diceTotal == 2
%assigns player with points if score is correct, value changes
%every round
scoreVector(i) = scoreVector(i) + 2;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else %player gets no points if not the desired number, so points stay the same
scoreVector(i) = scoreVector(i);
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end
%Same round of code is repeated 10 more times, target su, increases by one
%each round, and so does the number that gets added to total score.
%Round 2
fprintf(‘\n\nBegin round 2! Try to score 3 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 3
scoreVector(i) = scoreVector(i) + 3;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 3
fprintf(‘\n\nBegin round 3! Try to score 4 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 4
scoreVector(i) = scoreVector(i) + 4;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 4
fprintf(‘\n\nBegin round 4! Try to score 5 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 5
scoreVector(i) = scoreVector(i) + 5;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 5
fprintf(‘\n\nBegin round 5! Try to score 6 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 6
scoreVector(i) = scoreVector(i) + 6;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 6
fprintf(‘\n\nBegin round 6! Try to score 7 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 7
scoreVector(i) = scoreVector(i) + 7;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 7
fprintf(‘\n\nBegin round 7! Try to score 8 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 8
scoreVector(i) = scoreVector(i) + 8;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 8
fprintf(‘\n\nBegin round 8! Try to score 9 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 9
scoreVector(i) = scoreVector(i) + 9;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 9
fprintf(‘\n\nBegin round 9! Try to score 10 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 10
scoreVector(i) = scoreVector(i) + 10;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Round 10
fprintf(‘\n\nBegin round 10! Try to score 11 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 11
scoreVector(i) = scoreVector(i) + 11;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end
%Round 11
fprintf(‘\n\nBegin round 11, the last round! Try to score 12 points.\n\n’)

for i = 1:numPlayers
fprintf(‘Player %i roles the dice.\n\n’, i)
diceVector = randi(6,[1,2]);
pause(1)
diceTotal = sum(diceVector);
fprintf(‘Player %i has rolled a %i and a %i, for a total of %i.\n\n’, i, diceVector(1), diceVector(2), diceTotal)
pause(1)
if diceTotal == 12
scoreVector(i) = scoreVector(i) + 12;
fprintf(‘Player %i now has %i points.\n\n’, i, scoreVector(i))
else
fprintf(‘No points earned, player %i still has %i points.\n\n’, i, scoreVector(i))
pause(1)
end
end

%Begin score reporting sequence and win/lose calculation
fprintf(‘Game has completed, thanks for playing!\n’)

for i = 1:numPlayers
%display each players score
fprintf(‘Player %i finished the game with %i points.\n\n’, i, scoreVector(i))
pause(1)
%Use for loop to determine if player i had max score
if scoreVector(i) == max(scoreVector)
fprintf(‘Player %i wins the game!\n\n’, i)
else %if not max score, player i is a loser
fprintf(‘Player %i loses… try again!\n\n’, i)
pause(1)
end
end
%game over