Creating a player vs AI dice game
4 ビュー (過去 30 日間)
古いコメントを表示
So my objective is to make a script that can play the game farkle with multiple human players and computer players however im having an error showing up in my script when it becomes the computer players turn and im not sure how ill be able to fix it. This is what I have so far:
Any help would be appreciated
clc;
clear;
rng('shuffle');
HP=input('Enter the number of HUMAN players:');
HumanPlayers=round(HP,0);
if (HumanPlayers<=0)
HumanPlayers=0;
end
CP=input('Enter the number of AI players:');
ComputerPlayers=round(CP,0);
if (ComputerPlayers<=0)
ComputerPlayers=0;
end
TotalPlayers = (ComputerPlayers + HumanPlayers);
TPVec = [TotalPlayers:-1:1];
TotalGameScore = zeros(size(TPVec)); %Makes the Zero Vector
tsVec = [TotalPlayers:-1:1];
TurnScore = zeros(size(tsVec));
PVec = [TotalPlayers:-1:1];
PT = zeros(size(PVec)) .* 1; % Player Turn Amount
PT = PT + 1;
%fprintf('\nTurn Number: %d \n', turn);
LC1 = 1;
%%
while (TotalPlayers>0 && max(TotalGameScore)<10000)
if (LC1 > TotalPlayers)
LC1 = 1;
end
%PT(LC1) = PT(LC1) + 1;
%fprintf('\nTurn Number: %d \n', PT(LC1));
fprintf('\n\n***** Player number: %d *****\n',LC1);
TS1 = 1;
while(TS1 ~= 0)
if (LC1 > TotalPlayers)
LC1 = 1;
end
if( LC1 <= HumanPlayers )
choice = input('\nRoll or Pass? Please make your selection:\n ','s');
if (choice == 'r' || choice == 'R')
choice = 'r';
elseif (choice == 'p' || choice == 'P')
choice = 'p';
else
fprintf('That doesnt work for me \n');
score = 100;
end
else
choice =(TotalGameScore(LC1):TurnScore(LC1):TotalGameScore);
fprintf('\nComputer player %d decides to select %s.\n',LC1,choice);
end
if choice == 'r'
Roll = randi(6,1,6);
fprintf('---Player %d rolled!---\n',LC1);
fprintf('*******Dice Values*******:\n')
fprintf('%6.0f\t',Roll)
fprintf('\n')
SortRoll=sort(Roll);
fprintf('*******Sorted Dice Values*******:\n')
fprintf('%6.0f\t',SortRoll)
fprintf('\n')
DiceKeep=input('Enter the dice number that you wish to keep:');
Rollnew=randi(6,1,(6-DiceKeep));
SortRollnew=sort(Rollnew,'descend');
fprintf('*******New Sorted Dice Values*******\n')
fprintf('%6.0f\t',SortRollnew)
fprintf('\n')
NewNumberOfDice=6-DiceKeep; %This subtracts the amount of dice chosen from the amount of dice available
disp('*******Number of dice available*******')
fprintf('%4.0f\n',NewNumberOfDice)
if NewNumberOfDice==0
disp('All 6 dice have been set aside, the player can roll 6 dice again!')
NumberOfDice=6;
end
TurnScore=input('Enter the score of this roll:');
if TurnScore == 0
PT(LC1) = PT(LC1) + 1;
TurnScore(LC1) = 0;
fprintf('\nCurrent turn is over with no score. Moving to next player.');
fprintf('\nPlayer %d game score is %d!',LC1,TotalGameScore(LC1));
LC1 = LC1 + 1;
TS1 = 0;
else
TurnScore(LC1) = TurnScore(LC1);
fprintf('\nPlayer %d accumulated %d points for this turn so far...',LC1,TurnScore(LC1));
end
else
PT(LC1) = PT(LC1) + 1;
TotalGameScore(LC1) = TotalGameScore(LC1) + TurnScore(LC1);
fprintf('Player %d accumulated %d points for this turn so far...',LC1,TurnScore(LC1));
TurnScore(LC1)=0;
fprintf('\nPlayer %d game score is %d!',LC1,TotalGameScore(LC1));
fprintf('\nTurn Number: %d \n', PT(LC1));
LC1 = LC1 + 1;
TS1 = 0;
end
% fprintf('\nYour total game score is %d!',TotalGameScore(LC1));
end
end
3 件のコメント
Walter Roberson
2019 年 3 月 24 日
On line 72, you overwrite all of TurnScore with an input() statement, but in several other places you expect TurnScore to be a vector that records the score for the turn for one particular player.
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!