package relocation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public ExceptionParameter
(String s
){
super(s);
}
public ExceptionParameter
(String s,location pms
){
super(s);
}
}
public class trial {
public static void main
(String[] args
)throws ExceptionParameter
{
location dis = null;
try{
dis = new location();
dis.gameStart();
}catch(ExceptionParameter abc){
new ExceptionParameter("Wrong input by User:"+abc);
}
}
}
class location{
boolean[][]openFlag;
boolean[][]mineFlag;
int[][]numberOfmine;
int counter;
int fieldX;
int lengthend;
@SuppressWarnings("OverridableMethodCallInConstructor")
public location()throws ExceptionParameter{
int x,y,inputMine;
Scanner scan
= new Scanner
(System.
in);
System.
out.
println("Enter length for x axis: (integer value)");
x = scan.nextInt();
System.
out.
println("Enter length for y axis: (integer value) ");
y = scan.nextInt();
System.
out.
println("Enter number of mind: (integer value) ");
inputMine = scan.nextInt();
standardInputLength();
while(lengthend != 0){
if(lengthend == 1){
showSelection(x,y,inputMine + 2);
initializedField(x,y,inputMine + 2);
}else if(lengthend == 2){
showSelection(x,y,inputMine + 3);
initializedField(x,y,inputMine + 3);
}else if(lengthend == 3){
showSelection(x,y,inputMine + 5);
initializedField(x,y,inputMine + 5);
}else if( lengthend >= 3){
standardInputLength();
}else{
throw new ExceptionParameter("Invalid input!");
}
break;
}
}
void standardInputLength()throws ExceptionParameter{//use for optional input
Scanner scan
= new Scanner
(System.
in);
System.
out.
println("Enter Difficulty: ");
System.
out.
println("1: NORMAL 2: HARD 3: DIFFICULT");
lengthend = scan.nextInt();
if(!(1 <= lengthend && lengthend <= 3)){
throw new ExceptionParameter("Invalid input!");
}
}
void showSelection(int x,int y,int inputMine)throws ExceptionParameter{
System.
out.
printf("Field size %d x %d\n",x,y,inputMine
);
System.
out.
println("Number of mine: "+inputMine
);
System.
out.
println("X represents a mine \n\n if you select a grid that contains mine,\n you lose the game.");
System.
out.
println("--------------------");
}
void printField(){
char c = 'a';
for(int i = 0; i < fieldX;i++){
}
for(int i = 0; i <fieldX;i++){
System.
out.
printf("%02d ", i
+ 1);
for(int j
= 0; j
< Integer.
parseInt(""+fieldY
);j
++){
if(openFlag[i][j] == false){
}else if(mineFlag[i][j] == true){
}else{
System.
out.
printf("%d ",numberOfmine
[i
][j
]);
}
}
}
}
void printFieldStatus(){
for(int i = 0; i <fieldX; i++){
for(int j
= 0; j
< Integer.
parseInt(""+fieldY
);j
++){
if(mineFlag[i][j] == true){
}else{
}
}
}
}
void initializedField(int x, int y, int inMine){
openFlag = new boolean[x][y];
mineFlag = new boolean[x][y];
numberOfmine= new int[x][y];
this.fieldX = x;
this.
fieldY = String.
valueOf(y
);
for(int i = 0; i < inMine;i++){
int randx = 0;
int randy = 0;
do{
randx = rand.nextInt(x);
randy = rand.nextInt(y);
}while(mineFlag[randx][randy]);
mineFlag[randx][randy] = true;
for(int j = -1; j < x;j++){
for(int k = -1; k < y;k++){
if(randx + j >=0 && randx + j < x && randy + k >=0 && randy + k < y){
numberOfmine[randx +j][randy +k]++;
}
}
}
}
}
void handlingInput(){
try{
while(true){
String temp
= reader.
readLine();
if(!temp.equals(null) && !temp.equals("")){
inputStdIn = temp.split("[\\s]+");
}else{
continue;
}
if(inputStdIn[0].charAt(0) == 'q'){
}
break;
}
System.
out.
println(abc.
getMessage());
System.
out.
println("Force termination");
}
}
boolean checkField(){
for(int i = 0; i < fieldX;i++){
for(int j
= 0; j
< Integer.
parseInt(""+fieldY
);j
++){
if(openFlag[i][j] == false && mineFlag[i][j] == false){
return false;
}
}
}
return true;
}
void openField(int px, int py){
if(px
< 0 || px
>= fieldX
|| py
<0 || py
>= Integer.
parseInt(""+fieldY
)){
return;
}
if(openFlag[px][py] == true){
return;
}
openFlag[px][py] = true;
if(mineFlag[px][py] == true){
return;
}
if(mineFlag[px][py] == false){
counter++;
System.
out.
println("Your score is: "+counter
);
}
else{
System.
out.
println("You got no score");
}
// counter++;
// System.out.println("your score: "+counter);
if(mineFlag[px][py] == false && numberOfmine[px][py] == 0 ){
for(int i = -1; i < 2;i++){
for(int j = -1; j < 2; j++){
openField(px +1,py +1);
}
}
counter++;
System.
out.
println("your score: "+counter
);
}
}
void gameStart()throws ExceptionParameter{
boolean gameover = false;
while(!gameover){
printField();
char inputX;
int sx = 0;
int sy = 0;
while(true){
System.
out.
print("To Open the mind!\n please input letter (space) number.");
System.
out.
println(" (((q -> force quit)))");
handlingInput();
if(inputStdIn.length > 2){
continue;
}
inputX = inputStdIn[0].charAt(0);
inputY = inputStdIn[1];
if('a' <= inputX && inputX <= 'z'){
sx = (int)(inputX - 'a');
}
else if('A' <= inputX && inputX <= 'Z'){
sx = (int)(inputX - 'A');
}
else{
System.
out.
println("Please input again.");
continue;
}
sy
= Integer.
parseInt("" + inputY
) - 1;
}
else{
System.
out.
println("Please input again.");
continue;
}
break;
}
openField(sy, sx);
if(mineFlag[sy][sx]){
break;
}
gameover = checkField();
}
gameFinish(gameover);
}
void gameFinish(boolean flag){
printField();
/* output */
if(flag){
System.
out.
println("You've finished searching whole field avoiding mine!");
System.
out.
println("Game clear!");
}
else{
System.
out.
println("You were blown by mine.");
System.
out.
println("Game over.");
System.
out.
println("You only got:"+counter
+" score.");
}
}
}
I'm a newbie programmer