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

2013/6/17

Help me with the right condition in the If-Else-expression

1
2
Omletti Omletti

2013/6/17

#
Hey :)) (btw I'm german sry for my englisch) I have to do a project for school, and I'm programming "Conway's Game Of Life" and I'm almost done but theres one problem... So I have the variables x and y who represent coordinates of certain fields (x/y). I want to have acces of the field before, and one field above of the the actual field (x/y), so the field (x-1/y+1). In an If-Else Expression I wanna "ask" if on the field (x-1/y+1) is an object (in my case it's "lebendigeZelle" a german expression), but I don't know how i can write that??? I wrote it that way but just as a kind of wildcard:
1
2
3
4
5
6
7
if(i - 1,j + 1 = lebendigeZelle)
{
neighbours = neighbours + 1;
else
{
}
How do I have to change the condition that the program works?? Anyone an idea? Thanks :)
Gevater_Tod4711 Gevater_Tod4711

2013/6/17

#
If you want to access the field abouve your starting field you need to use j - 1 because in greenfoot the coordinates are not like in a mathemagic coordinate system. You start in the top left corner. If you just want to know if the is an object in this field you can use this:
1
2
3
4
if (!getWorld().getObjectAt(getX() - 1, getY() - 1, lebendigeZelle.class).isEmpty()) {
    //lebendigeZelle should be the classname; If it isn't the right name you'll have to change that.
    //if this condition is true there is an object which type is lebendigeZelle in the field.
}
Omletti Omletti

2013/6/17

#
Thank you! I'll try it immediately! :))
Omletti Omletti

2013/6/17

#
When I say "compile" it greenfoot highlights getWorld (!GETWORLD().getObjectAt(getX() - 1, getY - 1, LebendigeZelle.class).isEmpty()) GETWORLD is the problem :( It says "cannot find symbol - method getWorld()" What am I supposed to do now? Create the method "getWorld"? How?
danpost danpost

2013/6/17

#
If the code is in the world class, you do not need the 'getWorld().' part. 'getWorld()' is an Actor class method and can only be used on an Actor object.
Omletti Omletti

2013/6/17

#
Okay thank you... Another thing... Now it's the "getObjectAt" -.-' "cannot find symbol - method getObjectAt(int,int,java.lang.Class<LebendigeZelle>)"
Gevater_Tod4711 Gevater_Tod4711

2013/6/17

#
You should try to use getWorld() instead of GETWORLD() because java is case sensitive. If also this doesn't work you probably try to do this in a world class (a subclass of greenfoot.World). If this is the case you'll have to delete the 'getWorld().' . But in this case also the methods getX() and getY() will cause problems. getX() and getY() are the x and y components of the starting field. If you know this components (or you have stored them in an int variable) you can use this variables (like in your first post i and j).
Gevater_Tod4711 Gevater_Tod4711

2013/6/17

#
Oh sorry the getObjectAt should be getObjectsAt. My fail.
Omletti Omletti

2013/6/17

#
Ok thank you very much it worked! There are no syntax errors! :'D Oh thank god, I was so desperate! ;D Theres just one thing now... When I say "act" a text shows up saying "java.lang.StackOverflowError at GameOfLife.getNeighbours(GameOfLife.java:60)" What's wrong now? :D
Omletti Omletti

2013/6/17

#
Btw here's the whole code, maybe it's easier to understand it if you can see the whole thing :D
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public class GameOfLife extends World  //to build the world
{
  int neighbours;
  int z;
   
    public GameOfLife() 
    
        super(50, 50, 10);
        populate();
        neighbours = 0;
   
    
   
    public void populate()  //"lebendigeZelle" and "toteZelle" are spread all over the field by a random number
    
   
        for (int i = 0; i < 50; i++) 
        
   
            for (int j = 0; j < 50; j++) 
            {     
                int zufall = Greenfoot.getRandomNumber(100); 
   
                if (zufall < 50
                
                    LebendigeZelle lebZ = new LebendigeZelle(); 
                    addObject(lebZ,i,j);       
                
                else 
                
                    ToteZelle z = new ToteZelle(); 
                    addObject(z,i,j); 
   
                
            
             
        }     
   
    }  
 
   public int getX()
   {
       int x;
       {
           x=getX();
       }
       return x;
   }
    
   public int getY()
   {
       int y;
       {
           y=getY();
       }
       return y;
   }
    
 
   public int getNeighbours()
   {   
      return neighbours;
   }
   
    public void act() //what the program is supposed to do
    {
        int x; int y; int i; int j;
        {
            for(i = 0; i < 50; i++)
            {
                for(j = 0; j < 50; j++)
                {
                    i=getX();
                    j=getY();
                     
                    if(!getObjectsAt(getX() - 1, getY() + 1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }   
                     
                    if(getObjectsAt(getX(), getY() + 1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX() + 1, getY() + 1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX() - 1, getY(), LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX() + 1, getY(), LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX() - 1, getY() -1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX(), getY() - 1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(getObjectsAt(getX() + 1, getY() - 1, LebendigeZelle.class).isEmpty())
                    {
                        neighbours = neighbours + 1;
                    }
                     
                    if(neighbours < 2)
                    {
                        ToteZelle z = new ToteZelle();
                        addObject(z,i,j);
                    }
                    else
                    {
                        if(neighbours > 3)
                        {
                             ToteZelle z = new ToteZelle();
                             addObject(z,i,j);
                        }
                        else
                        {
                            LebendigeZelle lebZ = new LebendigeZelle(); 
                            addObject(lebZ,i,j);  
                        }
                    }
                                      
                }
            }   
        }
    }
}  
Gevater_Tod4711 Gevater_Tod4711

2013/6/17

#
Are you shure that the exception says that getNeighbours causes the StackOverflowError? I don't see how this should be possible. It would be helpfull if you yould post the stack trace here (the text printed your console when the error occours).
danpost danpost

2013/6/17

#
The problem is that the methods 'getX' and 'getY' call themselves. Each call will in turn call it again overflowing the stack. I do not think you need (or want) those methods anyway, so just remove them. Just use 'i' and 'j' in the offsets.
Omletti Omletti

2013/6/17

#
I know it's weird... Well there just new tab that opens up "Terminal Window" then the sentence "java.lang.StackOverflowError" and then really often the sentence "at GameOfLife.getNeighbours(GameOfLife.java:60)" but when i go on one of them my Code opens up and something is highlighted, but it's always another thing... Wait, When I go on "at GameOfLife.getNeighbours(GameOfLife.java:60)" line 47 ("return x;") is highlighted and when I go on the other sentence (I just realized there are two) line 45 is highlighted... Uh and it says ("at GameOfLife.getX(GameOfLife.java:58)") I'm really confused right now...
Omletti Omletti

2013/6/17

#
@danpost I tried it, but then all lines in which "getX" or "getY" appear are highlighted :(( and it says "cannot find symbol - method getX()"
danpost danpost

2013/6/17

#
Also, from line 116 on, you are changing the objects while in the process of determining what they all should be (within the loop). In doing so, you are changing the neighbours of cells you have not check yet, possibly altering what they should be in the next frame. You should determine what all cells need to become before changing any of them; then, change all of them to their new state at once.
There are more replies on the next page.
1
2