2016-05-06 2 views
0

У меня возникают проблемы с моей петлей в моем коде. Мой код - карточная игра войны и забивается, 1 очко за раунд и 2 очка за выигранную войну. Проблемы, с которыми я столкнулся, - это когда игрок 1 выигрывает раунд, он добавляет точку для обоих игроков, и когда вы получаете 20 очков, чтобы выиграть программу, не говорит, кто победит, он просто перезапускается.У меня проблемы с моей петлей matlab

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

Это потому, что вы используете его в двойной цикл - для контуров автоматического увеличения переменных они Перебор. Если вы удалите это и вместо этого используете одиночный цикл while, т. Е. Удалите for n1 = 0:20 и for n2 = 0:20 и вставьте while (n1 < 20 && n2 < 20), ваша программа работает по назначению. Вам нужно будет удалить один end.

Смежные вопросы