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

2019/6/18

How to set win condition for a free moving connect four game

BagBeGone BagBeGone

2019/6/18

#
public class Red extends Actor
{
    int y;
    int g = 2;
    int didTouch1 = 1;
    int didTouch2 = 1;
    int didTouch3 = 1;
    int didTouch4 = 1;
    int touch1 = 0;
    int touch2 = 0;
    int touch3 = 0;
    int touch4 = 0;
    int mouseClick = 0;
    public void act()
    {
        fallingCheck();
        clickCheck();

    }

    public void fallingCheck()
    {
        if (isTouching(Red.class) || isTouching(Yellow.class) || isTouching(StopChecker.class)){
            //nothing
        } else {
            gravity();
        }
    }

    public void clickCheck()
    {
        if (Greenfoot.mouseClicked(null)) {
            mouseClick = mouseClick + 1;
            touchCheck();
        }
    }
    
    public void gravity()
    {
        g = g + 1;
        y = getY() + g;
        setLocation(getX(), y);

    }

    public void touchCheck()
    {
        if (didTouch1 == 1) {
            touchCheck1();
        } else if (didTouch2 == 0) {
            touchCheck2();
        } else if (didTouch3 == 0) {
            touchCheck3();
        } else if (didTouch4 == 0) {
            touchCheck4();
        } else {
            winCheck();
        }

    }

    public void touchCheck1()
    {
        if (isTouching(Red.class) && didTouch1 == 1) {
            touch1 = touch1 + 1;
            didTouch1 = 0;
            didTouch2 = 0;
        }
    }

    public void touchCheck2()
    {
        if (isTouching(Red.class) && didTouch2 == 0) {
            touch2 = touch2 + 1;
            didTouch2 = 1;
            didTouch3 = 0;
        }
    }

    public void touchCheck3()
    {
        if (isTouching(Red.class) && didTouch3 == 0) {
            touch3 = touch3 + 1;
            didTouch3 = 1;
            didTouch4 = 0;
        }
    }

    public void touchCheck4()
    {
        if (isTouching(Red.class) && didTouch4 == 0) {
            touch4 = touch4 + 1;
            didTouch4 = 1;
        }
    }

    public void winCheck()
    {
        if (touch1 >= 1 && touch2 >= 1 && touch3 >= 1 && touch4 >= 1) {
            getWorld().addObject(new Reset(), 600, -800);
        }
    }
}
I need help setting up a win condition for when 4 red.class touch each other the game ends Is there a way I could maybe differentiate between objects in the same class and see if 4 of them touch each other or something?
You need to login to post a reply.