import java.util.Random;
import java.util.Scanner;
public class field {
private int[][] mines;
private char[][]game;
private int Row, Column;
Random random = new Random();
Scanner keyboard = new Scanner(System.in);
//create field array
public field (){
mines = new int[10][10];
game = new char [10][10];
startMines();
randomMines();
fillNum();
startField();
}
//set win conditions
public boolean win(){
int count=0;
for(int Row = 1; Row<9; Row++)
for(int column = 1; column<9; column++)
if(game[Row][column]=='_')
count++;
//if 10 mines are found you win
if(count==10)
return true;
else
return false;
}
public void openNeighbors(){
for(int i=-1;i<2;i++)
for(int j=-1;j<2;j++)
if((mines[Row+i][Column+j] != -1) && (Row !=0 && Column != 0 && Column !=9))
game[Row+i][Column+j]=Character.forDigit(mines[Row+i][Column+j], 10);
}
//get the suspected mines position
public int getPosition(int Row, int Column){
return mines[Row][Column];
}
//have the user input the position of the suspected mine
public boolean setPosition(){
do{
System.out.print("\nRow: ");
Row = keyboard.nextInt();
System.out.print("Column: ");
Column = keyboard.nextInt();
if( (game[Row][Column] != '_')&&((Row < 9 && Row > 0) && (Column< 9 && Column >0)))
System.out.println("Field already shown");
if(Row < 1 || Row >8 || Column < 1 || Column>8)
System.out.println("Chose a number between 1 and 8.");
}while((Row <1 || Row>8 || Column<1 || Column>8 || (game[Row][Column] != '_')));
if(getPosition(Row, Column)==-1)
return true;
else
return false;
}
//show the mine field
public void show(){
System.out.println("\n Rows");
for(int Row = 8; Row>0;Row++){
System.out.print(" "+Row+" ");
for(int Column=1; Column<9;Column++){
System.out.print(" "+game[Row][Column]);
}
System.out.println();
}
System.out.println("\n 1 2 3 4 5 6 7 8");
System.out.println(" Columns");
}
//show the hints for the game when selected
public void fillNum(){
for(int row=1; row<9;row++)
for(int column=1; column<9; column++){
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
if(mines[row][column]!=-1)
if(mines[row+i][column+j]==-1)
mines[row][column]++;
}
}
//show the mines on the mine field
public void showMines(){
for(int i=1; i<9;i++)
for(int j=1;j<9;j++)
if(mines[i][j]==-1)
game[i][j]='*';
show();
}
//generate the field
public void startField(){
for(int i=1; i<mines.length; i++)
for(int j=1; j<mines.length; j++)
game[i][j]='_';
}
//start the mines
public void startMines(){
for(int i=0; i<mines.length ; i++)
for(int j=0; j<mines.length; j++)
mines[i][j]=0;
}
//randomly place the 10 mines
public void randomMines(){
boolean raffled;
int Row, Column;
for(int i=0;i<10;i++){
do{
Row = random.nextInt(8) +1;
Column = random.nextInt(8) +1;
if(mines[Row][Column]==-1)
raffled=true;
else
raffled=false;
}while(raffled);
mines[Row][Column]=-1;
}
}
}
矿场代码
public class Play {
boolean end = false;
boolean win = false;
private field field;
int turn=0;
public void Begin(){
field = new field();
Start(field);
}
public void Start(field field){
do{
turn++;
System.out.println("Turn " +turn);
field.show();
end = field.setPosition();
if(!end){
field.openNeighbors();
end = field.win();
}
}while(!end);
if(field.win()){
System.out.println("You found all 10 of the hidden mines in the minefield in " +turn +"turns.");
field.showMines();
}else{
System.out.println("You hit a mine. You lose.");
field.showMines();
}
}
}
玩游戏代码
这些是我创建雷场和玩游戏的程序。当我编译并运行代码时,程序立即终止,并且在控制台中不显示任何其他内容。代码似乎没有任何编译错误。任何人都可以提供解决这个问题的任何帮助将不胜感激。
答案 0 :(得分:0)
修改您的Play类,使其开头如下所示:
public class Play {
boolean end = false;
boolean win = false;
private field field;
int turn=0;
// All Java programs require the following method to run, it must
// be declared public, static, void, named "main" and take a
// String array as an argument.
public static void main(String[] args) {
// Because main must be static (defined at compile time, once per class)
// you need to create an instance of your play class before you can
// call Begin()
new Play().Begin();
}
public void Begin(){
field = new field();
Start(field);
}
注意:根据Java惯例,方法名称以小写字母开头,因此Begin()应为begin(),类名称以大写字母开头,因此字段应为Field(和Field.java)。变量名通常也是小写的。不是必需的,但从长远来看,它使代码更容易阅读。
一旦您添加了主要方法。您的&#34; show&#34;中也有错误。 for循环。我想你想根据循环的设置方式减少Row而不是增量行。
//show the mine field
public void show(){
System.out.println("\n Rows");
for(int Row = 8; Row>0;Row++){ // <-- Try Row-- here instead.
快乐的节目!