clc,clf
col = 1;
row = 1;
board = zeros(6,7); %Creates a board of zeros
maxturns = 0;
vertwin = 0;
horizwin = 0;
leftdiagwin = 0;
rightdiagwin = 0;
while maxturns < 42
maxturns = maxturns + 1; %increments maxturns bcol 1 each time pcOne puts a 1 on the board
pcOne = randi(7); % pcOne generates a number 1-7
col = pcOne; %pcOne sends the number to col to place 1 in the correct column
while (board(row,col)~=0)%loops the input of row and col to ensure it isnt replacing a number that is already in place
pcOne = randi(7);
col = randi(7);
end
row = 6; %starts the row at the bottom of the array
played = 0;
while (played == 0)
if board(row,col) == 0
board(row,col) = 1; % if board space = 0 then it is replaced with a 1
played = 1; %when the loop is true played will equal 1 and exit the loop
end
row = row - 1;
if (row == 0)
row = row + 1;
end
end
if vertwin == 0 %check vert win for pcOne
if board(row,col) == 1
if board(row + 1,col) == 1
if board(row + 2,col) == 1
if board(row + 3,col) == 1
fprintf('pcOne Wins');
end
end
end
end
end
%add in checking loops
maxturns = maxturns + 1; %increments maxturns by 1 each time pcOne puts a 1 on the board
pcTwo = randi(7); % pcOne generates a number 1-7
col = pcTwo; %pcOne sends the number to col to place 1 in the correct column
while (board(row,col)~=0)%loops the input of row and col to ensure it isnt replacing a number that is alreadcol in place
pcTwo = randi(7);
col = randi(7);
end
row = 6; %starts the row at the bottom of the array
played = 0;
while (played == 0)
if board(row,col) == 0
board(row,col) = 2; % if board space = 0 then it is replaced with a 1
played = 1; %when the loop is true played will equal 1 and exit the loop
end
row = row - 1;
if (row == 0)
row = row + 1;
end
end
%add in checking loops
disp(board); %should display a board full of 1' and 2's
end% loop ends after 42 moves are made
fprintf('Draw, Max Number of Moves Made!');
댓글 수: 2
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/482631-unable-to-write-loops-for-self-playing-connect4-win-conditions#comment_750896
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/482631-unable-to-write-loops-for-self-playing-connect4-win-conditions#comment_750896
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/482631-unable-to-write-loops-for-self-playing-connect4-win-conditions#comment_750898
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/482631-unable-to-write-loops-for-self-playing-connect4-win-conditions#comment_750898
댓글을 달려면 로그인하십시오.