Code for Pig Dice game not working.

1 view (last 30 days)
John Goodwing
John Goodwing on 7 Feb 2016
Commented: Geoff Hayes on 7 Feb 2016
I've been working on this code for hours and cannot solve why it won't change players. I'm not sure if it's my loop that is incorrect or what. Rules for Pig: 1. Pig is a dice game played with 2 dice and any number of players. 2. The first player to reach a score of 100 wins. 3. Play begins with Player 1 making the first roll. At the end of each roll, the player can choose to hold (end his/her turn) or roll again. A player can roll as many times as he/she chooses and keeps a running total of the sum of the two dice. However, if the player rolls a 1, he/she loses all of the points for that particular turn.
The dice file is attached.
My code:
clc
clear
home
load Dice
%initial variables
players=input('How many players would you like?: ');
player=1;
roll=[2 2];
againagain=1;
%while player wants to play again
while againagain==1
%game is 2-7 players; input acceptable value
if players > 7
fprintf('Pick a value between 2-7\n')
players=input('How many players would you like?: ');
else
end
%empty vector for game types of players
valuetotal=[];
pname={};
% compnames=['Tom','Mike','Susan','Joe','Billy','Evan','Sarah'];(not
% inputted)
%aks and creates vector for modes of player(1) and computer(2)
for p=1:players;
p2=num2str(p);
test=strcat('What mode is player ', p2,' in?');
value=menu(test,' Player','Computer');
if value == 1
pname(p)=cellstr(input('What is your name?','s'));
else
pname(p)=cellstr('computer');
end
valuetotal=[valuetotal,value];
end
%empty score vector and initial variables for loop
scores = zeros(1,players);
k=1;
again=1;
compchoice=1;
while all(scores<100)
for k=1:players;
if valuetotal(k) == 1
while again ==1
figure1=figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure1=figure('WindowStyle','docked');
close;
imshow([Dice{roll}],'InitialMagnification','fit');
%rules
if roll(1)==1 || roll(2)==1
fprintf('You rolled a 1, you lose points. Next players turn.')
scores(k)=scores(k)-sum(roll)
again=2
elseif roll(1)==roll(2)
fprintf('You rolled doubles! You got 2x the points!')
scores(k)=scores(k)+2*sum(roll)
again=menu('Would you like to roll again?','Yes','No')
else
scores(k)=scores(k)+sum(roll)
again=menu('Would you like to roll again?','Yes','No');
end
end
%wouldn't break out to go to k=2
else valuetotal(k) ==2
while compchoice == 1
figure1=figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure1=figure('WindowStyle','docked');
close;
imshow([Dice{roll}],'InitialMagnification','fit')
%rules
if roll(1)==1 || roll(2)==1
fprintf('Computer %i rolled a 1, they lose points. Next players turn.',k)
scores(k)=scores(k)-sum(roll)
compchoice==2
elseif roll(1)==roll(2)
fprintf('Computer %i rolled doubles! They got 2x the points!',k)
scores(k)=scores(k)+2*sum(roll)
compchoice=randi(1,2)
else
scores(k)=scores(k)+sum(roll)
%random decision for comp to roll again
compchoice=randsample(2,1)
end
end
end
%wouldn't break out to go to next k
end
end
%finds winner and states they have won
winnerIdx = find(scores>=100,1)
fprintf('Player', winnerIdx,'has won!')
againagain=menu('Would you like to play again?','Yes','No')
end
fprintf('Thanks for playing!')
  2 Comments
Walter Roberson
Walter Roberson on 7 Feb 2016
What is the purpose of your code
figure1=figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure1=figure('WindowStyle','docked');
close;
You create a figure, you do something non-graphical, you create a second figure, you delete the current figure (default meaning of close), leaving that first figure alone. Why do you create a figure just to close it immediately? Why are you creating either of those figures?
Geoff Hayes
Geoff Hayes on 7 Feb 2016
John - why not further the discussion at http://www.mathworks.com/matlabcentral/answers/266457-code-for-pig-dice-game-incorrect? I see that you have corrected some of your mistakes but your opening paragraph is near identical to the previous one.

Sign in to comment.

Answers (0)

Categories

Find more on Video 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!