I'm making a AI for my connect five game http://www.greenfoot.org/scenarios/9254. (AI code not up to date)
when i started making the AI i used a if else block and it was fine, but as i started adding more complex stuff in it, the block became gigantic. So, I'm rewriting the whole thing as a loop.
(world is 19, 19)
Problem is when i run the loop the game goes unresponsive, i can't click on reset or pause and i can only terminate the game and restart (from experience i think its a loop problem i made) The loop code :
the if else block im trying to convert (Warning : it's gigantic):
the if else statements work fine as intended so the problem is definitely with the loops.
public void AIAction(int AI)
{
x_ = 0;
y_ = 0;
int bestY = 0;
int bestX = 0;
int priority = 0;
int priority2 = -999;
int cP = 0;
int cP2 = -999;
int player = opP();
int i = 5;
int player2 = player;
int i2 = i;
int n = 4;
while(x_ < 19)
{
if(board_[x_][y_] == BLANK_)
{
while(i != 1)
{
while(!(scanAll(i,player)))
{
if(player == opP())
{
player = AI;
}
else
{
player = opP();
i--;
}
priority --;
}
i2 = i;
player2 = player;
n = 4;
while(!(scanB(i2, player2) >= n))
{
if(player2 == AI && n == 1)
{
player2 = opP();
i--;
n = 4;
}
else if(n == 1)
{
player2 = AI;
n = 4;
}
n--;
cP--;
}
}
if(priority > priority2)
{
bestX = x_;
bestY = y_;
priority2 = priority;
}
else if(priority == priority2)
{
if(cP >= cP2)
{
bestX = x_;
bestY = y_;
cP2 = cP;
}
}
priority = 0;
cP = 0;
i = 5;
player = opP();
}
if(x_ == 18 && y_ < 18)
{
x_ = -1;
y_ ++;
}
x_ ++;
}
x_ = bestX;//updates the position because scanAll() uses x_ & y_ to scan
y_ = bestY;
board_[bestX][bestY] = AI;
if(AI == WHITE_)
{
White w = new White();
addObject(w, bestX, bestY);
}
else
{
Black b = new Black();
addObject(b, bestX, bestY);
}
player_ = opP();// switch turns
}
public int opP()//gives the opposite player
{
if(player_ == BLACK_)
{
return WHITE_;
}
else
{
return BLACK_;
}
}
public int scanB(int win, int player_)//returns how many scans return ture ie. (if(scanVertically()) + 1)
{
int r = 0;
if(scanVertically(win, player_))
{
r += 1;
}
if(scanHorizontally(win, player_))
{
r += 1;
}
if(scanLRD(win, player_))
{
r += 1;
}
if(scanRLD(win, player_))
{
r += 1;
}
return r;
}public void AIAction(int AI)
{
x_ = 0;//resets both x_ and y_ to 0 so start scanning from 0,0
y_ = 0;
int bestX = 0;
int bestY = 0;
int help = 0;//sets the priority of the position, if a lower/worse position is found, int help will filter it so it does not replace bestX/Y
while(x_ < 19)
{
if(board_[x_][y_] == BLANK_)//only blank points can a stone be placed
{
if(scanAll(5, opP()))//this is scanning for the conputer's stone because it scanns for the OPPOSITE of what is inputed
{
bestX = x_;
bestY = y_;
help = 29;//biggest priority because win
}
else if(scanAll(5, player_) && help <= 28)// (5 because it counts BLANK_ as BLACK_) black have 4 stones in a row, must block
{
bestX = x_;
bestY = y_;
help = 28;
}
else if(scanAll(4, opP()) && help <= 27)
{
if(scanB(4, opP()) >= 2)//only 2 is required because it's already a garenteed win
{
bestX = x_;
bestY = y_;
help = 27;
}
else if(scanB(4, player_) >= 2 && help <= 25)//only 2 is required because it's already a garenteed win
{
bestX = x_;
bestY = y_;
help = 26;
}
else if(scanB(3, opP()) >= 2 && help <= 24)//2 is enaugh
{
bestX = x_;
bestY = y_;
help = 25;
}
else if(scanB(3, player_) >= 2 && help <= 23)
{
bestX = x_;
bestY = y_;
help = 24;
}
else if(scanB(2, opP()) >= 2 && help <= 22)
{
bestX = x_;
bestY = y_;
help = 23;
}
else if(scanB(2, player_) >= 2 && help <= 21)
{
bestX = x_;
bestY = y_;
help = 22;
}
else if(help <= 21)
{
bestX = x_;
bestY = y_;
help = 21;
}
}
else if(scanAll(4, player_) && help <= 20)//scans for 3 in a row enemy stones
{
if(scanB(4, player_) >= 2)//blocks 2 3 in a row stones at once
{
bestX = x_;
bestY = y_;
help = 20;
}
else if(scanB(3, opP()) >= 2 && help <= 18)
{
bestX = x_;
bestY = y_;
help = 19;
}
else if(scanB(3, player_) >= 2 && help <= 17)
{
bestX = x_;
bestY = y_;
help = 18;
}
else if(scanB(2, opP()) >= 2 && help <= 16)
{
bestX = x_;
bestY = y_;
help = 17;
}
else if(scanB(2, player_) >= 2 && help <= 15)
{
bestX = x_;
bestY = y_;
help = 16;
}
else if(help <= 15)
{
bestX = x_;
bestY = y_;
help = 15;
}
}
else if(scanAll(3, opP()) && help <= 14)
{
if(scanB(3, opP()) >= 2)
{
bestX = x_;
bestY = y_;
help = 14;
}
else if(scanB(3, player_) >= 2 && help <= 12)
{
bestX = x_;
bestY = y_;
help = 13;
}
else if(scanB(2, opP()) >= 2 && help <= 11)
{
bestX = x_;
bestY = y_;
help = 12;
}
else if(scanB(2, player_) >= 2 && help <= 10)
{
bestX = x_;
bestY = y_;
help = 11;
}
else if(help <= 10)
{
bestX = x_;
bestY = y_;
help = 10;
}
}
else if(scanAll(3, player_) && help <= 9)
{
if(scanB(3, player_) >= 2)
{
bestX = x_;
bestY = y_;
help = 9;
}
else if(scanB(2, opP()) >= 2 && help <= 7)
{
bestX = x_;
bestY = y_;
help = 8;
}
else if(scanB(2, player_) >= 2 && help <= 6)
{
bestX = x_;
bestY = y_;
help = 7;
}
else if(help <= 6)
{
bestX = x_;
bestY = y_;
help = 6;
}
}
else if(scanAll(2, opP()) && help <= 5)
{
if(scanB(2, opP()) >= 2)
{
bestX = x_;
bestY = y_;
help = 5;
}
else if(scanB(2, player_) >= 2 && help <= 3)
{
bestX = x_;
bestY = y_;
help = 4;
}
else if(help <= 3)
{
bestX = x_;
bestY = y_;
help = 3;
}
}
else if(scanAll(2, player_) && help <= 2)// <= 1 is baseline for starting the scan
{
if(scanB(2, player_) >= 2)
{
bestX = x_;
bestY = y_;
help = 2;
}
else if(help <= 1)
{
bestX = x_;
bestY = y_;
help = 1;
}
}
else if(help <= 0)//incase computor is first to start, will pick random
{
bestX = Greenfoot.getRandomNumber(18);
bestY = Greenfoot.getRandomNumber(18);
while(board_[bestX][bestY] != BLANK_)
{
bestX = Greenfoot.getRandomNumber(18);
bestY = Greenfoot.getRandomNumber(18);
}
}
}
if(x_ == 18 && y_ < 18)//resets the loop so it runs again till all points covered
{
x_ = -1;
y_ ++;
}
x_++;
}
x_ = bestX;//updates the position because scanAll() uses x_ & y_ to scan
y_ = bestY;
board_[bestX][bestY] = AI;
if(AI == WHITE_)
{
White w = new White();
addObject(w, bestX, bestY);
}
else
{
Black b = new Black();
addObject(b, bestX, bestY);
}
player_ = opP();// switch turns
}

