Advertisement
Guest User

puzzlecontroller

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.collections.ObservableList;
  4. import javafx.concurrent.Task;
  5. import javafx.fxml.FXML;
  6. import javafx.scene.Node;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.image.Image;
  9. import javafx.scene.layout.GridPane;
  10.  
  11. import java.util.ArrayList;
  12.  
  13. public class PuzzleGameController {
  14. @FXML
  15. GridPane puzzleBox;
  16. @FXML
  17. Label timer;
  18. static PuzzleImage usedImage;
  19. ArrayList<Integer> actualRows = new ArrayList<Integer>(usedImage.rowInd);
  20. ArrayList<Integer> actualColumns = new ArrayList<Integer>(usedImage.colInd);
  21. ArrayList<Integer> winRows = new ArrayList<Integer>(usedImage.rowInd);
  22. ArrayList<Integer> winColumns = new ArrayList<Integer>(usedImage.colInd);
  23. TileView[][] tileList = new TileView[winRows.size()][winColumns.size()];
  24. int blankRow;
  25. int blankCol;
  26. long minutes, startMilis, seconds;
  27. String time;
  28. boolean won = false;
  29. private Thread thread;
  30. String timeText;
  31.  
  32. @FXML
  33. public void startTime(){
  34. boolean start = true;
  35. Task timeTask = new Task(){
  36. @Override
  37. protected String call() throws InterruptedException {
  38. startMilis = System.currentTimeMillis();
  39. minutes = 0;
  40. while (start) {
  41. seconds = (System.currentTimeMillis() - startMilis) / 1000;
  42. if (seconds == 60) {
  43. minutes++;
  44. startMilis = System.currentTimeMillis();
  45. seconds = 0;
  46. }
  47. time = ("Minutes: " + minutes + " Seconds: " + seconds);
  48. updateMessage(time);
  49. Thread.sleep(10);
  50. if (won) {
  51. timeText = time.toString();
  52. }
  53. }
  54. return null;
  55. }
  56. };
  57. timer.textProperty().bind(timeTask.messageProperty());
  58. thread = new Thread(timeTask);
  59. thread.setDaemon(true);
  60. thread.start();
  61. }
  62.  
  63. @FXML
  64. public boolean allRight(ObservableList<Node> children){
  65. ArrayList<TileView> tiles = new ArrayList<>();
  66. for (Node currentNode : children){
  67. if (currentNode instanceof TileView){
  68. tiles.add((TileView)currentNode);
  69. }
  70. }
  71. for(TileView tile:tiles) if(!tile.isRight()) return false;
  72. return true;
  73. }
  74.  
  75. public int whereIsBlank(int row, int col) throws NullPointerException,IndexOutOfBoundsException {
  76. try {
  77. //gora
  78. if (row - 1 >= 0 && tileList[row - 1][col].getImage() == (null)){
  79. return 0;
  80. }
  81. }catch(NullPointerException nullEx){
  82. nullEx.printStackTrace();
  83. }
  84. try{
  85. //prawo
  86. if (col + 1 <= usedImage.rectNum/2 -1 && tileList[row][col+1].getImage() == (null)) {
  87. return 1;
  88. }
  89. }catch(NullPointerException nullEx){
  90. nullEx.printStackTrace();
  91. }
  92. try{
  93. //dol
  94. if (row + 1 <= 1 && tileList[row+1][col].getImage() == (null)) {
  95. return 2;
  96. }
  97. }catch(NullPointerException nullEx){
  98. nullEx.printStackTrace();
  99. }
  100. try{
  101. //lewo
  102. if (col - 1 >= 0 && tileList[row][col-1].getImage() == (null)) {
  103. return 3;
  104. }
  105. }catch(NullPointerException nullEx){
  106. nullEx.printStackTrace();
  107. }
  108. //zaden warunek nie spelniony -> to obiekt klikany jest pusty
  109. if(true){
  110. System.err.println("Zablokowany");
  111. return -1;
  112. }
  113. System.err.println("Co");
  114. return -1;
  115. }
  116.  
  117. public void moveImg(int where, int row, int col){
  118. Image tmp;
  119. int idClicked, idFilled;
  120. idClicked = (col+(usedImage.rectNum/2)*row);
  121. idFilled = (blankCol+(usedImage.rectNum/2)*blankRow);
  122. int tempA = row;
  123. int tempB = col;
  124. int tempC = blankRow;
  125. int tempD = blankCol;
  126. switch(where){
  127. case 0:
  128. //podmieniamy kafelek klikniety z pustym
  129. tmp = tileList[row][col].getImage();
  130. tileList[blankRow][blankCol].setImage(tmp);
  131. tileList[row][col].setImage(null);
  132.  
  133. //zamieniono obraz na null
  134. //podmieniamy kolumny do porownania z ulozeniem prawidlowym
  135. tileList[row][col].nowY -= 1;
  136. tileList[row][col].movedUp += 1;
  137. blankCol = col;
  138. blankRow = row;
  139. System.out.println("W gore!");
  140. if(allRight(puzzleBox.getChildren())){
  141. win();
  142. }
  143. break;
  144.  
  145. case 1:
  146. //podmieniamy kafelek klikniety z pustym
  147. tmp = tileList[row][col].getImage();
  148. tileList[blankRow][blankCol].setImage(tmp);
  149. tileList[row][col].setImage(null);
  150.  
  151. tileList[row][col].nowX += 1;
  152. tileList[row][col].movedRight += 1;
  153. blankCol = col;
  154. blankRow = row;
  155. System.out.println("W prawo!");
  156.  
  157. if(allRight(puzzleBox.getChildren())){
  158. win();
  159. }
  160. break;
  161.  
  162. case 2:
  163. //podmieniamy kafelek klikniety z pustym
  164. tmp = tileList[row][col].getImage();
  165. tileList[blankRow][blankCol].setImage(tmp);
  166. tileList[row][col].setImage(null);
  167. tileList[row][col].nowY -= 1;
  168. tileList[row][col].movedDown += 1;
  169. blankCol = col;
  170. blankRow = row;
  171. System.out.println("W dol!");
  172. if(allRight(puzzleBox.getChildren())){
  173. win();
  174. }
  175. break;
  176.  
  177. case 3:
  178. //podmieniamy kafelek klikniety z pustym
  179. tmp = tileList[row][col].getImage();
  180. tileList[blankRow][blankCol].setImage(tmp);
  181. tileList[row][col].setImage(null);
  182. tileList[row][col].nowX -= 1;
  183. tileList[row][col].movedLeft += 1;
  184. blankCol = col;
  185. blankRow = row;
  186. System.out.println("W lewo!");
  187. if(allRight(puzzleBox.getChildren())){
  188. win();
  189. }
  190. break;
  191.  
  192. default:
  193. System.out.println("Nie do ruszenia");
  194. break;
  195. }
  196. }
  197.  
  198. public void win(){
  199. System.out.println("Wygrales!!!");
  200. }
  201.  
  202. public void initialize(){
  203. blankRow = (int)(Math.random()*(2 - 1 - 0 + 1) + 0);
  204. blankCol = (int)(Math.random()*(usedImage.rectNum/2 - 1 - 0 + 1) + 0);
  205. for(int i = 0; i < 2; i++) {
  206. for(int j = 0; j < usedImage.rectNum/2; j++) {
  207. tileList[i][j] = new TileView(usedImage.cutPieces.get(i*(usedImage.rectNum/2)+j));
  208. int finalRow = i;
  209. tileList[i][j].xPos = new Integer(i);
  210. tileList[i][j].yPos = new Integer(j);
  211. int finalColumn = j; //= tileList[i][j].y;
  212. tileList[i][j].nowX=new Integer(tileList[i][j].xPos);
  213. tileList[i][j].nowY=new Integer(tileList[i][j].yPos);
  214. tileList[i][j].setOnMouseClicked(Event ->{
  215. moveImg(whereIsBlank(finalRow, finalColumn),finalRow,finalColumn);
  216. //System.out.println("finalRow:"+finalRow);
  217. //System.out.println("finalColumn"+finalColumn);
  218.  
  219. });
  220. puzzleBox.getChildren().add(tileList[i][j]);
  221. puzzleBox.setConstraints(tileList[i][j], j, i);
  222. }
  223. }
  224. tileList[blankRow][blankCol].setImage(null);
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement