好了,这又是一周中的那个时候,我正式用MATLAB举起双手并寻求帮助。我本周的目标是尝试创建一个函数,该函数接受两个输入,即说“Rock,Paper,剪刀”的字符串(或其他选择),然后输出三个字符串“Player 1 Wins!”之一。 ,“玩家2获胜!”或“继续播放!”。为了赢球,玩家必须三分之二击败其他两人(平局视为双方的损失)

function[winner] = RockPaperScissors(player1, player2)
[move1, others] = strtok(player1, ',');
[move2, rest] = strtok(others, ',');
[move3, ~] = strtok(rest, ',');

[go1, others] = strtok(player2, ',');
[go2, rest] = strtok(others, ',');
[go3, ~] = strtok(rest, ',');

Counter1 = 0;
Counter2 = 0;

for i = 1:3
if strcmp(move1, 'Rock') && strcmp(go1, 'Paper')
    Counter2 = Counter2 + 1;
elseif strcmp(move1, 'Rock') && strcmp(go1, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move1, 'Rock') && strcmp(go1, 'Rock')
    Counter1 = 0;
elseif strcmp(move1, 'Paper') && strcmp(go1, 'Paper')
    Counter1 = 0;
elseif strcmp(move1, 'Paper') && strcmp(go1, 'Rock')
    Counter1 = Counter1 + 1;
elseif strcmp(move1, 'Paper') && strcmp(go1, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move1, 'Scissors') && strcmp(go1, 'Scissors')
    Counter1 = 0;
elseif strcmp(move1, 'Scissors') && strcmp(go1, 'Paper')
    Counter1 = Counter1 + 1;
elseif strcmp(move1, 'Scissors') && strcmp(go1, 'Rock')
    Counter2 = Counter2 + 1;
end
if strcmp(move2, 'Rock') && strcmp(go2, 'Paper')
    Counter2 = Counter2 + 1;
elseif strcmp(move2, 'Rock') && strcmp(go2, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move2, 'Rock') && strcmp(go2, 'Rock')
    Counter1 = 0;
elseif strcmp(move2, 'Paper') && strcmp(go2, 'Paper')
    Counter1 = 0;
elseif strcmp(move2, 'Paper') && strcmp(go2, 'Rock')
    Counter1 = Counter1 + 1;
elseif strcmp(move2, 'Paper') && strcmp(go2, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move2, 'Scissors') && strcmp(go2, 'Scissors')
    Counter1 = 0;
elseif strcmp(move2, 'Scissors') && strcmp(go2, 'Paper')
    Counter1 = Counter1 + 1;
elseif strcmp(move2, 'Scissors') && strcmp(go2, 'Rock')
    Counter2 = Counter2 + 1;
end
if strcmp(move3, 'Rock') && strcmp(go3, 'Paper')
    Counter2 = Counter2 + 1;
elseif strcmp(move3, 'Rock') && strcmp(go3, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move3, 'Rock') && strcmp(go3, 'Rock')
    Counter1 = 0;
elseif strcmp(move3, 'Paper') && strcmp(go3, 'Paper')
    Counter1 = 0;
elseif strcmp(move3, 'Paper') && strcmp(go3, 'Rock')
    Counter1 = Counter1 + 1;
elseif strcmp(move3, 'Paper') && strcmp(go3, 'Scissors')
    Counter2 = Counter2 + 1;
elseif strcmp(move3, 'Scissors') && strcmp(go3, 'Scissors')
    Counter1 = 0;
elseif strcmp(move3, 'Scissors') && strcmp(go3, 'Paper')
    Counter1 = Counter1 + 1;
elseif strcmp(move3, 'Scissors') && strcmp(go3, 'Rock')
    Counter2 = Counter2 + 1;
end

if max(Counter1, Counter2) == Counter2
    winner = 'Player 2 Wins!';
elseif max(Counter1, Counter2) == Counter1
    winner = 'Player 1 Wins!';
elseif max(Counter1, Counter2) ~= (Counter1 || Counter2) % I tried making a Counter 3, did not work out
    winner = 'Keep Playing!';
end

end

如您所见,我有很多这个坏男孩在跑。现在的问题是,当我运行测试用例'[winner1] = rockPaperScissors('Rock,Scissors,Scissors','Paper,Rock,Scissors')
它以垂直的“ans”形式输出我的答案,而不是获胜者。此外,它还为我提供了“Player 2 Win!”。即使应该平局。我尝试全部调试,但无法弄清楚问题出在哪里。呸!

最佳答案

您是否真的考虑过使用 table ?非常适合您的目的!

只是一个想法,如何做。请不要为我所困扰,我没有考虑您的所有限制和条件,但是您应该可以轻松地按自己的喜好调整我的功能。

function RockPaperScissorsLizardSpock(player1, player2, rounds)

%// creating the table with all combinations
header = {'Rock';'Paper';'Scissors';'Lizard';'Spock'};
Rock = [0;-1;1;1;-1];
Paper = [1;0;-1;-1;1];
Scissors = [-1;1;0;1;-1];
Lizard = [-1;1;-1;0;1];
Spock = [1;-1;1;-1;0];
T = table(Rock,Paper,Scissors,Lizard,Spock,'RowNames',header);


%// play and display winners of every round
points = 0;
for ii = 1:rounds

   pointsRound  =  T{player1(ii), player2(ii)};   %// no need for if-conditions
                                                  %// one line is enough
   points = points + pointsRound;

   if     pointsRound > 0;  disp(['Player 1 wins round ' num2str(ii) '!'])
   elseif pointsRound < 0;  disp(['Player 2 wins round ' num2str(ii) '!'])
   else                     disp(['Draw in round ' num2str(ii) '!'])
   end

end

%// display overall winner
if     points >  rounds/2; disp('Player 1 Wins!')
elseif points == 0;        disp(['Draw!' num2str(ii) '!'])
else                       disp('Player 2 Wins!')

end

现在开始播放:
player1 = {'Rock','Scissors','Scissors'}
player2 = {'Paper','Rock','Scissors'}

RockPaperScissorsLizardSpock(player1, player2, 3)

返回:
Player 1 wins round 1!
Player 1 wins round 2!
Tie in round 3!
Player 1 Wins!

您还可以实现一些高级功能,例如为播放器命名:
function RockPaperScissorsLizardSpock(player1, player2, rounds)

plname = inputname(1);
p2name = inputname(2);

...

   if     pointsRound > 0;  disp([plname ' wins round ' num2str(ii) '!'])

...

else                disp([p2name ' Wins!'])

end


Sheldon = {'Spock','Spock','Spock'}
Penny = {'Paper','Rock','Scissors'}

RockPaperScissorsLizardSpock(sheldon, penny, 3)

产量
Sheldon wins round 1!
Penny wins round 2!
Penny wins round 3!
Penny Wins!

如果您坚持使用逗号输入字符串:
Sheldon = 'Spock,Spock,Spock'
Penny = 'Paper,Rock,Scissors'

您需要将 strsplit 添加到函数中:
player1= strsplit(player1,',')
player2= strsplit(player2,',')

10-08 02:56