📄 group.java
字号:
/**
*
* 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.
*
*
*/
public class Group
{
private Grid grid; // The grid object for this game
private int nRows; // number of rows in grid
private int nCols; // number of cols in grid
boolean[][] seen; // indicates which bubbles are in this group
int color; // cooor this group
int size; // size of this group
public static final int TOP = 0;
public static final int BOT = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
public static final int NUM_LOCS = 4;
boolean[] locFlags;
int[] locations;
int currentRow =-1,
currentCol = -1;
/**
* Constructor to initialzie all instacne variables
* @param g
* @param sRow
* @param sCol
*/
public Group(Grid g, // grid for this game
int sRow, // start bubble row position
int sCol) // start bubble col position
{
grid = g;
nRows = g.nrows();
nCols = g.ncols();
color = g.get(sRow, sCol);
seen = new boolean[nRows][nCols];
for(int i=0; i<nRows; i++)
{
for(int j=0; j<nCols; j++)
{
seen[i][j]=false;
}//End for
}//End for i
size = flood(sRow, sCol);
System.out.println("size : " + size);
locFlags = new boolean[NUM_LOCS];
locations = new int[NUM_LOCS];
for(int i=0;i<NUM_LOCS; i++)
{
locFlags[i]=false;
locations[i]= i;
}//End for
return;
}//End Group constructor method
/**
* return size of bubbles
* @return
*/
public int size()
{
return size;
}//End size method
/**
* return the score for this group
* @return
*/
public int score()
{
return size*(size-1);
}//End score method
/**
* return true if this position is in this group, otherwise false
* @param row
* @param col
* @return
*/
public boolean contains(int row, int col)
{
if(row>=0 && row<nRows && col>=0 && col<nCols)
{
return seen[row][col];
}
else
{
return false;
}
}//End contains method
/**
* determines the bubbles in this group given the starting
* postion
* @param row
* @param col
* @return
*/
private int flood(int row, int col)
{
int
count = 0;
count = visit(row, col,-1);
return count;
}//End flood method
/**
* Recursively visit neighbouring pos around row and col
* except prevLoc
* @param row
* @param col
* @param prevLoc
* @return
*/
private int visit(int row, int col, int prevLoc)
{
int
sum = 0;
int
skipLoc;
System.out.print("new Row : " + row + "\t");
System.out.print("new Col : " + col + "\n");
// find skip location
switch(prevLoc)
{
case TOP: skipLoc = BOT; break;
case BOT: skipLoc = TOP; break;
case LEFT: skipLoc = RIGHT; break;
case RIGHT: skipLoc = LEFT; break;
default: skipLoc = -1; break;
}
// check row, col is within boundary
if( row<nRows && row >= 0 &&
col<nCols && col >= 0
&& seen[row][col] == false // has not seen before
&& grid.get(row, col) == this.color // and they have same color
)
{
seen[row][col] = true;
sum++;
System.out.println("skip loc: " + skipLoc);
//loop through every neighbouring position except preLoc
for(int i=0; i<NUM_LOCS; i++)
{
// get updated row and col based on i
int
newRow = -1,
newCol = -1;
switch(i)
{
case TOP: newRow = row-1; newCol= col; break;
case BOT: newRow = row+1; newCol= col; break;
case LEFT: newRow = row; newCol= col-1;break;
case RIGHT: newRow = row; newCol= col+1; break;
default: newRow = -1; newCol= -1;; break;
}
// recursively visit every direction except skip direction
if(i!=skipLoc)
{
sum = sum + visit(newRow, newCol, i);
}
}//End for
}//End if
return sum;
}//End
/**
* @main method
*/
public static void main(String[] args)
{
/**
* Create a grid
*/
Grid
g = new Grid(9,9,3);
g.fill();
g.set(2,2,0);
g.set(2,3,0);
g.set(2,4,0);
g.set(2,5,0);
g.set(3,2,0);
g.set(3,3,1);
g.set(3,4,1);
g.set(3,5,0);
g.set(4,2,0);
g.set(4,3,1);
g.set(4,4,1);
g.set(4,5,0);
g.set(5,2,0);
g.set(5,3,0);
g.set(5,4,0);
g.set(5,5,0);
g.display();
System.out.println("\n\n\n");
Group
gp = new Group(g, // grid for this game
3, // start bubble row position
3); // start bubble col position
System.out.println("size of group: " + gp.size());
/**
*
*/
//g.display();
return;
}//End main
}//End Group Class defintion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -