Unable to write loops for self playing connect4 win conditions
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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 comentarios
Respuestas (1)
  Swatantra Mahato
    
 el 27 de Ag. de 2020
        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
0 comentarios
Ver también
Categorías
				Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


