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

2016/3/30

Ending Game Using Counter

HolyMatrixManiac HolyMatrixManiac

2016/3/30

#
I'm making my own form of a battleship game where you only have a certain amount of guesses. This may not be the most efficient way but my ships spawn randomly on my background. Anywhere the ships are not placed an actor that displays the word "miss" is placed. On top of that a tile is placed on each space covering the board no matter what is underneath. The tile disappears if you click on it, leaving you with part of a ship or "miss." I'm attempting to count the number of tiles that are clicked and removed using
  public void count()
    {
     if(Greenfoot.mouseClicked(Tiles.class))
        {
            M++;
        }
}
I am then attempting to use int M to transfer to the lose screen using
public void end()
{
    if (M == 25)
            {
        new Lose();
    }
}
This is all contained inside my main world. My problem is that I am still able to play once I've clicked 25 squares. Thanks in advance!
danpost danpost

2016/3/30

#
You probably have a set number of tiles to begin with, which means you can just ask if the tile count is 25 less than what you started with:
if (getObjects(Tiles.class).size() == getWidth()*getHeight()-25) new Lose();
For further help, please show the code for your Lose class.
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
My Lose class is just a blank screen at the moment. I thought I would finish it up if I got this to work. Do I need something there for this to work? I thought about the 25 less tile option but I wasn't exactly sure what that would look like. I'm assuming it's similar to the isEmpty code. Could you help me with this?
danpost danpost

2016/3/31

#
Well, I wanted to determine if you were using a subclass of World or a subclass of Actor for the Lose class. As coded, I would presume it to be a subclass of World; but, it might be the case the it is not coded properly. As far as tile counting, you will need to post the code for your subclass of World that creates and adds Tiles objects to itself.
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
private void prepare()
    { 
        int x = 0;
        int y = 0;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 1;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 2;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 3;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 4;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 5;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
        x = 0;
        y = 6;
        for(int i = 0; i < 7; i++)
        {
            addObject(new Tiles(),x,y);
            x++;
        }
    }
That's the code for adding in tiles. It's most likely more complicated than it needs to be but adding them in rows like this is what came to mind first.
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
Also the Lose class is a subclass of world. Its currently just has an image set for it.
danpost danpost

2016/3/31

#
Yeah, it could be written a lot simpler:
private void prepare()
{
    for (int y=0; y<7; y++)
    {
        for (int x=0; x<7; x++)
        {
            addObject(new Tiles(), x, y);
        }
    }
}
Since you are adding 49 tiles into the world, the act method of this class should look something like this:
public void act()
{
    if (getObjects(Tiles.class).size() == 24) // (49-25 = 24)
    {
        Lose lose = new Lose();
        // do whatever you need to do with 'lose' here
    }
}
danpost danpost

2016/3/31

#
The Lose class can simply be this:
import greenfoot.*;

public class Lose extends World
{
    public Lose()
    {
        Greenfoot.setWorld(this);
        Greenfoot.stop();
    }
}
Then line 5 of the act method above could simply be:
new Lose();
as you had it coded earlier.
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
I'm still doing something wrong Haha
public void end()
    {
        if (getObjects(Tiles.class).size() == 24) 
        {
            Lose lose = new Lose();
            Greenfoot.setWorld(lose);
        }
    }
That's my current code and I can click on more than 25 tiles and it doesn't do anything.
danpost danpost

2016/3/31

#
Change
public void end()
to
public void act()
If it still does not work, you will need to be more specific and thorough as to what code you have and where it is located. Seeing only bits and pieces and not knowing where they are coming from and not seeing related code makes it quite difficult to help in a constructive way. What would help is if you posted the following codes: * the entire Lose class code (copy/paste from line 1 to the end) * the entire code for the World subclass that adds tiles into itself (from line 1 to the end)
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
I thought that might be it so i tried that but sadly, noting. Lose class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Lose extends World
{
    public Lose()
    {    
        super(600, 400, 1); 
    }
}
Game class (subclass of world):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Game extends World
{
    public Game()
    {    
        super(7, 7, 100); 
        prepare();
        end();
    }
    private void prepare()
    { 
        for(int i = 0; i < 5; i++) //Places Apple "ship"
        {
            int x = Greenfoot.getRandomNumber(9) + 1;
            int y = Greenfoot.getRandomNumber(7) + 1;
            addObject(new Apple(),x,y);
        }
        for(int i = 0; i < 4; i++) //Places Banana "ship"
        {
            int x = Greenfoot.getRandomNumber(4) + 2;
            int y = Greenfoot.getRandomNumber(7) + 1;
            addObject(new Banana(),x,y);
        }
        for(int i = 0; i < 4; i++) //Places Carrot "ship"
        {
            int x = Greenfoot.getRandomNumber(7) + 1;
            int y = Greenfoot.getRandomNumber(4) + 2;
            addObject(new Carrot(),x,y);
        }
        //used save for misses
        Miss miss = new Miss();
        addObject(miss,6,6);
        Miss miss2 = new Miss();
        addObject(miss2,6,5);
        Miss miss3 = new Miss();
        addObject(miss3,6,4);
        Miss miss4 = new Miss();
        addObject(miss4,6,3);
        Miss miss5 = new Miss();
        addObject(miss5,6,2);
        Miss miss6 = new Miss();
        addObject(miss6,6,1);
        Miss miss7 = new Miss();
        addObject(miss7,6,0);
        Miss miss8 = new Miss();
        addObject(miss8,5,0);
        Miss miss9 = new Miss();
        addObject(miss9,5,1);
        Miss miss10 = new Miss();
        addObject(miss10,5,2);
        Miss miss11 = new Miss();
        addObject(miss11,5,3);
        Miss miss12 = new Miss();
        addObject(miss12,5,4);
        Miss miss13 = new Miss();
        addObject(miss13,5,5);
        Miss miss14 = new Miss();
        addObject(miss14,5,6);
        Miss miss15 = new Miss();
        addObject(miss15,4,6);
        Miss miss16 = new Miss();
        addObject(miss16,4,5);
        Miss miss17 = new Miss();
        addObject(miss17,4,4);
        Miss miss18 = new Miss();
        addObject(miss18,4,3);
        Miss miss19 = new Miss();
        addObject(miss19,4,2);
        Miss miss20 = new Miss();
        addObject(miss20,4,1);
        Miss miss21 = new Miss();
        addObject(miss21,3,2);
        Miss miss22 = new Miss();
        addObject(miss22,4,0);
        Miss miss23 = new Miss();
        addObject(miss23,3,0);
        Miss miss24 = new Miss();
        addObject(miss24,3,1);
        Miss miss25 = new Miss();
        addObject(miss25,3,3);
        Miss miss26 = new Miss();
        addObject(miss26,3,4);
        Miss miss27 = new Miss();
        addObject(miss27,3,5);
        Miss miss28 = new Miss();
        addObject(miss28,3,6);
        Miss miss29 = new Miss();
        addObject(miss29,2,6);
        Miss miss30 = new Miss();
        addObject(miss30,2,5);
        Miss miss31 = new Miss();
        addObject(miss31,2,4);
        Miss miss32 = new Miss();
        addObject(miss32,2,3);
        Miss miss33 = new Miss();
        addObject(miss33,2,2);
        Miss miss34 = new Miss();
        addObject(miss34,2,1);
        Miss miss35 = new Miss();
        addObject(miss35,2,0);
        Miss miss36 = new Miss();
        addObject(miss36,1,0);
        Miss miss37 = new Miss();
        addObject(miss37,1,1);
        Miss miss38 = new Miss();
        addObject(miss38,1,2);
        Miss miss39 = new Miss();
        addObject(miss39,1,3);
        Miss miss40 = new Miss();
        addObject(miss40,1,4);
        Miss miss41 = new Miss();
        addObject(miss41,1,5);
        Miss miss42 = new Miss();
        addObject(miss42,1,6);
        Miss miss43 = new Miss();
        addObject(miss43,0,6);
        Miss miss44 = new Miss();
        addObject(miss44,0,5);
        Miss miss45 = new Miss();
        addObject(miss45,0,4);
        Miss miss46 = new Miss();
        addObject(miss46,0,3);
        Miss miss47 = new Miss();
        addObject(miss47,0,2);
        Miss miss48 = new Miss();
        addObject(miss48,0,1);
        Miss miss49 = new Miss();
        addObject(miss49,0,0);
        for (int y=0; y<7; y++) //Places tiles
        {
            for (int x=0; x<7; x++)
            {
                addObject(new Tiles(), x, y);
            }
        }
    }

    public void end() //Ends game
    {
        if (getObjects(Tiles.class).size() == 24) 
        {
            Lose lose = new Lose();
            Greenfoot.setWorld(lose);
        }
    }
}
Sorry about all the miss objects. I just used save the world to place them as it was simple, though not neat
danpost danpost

2016/3/31

#
Okay -- you are not calling 'end' from the right place. Your 'prepare' method adds 49 tiles into the world (line 8) and you immediately call 'end' (line 9). At that point, there are still 49 tiles in the world and line 141 will never be true. From where you call 'end' from (line 9), there is no continuous checking on the number of tiles in the world. For continuous check, you need the code to be within an 'act' method or a method that is called from an 'act' method:
public void act()
{
    end();
}
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
Thank you so much! Sorry if I was a little naive when it comes to how i formatted this. First time post!
danpost danpost

2016/3/31

#
You can condense your 'prepare' method even more by replacing lines 31 through 136 of the Game world code with the following:
for (int y=0; y<7; y++) //Places miss and tiles
{
    for (int x=0; x<7; x++)
    {
        addObject(new Miss(), x, y);
        addObject(new Tiles(), x, y);
    }
}
However, it appears that the placing of the ships is flawed. Some of the generated values are outside the range of the width or height of the world and no will be no arrangement for the sets of fruit (I cannot say if this is actually what you want or not, however).
HolyMatrixManiac HolyMatrixManiac

2016/3/31

#
Thanks for condensing the prepare method! I'll check the code for placing the fruit. Thought I had it in bounds but I'll review. Thanks!
You need to login to post a reply.