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;
}
}
}
Code champ de minesprogramme Démineur se termine dès que je le lance
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();
}
}
}
Jouer le code du jeu
Ce sont mes programmes pour créer le champ de mines et de jouer le jeu. Lorsque je compile et exécute le code, le programme se termine immédiatement et ne montre rien d'autre dans la console. Le code ne semble pas avoir d'erreurs de compilation. Toute aide que quelqu'un pourrait fournir pour résoudre ce problème serait grandement appréciée.
Avez-vous déjà appris à propos de la méthode Java ['main'] (https://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html)? – davidbak
Comment essayez-vous de lancer ceci? Aucune idée que je sais ne donnera pas une méthode principale pas une erreur de foorm. –
'public void principal (String [] args) {new Play(). Begin();' ' – flakes