Ich habe Probleme mit meiner for-Schleife in meinem Code. Mein Code ist das Kartenspiel des Krieges und wird mit 1 Punkt pro gewonnener Runde und 2 Punkten pro gewonnenem Krieg gewertet. Die Probleme, die ich habe, wenn Spieler 1 eine Runde gewinnt, fügt beiden Spielern einen Punkt hinzu und wenn Sie die 20 Punkte bekommen, sagt das Programm nicht, wer gewinnt, es startet einfach neu.Ich habe Probleme mit meiner for-Schleife 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