我的代码中的for循环遇到了麻烦。我的代码是战争的纸牌游戏,得分为:每轮赢1分,每场战2分。我遇到的问题是,当玩家1赢得一轮比赛时,它会给两个玩家增加一个点数,当你获得20分赢得该计划时,不会告诉谁赢了,它只会重新启动。
player_1_name = input('Enter player 1`s name ','s');
player_2_name = input('Enter player 2`s name ','s');
clc
fprintf('Hello %s and %s!\n Welcome to my version of the card game of War.\n Here are some rules to get you started:\n 1)The first person to 20 points wins. \n 2)You will win 1 point for winning a round and 2 points for winning a war. \n 3)The jack is represented as an 11, \n the queen is represented as a 12, \n the king is represented as a 13, \n and the ace is represented as a 14. \n 4)Remember to have fun!\n',player_1_name,player_2_name)
disp('Press enter when you are ready to begin! ')
pause
clc
A = repmat(2:14, [1, 4]);
shuffled_deck = A(randperm(length(A)));
disp('Ok, lets get started!')
fprintf('Dealing out %s`s and %s`s hands. \n',player_1_name,player_2_name)
pause(5)
disp('The hands are dealt.')
disp('Now we are ready to start!')
disp('Press enter when you are ready to begin. ')
player1_hand = shuffled_deck(1:26);
player2_hand = shuffled_deck(27:52);
pause
clc
T = 10;
J = 11;
Q = 12;
K = 13;
A = 14;
n1= 0;
n2 = 0;
for n1 = 0:20
for n2 = 0:20
fprintf('\n %s has %2.0f points. \n ',player_1_name,n1)
fprintf('%s has %2.0f points. ',player_2_name,n2)
pause
clc
disp('Lets draw cards! ')
disp('3....2....1...DRAW!' )
pause
player_1_card = randsample(player1_hand,1);
fprintf('%s drew a %2.0f. ',player_1_name,player_1_card)
pause
player_2_card = randsample(player2_hand,1);
fprintf('%s drew a %2.0f. \n',player_2_name,player_2_card)
pause
if player_1_card > player_2_card;
n1 = n1+1;
fprintf('%s wins this round and wins one point! \n',player_1_name)
elseif player_1_card < player_2_card;
n2 = n2+1;
fprintf('%s wins this round and wins one point! \n',player_2_name)
else player_1_card = player_2_card;
display('War!')
x2 = randsample(player1_hand,1);
x3 = randsample(player1_hand,1);
x4 = randsample(player1_hand,1);
x5 = randsample(player1_hand,1);
x6 = randsample(player2_hand,1);
x7 = randsample(player2_hand,1);
x8 = randsample(player2_hand,1);
x9 = randsample(player2_hand,1);
fprintf('%s drew a%2.0f, a%2.0f, a%2.0f, and a%2.0f. \n',player_1_name,x2,x3,x4,x5)
fprintf('%s drew a%2.0f, a%2.0f, a%2.0f, and a%2.0f. \n',player_2_name,x6,x7,x8,x9)
if x5 > x9;
n1 = n1 + 2;
fprintf('%s wins this war and wins two points! \n',player_1_name)
else x5 < x9;
n2 = n2 + 2;
fprintf('%s wins this war and wins two points! \n' ,player_2_name)
end
end
a = input(' \nPress 1 to play another hand. \n Press 2 to restart the game. \n Press 3 to end the game. ');
if a == 1;
continue
elseif a == 2;
clc
run('C:\Users\Derek\Documents\MATLAB\Week 12\warimproved.m')
else a == 3;
return
end
end
end
if n1 >= 20
fprintf('%s wins ', player_1_name)
return
elseif n2 >= 20
fprintf('%s wins ', player_2_name)
return
else
end
答案 0 :(得分:0)
这是因为你在双for循环中运行它 - for循环自动增加它们迭代的变量。如果您将其删除,而是使用单个while循环,即删除for n1 = 0:20
和for n2 = 0:20
并插入while (n1 < 20 && n2 < 20)
,则您的程序将按预期工作。您需要删除一个end
。