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

2019/6/20

Connect 4 win condition help

BagBeGone BagBeGone

2019/6/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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? More info about the game- Red & Yellow pieces that fall and stick to each other, there is no board. I have gravity & the piece spawning check, the only problem is the win condition (when 4 red touch each other or 4 yellow touch each other) I have tried 1. Making it so if touching red.class it adds 1 to touch, but this doesnt work because touch will increase indefinitely (red will always be touching red) 2. I have tried making multiple variable to check if touch 1 is done etc, but that doesnt work because It cant differentiate between the same class Help will be appreciated!
danpost danpost

2019/6/20

#
BagBeGone wrote...
<< Code Omitted >> I need help setting up a win condition for when 4 red.class touch each other the game ends
I have tried 1. Making it so if touching red.class it adds 1 to touch, but this doesnt work because touch will increase indefinitely (red will always be touching red)
It wouldn't if you increased it at the moment it stopped falling
2. I have tried making multiple variable to check if touch 1 is done etc, but that doesnt work because It cant differentiate between the same class
Not sure what you mean by "differentiate between the same class."
Help will be appreciated!
I would thing that:
1
if (getIntersectingObjects(Red.class).size() > 3)
should be sufficient for image touching. Or, for actual color touching:
1
if (getObjectsInRange(getImage().getWidth()/2, Red.class).size() > 3)
BagBeGone BagBeGone

2019/6/20

#
I would thing that:
1
if (getIntersectingObjects(Red.class).size() > 3)
should be sufficient for image touching. Or, for actual color touching:
1
if (getObjectsInRange(getImage().getWidth()/2, Red.class).size() > 3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Red extends Actor
{
    int y;
    int g = 2;
   int touch = 0;
    public void act()
    {
        fallingCheck();
        winCheck();
    }
  
    public void fallingCheck()
    {
        if (isTouching(Red.class) || isTouching(Yellow.class) || isTouching(StopChecker.class)){
            //nothing
        } else {
            gravity();
        }
    }
  
      
    public void gravity()
    {
        g = g + 1;
        y = getY() + g;
        setLocation(getX(), y);
  
    }
  
    public void winCheck()
    {
        if (getObjectsInRange(getImage().getWidth()/2, Red.class).size() > 3) {
            Greenfoot.stop();
        }
    }
}
Like this? when I put this instead, fallingCheck() doesnt work any more (balls stop falling)
danpost danpost

2019/6/20

#
Try this for fallingCheck:
1
2
3
4
private void fallingCheck()
{
    if (getObjectsInRange(getImage().getWidth()/2, Actor.class).isEmpty()) gravity();
}
BagBeGone BagBeGone

2019/6/21

#
danpost wrote...
Try this for fallingCheck:
1
2
3
4
private void fallingCheck()
{
    if (getObjectsInRange(getImage().getWidth()/2, Actor.class).isEmpty()) gravity();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import lang.stride.*;
import java.util.*;
import greenfoot.*;
 
/**
 *
 */
public class Red extends Actor
{
    int y;
    int g = 2;
    int gameState = 0;
    public void act()
    {
        fallingCheck();
 
    }
 
    private void fallingCheck()
    {
        if (getObjectsInRange(getImage().getWidth()/2, Actor.class).isEmpty()) gravity();
    }
 
    public void gravity()
    {
        g = g + 1;
        y = getY() + g;
        setLocation(getX(), y);
 
    }
 
}
gravity(); never starts so the balls never fall. The balls don't move when you click
danpost danpost

2019/6/21

#
BagBeGone wrote...
<< Code Omitted >> gravity(); never starts so the balls never fall. The balls don't move when you click
Okay -- you must have an actor in the world that I do not know about the is preventing it from moving. Try this:
1
if (getObjectsInRange(getImage().getWidth()/2, Red.class).isEmpty() && getObjectsInRange(getImage().getWidth()/2, Yellow.class).isEmpty() && !isTouching(StopChecker.class)) gravity();
You need to login to post a reply.