我目前正在开发一个扫雷程序,我需要一些帮助来揭示其中的邻居。目前我的程序可以做的揭示如下
1 将是被选中的按钮,线条是我需要填写的内容。目前,所选按钮周围的按钮是我可以填写的。
如有必要,我可以发布代码。
我在这里先向您的帮助表示感谢。
1 是地雷,4 是阵列上的标记点
public int findneighbors(int row, int col) {
int count = 0;
if (board[row][col] == 2)
try {
if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col + 1] == 1 || board[row + 1][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col - 1] == 1 || board[row + 1][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col - 1] == 1 || board[row - 1][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col] == 1 || board[row + 1][col] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col] == 1 || board[row - 1][col] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row][col - 1] == 1 || board[row][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col + 1] == 1 || board[row - 1][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
return count;
}
public int buttonFloodFill(int r, int c)
{
int loopCount = 0;
int rowCount = 1;
int colCount = 1;
while (loopCount < 1)
{
try {
if (g.getFloodValue(r,c + colCount) == true) {
board[r][c + colCount].setText(Integer.toString(g.findneighbors(r,c + colCount)));
board[r][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r,c - colCount) == true) {
board[r][c - colCount].setText(Integer.toString(g.findneighbors(r,c - colCount)));
board[r][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c + colCount) == true) {
board[r + rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c + colCount)));
board[r + rowCount][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c - colCount) == true) {
board[r + rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c - colCount)));
board[r + rowCount][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c - colCount) == true) {
board[r - rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c - colCount)));
board[r - rowCount][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c + colCount) == true) {
board[r - rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c + colCount)));
board[r - rowCount][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c) == true) {
board[r - rowCount][c].setText(Integer.toString(g.findneighbors(r - rowCount,c)));
board[r - rowCount][c].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c) == true) {
board[r + rowCount][c].setText(Integer.toString(g.findneighbors(r+ rowCount,c)));
board[r + rowCount][c].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
rowCount ++;
colCount ++;
loopCount ++;
}
return 0;
}