- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个扫雷程序,我正在尝试编写代码来显示相邻网格中有多少地雷,但是我收到一条错误消息,指出需要一个类,我不确定为什么。我的假设是因为这两种方法都在MSgrid方法中,所以没问题。我已将出错的行注释为 ERROR HERE。这是我的代码:
/**
* Represents a single square in Minesweeper.
*
* @author Sophia Ali
* June 17, 2012
*/
public class MineSquare
{
// Fields:
/**
* initialize variables
*/
private String _shown; // What a square is showing now
private boolean _mined; // Square is mined or not
private boolean _flagged; // Square is flagged or not
private boolean _questioned; // Square is question marked or not
private int _minecount; // Square's surrounding mine count
private boolean _opened; // Player has opened this square or not
// Constructors & Methods:
/**
* Default constructor
* Sets _mined and _opened to false.
*/
public MineSquare()
{
_mined = false;
_opened = false;
setShown(" ");
}
/**
* Returns flagged status of square.
* @return _flagged Flagged status
*/
public boolean isFlagged() {
return _flagged;
}
/**
* Sets or unsets flag on a square.
* @param flagged True or false (square is flagged or not)
*/
public void setFlagged(boolean flagged, boolean opened) {
_flagged = flagged;
_opened = opened;
/*
// If Minesquare opened do nothing:
if (opened == true)
setShown(" ");*/
if ( isOpened() == false )
{
// If flagged, square should show "F":
if ( isFlagged() == true )
setShown("F");
else
setShown(" ");
}
/* else
{} not needed but shows it does nothing*/
}
/**
* Returns _minecount amount.
* @return _minecount
*/
public int getMinecount() {
return _minecount;
}
/**
* Checks minecount
* If minecount between 0 and 8 _minecount not set and error message outputs.
*/
public void setMinecount(int minecount) {
if(minecount >=0 && minecount <= 8)
_minecount = minecount;
else //invalid minecount
System.out.println("Invalid minecount in setMinecount: " +minecount);
/*
if ( minecount > 0 && minecount < 8 )
{
getMinecount();
}
else System.out.println("Error :" + minecount);
*/
}
/**
* Returns mined status of square.
* @return _mined Mined status
*/
public boolean isMined() {
return _mined;
}
/**
* Sets or unsets mine on a square.
* @param mined True or false (square is mined or not)
*/
public void setMined(boolean mined) {
_mined = mined;
// If mine, square should show "F":
if ( isMined() == true )
setShown("F");
else
setShown(" ");
}
/**
* Returns opened status of square.
* @return _opened Opened status
*/
public boolean isOpened() {
return _opened;
}
/**
* Open a square.
* (Once opened, a square can't be unopened.)
*/
public void setOpened() {
_opened = true;
if ( isMined() == true )
setShown("X");
else if ( getMinecount() > 0 )
setShown(_minecount + "");
else // blank space for _minecount = 0
setShown(" ");
}
/**
* Returns openequestion status of square.
* @return _questioned Questioned status
*/
public boolean isQuestioned() {
return _questioned;
}
/**
* Sets or unsets question on a square.
* @param questioned True or false (square is questioned or not)
*/
public void setQuestioned(boolean questioned, boolean opened) {
_questioned = questioned;
_opened = opened;
// If Minesquare opened do nothing:
/*if (opened == true)
setShown(" ");
// If Questioned, square should show "F":
if ( isQuestioned() == true )
setShown("F");
else
setShown(" ");
*/
if ( isOpened() == false )
{
// If flagged, square should show "F":
if ( isQuestioned() == true )
setShown("?");
else
setShown(" ");
}
/* else
{} not needed but shows it does nothing*/
}
/**
* Returns what is in getShown.
* @return _shown
*/
public String getShown() {
return _shown;
}
/**
* Increment _minecount by 1.
* Calls setMinecount() to make sure that an illegal value (>8) is
* not assigned to _minecount.
*/
public void increMinecount()
{
int mc = _minecount;
mc++;
setMinecount(mc);
}
/**
* Checks shown
* If _shown is one of legal values prints _shown if not prints error.
*/
public void setShown(String shown) {
if ( shown.equals ("X") || shown.equals("F") || shown.equals("?") ||
shown.equals (" ") || shown.equals ("1") || shown.equals ("2") || shown.equals ("3") ||
shown.equals ("4") || shown.equals ("5") || shown.equals ("6") || shown.equals ("7") ||
shown.equals ("8") )
_shown = shown;
else //invalid value of shown
System.out.println("Invalid value of shown in setShown: " + shown);
}
}
/**
* MSGrid class that contains a 2-D array of MineSquare objects
*
* @author J. Chung
* @version CS-501B
*/
public class MSGrid
{
// instance variables - replace the example below with your own
// 2-D array of MineSquare objects:
private final int ROWS = 20;
private final int COLS = 20;
private MineSquare [][] grid = new MineSquare[ROWS][COLS];
// Actual size of grid that we use in rows and cols:
private int rows = 9;
private int cols = 9;
// Number of mines that go in grid:
private int mines = 10;
/**
* Constructor for objects of class MSGrid
*/
public MSGrid()
{
// initialise the grid of MineSquare objects:
// (construct individual MineSquare objects within grid array)
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
grid[r][c] = new MineSquare();
}
}
}
/*
* MSGrid methods:
*
* - Set mines
* - Compute and set minecounts
*/
/**
* Set some number of mines at random within the grid.
*/
public void setMines() //scattering mines
{
// Choose random row, choose random col, place mine there:
for ( int i = 1; i <= mines; i++ )
{
int randomrow = randbetween( 1, rows );
int randomcol = randbetween( 1, cols );
// If square is already mined, do it again:
while ( grid[randomrow][randomcol].isMined() == true )
{
randomrow = randbetween( 1, rows );
randomcol = randbetween( 1, cols );
}
grid[randomrow][randomcol].setMined(true);
}
}
/*
* Compute and set square minecounts.
*/
public void setMinecounts()
{
// Approach #1: Visit each square in grid; examine all adjacent
// squares; for each mine found, increment minecount
// by 1.
// Approach #2: Visit each mined square in grid; increment minecount
// of all adjacent squares by 1. plus one for all neighbors of mined grid (even if it already +1) - easier way to do it
//**do nested for loop to access every square in grid
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
// if current square at r,c has a mine:
if ( grid[r][c].isMined() == true )
{
if (grid[r][c].isValidSquare(int rr, int cc) == true) //***ERROR HERE***
{
grid[r-1][c-1].increMinecount();
grid[r][c-1].increMinecount();
grid[r+1][c-1].increMinecount();
grid[r+1][c].increMinecount();
grid[r-1][c].increMinecount();
grid[r+1][c+1].increMinecount();
grid[r][c+1].increMinecount();
grid[r-1][c+1].increMinecount();
}
}
}
}
}
// Note: In both approaches, must exclude squares that are not in grid.
// (Must use the isValidSquare() method below.)
/*
* See if a square at some row, col is within the grid.
*
* @param rr row of square in question
* @param cc col of square in question
* @return True if square is in grid, false if square not in grid
*/
private boolean isValidSquare( int rr, int cc )
{
if ( rr >= 1 && rr <= rows && cc >= 1 && cc <= cols )
return true;
else
return false;
}
/**
* Show the grid, for testing purposes only.
*/
public void showMSGrid()
{
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
// Call a MineSquare method:
int mc = grid[r][c].getMinecount();
// Show a mine or a minecount number:
if ( grid[r][c].isMined() == true )
System.out.print(" " + "X" );
else
System.out.print(" " + mc);
} // end of column
System.out.println(); // line break
} // end of row
}
/**
* randbetween: Return a random integer between low and high values
*
* @param: low - low value
* @param: high - high value
* @return: random integer b/w low and high
*/
private int randbetween( int low, int high ) {
// Make sure that low and high values are in correct positions:
// If low > high, swap low and high.
if ( low > high ) {
int temp = low;
low = high;
high = temp;
}
int scale = high - low + 1;
int shift = low;
int randnum = (int)(Math.random() * scale) + shift;
return randnum;
}
/*first setmines
then setminecount
then showmsgrid
*/
}
最佳答案
我相信你想要
if(isValidSquare(r, c))
而不是
if (grid[r][c].isValidSquare(int rr, int cc) == true)
MSGrid
而不是 MineSquare
定义的,但 grid[r][c] 是一个 MineSquare
int
是无效的(啊,讽刺)代码。== true
这里是可选的。您现在得到 NPE 是因为假设 if (isValidSquare(int rr, int cc) )
,它有 8 个有效邻居。这是错误的,因为如果这个正方形位于网格的边缘 ( r == 0 || c == 0 || r == rows-1 || c == cols-1)
,那么它可能没有很多邻居
在增加地雷数量之前,您需要检查每个条件。例如
if( r > 0){
grid[r-1][c+1].increMinecount();
grid[r-1][c].increMinecount();
grid[r-1][c-1].increMinecount();
}
请注意,这只是一个示例,您还需要同时检查其中的 c
。或者您可以使用函数 UpdateMineCount(r,c)
,它首先调用 isValidSquare
,然后才调用 increMinecount
。
public void IncrementMineCount(MineSquare ms, int r, int c){
if(isValidSquare(r,c)){
ms.increMineCount();
}
}
现在用 IncrementMineCount(grid[r-1][c-1],r,c) 替换
grid[r-1][c-1].increMinecount();
关于java - 扫雷舰增加邻近地雷网格的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332963/
我需要从 1024 增加 FD_SETSIZE 值至 4096 .我知道最好使用 poll()/epoll()但我想了解什么是优点/缺点。主要问题是:我要重新编译glibc吗? ?我读了几个线程,其中
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在 HTML 文件中有这样的内容: var value = 0; add(x){ x++; do
有没有办法在用户向上滚动时增加变量,并在用户使用 JavaScript 向下滚动时减少变量?变量没有最大值或最小值,如果能够调整灵敏度就好了。我不知道从哪里开始,感谢您的帮助! 编辑:没有滚动条,因为
我是 ios 新手,遇到以下问题。 我想根据表格 View 中元素的数量增加和减少表格 View 的高度大小。如果在输入时客户端在输出时给出 3 个或超过 3 个元素,我希望看到一个比默认行大 2 行
所以我一直在四处搜索,似乎大多数人认为以下列方式递增 indexPath 是正确的方法: NSIndexPath *newIndexPath = [NSIndexPath indexPathForRo
我有一个关于 connSupervisionTimeout 的问题。 我正在使用 CoreBluetooth 编写应用程序。我检查了连接参数和 connSupervisionTimeout = 720
我正在尝试根据页面的滚动位置更改元素的填充;当用户向下滚动页面时,填充会增加,而当他们向上滚动时,填充会减少。 我的主要问题是滚动不是很流畅,有时如果我滚动到页面顶部太快,每次元素的填充大小都不一样。
我正在尝试计算 18456 个基因的相关性度量,但编译器 (Dev C) 在将宏 GENE 或 INDEX 增加到 4000 到 5000 之间的值后退出或大。例如,它适用于: # define GE
我有一个带有 position: absolute 和 CSS3 过渡的圆形元素(a 元素)。在 hover 事件中,我想增加圆的高度和宽度,但我想在所有边上添加像素,而不仅仅是在左侧或右侧。 示例如
为了改善用户体验,我计划在我网站的所有页面(A-、A、A+)上增加/减少/重置字体大小 我面临的问题是页面上不同元素使用的字体大小不统一。有些是 14px,有些是 18px,有些是 12px,有些是
本文实例讲述了Yii框架数据库查询、增加、删除操作。分享给大家供大家参考,具体如下: Yii 数据库查询 模型代码: ?
sql替换语句,用该命令可以整批替换某字段的内容,也可以批量在原字段内容上加上或去掉字符。 命令总解:update 表的名称 set 此表要替换的字段名=REPLACE(此表要替换的字段名, '原
sql不常用函数总结以及事务,增加,删除触发器 distinct 删除重复行 declare @x 申明一个变量 convert(varchar(20),t
要增加我使用的最大可用内存: export SPARK_MEM=1 g 或者我可以使用 val conf = new SparkConf() .setMaster("loca
我正在尝试将文本(自定义文本按钮)放入 AppBar 的前导属性中。但是,当文本太长时,文本会变成多行 Scaffold( appBar: AppBar( centerTi
我正在使用最新版本的 NetBeans,我需要增加输出和菜单的字体大小(不是代码部分)。我试过: netbeans_default_options=".... --fontsize 16" 但是当我将
我必须将 180000 个点绘制到一个 EPS 文件中。 使用标准 gnuplot 输出尺寸点彼此太接近,这使得它们无法区分。有没有办法增加图像的宽度和高度? 最佳答案 是的。 set termina
我有一个带有输入字段的 twitter bootstrap 3 导航栏。我想增加输入字段的宽度。我已尝试设置 col 大小,但它不起作用。 html比较长,请引用bootply http://www.
我正在尝试增加 ggplot 标题中下划线的大小/宽度/厚度。我曾尝试使用大小、宽度和长度,但没有成功。 这是我所做的一个例子。 test <- tibble(x = 1:5, y = 1, z =
我是一名优秀的程序员,十分优秀!