Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.collections.ObservableList;
- import javafx.concurrent.Task;
- import javafx.fxml.FXML;
- import javafx.scene.Node;
- import javafx.scene.control.Label;
- import javafx.scene.image.Image;
- import javafx.scene.layout.GridPane;
- import java.util.ArrayList;
- public class PuzzleGameController {
- @FXML
- GridPane puzzleBox;
- @FXML
- Label timer;
- static PuzzleImage usedImage;
- ArrayList<Integer> actualRows = new ArrayList<Integer>(usedImage.rowInd);
- ArrayList<Integer> actualColumns = new ArrayList<Integer>(usedImage.colInd);
- ArrayList<Integer> winRows = new ArrayList<Integer>(usedImage.rowInd);
- ArrayList<Integer> winColumns = new ArrayList<Integer>(usedImage.colInd);
- TileView[][] tileList = new TileView[winRows.size()][winColumns.size()];
- int blankRow;
- int blankCol;
- long minutes, startMilis, seconds;
- String time;
- boolean won = false;
- private Thread thread;
- String timeText;
- @FXML
- public void startTime(){
- boolean start = true;
- Task timeTask = new Task(){
- @Override
- protected String call() throws InterruptedException {
- startMilis = System.currentTimeMillis();
- minutes = 0;
- while (start) {
- seconds = (System.currentTimeMillis() - startMilis) / 1000;
- if (seconds == 60) {
- minutes++;
- startMilis = System.currentTimeMillis();
- seconds = 0;
- }
- time = ("Minutes: " + minutes + " Seconds: " + seconds);
- updateMessage(time);
- Thread.sleep(10);
- if (won) {
- timeText = time.toString();
- }
- }
- return null;
- }
- };
- timer.textProperty().bind(timeTask.messageProperty());
- thread = new Thread(timeTask);
- thread.setDaemon(true);
- thread.start();
- }
- @FXML
- public boolean allRight(ObservableList<Node> children){
- ArrayList<TileView> tiles = new ArrayList<>();
- for (Node currentNode : children){
- if (currentNode instanceof TileView){
- tiles.add((TileView)currentNode);
- }
- }
- for(TileView tile:tiles) if(!tile.isRight()) return false;
- return true;
- }
- public int whereIsBlank(int row, int col) throws NullPointerException,IndexOutOfBoundsException {
- try {
- //gora
- if (row - 1 >= 0 && tileList[row - 1][col].getImage() == (null)){
- return 0;
- }
- }catch(NullPointerException nullEx){
- nullEx.printStackTrace();
- }
- try{
- //prawo
- if (col + 1 <= usedImage.rectNum/2 -1 && tileList[row][col+1].getImage() == (null)) {
- return 1;
- }
- }catch(NullPointerException nullEx){
- nullEx.printStackTrace();
- }
- try{
- //dol
- if (row + 1 <= 1 && tileList[row+1][col].getImage() == (null)) {
- return 2;
- }
- }catch(NullPointerException nullEx){
- nullEx.printStackTrace();
- }
- try{
- //lewo
- if (col - 1 >= 0 && tileList[row][col-1].getImage() == (null)) {
- return 3;
- }
- }catch(NullPointerException nullEx){
- nullEx.printStackTrace();
- }
- //zaden warunek nie spelniony -> to obiekt klikany jest pusty
- if(true){
- System.err.println("Zablokowany");
- return -1;
- }
- System.err.println("Co");
- return -1;
- }
- public void moveImg(int where, int row, int col){
- Image tmp;
- int idClicked, idFilled;
- idClicked = (col+(usedImage.rectNum/2)*row);
- idFilled = (blankCol+(usedImage.rectNum/2)*blankRow);
- int tempA = row;
- int tempB = col;
- int tempC = blankRow;
- int tempD = blankCol;
- switch(where){
- case 0:
- //podmieniamy kafelek klikniety z pustym
- tmp = tileList[row][col].getImage();
- tileList[blankRow][blankCol].setImage(tmp);
- tileList[row][col].setImage(null);
- //zamieniono obraz na null
- //podmieniamy kolumny do porownania z ulozeniem prawidlowym
- tileList[row][col].nowY -= 1;
- tileList[row][col].movedUp += 1;
- blankCol = col;
- blankRow = row;
- System.out.println("W gore!");
- if(allRight(puzzleBox.getChildren())){
- win();
- }
- break;
- case 1:
- //podmieniamy kafelek klikniety z pustym
- tmp = tileList[row][col].getImage();
- tileList[blankRow][blankCol].setImage(tmp);
- tileList[row][col].setImage(null);
- tileList[row][col].nowX += 1;
- tileList[row][col].movedRight += 1;
- blankCol = col;
- blankRow = row;
- System.out.println("W prawo!");
- if(allRight(puzzleBox.getChildren())){
- win();
- }
- break;
- case 2:
- //podmieniamy kafelek klikniety z pustym
- tmp = tileList[row][col].getImage();
- tileList[blankRow][blankCol].setImage(tmp);
- tileList[row][col].setImage(null);
- tileList[row][col].nowY -= 1;
- tileList[row][col].movedDown += 1;
- blankCol = col;
- blankRow = row;
- System.out.println("W dol!");
- if(allRight(puzzleBox.getChildren())){
- win();
- }
- break;
- case 3:
- //podmieniamy kafelek klikniety z pustym
- tmp = tileList[row][col].getImage();
- tileList[blankRow][blankCol].setImage(tmp);
- tileList[row][col].setImage(null);
- tileList[row][col].nowX -= 1;
- tileList[row][col].movedLeft += 1;
- blankCol = col;
- blankRow = row;
- System.out.println("W lewo!");
- if(allRight(puzzleBox.getChildren())){
- win();
- }
- break;
- default:
- System.out.println("Nie do ruszenia");
- break;
- }
- }
- public void win(){
- System.out.println("Wygrales!!!");
- }
- public void initialize(){
- blankRow = (int)(Math.random()*(2 - 1 - 0 + 1) + 0);
- blankCol = (int)(Math.random()*(usedImage.rectNum/2 - 1 - 0 + 1) + 0);
- for(int i = 0; i < 2; i++) {
- for(int j = 0; j < usedImage.rectNum/2; j++) {
- tileList[i][j] = new TileView(usedImage.cutPieces.get(i*(usedImage.rectNum/2)+j));
- int finalRow = i;
- tileList[i][j].xPos = new Integer(i);
- tileList[i][j].yPos = new Integer(j);
- int finalColumn = j; //= tileList[i][j].y;
- tileList[i][j].nowX=new Integer(tileList[i][j].xPos);
- tileList[i][j].nowY=new Integer(tileList[i][j].yPos);
- tileList[i][j].setOnMouseClicked(Event ->{
- moveImg(whereIsBlank(finalRow, finalColumn),finalRow,finalColumn);
- //System.out.println("finalRow:"+finalRow);
- //System.out.println("finalColumn"+finalColumn);
- });
- puzzleBox.getChildren().add(tileList[i][j]);
- puzzleBox.setConstraints(tileList[i][j], j, i);
- }
- }
- tileList[blankRow][blankCol].setImage(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement