Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,009 people online right now. Registration is fast and FREE... Join Now!




Need Help with Grid Class (bubblet games)

 
Reply to this topicStart new topic

Need Help with Grid Class (bubblet games)

kamiz
22 Sep, 2008 - 04:00 AM
Post #1

New D.I.C Head
*

Joined: 22 Sep, 2008
Posts: 2

hey there

i'm new to programming....i'm jux wondering if u guy could help me out..........this is what i did so far. but not sure if it correct.

this is my task.......

The Grid Class
This class represents a grid of balloons for bubblet. This grid keeps track of which position has bubble of a particular color. Grid has the following private fields:
• grid – which is a 2 dimensional array of int. The int value indicates the color of the bubble in that position. Note, rows in grid are displayed vertically on the graphical user interface.
• length – is an array of int. This array keeps track of the number of bubbles in each row.
• nColors – is an int. This field indicate the number of colors in use.
• nRows – is an int, which stores the number of rows in grid.
• nCols – is an int, which stores the number of columns in grid.


Grid has a constructor which takes in 3 parameters: nrow, ncol, and ncolor, of type int.
nrow and ncol are the number of rows and columns in grid, and will need to be assigned to nRows and nCols respectively. ncolor is the number of colors in use, and will need to be assigned to nColors. The length field will be assigned a new int array with nRows elements. Assign each element in length to nCols. The grid field will be assigned a new 2 dimensional int array with nRows rows and nCols columns. Assign each element in grid to 0.

Grid has the following public instance methods:
• nrows() – which returns the value in nRows field.
• ncols() – which returns the value in nCols field.
• length(int row) – return the number of bubbles in row row.
• setLength(int row, int val) – set the number of columns in use to val for row row.

• get(int row) – returns row row of grid.
• get(int row, int col) – returns the value in row, col of grid.
• set(int to, int from) – set row to to row from.
• fill() – fills every position in grid with a random value between 0 and nColors -1 (inclusive of 0 and nColors-1).
• remove(int row, int col) – removes the bubble at row row and column col. To remove a bubble, simply move the bubbles behind the bubble we want to remove at row row forward (towards 0) by one position. Then reduce the position row of length by 1. If a row has no bubble left, length of that row will be 0, however, that row will still remain in grid (the GUI class will skip that row when it sees the length is 0).

the sentence highlight in red is what the stuff i have done..........

here is my code;

CODE
public class Grid
{
  private int grid [][];
  private int length [][];
  private int nColors;
  private int nRows;
  private int nCols;
    
{

public Grid ( int nrow, int ncol, int ncolor);



public void nrows(int nRows);
{
              return value [nRows];
}

public void ncols(int nCols)
{
                return value [nCols];


            }
                
public void length (int row);
  {
                    return row [row];
                }
                
public  void setLength (int row, int val)
                {
                    stack=new stack ();
                    row= Row-1;
                    val= Val-1;
                    myArr= new myarr[Row,Val];
                }
                
                
public void get (int row){
    
                    



hope u guy can help me out.... or show me an example how it work.........


thank

This post has been edited by kamiz: 22 Sep, 2008 - 04:01 AM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Need Help With Grid Class (bubblet Games)
23 Sep, 2008 - 06:36 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 428



Thanked: 4 times
Dream Kudos: 150
My Contributions
I've had a go at this for you, but some of the instructions are a bit confusing, so hopefully you can use my partial solution to help you.

CODE
import java.util.Random;

public class Grid
{
  private int grid [][];
  private int length [];
  private int nColors;
  private int nRows;
  private int nCols;
    


public Grid ( int nrow, int ncol, int ncolor)
{
    
    nRows = nrow;
    nCols = ncol;
    grid = new int[nRows][nCols];
    for (int row = 0; row < grid.length; row++){
      for (int column = 0; column < grid[row].length; column++)
      grid[row][column] = 0;
      }
      length = new int[nRows];
      for (int i = 0; i < nRows; i++)
      length[i] = nCols;
      
    nColors = ncolor;
}


public int nrows()
{
              return nRows;
}

public int ncols()
{
                return nCols;


            }
                
public int length (int row)
  {
                  int bubbles = 0;
                  for (int i = 0; i < nCols; i++)
                  {
                      if (grid[row][i] != 0)
                      bubbles++;
                  }
                  return bubbles;
                  
  }
                
public  void setLength (int row, int val)
                {
                    length[row] = val;
                }
                
                
public int get (int row, int col){
    
    return grid[row][col];
    
     }
    
public void set(int row, int col, int colorVal)
{
    grid[row][col] = colorVal;
}
public void fill()
{
    Random gen = new Random();
    
    for (int row = 0; row < grid.length; row++){
      for (int column = 0; column < grid[row].length; column++)
      grid[row][column] = gen.nextInt(nColors);
      }
}
     public void printGrid()
     {
         for (int row = 0; row < grid.length; row++){
             System.out.println();
      for (int column = 0; column < grid[row].length; column++)
      System.out.print(grid[row][column]);
      }
     }
    
     public static void main (String args[]){
         Grid bubbleGrid = new Grid(6, 5, 3);
         bubbleGrid.printGrid();
         System.out.println();
         bubbleGrid.fill();
         bubbleGrid.printGrid();
         
     }
}


I've put a tester main method on the end because I think it's important to check what you are doing (in this case with printing to the command line).

If you have any questions, just post here again.
User is offlineProfile CardPM
+Quote Post

kamiz
RE: Need Help With Grid Class (bubblet Games)
24 Sep, 2008 - 04:52 AM
Post #3

New D.I.C Head
*

Joined: 22 Sep, 2008
Posts: 2

thank alot for the help...........but some of the method were incorrect and were missing but i manage to fix some.

i'm jux wonder if u could help out on the remove method... and the display method and the group class.
which i was very confuse.

task.

method

remove(int row, int col) – removes the bubble at row row and column col. To remove a bubble, simply move the bubbles behind the bubble we want to remove at row row forward (towards 0) by one position. Then reduce the position row of length by 1. If a row has no bubble left, length of that row will be 0, however, that row will still remain in grid (the GUI class will skip that row when it sees the length is 0).

display() – print (the color of) bubbles in every row with their row numbers, excluding empty rows, to standard output. An example is shown below.

0: 2 0 0 4 2
1: 2 1 3 2 3 0 4
2: 2 0 4 1 1 2 0
3: 4 4 0 2 3 4
4: 1 1 0 1 2 4
5: 2 0 0 1
6: 1 0
7: 0 3 1 0 3
8: 4 2 1 1 2 0 3

my display method;

CODE
public void display()
    {
        System.out.print("   ");
        for (int k =0; k < board.length; k++)
        {
            System.out.print(k + " ");
        }
        System.out.println();
        for (int i = 0; i < board.length; i++)
        {
            System.out.print(i + ": ");
            for (int j = 0; j < board[i].length; j++)
            {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }    
    }    


The Group class

instruction

The Group class
This class represents a group of connected same colored bubbles. When user clicks on a bubble, a Group object is created containing that bubble and connected bubbles of the same color. This class has the following private fields:
grid – of type Grid. The grid object for this game.
nRows – is an int, which stores the number of rows in grid.
nCols – is an int, which stores the number of columns in grid.
seen – is a 2 dimensional boolean array. This 2D array indicates which bubbles are in this group. If row 1, column 3 in this array is set to true, then the bubble at that position is in this group.
color – is an int, which is the color of this group.
size – is an int, which is the number of bubbles in this group.

Group has one constructor. This constructor takes 3 parameters. The first parameter is a grid object, which will be assigned to the grid field. The second and third parameters are of type int, together these two values define the starting position, row and column, in grid to search for the bubbles in this group. Furthermore, the color of the bubble in that position is the color that is assigned to the color field. The fields nRows and nCols will be assigned the number of rows and columns of grid. A new 2 dimensional boolean array, with the same number of row and column as grid, will be created and assigned to the field seen. All elements in seen will then be initialised to false. The flood method will be called, and return result assigned to the field size.

Group has the following instance methods:
size() – which returns the size of this group.

score() – which returns an int value which is the score for this group. The scored is calculated by: size * (size -1).
contains(int row, int col) – given a position in grid defined by row and col, this method returns true if the position is in this group, otherwise return false.
flood(int row, int col) – determines the bubbles in this group given the starting position row, col. To indicate the bubbles in this group, the positions of the bubbles in this group will be set to true in seen. This method returns the number of bubbles in this group. As this method is only used within this class, the access modifier for this method is private.

highlight in red is what i started.


my code for group class;
CODE
public class Group  
{
  private grid;
  private int nRows;
  private int nCols;
  private seen [][];
  private int color;
  private int size;
}

public Group ( grid, int nRows, int nCols)
{
    grid=grid;
        color=color;
        grid = new int [nRows][nCols]
        =
        
        
        public int size() {
            
          {
            
        public int score(){

{



This post has been edited by kamiz: 24 Sep, 2008 - 05:03 AM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Need Help With Grid Class (bubblet Games)
26 Sep, 2008 - 01:01 AM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 428



Thanked: 4 times
Dream Kudos: 150
My Contributions
OK, here's the first part, remove() and display() methods.

CODE
import java.util.Random;

public class Grid
{
  private int grid [][];
  private int length [];
  private int nColors;
  private int nRows;
  private int nCols;
    


public Grid ( int nrow, int ncol, int ncolor)
{
    
    nRows = nrow;
    nCols = ncol;
    grid = new int[nRows][nCols];
    for (int row = 0; row < grid.length; row++){
      for (int column = 0; column < grid[row].length; column++)
      grid[row][column] = 0;
      }
      length = new int[nRows];
      for (int i = 0; i < nRows; i++)
      length[i] = nCols;
      
    nColors = ncolor;
}


public int nrows()
{
              return nRows;
}

public int ncols()
{
                return nCols;


            }
                
public int length (int row)
  {
                  int bubbles = 0;
                  for (int i = 0; i < nCols; i++)
                  {
                      if (grid[row][i] != 0)
                      bubbles++;
                  }
                  return bubbles;
                  
  }
                
public  void setLength (int row, int val)
                {
                    length[row] = val;
                }
                
                
public int get (int row, int col){
    
    return grid[row][col];
    
     }
    
public void set(int row, int col, int colorVal)
{
    grid[row][col] = colorVal;
}
public void fill()
{
    Random gen = new Random();
    
    for (int row = 0; row < grid.length; row++){
      for (int column = 0; column < grid[row].length; column++)
      grid[row][column] = gen.nextInt(nColors);
      }
}
     public void printGrid()
     {
         for (int row = 0; row < grid.length; row++){
             System.out.println();
      for (int column = 0; column < grid[row].length; column++)
      System.out.print(grid[row][column]);
      }
     }
    
     public void display()
    {
        System.out.print("   ");
        for (int k =0; k < grid.length-1; k++)
        {
            System.out.print(k + " ");
        }
        System.out.println();
        for (int i = 0; i < grid.length; i++)
        {
            System.out.print(i + ": ");
            for (int j = 0; j < length[i]; j++)
            {
                System.out.print(grid[i][j] + " ");
            }
            System.out.println();
        }    
    }
    public void remove(int row, int col)
    {
        for (int j = 0; j < length[row]-1; j++)
        {
            if (j >= col)
            {
                grid[row][j] = grid[row][j+1];
            }
            
        }
        length[row] --;
    }
    
     public static void main (String args[]){
         Grid bubbleGrid = new Grid(6, 5, 3);
         bubbleGrid.printGrid();
         System.out.println();
         bubbleGrid.fill();
         bubbleGrid.printGrid();
         System.out.println();
         bubbleGrid.display();
         System.out.println();
         bubbleGrid.remove(1, 1);
         bubbleGrid.display();
         System.out.println();
         bubbleGrid.remove(5, 5);
         bubbleGrid.display();
         bubbleGrid.remove(5, 2);
         bubbleGrid.display();
     }
}


I'm not sure I understand the point of the second class. How do you determine connected bubbles? What directions can they be connected in?

This post has been edited by Ellie: 26 Sep, 2008 - 01:06 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:50AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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