Jump to content
  • Advertisement
Sign in to follow this  
mattnenterprise

Minimax connect 4

This topic is 2377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been programming Connect 4 using Matlab and im using the minimax algorithm and when I play it it blocks be on on the horizontals, and vertical, but when I can when using diagonals it wont block me at all and I have no reason why. Here is my code.


function [ max ] = maxMove(BoardMax,depthMax,alphaMax,betaMax)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
global bestMove;
max = -inf;
m = checkForWinner(BoardMax);
if(m)
max = m;
elseif(depthMax >= 5)
max = analysis(BoardMax);
else

%find all legal moves
legalMoves = findAllLegalMoves(BoardMax);
sizeOf = size(legalMoves,1);
%loop through all the moves
for i = 1:1:sizeOf
%check for winner
cMax = BoardMax;
if(legalMoves(i,1) == 0)
break;
end
cMax( legalMoves(i,1) , legalMoves(i,2) ) = 2;
temp = minMove(cMax,depthMax + 1,alphaMax,betaMax);
if(temp > max)
max = temp;
if(depthMax == 0)
bestMove = [legalMoves(i,1),legalMoves(i,2)];
end
end
if(temp > alphaMax)
alphaMax = max;
end
if(alphaMax > betaMax)
max = alphaMax;
end
end
end

end




function [ min ] = minMove(BoardMin,depthMin,alphaMin,betaMin)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
global bestMove;min = inf;
m = checkForWinner(BoardMin);
if(m)
min = m;
elseif(depthMin >= 5)
min = analysis(BoardMin);
else

%find all legal moves
legalMoves = findAllLegalMoves(BoardMin);
sizeOf = size(legalMoves,1);
%loop through all the moves
for i = 1:1:sizeOf
cMin = BoardMin;
if(legalMoves(i,1) == 0)
break;
end
cMin(legalMoves(i,1),legalMoves(i,2)) = 1;
temp = maxMove(cMin,depthMin + 1,alphaMin,betaMin);
if(temp < min)
min = temp;
if(depthMin == 0)
bestMove = [legalMoves(i,1),legalMoves(i,2)];
end
end
if(temp < betaMin)
betaMin = min;
end
if(alphaMin > betaMin)
min = betaMin;
end
end
endend



function [winner] = checkForWinner(Board)
global bestMove;
winner = 0;

% check for vertical win

% loop through each column
for j = 1:1:7
% check for four together in that column
for k = 1:1:3
% if four ones are together the human wins
if((Board(k,j) == 1) && (Board(k + 1,j) == 1) && (Board(k+2,j) == 1) && (Board(k+3,j) == 1))
winner = -500000;
%else if four twos are together the computer wins
elseif((Board(k,j) == 2) && (Board(k + 1,j) == 2) && (Board(k+2,j) == 2) && (Board(k+3,j) == 2))
winner = 500000;
end
end
end


% check for horizontal win

%loop through each row
for i = 1:1:6
for k = 1:1:4
if((Board(i,k) == 1) && (Board(i,k + 1) == 1) && (Board(i,k + 2) == 1) && (Board(i,k + 3) == 1))
winner = -500000;
elseif((Board(i,k) == 2) && (Board(i,k + 1) == 2) && (Board(i,k + 2) == 2) && (Board(i,k + 3) == 2))
winner = 500000;
end

end
end

% check for diagonal win left top to right down

for row = 1:1:3
for column = 1:1:4
if((Board(row,column) == 1) && (Board(row + 1,column + 1) == 1) && (Board(row + 2,column + 2) == 1) && (Board(row + 3,column + 3) == 1))
winner = -500000;
elseif((Board(row,column) == 2) && (Board(row + 1,column + 1) == 2) && (Board(row + 2,column + 2) == 2) && (Board(row + 3,column + 3) == 2))
winner = 500000;
end
end
end

%check for diagonal win right top to left down
for row = 1:1:3
for column = 7:-1:4
if((Board(row,column) == 1) && (Board(row + 1,column - 1) == 1) && (Board(row + 2,column - 2) == 1) && (Board(row + 3,column - 3) == 1))
winner = -500000;
elseif((Board(row,column) == 2) && (Board(row + 1,column - 1) == 2) && (Board(row + 2,column - 2) == 2) && (Board(row + 3,column - 3) == 2))
winner = 500000;
end
end
end
end


This still needs coded

function [ whoWon ] = analysis(BoardAnalysis)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
global bestMove;
whoWon = 0;
end



function [ coordinates ] = findAllLegalMoves(Board)
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
global bestMove;
numberOfFutureMoves = 0;
coordinates(numberOfFutureMoves + 1,1) = 1;
coordinates(numberOfFutureMoves + 1,2) = 1;
for i = 1:1:7
for j = 1:1:6
row = 0;
if(j == 1)
if(Board(j,i) ~= 0)
break;
end
end

if(Board(j,i) == 0)
if(j == 6)
row = j;
break;
end
continue;
elseif(Board(j,i) ~= 0)
row = j - 1;
break;
end
end
if(row ~= 0)
numberOfFutureMoves = numberOfFutureMoves + 1;
coordinates(numberOfFutureMoves,1) = row;
coordinates(numberOfFutureMoves,2) = i;
end
end

coordinates(numberOfFutureMoves + 1,1) = 0;
coordinates(numberOfFutureMoves + 1,2) = 0;
end


And I hope this is enough information. Just ask if more information is needed.

Share this post


Link to post
Share on other sites
Advertisement
I doubt that many people are going to be eager to debug your program for you. Especially given that Matlab is not a common language for game developers. Go back to basics, for example:

- Does it see diagonals as a legal move?
- Does it ever evaluate a configuration with diagonals?
- Force it to evaluate a known board one move away from a diagonal win. What happens?
- Test the values returned for known board configurations. Do they seem right?
- Is it evaluating win/loss conditions correctly?
- Is it performing the min/max correctly, e.g. if it finds a better configuration are variables being updated appropriately?

I don't know if Matlab has proper debugging, but printing output during execution always works.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!