Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.Menu;
- import javafx.scene.control.MenuBar;
- import javafx.scene.control.MenuItem;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.MouseButton;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- public class Solution6 extends Application {
- // 0=empty, -1 = mine, 1+=number
- private static boolean flag = false;
- private static boolean firstClick = true;
- private static boolean gameIsOver = false;
- private static int[][] mField;
- private static boolean[][] isFlagged;
- private static boolean[][] isClicked;
- private static boolean[][] numberRevealed;
- private static Button[][] bList;
- private static int fieldSizeX;
- private static int fieldSizeY;
- private static int spaces = 0;
- private static int bombs = 0;
- private static int bombsRemaining = 0;
- private static boolean win = false;
- private static String difficulty = "b";
- private static int counter = 0;
- //private static MSButton coverdTile;
- public static void main(String[] args) {
- resetGame(difficulty);
- launch(args);
- }
- public static void staticPos() {
- int i = 0;
- int[][] places = new int[bombs * 2][2];
- while (i < bombs) {
- int rand = (int) (Math.random() * (fieldSizeX));
- int rand2 = (int) (Math.random() * (fieldSizeY));
- mField[rand2][rand] = -1;
- places[i][0] = rand;
- places[i][1] = rand2;
- try {
- for (int m = 0; m < i; m++) {
- while (places[m][0] == rand && places[m][1] == rand2) {
- if (places[m][0] == rand && places[m][1] == rand2) {
- m = 0;
- }
- rand = (int) (Math.random() * (fieldSizeX));
- rand2 = (int) (Math.random() * (fieldSizeY));
- mField[rand2][rand] = -1;
- }
- }
- } catch (IndexOutOfBoundsException e) {
- }
- i++;
- }
- setNumbers();
- }
- public void start(Stage theStage) {
- try {
- FileInputStream input = new FileInputStream("res/face-smile.png");
- Image image = new Image(input);
- ImageView imageView = new ImageView(image);
- HBox hbox2 = new HBox();
- hbox2.setPrefWidth((fieldSizeX * 30) / 3);
- Button resetButton = new Button("", imageView);
- resetButton.setMinSize(50, 50);
- resetButton.setMaxSize(50, 50);
- imageView.setFitHeight(0);
- imageView.setFitWidth(0);
- hbox2.setAlignment(Pos.CENTER);
- hbox2.getChildren().add(resetButton);
- input.close();
- BorderPane bPane = new BorderPane();
- BorderPane bPane2 = new BorderPane();
- BorderPane bPane3 = new BorderPane();
- HBox hbox = new HBox();
- hbox.setPrefWidth(fieldSizeX * 10);
- hbox.getChildren().add(new Label("000"));
- HBox hbox3 = new HBox();
- hbox3.setPrefWidth(fieldSizeX * 10);
- String bombsLeft = ("0" + bombsRemaining);
- hbox3.getChildren().add(new Label(bombsLeft));
- // Button newButton = new Button("top");
- GridPane gridTop = new GridPane();
- gridTop.add(bPane2, 0, 0);
- gridTop.add(bPane3, 2, 0);
- bPane2.setCenter(hbox);
- bPane3.setCenter(hbox3);
- bPane.setTop(gridTop);
- gridTop.setMinSize(100, 30);
- gridTop.setMaxSize(1000, 100);
- gridTop.add(hbox3, 0, 0);
- gridTop.add(hbox2, 1, 0);
- gridTop.add(hbox, 2, 0);
- GridPane grid = new GridPane();
- bPane.setCenter(grid);
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- final int a = i;
- final int b = j;
- ImageView img = new ImageView(new Image(new FileInputStream("res/cover.png")));
- Button MSButton = new Button("res/cover.png");
- Button button = new Button("", img);
- button.setMinSize(30, 30);
- button.setMaxSize(30, 30);
- img.setFitHeight(0);
- img.setFitWidth(0);
- img.fitWidthProperty().add(button.widthProperty());
- img.fitHeightProperty().add(button.widthProperty());
- bList[j][i] = button;
- button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- if (e.getButton() == MouseButton.SECONDARY) {
- if (numberRevealed[b][a] == false) {
- if (isFlagged[b][a] == false)
- isFlagged[b][a] = true;
- else
- isFlagged[b][a] = false;
- if (isFlagged[b][a] == true) {
- if (bombsRemaining > 0) {
- try {
- ImageView img = new ImageView(
- new Image(new FileInputStream("res/flag.png")));
- bList[b][a].setMaxHeight(0);
- bList[b][a].setMaxWidth(0);
- img.fitWidthProperty().add(bList[b][a].widthProperty());
- img.fitHeightProperty().add(bList[b][a].heightProperty());
- bList[b][a].setGraphic(img);
- bombsRemaining--;
- String bombsLeft = ("0" + bombsRemaining);
- hbox3.getChildren().clear();
- hbox3.getChildren().add(new Label(bombsLeft));
- } catch (IOException g) {
- }
- } else {
- isFlagged[b][a] = false;
- }
- } else if (isFlagged[b][a] == false) {
- try {
- ImageView img = new ImageView(
- new Image(new FileInputStream("res/cover.png")));
- bList[b][a].setMaxHeight(0);
- bList[b][a].setMaxWidth(0);
- img.fitWidthProperty().add(bList[b][a].widthProperty());
- img.fitHeightProperty().add(bList[b][a].heightProperty());
- bList[b][a].setGraphic(img);
- bombsRemaining++;
- String bombsLeft = ("0" + bombsRemaining);
- hbox3.getChildren().clear();
- hbox3.getChildren().add(new Label(bombsLeft));
- } catch (IOException g) {
- }
- }
- }
- }
- if (e.getButton() == MouseButton.PRIMARY) {
- if (isFlagged[b][a] == false) {
- if (mField[b][a] == 0) {
- recur(bList[b][a], b, a, 0);
- }
- if (mField[b][a] == -1) {
- gameOver(resetButton, bList[b][a]);
- } else if (isClicked[b][a] == true) {
- checkFlags(b, a, bList[b][a], resetButton);
- } else if (mField[b][a] >= 0) {
- showNumber(bList[b][a], b, a);
- }
- if (counter == fieldSizeX * fieldSizeY - bombs)
- won(resetButton);
- }
- // if(firstClick == true)
- // staticPos();
- }
- e.consume();
- }
- });
- grid.add(bList[j][i], i, j);
- }
- }
- resetButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- resetGame(difficulty);
- start(theStage);
- }
- });
- theStage.setScene(new Scene(bPane, fieldSizeX * 30, (fieldSizeY * 30) + 50));
- } catch (IOException e) {
- }
- theStage.show();
- }
- public static void showNumber(Button b, int c, int a) {
- if (flag == false) {
- numberRevealed[c][a] = true;
- }
- flag = false;
- if (mField[c][a] == 0) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/0.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 2) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/2.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 1) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/1.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 3) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/3.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 4) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/4.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 5) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/5.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 6) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/6.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 7) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/7.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 8) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/8.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- }
- }
- public static void showNumber(Button b, int c, int a, int k) {
- if (flag == false) {
- numberRevealed[c][a] = true;
- }
- flag = false;
- if (mField[c][a] == 0) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/0.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 2) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/2.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 1) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/1.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 3) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/3.png")));
- b.setMaxHeight(0);
- b.setMaxWidth(0);
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 4) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/4.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 5) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/5.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 6) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/6.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 7) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/7.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- } else if (mField[c][a] == 8) {
- try {
- ImageView img = new ImageView(new Image(new FileInputStream("res/8.png")));
- img.fitWidthProperty().add(b.widthProperty());
- img.fitHeightProperty().add(b.heightProperty());
- b.setGraphic(img);
- if (isClicked[c][a] == false) {
- counter++;
- isClicked[c][a] = true;
- }
- } catch (IOException e) {
- }
- }
- if (k > 0)
- recur(bList[c][a], c, a, k);
- }
- public static void gameOver(Button b, Button b2) {
- gameIsOver = true;
- try {
- b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-dead.png"))));
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- if (mField[j][i] == -1) {
- Button bu = bList[j][i];
- bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-grey.png"))));
- }
- }
- }
- b2.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- bList[j][i].setDisable(true);
- }
- }
- } catch (IOException e) {
- }
- }
- public static void gameOver(Button b, Button[][][] b2, Button[][][] b3) {
- gameIsOver = true;
- try {
- b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-dead.png"))));
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- if (isFlagged[j][i] == true && mField[j][i] != -1) {
- Button bu = bList[j][i];
- bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- }
- if (mField[j][i] == -1) {
- Button bu = bList[j][i];
- bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-grey.png"))));
- }
- }
- }
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- bList[j][i].setDisable(true);
- }
- }
- } catch (IOException e) {
- }
- }
- public static void won(Button b) {
- gameIsOver = true;
- try {
- b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-win.png"))));
- } catch (IOException e) {
- }
- }
- public static void checkFlags(int b, int a, Button button, Button resetButton) {
- boolean endGame = false;
- Button[][][] changeButtons = new Button[8][1][1];
- Button[][][] redButtons = new Button[8][1][1];
- int u = 0;
- int p = 0;
- int tilesToGet = 0;
- int[][][] field = new int[8][2][1];
- int valueOfFlag = mField[b][a];
- int counter = 0;
- boolean ul = true;
- boolean um = true;
- boolean ur = true;
- boolean ml = true;
- boolean mr = true;
- boolean bm = true;
- boolean br = true;
- boolean bl = true;
- if (b == 0) {
- bl = false;
- ul = false;
- ml = false;
- }
- if (b == mField.length - 1) {
- br = false;
- ur = false;
- mr = false;
- }
- if (a == 0) {
- um = false;
- ul = false;
- ur = false;
- }
- if (a == mField[0].length - 1) {
- bl = false;
- bm = false;
- br = false;
- }
- // 1
- if (ul)
- if (isFlagged[b - 1][a - 1] == true)
- u++;
- if (um)
- if (isFlagged[b][a - 1] == true)
- u++;
- if (ur)
- if (isFlagged[b + 1][a - 1] == true)
- u++;
- if (ml)
- if (isFlagged[b - 1][a] == true)
- u++;
- if (mr)
- if (isFlagged[b + 1][a] == true)
- u++;
- if (bl)
- if (isFlagged[b - 1][a + 1] == true)
- u++;
- if (br)
- if (isFlagged[b + 1][a + 1] == true)
- u++;
- if (bm)
- if (isFlagged[b][a + 1] == true)
- u++;
- if (ul) {
- tilesToGet++;
- if (mField[b - 1][a - 1] != -1 && isFlagged[b - 1][a - 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a - 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b - 1][a - 1] == -1 && isFlagged[b - 1][a - 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a - 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b - 1][a - 1] == -1 && isFlagged[b - 1][a - 1] == true) {
- } else {
- field[counter][0][0] = (b - 1);
- field[counter][1][0] = (a - 1);
- counter++;
- }
- }
- // 2
- if (um) {
- tilesToGet++;
- if (mField[b][a - 1] != -1 && isFlagged[b][a - 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b][a - 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b][a - 1] == -1 && isFlagged[b][a - 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b][a - 1].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b][a - 1] == -1 && isFlagged[b][a - 1] == true) {
- } else {
- field[counter][0][0] = (b);
- field[counter][1][0] = (a - 1);
- counter++;
- }
- }
- // 3
- if (ur) {
- tilesToGet++;
- if (mField[b + 1][a - 1] != -1 && isFlagged[b + 1][a - 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a - 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b + 1][a - 1] == -1 && isFlagged[b + 1][a - 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a - 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b + 1][a - 1] == -1 && isFlagged[b + 1][a - 1] == true) {
- } else {
- field[counter][0][0] = (b + 1);
- field[counter][1][0] = (a - 1);
- counter++;
- }
- }
- // 4
- if (ml) {
- tilesToGet++;
- if (mField[b - 1][a] != -1 && isFlagged[b - 1][a] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b - 1][a] == -1 && isFlagged[b - 1][a] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b - 1][a] == -1 && isFlagged[b - 1][a] == true) {
- } else {
- field[counter][0][0] = (b - 1);
- field[counter][1][0] = (a);
- counter++;
- }
- }
- // 5
- if (mr) {
- tilesToGet++;
- if (mField[b + 1][a] != -1 && isFlagged[b + 1][a] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b + 1][a] == -1 && isFlagged[b + 1][a] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b + 1][a] == -1 && isFlagged[b + 1][a] == true) {
- } else {
- field[counter][0][0] = (b + 1);
- field[counter][1][0] = (a);
- counter++;
- }
- }
- // 6
- if (bl) {
- tilesToGet++;
- if (mField[b - 1][a + 1] != -1 && isFlagged[b - 1][a + 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a + 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b - 1][a + 1] == -1 && isFlagged[b - 1][a + 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b - 1][a + 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b - 1][a + 1] == -1 && isFlagged[b - 1][a + 1] == true) {
- } else {
- field[counter][0][0] = (b - 1);
- field[counter][1][0] = (a + 1);
- counter++;
- }
- }
- // 7
- if (br) {
- tilesToGet++;
- if (mField[b + 1][a + 1] != -1 && isFlagged[b + 1][a + 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a + 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b + 1][a + 1] == -1 && isFlagged[b + 1][a + 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b + 1][a + 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b + 1][a + 1] == -1 && isFlagged[b + 1][a + 1] == true) {
- } else {
- field[counter][0][0] = (b + 1);
- field[counter][1][0] = (a + 1);
- counter++;
- }
- }
- // 8
- if (bm) {
- tilesToGet++;
- if (mField[b][a + 1] != -1 && isFlagged[b][a + 1] == true) {
- if (u == valueOfFlag) {
- try {
- bList[b][a + 1]
- .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- endGame = true;
- }
- if (mField[b][a + 1] == -1 && isFlagged[b][a + 1] == false) {
- if (u == valueOfFlag) {
- try {
- bList[b][a + 1].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
- } catch (FileNotFoundException e) {
- }
- }
- }
- if (mField[b][a + 1] == -1 && isFlagged[b][a + 1] == true) {
- } else {
- field[counter][0][0] = (b);
- field[counter][1][0] = (a + 1);
- counter++;
- }
- }
- if (endGame && u == valueOfFlag) {
- gameOver(resetButton, changeButtons, redButtons);
- }
- if ((tilesToGet - counter) == valueOfFlag) {
- endGame = false;
- for (int l = 0; l < counter; l++) {
- if (isFlagged[field[l][0][0]][field[l][1][0]] == false) {
- showNumber(bList[field[l][0][0]][field[l][1][0]], field[l][0][0], field[l][1][0]);
- }
- }
- }
- counter = 0;
- ul = true;
- um = true;
- ur = true;
- ml = true;
- mr = true;
- bm = true;
- br = true;
- bl = true;
- }
- public static void recur(Button b, int c, int a, int count) {
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- boolean ul = true;
- boolean um = true;
- boolean ur = true;
- boolean ml = true;
- boolean mr = true;
- boolean bm = true;
- boolean br = true;
- boolean bl = true;
- if (c == 0) {
- bl = false;
- ul = false;
- ml = false;
- }
- if (c == mField.length - 1) {
- br = false;
- ur = false;
- mr = false;
- }
- if (a == 0) {
- um = false;
- ul = false;
- ur = false;
- }
- if (a == mField[0].length - 1) {
- bl = false;
- bm = false;
- br = false;
- }
- // 1
- if (ul)
- if (isClicked[c - 1][a - 1] == false && mField[c][a] == 0) {
- showNumber(bList[c - 1][a - 1], c - 1, a - 1, count + 1);
- }
- if (um)
- if (isClicked[c][a - 1] == false && mField[c][a] == 0) {
- showNumber(bList[c][a - 1], c, a - 1, count + 1);
- }
- if (ur)
- if (isClicked[c + 1][a - 1] == false && mField[c][a] == 0) {
- showNumber(bList[c + 1][a - 1], c + 1, a - 1, count + 1);
- }
- if (ml)
- if (isClicked[c - 1][a] == false && mField[c][a] == 0) {
- showNumber(bList[c - 1][a], c - 1, a, count + 1);
- }
- if (mr)
- if (isClicked[c + 1][a] == false && mField[c][a] == 0) {
- showNumber(bList[c + 1][a], c + 1, a, count + 1);
- }
- if (bl)
- if (isClicked[c - 1][a + 1] == false && mField[c][a] == 0) {
- showNumber(bList[c - 1][a + 1], c - 1, a + 1, count + 1);
- }
- if (br)
- if (isClicked[c + 1][a + 1] == false && mField[c][a] == 0) {
- showNumber(bList[c + 1][a + 1], c + 1, a + 1, count + 1);
- }
- if (bm)
- if (isClicked[c][a + 1] == false && mField[c][a] == 0) {
- showNumber(bList[c][a + 1], c, a + 1, count + 1);
- }
- }
- }
- }
- public static void setNumbers() {
- boolean ul = true;
- boolean um = true;
- boolean ur = true;
- boolean ml = true;
- boolean mr = true;
- boolean bm = true;
- boolean br = true;
- boolean bl = true;
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- if (mField[j][i] == -1) {
- if (j == 0) {
- bl = false;
- ul = false;
- ml = false;
- }
- if (j == mField.length - 1) {
- br = false;
- ur = false;
- mr = false;
- }
- if (i == 0) {
- um = false;
- ul = false;
- ur = false;
- }
- if (i == mField[0].length - 1) {
- bl = false;
- bm = false;
- br = false;
- }
- if (ul) {
- if (mField[j - 1][i - 1] != -1)
- mField[j - 1][i - 1]++;
- }
- if (um) {
- if (mField[j][i - 1] != -1)
- mField[j][i - 1]++;
- }
- if (ur) {
- if (mField[j + 1][i - 1] != -1)
- mField[j + 1][i - 1]++;
- }
- if (ml) {
- if (mField[j - 1][i] != -1)
- mField[j - 1][i]++;
- }
- if (mr) {
- if (mField[j + 1][i] != -1)
- mField[j + 1][i]++;
- }
- if (bl) {
- if (mField[j - 1][i + 1] != -1)
- mField[j - 1][i + 1]++;
- }
- if (br) {
- if (mField[j + 1][i + 1] != -1)
- mField[j + 1][i + 1]++;
- }
- if (bm) {
- if (mField[j][i + 1] != -1)
- mField[j][i + 1]++;
- }
- ul = true;
- um = true;
- ur = true;
- ml = true;
- mr = true;
- bm = true;
- br = true;
- bl = true;
- }
- }
- }
- }
- public static void resetGame(String difficulty) {
- firstClick = true;
- spaces = 0;
- bombs = 0;
- bombsRemaining = 0;
- win = false;
- counter = 0;
- if (difficulty.equals("b")) {
- fieldSizeX = 8;
- fieldSizeY = 8;
- bombs = 10;
- } else if (difficulty.equals("i")) {
- fieldSizeX = 16;
- fieldSizeY = 16;
- bombs = 40;
- } else if (difficulty.equals("e")) {
- fieldSizeX = 32;
- fieldSizeY = 16;
- bombs = 99;
- }
- mField = new int[fieldSizeY][fieldSizeX];
- isFlagged = new boolean[fieldSizeY][fieldSizeX];
- isClicked = new boolean[fieldSizeY][fieldSizeX];
- numberRevealed = new boolean[fieldSizeY][fieldSizeX];
- bList = new Button[fieldSizeY][fieldSizeX];
- for (int i = 0; i < fieldSizeX; i++) {
- for (int j = 0; j < fieldSizeY; j++) {
- mField[j][i] = 0;
- isFlagged[j][i] = false;
- isClicked[j][i] = false;
- numberRevealed[j][i] = false;
- bList[j][i] = null;
- }
- }
- bombsRemaining = bombs;
- // if(firstClick == false)
- staticPos();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement