Code for Pig Dice game incorrect.
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I've been working on this code for hours and cannot solve why it won't give the player a turn, save scores correctly, and just keeps playing for the computer. 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:
clear;
home;
load Dice;
players=input('How many players would you like?: ');
if players > 7;
fprintf('Pick a value between 2-7\n');
players=input('How many players would you like?: ');
else;
end;
valuetotal=[];
pname={};
compnames=['Tom','Mike','Susan','Joe','Billy','Evan','Sarah'];
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;
scores = zeros(1,players);
while scores(p) < 100;
if scores(p)>=100;
fprintf('Player',p,'wins!');
clc;
clear;
home;
end;
switch p;
case 1;
if value == 1;
again=1;
while again ==1;
figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure('WindowStyle','docked');
close;
imshow([Dice{roll}],'InitialMagnification','fit');
scores(p)=scores(p)+sum(roll);
if roll(1)==1 || roll(2)==1;
scores(p)=scores(p)-sum(roll);
again=2;
end;
again=menu('Would you like to roll again?','Yes','No');
end;
end;
case 2;
compchoice=1;
if value == 2;
while compchoice == 1;
figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure('WindowStyle','docked');
close;
imshow([Dice{roll}],'InitialMagnification','fit');
scores(p)=scores(p)+sum(roll);
if roll(1)==1 || roll(2)==1;
scores(p)=scores(p)-sum(roll);
compchoice=2;
end;
compchoice=randi(1,2);
end;
end;
end;
end
5 comentarios
Geoff Hayes
el 9 de Feb. de 2016
John - when running the above code, the k does change so I don't understand why you think that it doesn't. Try using the debugger to step through the code to verify that k does change.
You also don't need to duplicate the code based on whether the computer is playing or whether a human in playing. You can consolidate much of the above into something like
%empty score vector and initial variables for loop
scores = zeros(1,players);
while all(scores<100)
for k=1:players;
rollAgain = true;
while rollAgain
figure('WindowStyle','docked');
roll = randi([1 6],[1 2]);
figure('WindowStyle','docked');
close;
image([Dice{roll}]);
%rules
if roll(1)==1 || roll(2)==1
fprintf('%s rolled a 1 and so loses points. Next player''s turn.\n',pname{k})
scores(k)=scores(k)-sum(roll)
rollAgain = false;
else
if roll(1)==roll(2)
fprintf('%s rolled doubles! 2x the points!\n',pname{k})
scores(k)=scores(k)+2*sum(roll)
else
fprintf('%s rolled %d %d!\n',pname{k},roll(1),roll(2));
scores(k)=scores(k)+sum(roll)
end
if playerTypes(k)==1
rollAgain = (menu('Would you like to roll again?','Yes','No') == 1);
else
rollAgain = randi(2,1) - 1;
end
end
end
end
end
You only need to be aware of how the messaging is handled (based on whether the computer is playing or not). Note that I changed the name of the variable valuetotal to playerTypes as the latter is more descriptive of what value it represents.
Try incorporating the above into your code.
Respuestas (1)
Geoff Hayes
el 3 de Feb. de 2016
Editada: Geoff Hayes
el 3 de Feb. de 2016
John - look closely at the condition for your while loop
while scores(p) < 100;
What is p? It is the indexing variable from the for loop and it will be set to the number of players of your game. So what are you trying to condition on here? You want the game to continue until at least one player has reached 100 points. Try
while all(scores<100)
As
scores<100
will return an array of ones (score less than one hundred) and zeros (scores greater than or equal to 100) we use all as we want to continue playing so long as all players have a score that is less than 100. You will then want to move your
if scores(p)>=100;
fprintf('Player',p,'wins!');
clc;
clear;
home;
end;
outside of the while loop (as it will never evaluate so long as it is inside). You will need to figure out what p is
winnerIdx = find(scores>=100,1);
to return the first winner whose score is greater than or equal to 100 (here, winnerIdx will be index of the player who satisfies the condition. (You may need to be more diligent and check to see if more than one player meets the condition.)
In your while loop, you will probably need to set up a for loop to iterate over each player, giving each a turn to roll. I don't understand why you have a switch statement though as the code is almost identical with the exception that there is no call to ask the user if he or she wishes to roll again. (The code should be identical for every player - no need to separate it like this.)
Try making the above changes and see what happens!
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!