This site requires JavaScript, please enable it in your browser!
Greenfoot back
RUMMAKER
RUMMAKER wrote ...

2013/9/8

i messed up somewhere, need help with loops

RUMMAKER RUMMAKER

2013/9/8

#
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 :
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;
    }
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;//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
    }
Wow, lots of code for the AI. When you use a loop, you need to make sure it ends at some point. If it doesn't, then everything becomes irresponsive, because it's going through the loop instead of running the game. I haven't looked through your code for the source of error, but you should make sure x_ does at some point go to 19 or more. I see you reset x_ to -1, make sure it goes back up to 19 at some point. You can use System.out.println() statements to double check where things are going. I recommend using this to see if x ever goes above 19.
RUMMAKER RUMMAKER

2013/9/8

#
it does get out of the first loop :/ i found the error on line 43 where it sais i --; its suppose to be i2 --; i fixed that but the freezing still happens so evidently theres a bigger problem then a typo :/
You need to login to post a reply.