Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,833 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,803 people online right now. Registration is fast and FREE... Join Now!




Bubblet game flood method

 
Reply to this topicStart new topic

Bubblet game flood method

eskimo9
23 Sep, 2008 - 04:48 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 3

Hi, I'm new at java, first post here, etc. I'm making a bubblet/jawbreaker style game where groups of same coloured, connecting bubbles are selected and removed from a grid of random coloured bubbles. Everything seems ok, except my flood method which needs to identify adjacent same-coloured bubbles. Mine finds all the same coloured bubbles, not just the adjacent ones.

CODE

public boolean contains(int row, int col)
    {
        if(grid.get(row,col)== color)
        {
            seen[row][col]=true;
        }
        return seen[row][col];
    }
    
    private int flood(int row, int col)
    {
        if(contains(row-1,col)==true)
        {
            size++;
        }
        if(contains(row+1,col)==true)
        {
            size++;
        }
        if(contains(row,col-1)==true)
        {
            size++;
        }
        if(contains(row,col+1)==true)
        {
            size++;
        }
        return size;
    }


This is the edited version, which should only return those bubbles immediately to either side of the one selected, but it returns all the bubbles on the grid.

#EDIT# I have a sneaking suspicion the problem is with the contains method, as it should take as parameters the coordinates of a bubble, and return whether or not the bubble is in the current group. what it is doing, is only finding whether the colours match, not whether it is adjacent. How can I get it to check whether it is connected, knowing it wouldnt be connected in a straight line? Any help appreciated.

This post has been edited by eskimo9: 23 Sep, 2008 - 04:57 PM
User is offlineProfile CardPM
+Quote Post

aiaophi
RE: Bubblet Game Flood Method
24 Sep, 2008 - 09:56 PM
Post #2

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 1

hmm, let me have a bit of a think about it and ill get back to you..

but post up the rest of your solution, im eager to see how it works for a cool little bubblet game lol!

regards.
User is offlineProfile CardPM
+Quote Post

eskimo9
RE: Bubblet Game Flood Method
25 Sep, 2008 - 12:28 AM
Post #3

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 3

Right. I've been at it for hours and hours, and I'm nearly (I think) there. I'll give you the group class, the class that finds all the same-coloured bubbles adjacent to your selected one, but the class to create a random grid is easy, so you can work it out.
CODE


/**
* Group is a group of bubbles from grid with same colour, and connected to the clicked
* bubble at row,col.
*
* @author Chris Turner  
* @version 26.09.2008
*/
public class Group
{
    private Grid grid;
    private int nRows;
    private int nCols;
    private boolean[][] seen;
    int color;
    int size=0;
    
    /*
     * Constructor method Group creates a new object of Group, calls the grid object,
     * sets seen as a new 2D boolean array set to false.
     *
     * @param grid, an object of type Grid, int row, int col, the starting position of
     * Group
     */
    public Group(Grid grid, int row, int col)
    {
        this.grid = grid;
        color = grid.get(row,col);
        nRows = grid.nRows();
        nCols = grid.nCols();
        
        seen = new boolean[nRows][nCols];
        for(int i=0; i<nRows; i++)
        {
            for(int j=0; j<nCols; j++)
            {
                seen[i][j] = false;
            }
        }
    }
    
    /*
     * Acessor method size returns value in the size field
     * @return int size
     */
    public int size()
    {
        return size;
    }
    
    /*
     * Accessor method score returns the value in the score field
     * @return int score
     */
    
    public int score()
    {
        int score = size*(size-1);
        return score;
    }
    
    
    /*
     * Accessor method contains returns value in the seen field at position [row][col]
     * @param int row, int col
     * @return boolean seen
     */
    public boolean contains(int row, int col)
    {
                  
        return seen[row][col];
        
    }
    
    /*
     * Mutator method flood determines which bubbles are in this group and returns the
     * size of the group. Starter point for the group is at row,col
     * @param int row, int col
     * @return int size
     */
    private int flood(int row, int col)
    {
        
        //Search whole array, find where color = group color and neighbour is in seen
        seen[row][col]=true;
        for(int i=0; i<nRows; i++)
        {
            for(int j=0; j<nCols; j++)
            {
                if(grid.get(i,j)==color)
                {
                    if(seen[i-1][j] == true || seen[i+1][j]==true || seen[i][j-1]==true || seen[i][j+1]==true)
                    {
                        seen[i][j]=true;
                        size++;
                    }
                }
            }
        }
        
        return size;
    }
    
    
}

It's all good except for the flood class. I've been told by the tutor that that is the correct idea, to find same color, and test if it has neighbours in the group. It just doesnt work as yet. When I click on a bubble, I get no response, which is different to before when I was getting every bubble with the right colour being returned. Any ideas why?

This post has been edited by eskimo9: 25 Sep, 2008 - 12:43 AM
User is offlineProfile CardPM
+Quote Post

nithesh
RE: Bubblet Game Flood Method
25 Sep, 2008 - 12:42 PM
Post #4

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 1

QUOTE(eskimo9 @ 25 Sep, 2008 - 01:28 AM) *

Right. I've been at it for hours and hours, and I'm nearly (I think) there. I'll give you the group class, the class that finds all the same-coloured bubbles adjacent to your selected one, but the class to create a random grid is easy, so you can work it out.
CODE


/**
* Group is a group of bubbles from grid with same colour, and connected to the clicked
* bubble at row,col.
*
* @author Chris Turner  
* @version 26.09.2008
*/
public class Group
{
    private Grid grid;
    private int nRows;
    private int nCols;
    private boolean[][] seen;
    int color;
    int size=0;
    
    /*
     * Constructor method Group creates a new object of Group, calls the grid object,
     * sets seen as a new 2D boolean array set to false.
     *
     * @param grid, an object of type Grid, int row, int col, the starting position of
     * Group
     */
    public Group(Grid grid, int row, int col)
    {
        this.grid = grid;
        color = grid.get(row,col);
        nRows = grid.nRows();
        nCols = grid.nCols();
        
        seen = new boolean[nRows][nCols];
        for(int i=0; i<nRows; i++)
        {
            for(int j=0; j<nCols; j++)
            {
                seen[i][j] = false;
            }
        }
    }
    
    /*
     * Acessor method size returns value in the size field
     * @return int size
     */
    public int size()
    {
        return size;
    }
    
    /*
     * Accessor method score returns the value in the score field
     * @return int score
     */
    
    public int score()
    {
        int score = size*(size-1);
        return score;
    }
    
    
    /*
     * Accessor method contains returns value in the seen field at position [row][col]
     * @param int row, int col
     * @return boolean seen
     */
    public boolean contains(int row, int col)
    {
                  
        return seen[row][col];
        
    }
    
    /*
     * Mutator method flood determines which bubbles are in this group and returns the
     * size of the group. Starter point for the group is at row,col
     * @param int row, int col
     * @return int size
     */
    private int flood(int row, int col)
    {
        
        //Search whole array, find where color = group color and neighbour is in seen
        seen[row][col]=true;
        for(int i=0; i<nRows; i++)
        {
            for(int j=0; j<nCols; j++)
            {
                if(grid.get(i,j)==color)
                {
                    if(seen[i-1][j] == true || seen[i+1][j]==true || seen[i][j-1]==true || seen[i][j+1]==true)
                    {
                        seen[i][j]=true;
                        size++;
                    }
                }
            }
        }
        
        return size;
    }
    
    
}

It's all good except for the flood class. I've been told by the tutor that that is the correct idea, to find same color, and test if it has neighbours in the group. It just doesnt work as yet. When I click on a bubble, I get no response, which is different to before when I was getting every bubble with the right colour being returned. Any ideas why?


User is offlineProfile CardPM
+Quote Post

cookie_jam
RE: Bubblet Game Flood Method
11 Oct, 2008 - 04:42 AM
Post #5

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 2

eskimo u are the dumbest person alive, i'm pretty sure aiaophi only wants to see your solution and copy it for the same project that you're doing. he said "but post up the rest of your solutions" <-- how does he know that you got the rest of the solutions? And also, he's just joined dream.in.code.net recently and this is his/her first post, you really think someone would join this forum JUST TO HELP YOU with your project huh? use your brain a bit more coz it helps. do ur coding byself and listen to the lecturer u idiot

This post has been edited by cookie_jam: 11 Oct, 2008 - 04:43 AM
User is offlineProfile CardPM
+Quote Post

cookie_jam
RE: Bubblet Game Flood Method
12 Oct, 2008 - 12:29 AM
Post #6

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 2

where's jonald
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 03:50PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month