How do I detect a win in Connect 4 MATLAB

조회 수: 4 (최근 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 CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by