Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I am unable to end the game when a win is detected in connect four on matlab

1 回表示 (過去 30 日間)
Jordan Rosales
Jordan Rosales 2018 年 11 月 27 日
閉鎖済み: John D'Errico 2018 年 11 月 27 日
I have the 2 player game essentially finished, but once a win is detected, the win variable will not set equal to zero, ending the loop. However, MATLAB is still able to print out that a player has won.
% Board and chip images are loaded
load Connect
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
row=zeros(1,7); % Counter of chips in row vector is created
connect=[0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0] % Matrix of board
win=1;
while win==1
% P1 input
fprintf('Player 1, select a column to place your chip in.\n')
x1 = input('Select a column 1-7:');
Board{6-row(x1),x1}=redchip;% Red chip appears in the board image
connect(6-row(x1),x1)=1; % 1 is placed in board matrix
row(x1)=row(x1)+1; % A chip is added to the row
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
check1=connect==1 % Matrix is made of only player 1 inputs (1s)
%check vertical win
for y=1:7
for x=1:3
if check1(x,y)==1&check1(x+1,y)==1&check1(x+2,y)==1&check1(x+3,y)==1
win==0
fprintf('Player 1 wins!')
break
end
end
end
%Check horizontal win
for y=1:4
for x=1:6
if check1(x,y)==1&check1(x,y+1)==1&check1(x,y+2)==1&check1(x,y+3)==1
win==0
fprintf('Player 1 wins!')
break
end
end
end
%check diagonal win bottom left top right
for y=1:4
for x=1:3
if check1(x,y)==1&check1(x+1,y+1)==1&check1(x+2,y+2)==1&check1(x+3,y+3)==1
win==0
fprintf('Player 1 wins!')
break
end
end
end
%check diagonal win bottom right top left
for y=4:7
for x=1:3
if check1(x,y)==1&check1(x+1,y-1)==1&check1(x+2,y-2)==1&check1(x+3,y-3)==1
win==0
fprintf('Player 1 wins!')
break
end
end
end
% P2 input
fprintf('Player 2, select a column to place your chip in.\n')
x2 = input('Select a column 1-7:');
Board{6-row(x2),x2}=blackchip;
connect(6-row(x2),x2)=2;
row(x2)=row(x2)+1;
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
check2=connect==2;
% Check vertical win
for y=1:7
for x=1:3
if check2(x,y)==1&check2(x+1,y)==1&check2(x+2,y)==1&check2(x+3,y)==1
win==0
fprintf('Player 2 wins!')
break
end
end
end
% Check horizontal win
for y=1:4
for x=1:6
if check2(x,y)==1&check2(x,y+1)==1&check2(x,y+2)==1&check2(x,y+3)==1
win==0
fprintf('Player 2 wins!')
break
end
end
end
% Checking diagonal win bottom left top right
for y=1:4
for x=1:3
if check2(x,y)==1&check2(x+1,y+1)==1&check2(x+2,y+2)==1&check2(x+3,y+3)==1
win==0
fprintf('Player 2 wins!')
break
end
end
end
% Checking diagonal win bottom right top left
for y=4:7
for x=1:3
if check1(x,y)==1&check1(x+1,y-1)==1&check1(x+2,y-2)==1&check1(x+3,y-3)==1
win==0
fprintf('Player 2 wins!')
break
end
end
end
end

回答 (0 件)

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by