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

2014/5/13

Tile Changing Color

1
2
3
4
danpost danpost

2014/5/15

#
Another way the 'for' loop could be written Is as follows:
for (int n=0; n<tiles.size(); n++)
{
    if (tiles.get(n).getColor()  == 0)
    {
        foundBlank = true;
        break;
    }
}
anonymousse anonymousse

2014/5/15

#
Trying it thank you
danpost danpost

2014/5/15

#
anonymousse wrote...
I added it to the tile class and it worked wonderfully but one tile just starts popping up right after the other. Is the act method supposed to be in the World class or the tile class? If I put it in the world class it can't find the changecolor method. And is there a way to edit it so that only one tile changes color at a time?
The act method should be in the Tile class; however, you now have a method that does the changing of tile colors. Your act method should now just be as follows:
public void act()
{
    runTileChangeCounter();
}
danpost danpost

2014/5/15

#
@molsbex, please start a new discussion on this issue and include how, in detail, you want your spiders to move around (the more detailed the description, the better). EDIT: Oh, now I see you have done so.
anonymousse anonymousse

2014/5/17

#
Just wondering, the difference between (!foundBlank) and (foundBlank) is that (!foundBlank) indicates there are no tiles that satisfy the condition? I tried making a game over function based on the concept with no colored tiles left and I guess I messed up on that part.
  public void checkGame() 
    {
        java.util.List<Tile> tiles = getObjects(Tile.class);    
        boolean foundColored = false;    
     for (int n=0; n<tiles.size(); n++)  
{  
    if (tiles.get(n).getColor()> 0) foundColored = true;  
    if (!foundColored) break;
    if (foundColored)return;  
    gameOver();
}  
}
    
    
    public void gameOver()
    {
         addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
        
        
    }
danpost danpost

2014/5/17

#
I do not think you are understanding the flow of the code you wrote. You want that if you do find a colored tile that the game continue (and that if all tiles are blank that the game ends). The key word in the parenthesis of the last sentence is 'all'. Line 8 says to exit the 'for' loop if the first tile looked at is blank (not colored). But you still need to check the rest of them. Line 9 does do what you want; it exits the method as soon as a colored tile is found. Between lines 8 and 9, line 10 will never be execute because both lines 8 and 9 exit the 'for' loop (line 9 exits the 'for' loop AND exits the method). The call to the 'gameOver' methods should be after the 'for' block ends.
public void checkGame()
{
    java.util.List<Tile> tiles = getObjects(Tile.class);
    for (int n=0; n<tiles.size(); n++) if (((Tile)tiles.get(n)).getColor()>0) return;
    gameOver();
}
Here, line 4 will exit the method as soon as a colored tile is found. If the 'for' loops completes without exiting the method, then all tiles were blank and it is game over.
anonymousse anonymousse

2014/5/17

#
Ok so for a final step, I'm trying to put a method that ends the game when there are no more possible moves (ie. the arrow is surrounded by tiles that are a different color). Would I be able to combine the getNeighbours function and getColor functions in my Arrow class? For example I have: List neighbours = getNeighbours(1, false, Tiles.class); but how can I check if all of the Neighbours are different colors from the Arrow?
public void checkGame2();
{
if (
    tile.getNeighbours(1, false, Tiles.class)) && (tile.getColor() != color))
    return true;
    blank.gameover
     

}
}
danpost danpost

2014/5/17

#
Before correcting this, there are a few things about the 'checkGame2' method you posted that you should be aware of. The first is the use of a semi-colon ( ';' ) at the end of line 1. I do not think that this will compile. Class, constructor and method declaration lines must always be followed by an immediate open curly bracket. Next, 'getNeighbours' is a collision type method of the Actor class and has 'protected' access. This means that you cannot call it on a tile from outside the Tile class. Then, 'getNeighbours' returns a List object, not a boolean value and cannot be used as a condition (as is) in an 'if'. Finally, line 6 will never get executed (and the compiler will probably complain about this also). You can never place any statements in the same block after an unconditional return. There is one more thing -- your return statement one line 5 has a boolean return value; however, the method is declared as returning nothing by the use of 'void' in its declaration. Fortunately, you want to get the neighbors of the arrow, not of any tile; so, you can use 'getNeighbours' to get a list of nearby tiles. But, you have to extract the tiles from the list to check their colors; and you need to check up to all of them (until one is found to be of the same color, if any). A 'for' loop will do the trick. A 'for-each' loop is better to iterate through a list.
public boolean checkGame2() // no semi-color at end of this line
{
    for (Object obj : getNeighbours(1, false, Tile.class))
    {
        if (((Tile)obj).getColor() == color) return false;
    }
    // blank.gameover
    return true;
}
I was not sure how the 'blank.gameover' line comes into play here. First, it is not a statement (no semi-colon at the end); second, it looks like a reference to a field in an object referenced by 'blank' (no parenthesis suggesting it is a method call); and, I do not see what 'blank' might refer to.
anonymousse anonymousse

2014/5/17

#
Blank is the world and gameover is the method that would be called if the colors returned are alll different from the arrow. I tried implementing it with ((Blank) getWorld()).gameOver();// blank.gameover And it worked now
anonymousse anonymousse

2014/5/17

#
I added the code and just added the ((Blank) getWorld()).gameOver();
public boolean checkGame2() // no semi-color at end of this line  
{  
    for (Object obj : getNeighbours(1, false, Tile.class))  
    {  
        if (((Tile)obj).getColor() == color) return false;  
    }  
    ((Blank) getWorld()).gameOver();// blank.gameover  
        return true;  
  
    
}  
But it isn't working. Am I supposed to have an act method or have a method that checks the booleon in the world method?
danpost danpost

2014/5/17

#
It may be that '1' is too small a range to find any tiles (or enough tiles). It may be your 'gameOver' method. It may be you are not calling this method from anywhere that it will be run each act cycle (or at least each time the arrow moves or eats a tile -- the best time to run this method depends on how you want your game to be played). I feel that it is probably that the '1' is too small; that your tiles are at least 3 cells wide and you would require at least a value of '4' to get adjacent tiles (and that is just the near ones). A value of '6' should get at least the four adjacent tiles -- above, below, and to both sides (if I recall your world structure correctly).
danpost danpost

2014/5/17

#
I thought that in the game the arrow was allowed to move between tiles and that the tiles were being colored again at different times. What exactly would your requirements be for game over?
anonymousse anonymousse

2014/5/17

#
The first requirement for game over worked fine when there are no colored tiles left. A green arrow cannot move to a yellow egg and vice versa. Thus, once the arrow is surrounded by the other colored eggs, the game should be over. However, there is also the problem of arrows stuck on the corners or sides. They are surrounded by only 2 eggs if on the corners and 3 on the sides so I also need to figure out a way to include that. How do I control when to run the method? I would want it to run every act cycle because I would want the game to be over as soon as it is surrounded by other colored tiles. I tried changing the numbers and it still won't detect it.
danpost danpost

2014/5/17

#
I seems like a totally different game. Are you changing it around as you go? Maybe you need to re-upload it so I can see what you have at this point?
anonymousse anonymousse

2014/5/17

#
Here it is http://www.greenfoot.org/scenarios/11494
There are more replies on the next page.
1
2
3
4