📄 grid.java
字号:
import java.util.Random;
/**
*
* Grid class represents a grid of balloons for bubblet.
* This class keeps track of which position has bubble of a particular color.
*/
public class Grid {
private int[][] grid = null; // rows in grid are displayed vertically on the GUI
private int[] length = null; // keeps track of the number of bubblets in each row
private int nColors; // number of color in use
private int nRows; // number of rows in grid
private int nCols; // number of cols in grid
public static final int VACANT = -1; // status of vacant position
/**
* Grid Constructor
*/
public Grid(int nR, int nC, int nColor)
{
nRows = nR;
nCols = nC;
this.nColors = nColor;
length = new int[nR];
for(int i=0; i<length.length; i++)
{
length[i] = nC;
}//End for each
grid = new int[nR][nC];
for(int r=0; r<nR; r++)
{
for(int c=0; c<nC; c++)
{
grid[r][c]= VACANT;
}//End for c
}//End for r
}//End Grid Constructor
/**
*
* @return number of rows in grid
*/
public int nrows()
{
return nRows;
}//End nrows
/**
*
* @return number of cols in each row
*/
public int ncols()
{
return nCols;
}//End ncols method
/**
* set the number of columns in use to val for row row
* @param row
* @param val
*/
public void setLength(int row, int val)
{
// number of bubblet in length[row] is val
length[row] = val;
return;
}//End setLength method
/**
* return grid[row]
* @param row
* @return
*/
public int[] get(int row)
{
return grid[row];
}//End get method
/**
* fill every position in grid with a random value between 0 and nColors -1
*/
public void fill()
{
Random
rand = new Random();
for(int r=0; r<nRows; r++)
{
for(int c=0; c<nCols; c++)
{
grid[r][c]= rand.nextInt(this.nColors) ;
}//End for c
}//End for r
}//End fill method
/**
* remove the bubble at row row and col col.
*
* @param row
* @param col
*/
public void remove(int r, int c)
{
// if length[r] == 0
// no remove
// else
// remove evey element at [r][c],
// move all elements in row r, starting from c+1,
// ending at length[row]-1 toward 0 position
// set last position [r][length[row]-1] to 0
// length[r] --
if(length[r] != 0 )
{
System.out.println("length "+ r + " : " + length[r]);
for(int i=c; i<length[r]-1; i++)
{
grid[r][i] = grid[r][i+1];
}//End for
grid[r][length[r]-1]= VACANT;
length[r]--;
}
}//End remove method
/*
* set row to to row from
*/
public void set(int to, int from)
{
grid[to] = grid[from];
length[to]=length[from];
return;
}
/**
* Testing purpose, set grid[row][col] to v
* @param row
* @param col
* @param v
*/
public void set(int row, int col, int v)
{
grid[row][col] = v;
}
/**
* return number of bubbles in row r
* @param row
* @return
*/
public int length(int r)
{
return length[r];
}//End length method
/**
* get grid[row][col] value
* @param row
* @param col
* @return
*/
public int get(int row, int col)
{
return grid[row][col];
}
/**
* print the color of all existing bubbles as text
*
*/
public void display()
{
System.out.print(" ");
for(int k=0; k<nCols; k++)
{
System.out.print(k+" ");
}//End for k
System.out.println("");
for(int i=0; i<nRows; i++)
{
System.out.print(i+": ");
for(int j=0; j<nCols; j++)
{
if(grid[i][j]!= VACANT)
{
System.out.print(grid[i][j]+" ");
}
else
{
System.out.print(" ");
}
}//End for
System.out.print("\n");
}//End for
return;
}//End display method
/**
* @param args
*/
public static void main(String[] args)
{
Grid
g = new Grid(9,9,3);
//g.display();
g.fill();
g.display();
System.out.println("\n\n\n");
g.remove(3,3);
g.display();
}
}//End Grid Class definition
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -