How do I detect a win in Connect 4 MATLAB

1 回表示 (過去 30 日間)
Jordan Rosales
Jordan Rosales 2018 年 11 月 26 日
コメント済み: Walter Roberson 2018 年 11 月 26 日
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 Center および File ExchangeStrategy & Logic についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by