我有一个java程序,我有点困难,非常感谢帮助。
-addMine方法(如果输入参数位于雷区的维度之外,则该方法不应更改类的状态,并返回false。)
-addMine方法(如果输入参数指示的方块上已有一个矿,那么该方法不应该改变类的状态,并返回false。)
-getValue方法(应该返回所请求的行和列的雷区的值。(此时,雷区中的值是MINE_SQUARE或EMPTY_SQUARE)。如果行和列位于雷区之外,则方法应该返回0。)
-addMinesToCorners(应该按照它说的做,增加4个地雷)
class MineFinderModel
{
public static int MINE_SQUARE = 10;
public static int EMPTY_SQUARE = 0;
int num_of_cols;
int num_of_rows;
int[][] the_minefield;
public MineFinderModel(int n_cols, int n_rows)
{
num_of_rows = n_rows;
num_of_cols = n_cols;
the_minefield = new int[num_of_cols][num_of_rows];
}
public boolean addMine(int thisCol, int thisRow)
{
return false; // but you sometimes need to return true;
}
public int getValue(int thisCol, int thisRow)
{
return 0; // but you need to return other numbers at times;
}
public void addMinesToCorners()
{
}
}
答案 0 :(得分:1)
public boolean addMine(int thisCol, int thisRow)
{
if(thisCol >= n_cols || thisRow >= n_rows)
return false;
if(the_minefield[thisCol][thisRow] == MINE_SQUARE)
return false;
the_minefield[thisCol][thisRow] = MINE_SQUARE;
return true;
}
public int getValue(int thisCol, int thisRow)
{
if(thisCol >= n_cols || thisRow >= n_rows)
return 0;
return the_minefield[thisCol][thisRow];
}
public void addMinesToCorners()
{
the_minefield[0][0] = MINE_SQUARE;
the_minefield[0][n_rows - 1] = MINE_SQUARE;
the_minefield[n_cols - 1][0] = MINE_SQUARE;
the_minefield[n_cols - 1][n_rows - 1] = MINE_SQUARE;
}
答案 1 :(得分:0)
public void addMinesToCorners()
{
/*Adding mines to four corners*/
the_minefield[0]0] = "Corner Mine1"
the_minefield[0][num_of_cols -1] = "Corner Mine2"
the_minefield[num_of_rows-1][0] = "Corner Mine2"
the_minefield[num_of_rows -1][num_of_cols -1] = "Corner Mine2"
}