diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9565642..f8cbea7 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -26,7 +26,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index cd1cc97..233a7f0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,85 +10,6 @@ - - - - - - - - - Android > Lint > Correctness - - - Android > Lint > Security - - - Android Lint for Kotlin - - - CFML - - - GPath inspectionsGroovy - - - GeneralJavaScript - - - Google Web Toolkit issues - - - Groovy - - - JPA issues - - - JSP Inspections - - - Java - - - Java EE issues - - - JavaScript - - - Probable bugsJava - - - RESTful Web Service - - - Security issuesJava - - - Spring - - - Spring MVCSpring - - - WebSocket issues - - - XML - - - XPath - - - - - JpaQlInspection - - - - - @@ -102,20 +23,4 @@ - - - - - 1.8 - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2048.iml b/2048.iml index 73f608b..d932926 100644 --- a/2048.iml +++ b/2048.iml @@ -1,6 +1,6 @@ - + diff --git a/pom.xml b/pom.xml index 4a01981..9ccd366 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ manzerbredes 2048 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + diff --git a/src/main/java/adapter/IModelAdapter.java b/src/main/java/adapter/IModelAdapter.java new file mode 100644 index 0000000..86139ee --- /dev/null +++ b/src/main/java/adapter/IModelAdapter.java @@ -0,0 +1,12 @@ +package adapter; + +/** + * Created by loic on 21/09/16. + */ +public interface IModelAdapter { + void goUp(); + void goDown(); + void goLeft(); + void goRight(); + boolean isLoosed(); +} diff --git a/src/main/java/adapter/ModelAdapter.java b/src/main/java/adapter/ModelAdapter.java new file mode 100644 index 0000000..1eac58a --- /dev/null +++ b/src/main/java/adapter/ModelAdapter.java @@ -0,0 +1,80 @@ +package adapter; + +import model.Board; +import observer.IObservable; +import observer.IObserver; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +/** + * Created by loic on 21/09/16. + */ +public class ModelAdapter implements IModelAdapter, IObservable{ + + private Board model; + private Collection observers; + + public ModelAdapter(Board model){ + this.model=model; + observers = new ArrayList(); + } + + @Override + public void goUp() { + model.goUp(); + model.addRandomNumber(); + this.notifyObservers(); + } + + @Override + public void goDown() { + model.goDown(); + model.addRandomNumber(); + this.notifyObservers(); + } + + @Override + public void goLeft() { + model.goLeft(); + model.addRandomNumber(); + this.notifyObservers(); + } + + @Override + public void goRight() { + model.goRight(); + model.addRandomNumber(); + this.notifyObservers(); + } + + + @Override + public boolean isLoosed() { + return this.model.isLoosed(); + } + + public void addRandomNumber() { + this.model.addRandomNumber(); + this.notifyObservers(); + } + + @Override + public void addObserver(IObserver observer) { + this.observers.add(observer); + } + + @Override + public void removeObserver(IObserver observer) { + this.observers.remove(observer); + } + + @Override + public void notifyObservers() { + Iterator i=this.observers.iterator(); + while(i.hasNext()){ + i.next().update(); + } + } +} diff --git a/src/main/java/app/Application.java b/src/main/java/app/Application.java index 7034373..9efd5e3 100644 --- a/src/main/java/app/Application.java +++ b/src/main/java/app/Application.java @@ -1,14 +1,44 @@ package app; -/** - * Created by loic on 21/09/16. - */ -public class Application { +import adapter.ModelAdapter; +import controller.MainWindowController; +import javafx.event.EventHandler; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.input.KeyEvent; +import javafx.stage.Stage; +import model.Board; +import model.IModel; +import observer.IObserver; + +import java.awt.*; + +public class Application extends javafx.application.Application{ + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) throws Exception { + + FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getClassLoader().getResource("JavafxView/MainWindow.fxml")); + Scene scene=new Scene((Parent)fxmlLoader.load()); + + primaryStage.setTitle("2048"); + primaryStage.setScene(scene); + // COnfigure controller + IModel model=new Board(4,4); + ModelAdapter adapter=new ModelAdapter((Board) model); + adapter.addRandomNumber(); + MainWindowController controller=fxmlLoader.getController(); + adapter.addObserver((IObserver)controller); + controller.loadComponent(adapter,model,scene); - public static void main(String[] args){ - System.out.println("Test"); + primaryStage.show(); } } diff --git a/src/main/java/controller/ConsoleController.java b/src/main/java/controller/ConsoleController.java new file mode 100644 index 0000000..95b1821 --- /dev/null +++ b/src/main/java/controller/ConsoleController.java @@ -0,0 +1,50 @@ +package controller; + +import adapter.IModelAdapter; +import adapter.ModelAdapter; +import model.Board; + +/** + * Created by loic on 21/09/16. + */ +public class ConsoleController implements IModelAdapter { + + private ModelAdapter adapter; + + public ConsoleController(ModelAdapter model){ + this.adapter=model; + } + + public void startGame(){ + this.adapter.addRandomNumber(); + } + + @Override + public void goUp() { + adapter.goUp(); + } + + @Override + public void goDown() { + adapter.goDown(); + } + + @Override + public void goLeft() { + adapter.goLeft(); + } + + @Override + public void goRight() { + adapter.goRight(); + } + + @Override + public boolean isLoosed() { + return this.adapter.isLoosed(); + } + + + + +} diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java new file mode 100644 index 0000000..7ed60a7 --- /dev/null +++ b/src/main/java/model/Board.java @@ -0,0 +1,147 @@ +package model; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Random; + +/** + * Created by loic on 21/09/16. + */ +public class Board implements IModel{ + + private int[][] board; + private Random rand = new Random(); + private LineAlgorithm lineAlgorithm; + + + public Board(int sizeX, int sizeY){ + board=new int[sizeY][sizeX]; + this.cleanBoard(); + } + + public void goUp() { + for(int i=0;i choices=new ArrayList(); + for(int i=0;i0){ + int index=0; + if(choices.size()>1){ + index=rand.nextInt(choices.size()-1) + 0; + } + Integer[] xy=(Integer[])choices.toArray()[index]; + this.board[xy[0]][xy[1]]=2; + } + } + + private boolean boardsIsEquals(int[][] board1, int[][] board2){ + if(board1.length!=board1.length){ + return false; + } + else { + for (int i=0;i=0;i--){ + if(i>0){ + int a=line[i]; + int b=line[i-1]; + if(a==b && a!=-1){ + line[i]=a+b; + line[i-1]=-1; + i--; + } + } + } + + line=gravityRight(line); + + return line; + } + + + private static int[] gravityRight(int[] line){ + for(int i=0;i= 0; j--) { + int a = line[j]; + if (j > 0) { + int b = line[j - 1]; + if (a == -1) { + line[j] = b; + line[j - 1] = -1; + } + } + } + } + return line; + } + + + public static int[] clearLine(int[] line){ + for(int i=0;i=0;i--){ + reversedLine[j]=line[i]; + j++; + } + + return reversedLine; + } + + public static boolean linesIsEquals(int[] line1, int[] line2){ + if(line1.length!=line2.length){ + return false; + } + else { + for (int i=0;i + + + + + + + + + + + + +
+ +
+ + +
\ No newline at end of file