How do I detect a win in Connect 4 MATLAB

1 次查看(过去 30 天)
Im unsure if i need a function or if there is an easier way to do it manually, also I cannot figure out a way to get win=0 and end the code
fprintf ('**************************************************\n')
fprintf ('**************************************************\n')
fprintf ('\n*********** WELCOME TO CONNECT FOUR ************\n')
fprintf ('\n************************************************\n')
fprintf ('**************************************************\n')
%Board and chips are loaded
load Connect
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
%Creates a vector for the number of chips in a row
row=zeros(1,7);
%Matrix of board
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]
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;
connect(6-row(x1),x1)=1
row(x1)=row(x1)+1;
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
%Check Horizontal win
%Check Verical win
% 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;
row(x2)=row(x2)+1;
connect(6-row(x1),x1)=2
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
% Check horizontal win
% Check vertical win
% Checking diagonal win
end
  3 个评论
Jordan Rosales
Jordan Rosales 2018-11-26
I'm having difficulty with figuring out where to put the condition that would make the win=0 and end the game. I have been placing the conditionthat changes win to 0, under the player 1 input in the while win=1 loop, and it seems to have no effect in ending the loop.
Walter Roberson
Walter Roberson 2018-11-26
if horizontal_win(Board, player_number) || vertical_win(Board, player_number) || diagonal_win(Board, player_number)
win = 0;
break
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Strategy & Logic 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by