How do I get my connect four game in MATLAB to end when a win is detected?
1 次查看(过去 30 天)
显示 更早的评论
I am almost finished with my MATLAB game, but the game will not end when a win is detected. I cannot get win to equal zero and break the loop, even though MATLAB is still able to print the winner when a win is detected.
load Connect %Chips are loaded
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);% Displays board
row=zeros(1,7);%Vector of # of chips in each row 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 connect four board is created
win=1;
while win==1 %Keeps asking for input while win=1
% P1 input
fprintf('Player 1, select a column to place your chip in.\n')
x1 = input('Select a column 1-7:');%P1 input
Board{6-row(x1),x1}=redchip; %Places a red chip on the board image
connect(6-row(x1),x1)=1; %Places a (1) in the matrix
row(x1)=row(x1)+1; % A chip is added to the row vector
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
check1=connect==1 %Displays only player 1 inputs
%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 = randi(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
回答(1 个)
Geoff Hayes
2018-11-30
Jordan - when a player wins, you probably want to set the win variable to zero. So instead of doing the equality check of
win==0 % just checks if win is identical to zero and is NOT an assignment
you would do
win = 0;
instead.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!