- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用与其相邻的地雷数量相关的值(数字)填充二维数组
。
我的思考过程是将每个单元格与相邻单元格进行比较,但我收到了越界异常。
我尝试了几种不同的方法,但似乎无法理解其中的逻辑。我觉得数组让我失望。
import java.util.Random;
import java.util.Scanner;
public class GameBoard
{
private int[][] mines;
private char[][] tileCount;
private int[][] sol;
private int Row, Column, mine;
Random random = new Random();
Scanner input = new Scanner(System.in);
public GameBoard(int Row, int Column, int mine)
{
this.Row = Row;
this.Column = Column;
this.mine = mine;
mines = new int[Row][Column];
tileCount = new char[Row][Column];
startMines();
randomMines();
fillTips();
startBoard();
}
public void startMines()
{
for(int i=0; i < mines.length; i++)
{
for(int j=0; j < mines[0].length; j++)
{
mines[i][j] = 0;
}
}
}
public void randomMines()
{
double x = (Row * Column) * .25;
int tempRow, tempColumn;
int j=0;
for(int i=0 ; j < this.mine ; i++)
{
tempRow = (int)(Math.random() * Row);
tempColumn = (int)(Math.random() * Column);
if(mines[tempRow][tempColumn] == 0)
{
mines[tempRow][tempColumn] = 9;
++j;
}
}
}
public void showMines()
{
for(int i=0 ; i < Row; i++)
{
for(int j=0 ; j < Column ; j++)
{
if ( mines[i][j] == '*')
{
System.out.print(tileCount[i][j]='*');
}
}
}
}
public void fillTips()
{
for(int line=0 ; line < Row ; line++)
{
for(int column=0 ; column < Column ; column++)
{
for(int i=-1 ; i<=0 ; i++)
{
for(int j=-1 ; j<=0 ; j++)
{
if(mines[line][column] != -1)
{
if(mines[line][column] == -1)
{
mines[line][column]++;
}
}
}
}
}
}
}
public void startBoard()
{
for(int i=0 ; i<mines.length ; i++)
{
for(int j=0 ; j<mines.length ; j++)
{
tileCount[i][j]= '.';
}
}
}
public String toString()
{
System.out.println("\n Lines");
for(int i = 0 ; i < Row ; i++)
{
System.out.print(" "+ (i+1) + " ");
}
for(int j = 0 ; j < Column ; j++)
{
System.out.print( " "+ mines[i][j]);
}
System.out.println();
return "\n 1 2 3 4 5 6 7 8\n Columns";
}
}
import java.util.Scanner;
import java.util.Arrays;
public class GameClient
{
int grid;
public static void main(String args[])
{
System.out.println("Welcome to the game minesweeper, I hope you enjoy your stay.");
System.out.println("We will begin by asking how large you would like the game.");
System.out.println("---------------------------------------------------------------");
System.out.println("Input two values please: ");
Scanner grid = new Scanner(System.in);
int grid1 = grid.nextInt();
int grid2 = grid.nextInt();
double mineCount = ((grid1*grid2)*.25);
System.out.println("Enter a number of mines, please 25% less than the total tiles which is " + mineCount);
Scanner mineCounts = new Scanner(System.in);
mineCount = mineCounts.nextInt();
GameBoard[][] tileSize = new GameBoard[grid1][grid2];
tileSize[0][0] = new GameBoard(grid1, grid2, (int)mineCount);
System.out.println(tileSize[0][0]);
}
}
最佳答案
代码中至少存在两个问题。
这些行中 randomMines
中的第一个
tempRow = (int)(Math.random() * Row);
tempColumn = (int)(Math.random() * Column);
Math.random()
可能返回 1.0,因此 tempRow
将等于 Row
,这是问题所在,因为最后一个索引是 行 - 1
(和列 - 1
)。所以让它像
tempRow = (int)(Math.random() * (Row - 1));
tempColumn = (int)(Math.random() * (Column - 1));
另一个问题是在循环中的 startBoard
for(int i=0 ; i<mines.length ; i++) {
for(int j=0 ; j<mines.length ; j++) {
...
}
}
您有 mines[0].length
列,正如您在 startMines
方法中编写的那样。
我看到的最后一个错误是在 toString
方法中。它甚至无法编译。它应该看起来像这样
public String toString()
{
System.out.println("\n Lines");
for(int i = 0 ; i < Row ; i++) {
System.out.print(" " + (i + 1) + " ");
for (int j = 0; j < Column; j++) {
System.out.print(" " + mines[i][j]);
}
System.out.println();
}
return "\n 1 2 3 4 5 6 7 8\n Columns";
}
但这并没有起到应有的作用,您打印值而不是将它们转换为String
。最好使用StringBuilder
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("\n Lines\n");
for(int i = 0 ; i < Row ; i++) {
builder.append(" " + (i + 1) + " ");
for (int j = 0; j < Column; j++) {
builder.append(" " + mines[i][j]);
}
builder.append("\n");
}
builder.append("\n 1 2 3 4 5 6 7 8\n Columns");
return builder.toString();
}
您也可以使用String#format
漂亮地打印你的主板。
关于java - 解决扫雷代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117802/
1)扫雷 简介 扫雷 是一种经典的单人电脑游戏,最初由微软公司在 1990 年代开发并内置在 Windows 操作系统中。游戏的目标是在一个由方块组成的网格上揭开所有非地雷的方块,而不触发地雷。每个
我正在制作 MineSweeper 的 Angular 版本,但我遇到了事件问题。我的意思是,我有这样的字段组件: import {Component, Input, OnInit} from '@a
我正在尝试制作一个简单的扫雷器,在 n*n 板上埋下 n*n/3 个地雷。地雷用*标记,空格用0标记。(它还不能作为游戏运行:我正在尝试制作扫雷的“答卷”)请注意,我还没有使用过任何有目的的方法。 我
我必须制作一个扫雷 GUI,但我不知道如何使每个按钮都有自己的 mouseAdapter。当我单击某个按钮时,它会更改屏幕上的每个按钮,而不仅仅是我单击的按钮。我可以提供一些关于如何执行此操作的指示吗
我想制作一款扫雷游戏。首先,这适用于按钮。我想我将研究二维数组,并且会有类似 boolean 数组来显示炸弹在哪里,例如,如果 booleanArray[0][4] 为 true,则有炸弹。 现在,我
我遇到了这个奇怪的问题,但我似乎无法弄清楚。我有一个按钮,上面写着“新游戏”。在我玩一次游戏后,然后按“新游戏”返回到 initGame() ,我遇到一个问题,因为我无法右键单击前一个游戏的地雷单元格
我现在面临一个问题,我认为我的 main 正在一遍又一遍地执行一个方法,而不是一次。如果我根据一个例子来解释一下就更好了。我已经能够编写扫雷游戏了。但我把这一切都写在一个 MAIN 类中。这次我尝试再
几个月来我一直在尝试创建自己的扫雷游戏(为了好玩)。唯一让我真正困惑的是如何使用递归函数(flood-fill)来填充游戏中的空白区域。 洪水填充仅部分起作用。它不会从任何单击的节点延伸到右侧或底部节
我正在尝试在 C++11 中使用 Qt 编写扫雷程序。 如果我按下一个带有 0 个炸弹的按钮,我想检查这个按钮周围的按钮以及它们是否也有 0 个炸弹。如果他们有 0 颗炸弹,我想检查按钮。 (图:红色
我想做的是能够“转动”每个相邻的 0,就像普通的扫雷游戏一样。 #include #include #include #include #include int buscamina(); u
有没有办法让某些事件 Action 特定于鼠标左键和右键单击? 我正在创建一个扫雷器图形用户界面,所以当左键单击一个方 block 时,它会被发现,而当它被右键单击时,它会被标记。 我不确定如何从语法
我正在尝试制作一个控制台版本的扫雷。 尽管我目前很努力,但我无法弄清楚扫雷的“洪水填充”部分,如果所选方格的周围邻居不包含炸弹,我们现在必须检查这些相邻方格以找到相邻方格炸弹。 下面的代码适用于所选方
我是论坛新手,因此如果这不符合该网站的标准,我想表示歉意,但是我对 2D 阵列扫雷程序有疑问。 我试图在棋盘上随机放置地雷,棋盘是通过数组构建的。不幸的是,地雷被随机放置在对角线上,而不是整个棋盘上。
我是 Java 编程新手,想寻求您的帮助。我正在尝试使用 Java 开发一个简单的扫雷游戏。但是,我不断收到错误“线程“main”java.lang.ArrayIndexOutOfBoundsExce
我在这段代码上工作了几个小时,终于接近完成了。一旦我弄清楚如何添加通过右键单击标记地雷的功能,我将添加一个地雷计数器和一个计时器。我了解到 JButton 无法识别右键单击,因此我必须使用 Mouse
for (var i = 0; i < 10; i++) { bommen[i] = [ Math.floor(Math.random() * 10), Math.floor(
我正在完成大学的一项作业,我们获得了扫雷游戏的源代码,并且我们有一定的实现要求。其中之一是,所有游戏都是在用户登录后从磁盘读取的,用户可以随时保存游戏进度。我一直在阅读序列化来保存/加载游戏进度,但我
我正在尝试用 Java 制作类似扫雷的游戏,并且我已经完成了其中的大部分工作。我需要帮助的是 FloodFill - http://en.wikipedia.org/wiki/Flood_fill .
我正在尝试为我正在制作的扫雷游戏制作一个函数。该函数的目的是根据给定的 x 和 y(它所在的位置)显示一个元素。这可能不是实现它的最优雅的方法,但我正在为每个标题为 newField 的图 block
这款游戏是扫雷。我试图实现的人工智能将采用已经在计算机上运行的扫雷游戏实例(本例为 Windows 7),获取窗口的矩形尺寸,计算窗口的位置,将其推送到前台在屏幕上,单击左上角的方 block ,然后
我是一名优秀的程序员,十分优秀!