matlab - MATLAB 中的石头剪刀布

标签 matlab if-statement for-loop conditional conditional-statements

好吧,又到了每周的那个时候,我正式举起手来使用 MATLAB 并寻求帮助。我这周的目标是尝试创建一个接受两个输入的函数,这两个输入是表示“Rock、Paper、Scissors”(或任何选择)的字符串,然后输出三个字符串之一“Player 1 Wins!” , '玩家 2 赢了!'或“继续玩!”。要获胜,玩家必须在 3 次中击败其他两个(平局对双方玩家来说都是输)

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”而不是获胜者的形式输出我的答案。此外,它还给我“玩家 2 赢了!”即使它应该是平局。我试过全部调试,但无法弄清楚我的问题出在哪里。呸!

最佳答案

您是否真的考虑过使用 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,',')

关于matlab - MATLAB 中的石头剪刀布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26061481/

相关文章:

arrays - MATLAB 结构数组赋值

c++ - 使用 MATLAB 编码器将 MATLAB 转换为 C++

c# - 无法使嵌套的 IF 在 C# 中正常工作。我究竟做错了什么?

c++ - 虽然不断重复字母但不重复#'s

php echo 在 elseif 之后不起作用

c++ - For循环不太有效

python - 在保持文件夹结构的同时读取图像

matlab - Numpy 相当于 MATLAB 的 hist

r - 确定集群相似度/集合重叠

ios 如何在for循环中提取数组的所有rangeOfString?