Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.77 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.geometry.Pos;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.Menu;
  11. import javafx.scene.control.MenuBar;
  12. import javafx.scene.control.MenuItem;
  13. import javafx.scene.image.Image;
  14. import javafx.scene.image.ImageView;
  15. import javafx.scene.input.MouseButton;
  16. import javafx.scene.input.MouseEvent;
  17. import javafx.scene.layout.BorderPane;
  18. import javafx.scene.layout.GridPane;
  19. import javafx.stage.Stage;
  20. import javafx.scene.layout.HBox;
  21. import javafx.scene.layout.VBox;
  22.  
  23. public class Solution6 extends Application {
  24. // 0=empty, -1 = mine, 1+=number
  25. private static boolean flag = false;
  26. private static boolean firstClick = true;
  27. private static boolean gameIsOver = false;
  28. private static int[][] mField;
  29. private static boolean[][] isFlagged;
  30. private static boolean[][] isClicked;
  31. private static boolean[][] numberRevealed;
  32. private static Button[][] bList;
  33. private static int fieldSizeX;
  34. private static int fieldSizeY;
  35. private static int spaces = 0;
  36. private static int bombs = 0;
  37. private static int bombsRemaining = 0;
  38. private static boolean win = false;
  39. private static String difficulty = "b";
  40. private static int counter = 0;
  41. //private static MSButton coverdTile;
  42.  
  43. public static void main(String[] args) {
  44.  
  45. resetGame(difficulty);
  46. launch(args);
  47. }
  48.  
  49. public static void staticPos() {
  50. int i = 0;
  51. int[][] places = new int[bombs * 2][2];
  52. while (i < bombs) {
  53. int rand = (int) (Math.random() * (fieldSizeX));
  54. int rand2 = (int) (Math.random() * (fieldSizeY));
  55. mField[rand2][rand] = -1;
  56. places[i][0] = rand;
  57. places[i][1] = rand2;
  58. try {
  59. for (int m = 0; m < i; m++) {
  60. while (places[m][0] == rand && places[m][1] == rand2) {
  61. if (places[m][0] == rand && places[m][1] == rand2) {
  62. m = 0;
  63. }
  64. rand = (int) (Math.random() * (fieldSizeX));
  65. rand2 = (int) (Math.random() * (fieldSizeY));
  66. mField[rand2][rand] = -1;
  67. }
  68. }
  69. } catch (IndexOutOfBoundsException e) {
  70. }
  71. i++;
  72. }
  73. setNumbers();
  74. }
  75.  
  76. public void start(Stage theStage) {
  77. try {
  78.  
  79. FileInputStream input = new FileInputStream("res/face-smile.png");
  80. Image image = new Image(input);
  81. ImageView imageView = new ImageView(image);
  82. HBox hbox2 = new HBox();
  83. hbox2.setPrefWidth((fieldSizeX * 30) / 3);
  84. Button resetButton = new Button("", imageView);
  85. resetButton.setMinSize(50, 50);
  86. resetButton.setMaxSize(50, 50);
  87. imageView.setFitHeight(0);
  88. imageView.setFitWidth(0);
  89. hbox2.setAlignment(Pos.CENTER);
  90. hbox2.getChildren().add(resetButton);
  91. input.close();
  92. BorderPane bPane = new BorderPane();
  93. BorderPane bPane2 = new BorderPane();
  94. BorderPane bPane3 = new BorderPane();
  95. HBox hbox = new HBox();
  96. hbox.setPrefWidth(fieldSizeX * 10);
  97. hbox.getChildren().add(new Label("000"));
  98. HBox hbox3 = new HBox();
  99. hbox3.setPrefWidth(fieldSizeX * 10);
  100. String bombsLeft = ("0" + bombsRemaining);
  101. hbox3.getChildren().add(new Label(bombsLeft));
  102. // Button newButton = new Button("top");
  103. GridPane gridTop = new GridPane();
  104. gridTop.add(bPane2, 0, 0);
  105. gridTop.add(bPane3, 2, 0);
  106. bPane2.setCenter(hbox);
  107. bPane3.setCenter(hbox3);
  108. bPane.setTop(gridTop);
  109. gridTop.setMinSize(100, 30);
  110. gridTop.setMaxSize(1000, 100);
  111. gridTop.add(hbox3, 0, 0);
  112. gridTop.add(hbox2, 1, 0);
  113. gridTop.add(hbox, 2, 0);
  114. GridPane grid = new GridPane();
  115. bPane.setCenter(grid);
  116. for (int i = 0; i < fieldSizeX; i++) {
  117. for (int j = 0; j < fieldSizeY; j++) {
  118. final int a = i;
  119. final int b = j;
  120. ImageView img = new ImageView(new Image(new FileInputStream("res/cover.png")));
  121. Button MSButton = new Button("res/cover.png");
  122. Button button = new Button("", img);
  123. button.setMinSize(30, 30);
  124. button.setMaxSize(30, 30);
  125. img.setFitHeight(0);
  126. img.setFitWidth(0);
  127. img.fitWidthProperty().add(button.widthProperty());
  128. img.fitHeightProperty().add(button.widthProperty());
  129. bList[j][i] = button;
  130. button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
  131. @Override
  132. public void handle(MouseEvent e) {
  133. if (e.getButton() == MouseButton.SECONDARY) {
  134. if (numberRevealed[b][a] == false) {
  135. if (isFlagged[b][a] == false)
  136. isFlagged[b][a] = true;
  137. else
  138. isFlagged[b][a] = false;
  139. if (isFlagged[b][a] == true) {
  140. if (bombsRemaining > 0) {
  141. try {
  142. ImageView img = new ImageView(
  143. new Image(new FileInputStream("res/flag.png")));
  144. bList[b][a].setMaxHeight(0);
  145. bList[b][a].setMaxWidth(0);
  146. img.fitWidthProperty().add(bList[b][a].widthProperty());
  147. img.fitHeightProperty().add(bList[b][a].heightProperty());
  148. bList[b][a].setGraphic(img);
  149. bombsRemaining--;
  150. String bombsLeft = ("0" + bombsRemaining);
  151. hbox3.getChildren().clear();
  152. hbox3.getChildren().add(new Label(bombsLeft));
  153. } catch (IOException g) {
  154. }
  155. } else {
  156. isFlagged[b][a] = false;
  157. }
  158. } else if (isFlagged[b][a] == false) {
  159. try {
  160. ImageView img = new ImageView(
  161. new Image(new FileInputStream("res/cover.png")));
  162. bList[b][a].setMaxHeight(0);
  163. bList[b][a].setMaxWidth(0);
  164. img.fitWidthProperty().add(bList[b][a].widthProperty());
  165. img.fitHeightProperty().add(bList[b][a].heightProperty());
  166. bList[b][a].setGraphic(img);
  167. bombsRemaining++;
  168. String bombsLeft = ("0" + bombsRemaining);
  169. hbox3.getChildren().clear();
  170. hbox3.getChildren().add(new Label(bombsLeft));
  171. } catch (IOException g) {
  172. }
  173. }
  174.  
  175. }
  176. }
  177. if (e.getButton() == MouseButton.PRIMARY) {
  178. if (isFlagged[b][a] == false) {
  179. if (mField[b][a] == 0) {
  180. recur(bList[b][a], b, a, 0);
  181. }
  182. if (mField[b][a] == -1) {
  183. gameOver(resetButton, bList[b][a]);
  184. } else if (isClicked[b][a] == true) {
  185. checkFlags(b, a, bList[b][a], resetButton);
  186. } else if (mField[b][a] >= 0) {
  187. showNumber(bList[b][a], b, a);
  188. }
  189. if (counter == fieldSizeX * fieldSizeY - bombs)
  190. won(resetButton);
  191. }
  192. // if(firstClick == true)
  193. // staticPos();
  194. }
  195. e.consume();
  196. }
  197. });
  198. grid.add(bList[j][i], i, j);
  199. }
  200. }
  201. resetButton.setOnAction(new EventHandler<ActionEvent>() {
  202. @Override
  203. public void handle(ActionEvent event) {
  204. resetGame(difficulty);
  205. start(theStage);
  206. }
  207. });
  208. theStage.setScene(new Scene(bPane, fieldSizeX * 30, (fieldSizeY * 30) + 50));
  209. } catch (IOException e) {
  210. }
  211. theStage.show();
  212. }
  213.  
  214. public static void showNumber(Button b, int c, int a) {
  215. if (flag == false) {
  216. numberRevealed[c][a] = true;
  217. }
  218. flag = false;
  219. if (mField[c][a] == 0) {
  220. try {
  221. ImageView img = new ImageView(new Image(new FileInputStream("res/0.png")));
  222. b.setMaxHeight(0);
  223. b.setMaxWidth(0);
  224. img.fitWidthProperty().add(b.widthProperty());
  225. img.fitHeightProperty().add(b.heightProperty());
  226. b.setGraphic(img);
  227. if (isClicked[c][a] == false) {
  228. counter++;
  229. isClicked[c][a] = true;
  230. }
  231. } catch (IOException e) {
  232. }
  233. } else if (mField[c][a] == 2) {
  234. try {
  235. ImageView img = new ImageView(new Image(new FileInputStream("res/2.png")));
  236. b.setMaxHeight(0);
  237. b.setMaxWidth(0);
  238. img.fitWidthProperty().add(b.widthProperty());
  239. img.fitHeightProperty().add(b.heightProperty());
  240. b.setGraphic(img);
  241. if (isClicked[c][a] == false) {
  242. counter++;
  243. isClicked[c][a] = true;
  244. }
  245. } catch (IOException e) {
  246. }
  247. } else if (mField[c][a] == 1) {
  248. try {
  249. ImageView img = new ImageView(new Image(new FileInputStream("res/1.png")));
  250. b.setMaxHeight(0);
  251. b.setMaxWidth(0);
  252. img.fitWidthProperty().add(b.widthProperty());
  253. img.fitHeightProperty().add(b.heightProperty());
  254. b.setGraphic(img);
  255. if (isClicked[c][a] == false) {
  256. counter++;
  257. isClicked[c][a] = true;
  258. }
  259. } catch (IOException e) {
  260. }
  261. } else if (mField[c][a] == 3) {
  262. try {
  263. ImageView img = new ImageView(new Image(new FileInputStream("res/3.png")));
  264. b.setMaxHeight(0);
  265. b.setMaxWidth(0);
  266. img.fitWidthProperty().add(b.widthProperty());
  267. img.fitHeightProperty().add(b.heightProperty());
  268. b.setGraphic(img);
  269. if (isClicked[c][a] == false) {
  270. counter++;
  271. isClicked[c][a] = true;
  272. }
  273. } catch (IOException e) {
  274. }
  275. } else if (mField[c][a] == 4) {
  276. try {
  277. ImageView img = new ImageView(new Image(new FileInputStream("res/4.png")));
  278. img.fitWidthProperty().add(b.widthProperty());
  279. img.fitHeightProperty().add(b.heightProperty());
  280. b.setGraphic(img);
  281. if (isClicked[c][a] == false) {
  282. counter++;
  283. isClicked[c][a] = true;
  284. }
  285. } catch (IOException e) {
  286. }
  287. } else if (mField[c][a] == 5) {
  288. try {
  289. ImageView img = new ImageView(new Image(new FileInputStream("res/5.png")));
  290. img.fitWidthProperty().add(b.widthProperty());
  291. img.fitHeightProperty().add(b.heightProperty());
  292. b.setGraphic(img);
  293. if (isClicked[c][a] == false) {
  294. counter++;
  295. isClicked[c][a] = true;
  296. }
  297. } catch (IOException e) {
  298. }
  299. } else if (mField[c][a] == 6) {
  300. try {
  301. ImageView img = new ImageView(new Image(new FileInputStream("res/6.png")));
  302. img.fitWidthProperty().add(b.widthProperty());
  303. img.fitHeightProperty().add(b.heightProperty());
  304. b.setGraphic(img);
  305. if (isClicked[c][a] == false) {
  306. counter++;
  307. isClicked[c][a] = true;
  308. }
  309. } catch (IOException e) {
  310. }
  311. } else if (mField[c][a] == 7) {
  312. try {
  313. ImageView img = new ImageView(new Image(new FileInputStream("res/7.png")));
  314. img.fitWidthProperty().add(b.widthProperty());
  315. img.fitHeightProperty().add(b.heightProperty());
  316. b.setGraphic(img);
  317. if (isClicked[c][a] == false) {
  318. counter++;
  319. isClicked[c][a] = true;
  320. }
  321. } catch (IOException e) {
  322. }
  323. } else if (mField[c][a] == 8) {
  324. try {
  325. ImageView img = new ImageView(new Image(new FileInputStream("res/8.png")));
  326. img.fitWidthProperty().add(b.widthProperty());
  327. img.fitHeightProperty().add(b.heightProperty());
  328. b.setGraphic(img);
  329. if (isClicked[c][a] == false) {
  330. counter++;
  331. isClicked[c][a] = true;
  332. }
  333. } catch (IOException e) {
  334. }
  335. }
  336.  
  337. }
  338.  
  339. public static void showNumber(Button b, int c, int a, int k) {
  340. if (flag == false) {
  341. numberRevealed[c][a] = true;
  342. }
  343. flag = false;
  344. if (mField[c][a] == 0) {
  345. try {
  346. ImageView img = new ImageView(new Image(new FileInputStream("res/0.png")));
  347. b.setMaxHeight(0);
  348. b.setMaxWidth(0);
  349. img.fitWidthProperty().add(b.widthProperty());
  350. img.fitHeightProperty().add(b.heightProperty());
  351. b.setGraphic(img);
  352. if (isClicked[c][a] == false) {
  353. counter++;
  354. isClicked[c][a] = true;
  355. }
  356. } catch (IOException e) {
  357. }
  358. } else if (mField[c][a] == 2) {
  359. try {
  360. ImageView img = new ImageView(new Image(new FileInputStream("res/2.png")));
  361. b.setMaxHeight(0);
  362. b.setMaxWidth(0);
  363. img.fitWidthProperty().add(b.widthProperty());
  364. img.fitHeightProperty().add(b.heightProperty());
  365. b.setGraphic(img);
  366. if (isClicked[c][a] == false) {
  367. counter++;
  368. isClicked[c][a] = true;
  369. }
  370. } catch (IOException e) {
  371. }
  372. } else if (mField[c][a] == 1) {
  373. try {
  374. ImageView img = new ImageView(new Image(new FileInputStream("res/1.png")));
  375. b.setMaxHeight(0);
  376. b.setMaxWidth(0);
  377. img.fitWidthProperty().add(b.widthProperty());
  378. img.fitHeightProperty().add(b.heightProperty());
  379. b.setGraphic(img);
  380. if (isClicked[c][a] == false) {
  381. counter++;
  382. isClicked[c][a] = true;
  383. }
  384. } catch (IOException e) {
  385. }
  386. } else if (mField[c][a] == 3) {
  387. try {
  388. ImageView img = new ImageView(new Image(new FileInputStream("res/3.png")));
  389. b.setMaxHeight(0);
  390. b.setMaxWidth(0);
  391. img.fitWidthProperty().add(b.widthProperty());
  392. img.fitHeightProperty().add(b.heightProperty());
  393. b.setGraphic(img);
  394. if (isClicked[c][a] == false) {
  395. counter++;
  396. isClicked[c][a] = true;
  397. }
  398. } catch (IOException e) {
  399. }
  400. } else if (mField[c][a] == 4) {
  401. try {
  402. ImageView img = new ImageView(new Image(new FileInputStream("res/4.png")));
  403. img.fitWidthProperty().add(b.widthProperty());
  404. img.fitHeightProperty().add(b.heightProperty());
  405. b.setGraphic(img);
  406. if (isClicked[c][a] == false) {
  407. counter++;
  408. isClicked[c][a] = true;
  409. }
  410. } catch (IOException e) {
  411. }
  412. } else if (mField[c][a] == 5) {
  413. try {
  414. ImageView img = new ImageView(new Image(new FileInputStream("res/5.png")));
  415. img.fitWidthProperty().add(b.widthProperty());
  416. img.fitHeightProperty().add(b.heightProperty());
  417. b.setGraphic(img);
  418. if (isClicked[c][a] == false) {
  419. counter++;
  420. isClicked[c][a] = true;
  421. }
  422. } catch (IOException e) {
  423. }
  424. } else if (mField[c][a] == 6) {
  425. try {
  426. ImageView img = new ImageView(new Image(new FileInputStream("res/6.png")));
  427. img.fitWidthProperty().add(b.widthProperty());
  428. img.fitHeightProperty().add(b.heightProperty());
  429. b.setGraphic(img);
  430. if (isClicked[c][a] == false) {
  431. counter++;
  432. isClicked[c][a] = true;
  433. }
  434. } catch (IOException e) {
  435. }
  436. } else if (mField[c][a] == 7) {
  437. try {
  438. ImageView img = new ImageView(new Image(new FileInputStream("res/7.png")));
  439. img.fitWidthProperty().add(b.widthProperty());
  440. img.fitHeightProperty().add(b.heightProperty());
  441. b.setGraphic(img);
  442. if (isClicked[c][a] == false) {
  443. counter++;
  444. isClicked[c][a] = true;
  445. }
  446. } catch (IOException e) {
  447. }
  448. } else if (mField[c][a] == 8) {
  449. try {
  450. ImageView img = new ImageView(new Image(new FileInputStream("res/8.png")));
  451. img.fitWidthProperty().add(b.widthProperty());
  452. img.fitHeightProperty().add(b.heightProperty());
  453. b.setGraphic(img);
  454. if (isClicked[c][a] == false) {
  455. counter++;
  456. isClicked[c][a] = true;
  457. }
  458. } catch (IOException e) {
  459. }
  460. }
  461.  
  462. if (k > 0)
  463. recur(bList[c][a], c, a, k);
  464.  
  465. }
  466.  
  467. public static void gameOver(Button b, Button b2) {
  468. gameIsOver = true;
  469. try {
  470. b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-dead.png"))));
  471. for (int i = 0; i < fieldSizeX; i++) {
  472. for (int j = 0; j < fieldSizeY; j++) {
  473. if (mField[j][i] == -1) {
  474. Button bu = bList[j][i];
  475. bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-grey.png"))));
  476. }
  477. }
  478. }
  479. b2.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  480. for (int i = 0; i < fieldSizeX; i++) {
  481. for (int j = 0; j < fieldSizeY; j++) {
  482. bList[j][i].setDisable(true);
  483. }
  484. }
  485. } catch (IOException e) {
  486. }
  487. }
  488.  
  489. public static void gameOver(Button b, Button[][][] b2, Button[][][] b3) {
  490. gameIsOver = true;
  491. try {
  492. b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-dead.png"))));
  493.  
  494. for (int i = 0; i < fieldSizeX; i++) {
  495. for (int j = 0; j < fieldSizeY; j++) {
  496. if (isFlagged[j][i] == true && mField[j][i] != -1) {
  497. Button bu = bList[j][i];
  498. bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  499. }
  500. if (mField[j][i] == -1) {
  501. Button bu = bList[j][i];
  502. bu.setGraphic(new ImageView(new Image(new FileInputStream("res/mine-grey.png"))));
  503. }
  504. }
  505. }
  506. for (int i = 0; i < fieldSizeX; i++) {
  507. for (int j = 0; j < fieldSizeY; j++) {
  508. bList[j][i].setDisable(true);
  509. }
  510. }
  511.  
  512. } catch (IOException e) {
  513. }
  514. }
  515.  
  516. public static void won(Button b) {
  517. gameIsOver = true;
  518. try {
  519. b.setGraphic(new ImageView(new Image(new FileInputStream("res/face-win.png"))));
  520. } catch (IOException e) {
  521. }
  522. }
  523.  
  524. public static void checkFlags(int b, int a, Button button, Button resetButton) {
  525. boolean endGame = false;
  526. Button[][][] changeButtons = new Button[8][1][1];
  527. Button[][][] redButtons = new Button[8][1][1];
  528. int u = 0;
  529. int p = 0;
  530. int tilesToGet = 0;
  531. int[][][] field = new int[8][2][1];
  532. int valueOfFlag = mField[b][a];
  533. int counter = 0;
  534. boolean ul = true;
  535. boolean um = true;
  536. boolean ur = true;
  537. boolean ml = true;
  538. boolean mr = true;
  539. boolean bm = true;
  540. boolean br = true;
  541. boolean bl = true;
  542. if (b == 0) {
  543. bl = false;
  544. ul = false;
  545. ml = false;
  546. }
  547. if (b == mField.length - 1) {
  548. br = false;
  549. ur = false;
  550. mr = false;
  551. }
  552. if (a == 0) {
  553. um = false;
  554. ul = false;
  555. ur = false;
  556. }
  557. if (a == mField[0].length - 1) {
  558. bl = false;
  559. bm = false;
  560. br = false;
  561. }
  562. // 1
  563. if (ul)
  564. if (isFlagged[b - 1][a - 1] == true)
  565. u++;
  566. if (um)
  567. if (isFlagged[b][a - 1] == true)
  568. u++;
  569.  
  570. if (ur)
  571. if (isFlagged[b + 1][a - 1] == true)
  572. u++;
  573. if (ml)
  574. if (isFlagged[b - 1][a] == true)
  575. u++;
  576. if (mr)
  577. if (isFlagged[b + 1][a] == true)
  578. u++;
  579. if (bl)
  580. if (isFlagged[b - 1][a + 1] == true)
  581. u++;
  582. if (br)
  583. if (isFlagged[b + 1][a + 1] == true)
  584. u++;
  585. if (bm)
  586. if (isFlagged[b][a + 1] == true)
  587. u++;
  588.  
  589. if (ul) {
  590. tilesToGet++;
  591. if (mField[b - 1][a - 1] != -1 && isFlagged[b - 1][a - 1] == true) {
  592. if (u == valueOfFlag) {
  593. try {
  594. bList[b - 1][a - 1]
  595. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  596. } catch (FileNotFoundException e) {
  597. }
  598. }
  599. endGame = true;
  600. }
  601. if (mField[b - 1][a - 1] == -1 && isFlagged[b - 1][a - 1] == false) {
  602. if (u == valueOfFlag) {
  603. try {
  604. bList[b - 1][a - 1]
  605. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  606. } catch (FileNotFoundException e) {
  607. }
  608. }
  609. }
  610. if (mField[b - 1][a - 1] == -1 && isFlagged[b - 1][a - 1] == true) {
  611.  
  612. } else {
  613. field[counter][0][0] = (b - 1);
  614. field[counter][1][0] = (a - 1);
  615. counter++;
  616. }
  617.  
  618. }
  619. // 2
  620. if (um) {
  621. tilesToGet++;
  622. if (mField[b][a - 1] != -1 && isFlagged[b][a - 1] == true) {
  623. if (u == valueOfFlag) {
  624. try {
  625. bList[b][a - 1]
  626. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  627. } catch (FileNotFoundException e) {
  628. }
  629. }
  630. endGame = true;
  631. }
  632. if (mField[b][a - 1] == -1 && isFlagged[b][a - 1] == false) {
  633. if (u == valueOfFlag) {
  634. try {
  635. bList[b][a - 1].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  636. } catch (FileNotFoundException e) {
  637. }
  638. }
  639. }
  640. if (mField[b][a - 1] == -1 && isFlagged[b][a - 1] == true) {
  641.  
  642. } else {
  643. field[counter][0][0] = (b);
  644. field[counter][1][0] = (a - 1);
  645. counter++;
  646. }
  647. }
  648. // 3
  649. if (ur) {
  650. tilesToGet++;
  651. if (mField[b + 1][a - 1] != -1 && isFlagged[b + 1][a - 1] == true) {
  652. if (u == valueOfFlag) {
  653. try {
  654. bList[b + 1][a - 1]
  655. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  656. } catch (FileNotFoundException e) {
  657. }
  658. }
  659. endGame = true;
  660. }
  661. if (mField[b + 1][a - 1] == -1 && isFlagged[b + 1][a - 1] == false) {
  662. if (u == valueOfFlag) {
  663. try {
  664. bList[b + 1][a - 1]
  665. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  666. } catch (FileNotFoundException e) {
  667. }
  668. }
  669. }
  670. if (mField[b + 1][a - 1] == -1 && isFlagged[b + 1][a - 1] == true) {
  671.  
  672. } else {
  673. field[counter][0][0] = (b + 1);
  674. field[counter][1][0] = (a - 1);
  675. counter++;
  676. }
  677. }
  678. // 4
  679. if (ml) {
  680. tilesToGet++;
  681. if (mField[b - 1][a] != -1 && isFlagged[b - 1][a] == true) {
  682. if (u == valueOfFlag) {
  683. try {
  684. bList[b - 1][a]
  685. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  686. } catch (FileNotFoundException e) {
  687. }
  688. }
  689. endGame = true;
  690. }
  691. if (mField[b - 1][a] == -1 && isFlagged[b - 1][a] == false) {
  692. if (u == valueOfFlag) {
  693. try {
  694. bList[b - 1][a]
  695. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  696. } catch (FileNotFoundException e) {
  697. }
  698. }
  699. endGame = true;
  700. }
  701. if (mField[b - 1][a] == -1 && isFlagged[b - 1][a] == true) {
  702.  
  703. } else {
  704. field[counter][0][0] = (b - 1);
  705. field[counter][1][0] = (a);
  706. counter++;
  707. }
  708. }
  709. // 5
  710. if (mr) {
  711. tilesToGet++;
  712. if (mField[b + 1][a] != -1 && isFlagged[b + 1][a] == true) {
  713. if (u == valueOfFlag) {
  714. try {
  715. bList[b + 1][a]
  716. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  717. } catch (FileNotFoundException e) {
  718. }
  719. }
  720. endGame = true;
  721. }
  722. if (mField[b + 1][a] == -1 && isFlagged[b + 1][a] == false) {
  723. if (u == valueOfFlag) {
  724. try {
  725. bList[b + 1][a].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  726. } catch (FileNotFoundException e) {
  727. }
  728. }
  729. }
  730. if (mField[b + 1][a] == -1 && isFlagged[b + 1][a] == true) {
  731. } else {
  732. field[counter][0][0] = (b + 1);
  733. field[counter][1][0] = (a);
  734. counter++;
  735. }
  736. }
  737. // 6
  738. if (bl) {
  739.  
  740. tilesToGet++;
  741. if (mField[b - 1][a + 1] != -1 && isFlagged[b - 1][a + 1] == true) {
  742. if (u == valueOfFlag) {
  743. try {
  744. bList[b - 1][a + 1]
  745. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  746. } catch (FileNotFoundException e) {
  747. }
  748. }
  749. endGame = true;
  750. }
  751. if (mField[b - 1][a + 1] == -1 && isFlagged[b - 1][a + 1] == false) {
  752. if (u == valueOfFlag) {
  753. try {
  754. bList[b - 1][a + 1]
  755. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  756. } catch (FileNotFoundException e) {
  757. }
  758. }
  759. }
  760. if (mField[b - 1][a + 1] == -1 && isFlagged[b - 1][a + 1] == true) {
  761.  
  762. } else {
  763. field[counter][0][0] = (b - 1);
  764. field[counter][1][0] = (a + 1);
  765. counter++;
  766. }
  767. }
  768. // 7
  769. if (br) {
  770. tilesToGet++;
  771. if (mField[b + 1][a + 1] != -1 && isFlagged[b + 1][a + 1] == true) {
  772. if (u == valueOfFlag) {
  773. try {
  774. bList[b + 1][a + 1]
  775. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  776. } catch (FileNotFoundException e) {
  777. }
  778. }
  779. endGame = true;
  780. }
  781. if (mField[b + 1][a + 1] == -1 && isFlagged[b + 1][a + 1] == false) {
  782. if (u == valueOfFlag) {
  783. try {
  784. bList[b + 1][a + 1]
  785. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  786. } catch (FileNotFoundException e) {
  787. }
  788. }
  789. }
  790. if (mField[b + 1][a + 1] == -1 && isFlagged[b + 1][a + 1] == true) {
  791. } else {
  792. field[counter][0][0] = (b + 1);
  793. field[counter][1][0] = (a + 1);
  794. counter++;
  795. }
  796. }
  797. // 8
  798. if (bm) {
  799. tilesToGet++;
  800. if (mField[b][a + 1] != -1 && isFlagged[b][a + 1] == true) {
  801. if (u == valueOfFlag) {
  802. try {
  803. bList[b][a + 1]
  804. .setGraphic(new ImageView(new Image(new FileInputStream("res/mine-misflagged.png"))));
  805. } catch (FileNotFoundException e) {
  806. }
  807. }
  808. endGame = true;
  809. }
  810. if (mField[b][a + 1] == -1 && isFlagged[b][a + 1] == false) {
  811. if (u == valueOfFlag) {
  812. try {
  813. bList[b][a + 1].setGraphic(new ImageView(new Image(new FileInputStream("res/mine-red.png"))));
  814. } catch (FileNotFoundException e) {
  815. }
  816. }
  817. }
  818. if (mField[b][a + 1] == -1 && isFlagged[b][a + 1] == true) {
  819.  
  820. } else {
  821. field[counter][0][0] = (b);
  822. field[counter][1][0] = (a + 1);
  823. counter++;
  824. }
  825. }
  826. if (endGame && u == valueOfFlag) {
  827. gameOver(resetButton, changeButtons, redButtons);
  828. }
  829. if ((tilesToGet - counter) == valueOfFlag) {
  830.  
  831. endGame = false;
  832. for (int l = 0; l < counter; l++) {
  833. if (isFlagged[field[l][0][0]][field[l][1][0]] == false) {
  834. showNumber(bList[field[l][0][0]][field[l][1][0]], field[l][0][0], field[l][1][0]);
  835. }
  836. }
  837.  
  838. }
  839. counter = 0;
  840. ul = true;
  841. um = true;
  842. ur = true;
  843. ml = true;
  844. mr = true;
  845. bm = true;
  846. br = true;
  847. bl = true;
  848. }
  849.  
  850. public static void recur(Button b, int c, int a, int count) {
  851. for (int i = 0; i < fieldSizeX; i++) {
  852. for (int j = 0; j < fieldSizeY; j++) {
  853.  
  854. boolean ul = true;
  855. boolean um = true;
  856. boolean ur = true;
  857. boolean ml = true;
  858. boolean mr = true;
  859. boolean bm = true;
  860. boolean br = true;
  861. boolean bl = true;
  862. if (c == 0) {
  863. bl = false;
  864. ul = false;
  865. ml = false;
  866. }
  867. if (c == mField.length - 1) {
  868. br = false;
  869. ur = false;
  870. mr = false;
  871. }
  872. if (a == 0) {
  873. um = false;
  874. ul = false;
  875. ur = false;
  876. }
  877. if (a == mField[0].length - 1) {
  878. bl = false;
  879. bm = false;
  880. br = false;
  881. }
  882. // 1
  883. if (ul)
  884. if (isClicked[c - 1][a - 1] == false && mField[c][a] == 0) {
  885. showNumber(bList[c - 1][a - 1], c - 1, a - 1, count + 1);
  886. }
  887. if (um)
  888. if (isClicked[c][a - 1] == false && mField[c][a] == 0) {
  889. showNumber(bList[c][a - 1], c, a - 1, count + 1);
  890. }
  891. if (ur)
  892. if (isClicked[c + 1][a - 1] == false && mField[c][a] == 0) {
  893. showNumber(bList[c + 1][a - 1], c + 1, a - 1, count + 1);
  894. }
  895. if (ml)
  896. if (isClicked[c - 1][a] == false && mField[c][a] == 0) {
  897. showNumber(bList[c - 1][a], c - 1, a, count + 1);
  898. }
  899. if (mr)
  900. if (isClicked[c + 1][a] == false && mField[c][a] == 0) {
  901. showNumber(bList[c + 1][a], c + 1, a, count + 1);
  902. }
  903. if (bl)
  904. if (isClicked[c - 1][a + 1] == false && mField[c][a] == 0) {
  905. showNumber(bList[c - 1][a + 1], c - 1, a + 1, count + 1);
  906. }
  907. if (br)
  908. if (isClicked[c + 1][a + 1] == false && mField[c][a] == 0) {
  909. showNumber(bList[c + 1][a + 1], c + 1, a + 1, count + 1);
  910. }
  911. if (bm)
  912. if (isClicked[c][a + 1] == false && mField[c][a] == 0) {
  913. showNumber(bList[c][a + 1], c, a + 1, count + 1);
  914. }
  915. }
  916. }
  917.  
  918. }
  919.  
  920. public static void setNumbers() {
  921. boolean ul = true;
  922. boolean um = true;
  923. boolean ur = true;
  924. boolean ml = true;
  925. boolean mr = true;
  926. boolean bm = true;
  927. boolean br = true;
  928. boolean bl = true;
  929. for (int i = 0; i < fieldSizeX; i++) {
  930. for (int j = 0; j < fieldSizeY; j++) {
  931. if (mField[j][i] == -1) {
  932. if (j == 0) {
  933. bl = false;
  934. ul = false;
  935. ml = false;
  936. }
  937. if (j == mField.length - 1) {
  938. br = false;
  939. ur = false;
  940. mr = false;
  941. }
  942. if (i == 0) {
  943. um = false;
  944. ul = false;
  945. ur = false;
  946. }
  947. if (i == mField[0].length - 1) {
  948. bl = false;
  949. bm = false;
  950. br = false;
  951. }
  952. if (ul) {
  953. if (mField[j - 1][i - 1] != -1)
  954. mField[j - 1][i - 1]++;
  955. }
  956. if (um) {
  957. if (mField[j][i - 1] != -1)
  958. mField[j][i - 1]++;
  959. }
  960. if (ur) {
  961. if (mField[j + 1][i - 1] != -1)
  962. mField[j + 1][i - 1]++;
  963. }
  964. if (ml) {
  965. if (mField[j - 1][i] != -1)
  966. mField[j - 1][i]++;
  967. }
  968. if (mr) {
  969. if (mField[j + 1][i] != -1)
  970. mField[j + 1][i]++;
  971. }
  972. if (bl) {
  973. if (mField[j - 1][i + 1] != -1)
  974. mField[j - 1][i + 1]++;
  975. }
  976. if (br) {
  977. if (mField[j + 1][i + 1] != -1)
  978. mField[j + 1][i + 1]++;
  979. }
  980. if (bm) {
  981. if (mField[j][i + 1] != -1)
  982. mField[j][i + 1]++;
  983. }
  984. ul = true;
  985. um = true;
  986. ur = true;
  987. ml = true;
  988. mr = true;
  989. bm = true;
  990. br = true;
  991. bl = true;
  992. }
  993. }
  994. }
  995. }
  996.  
  997. public static void resetGame(String difficulty) {
  998. firstClick = true;
  999. spaces = 0;
  1000. bombs = 0;
  1001. bombsRemaining = 0;
  1002. win = false;
  1003. counter = 0;
  1004.  
  1005. if (difficulty.equals("b")) {
  1006. fieldSizeX = 8;
  1007. fieldSizeY = 8;
  1008. bombs = 10;
  1009. } else if (difficulty.equals("i")) {
  1010. fieldSizeX = 16;
  1011. fieldSizeY = 16;
  1012. bombs = 40;
  1013. } else if (difficulty.equals("e")) {
  1014. fieldSizeX = 32;
  1015. fieldSizeY = 16;
  1016. bombs = 99;
  1017. }
  1018. mField = new int[fieldSizeY][fieldSizeX];
  1019. isFlagged = new boolean[fieldSizeY][fieldSizeX];
  1020. isClicked = new boolean[fieldSizeY][fieldSizeX];
  1021. numberRevealed = new boolean[fieldSizeY][fieldSizeX];
  1022. bList = new Button[fieldSizeY][fieldSizeX];
  1023. for (int i = 0; i < fieldSizeX; i++) {
  1024. for (int j = 0; j < fieldSizeY; j++) {
  1025. mField[j][i] = 0;
  1026. isFlagged[j][i] = false;
  1027. isClicked[j][i] = false;
  1028. numberRevealed[j][i] = false;
  1029. bList[j][i] = null;
  1030. }
  1031. }
  1032. bombsRemaining = bombs;
  1033. // if(firstClick == false)
  1034.  
  1035.  
  1036. staticPos();
  1037.  
  1038. }
  1039. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement