Unable to write loops for self playing connect4 win conditions

조회 수: 1 (최근 30일)
Preston Waddell
Preston Waddell 2019년 9월 28일
답변: Swatantra Mahato 2020년 8월 27일
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
David Hill
David Hill 2019년 9월 28일
You need to explain your question in more detail.
Preston Waddell
Preston Waddell 2019년 9월 28일
i am trying to create a loop to check for win conditions for either 1's or 2's. but when i attempt to create the code it either doesnt finish the code, or it become stuck in the loop i created.

댓글을 달려면 로그인하십시오.

답변 (1개)

Swatantra Mahato
Swatantra Mahato 2020년 8월 27일
Hi,
Assuming you want your code to end execution once the win condition is reached, you can use the return function as follows
if board(row + 3,col) == 1
fprintf('pcOne Wins');
return;
end
Additionally, for code getting stuck in the loop, you may want to verify that the variables used for checking the exit condition of the loop are being updated correctly.
Hope this helps

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by