From cb117797a68cf912b2c58d6e61b1e12025baace2 Mon Sep 17 00:00:00 2001 From: loic Date: Fri, 23 Sep 2016 09:37:06 +0200 Subject: [PATCH] Clean code --- src/main/java/adapter/IModelAdapter.java | 12 -- src/main/java/adapter/ModelAdapter.java | 46 +++++--- src/main/java/app/Application.java | 17 +-- .../java/controller/ConsoleController.java | 8 +- .../java/controller/MainWindowController.java | 61 +++++----- src/main/java/model/Board.java | 106 ++++++++++++------ src/main/java/model/IModel.java | 10 ++ src/main/java/model/LineAlgorithm.java | 43 ++++--- src/main/java/view/ConsoleView.java | 13 +-- src/main/java/view/IView.java | 13 --- 10 files changed, 182 insertions(+), 147 deletions(-) delete mode 100644 src/main/java/adapter/IModelAdapter.java delete mode 100644 src/main/java/view/IView.java diff --git a/src/main/java/adapter/IModelAdapter.java b/src/main/java/adapter/IModelAdapter.java deleted file mode 100644 index 86139ee..0000000 --- a/src/main/java/adapter/IModelAdapter.java +++ /dev/null @@ -1,12 +0,0 @@ -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 index 35eba94..90bba32 100644 --- a/src/main/java/adapter/ModelAdapter.java +++ b/src/main/java/adapter/ModelAdapter.java @@ -1,17 +1,13 @@ package adapter; import model.Board; -import observer.IObservable; -import observer.IObserver; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; +import observer.*; +import java.util.*; /** * Created by loic on 21/09/16. */ -public class ModelAdapter implements IModelAdapter, IObservable{ +public class ModelAdapter implements IObservable{ private Board model; private Collection observers; @@ -21,41 +17,55 @@ public class ModelAdapter implements IModelAdapter, IObservable{ observers = new ArrayList(); } - @Override + + /** + * Add a random number on the board + */ + public void addRandomNumber() { + this.model.addRandomNumber(); + this.notifyObservers(); + } + + /** + * Go up + */ public void goUp() { model.goUp(); this.notifyObservers(); } - @Override + /** + * Go down + */ public void goDown() { model.goDown(); this.notifyObservers(); } - @Override + /** + * Go left + */ public void goLeft() { model.goLeft(); this.notifyObservers(); } - @Override + /** + * Go right + */ public void goRight() { model.goRight(); this.notifyObservers(); } - - @Override + /** + * Return true if the game is loose, false else + * @return + */ 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); diff --git a/src/main/java/app/Application.java b/src/main/java/app/Application.java index 9efd5e3..0b8a814 100644 --- a/src/main/java/app/Application.java +++ b/src/main/java/app/Application.java @@ -2,18 +2,12 @@ package app; 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.scene.*; import javafx.stage.Stage; -import model.Board; -import model.IModel; +import model.*; import observer.IObserver; -import java.awt.*; - public class Application extends javafx.application.Application{ public static void main(String[] args) { @@ -23,14 +17,15 @@ public class Application extends javafx.application.Application{ @Override public void start(Stage primaryStage) throws Exception { + // Configure loader FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getClassLoader().getResource("JavafxView/MainWindow.fxml")); Scene scene=new Scene((Parent)fxmlLoader.load()); + // Configure window primaryStage.setTitle("2048"); primaryStage.setScene(scene); - - // COnfigure controller + // Configure controller IModel model=new Board(4,4); ModelAdapter adapter=new ModelAdapter((Board) model); adapter.addRandomNumber(); @@ -38,7 +33,7 @@ public class Application extends javafx.application.Application{ adapter.addObserver((IObserver)controller); controller.loadComponent(adapter,model,scene); - + // Show window primaryStage.show(); } } diff --git a/src/main/java/controller/ConsoleController.java b/src/main/java/controller/ConsoleController.java index 95b1821..8fd7061 100644 --- a/src/main/java/controller/ConsoleController.java +++ b/src/main/java/controller/ConsoleController.java @@ -1,13 +1,12 @@ package controller; -import adapter.IModelAdapter; import adapter.ModelAdapter; import model.Board; /** * Created by loic on 21/09/16. */ -public class ConsoleController implements IModelAdapter { +public class ConsoleController { private ModelAdapter adapter; @@ -19,27 +18,22 @@ public class ConsoleController implements IModelAdapter { 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/controller/MainWindowController.java b/src/main/java/controller/MainWindowController.java index 253ce33..21d0072 100644 --- a/src/main/java/controller/MainWindowController.java +++ b/src/main/java/controller/MainWindowController.java @@ -1,23 +1,15 @@ package controller; -import adapter.IModelAdapter; import adapter.ModelAdapter; -import javafx.event.ActionEvent; -import javafx.event.EventHandler; +import javafx.event.*; import javafx.fxml.FXML; -import javafx.scene.Group; import javafx.scene.Scene; -import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; +import javafx.scene.canvas.*; import javafx.scene.control.Button; import javafx.scene.input.KeyEvent; -import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; -import javafx.scene.text.Font; -import javafx.scene.text.Text; -import model.Board; -import model.IModel; -import model.LineAlgorithm; +import javafx.scene.text.*; +import model.*; import observer.IObserver; @@ -29,21 +21,28 @@ public class MainWindowController implements IObserver { @FXML Canvas boardCanvas; @FXML Text score; @FXML Button restartButton; + + private ModelAdapter adapter; private IModel model; - private int squareSize=100; private int squarePadding=10; private int[] boardPosition={30,0}; private int fontSize=60; - + /** + * Load component required to the view + * @param adapter + * @param model + * @param scene + */ public void loadComponent(ModelAdapter adapter, IModel model, Scene scene){ this.adapter=adapter; this.model=model; - this.update(); + this.update(); // Draw board + // Catch keyboard keys scene.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent event) { @@ -95,33 +94,36 @@ public class MainWindowController implements IObserver { } - + /** + * Draw the grid + */ private void draw(){ - - + // Set score font and draw this.score.setFont(new Font(40)); - if(this.adapter.isLoosed()){ this.score.setText("Score : " + this.model.getScore() + "\n You loose !!!"); } else{ this.score.setText("Score : " + this.model.getScore()); } + + // Get board int[][] board=this.model.getBoard(); + // Draw board GraphicsContext gc = boardCanvas.getGraphicsContext2D(); gc.clearRect(0,0,500,500); - gc.setFill(Color.rgb(187,173,160)); gc.fillRect(this.boardPosition[0],this.boardPosition[1], ((this.squareSize+squarePadding)*board.length)+squarePadding, ((this.squareSize+squarePadding)*board[0].length)+squarePadding); - - - + // Draw cells for(int i=0; i2048) { gc.setFill(Color.WHITE); } else{ gc.setFill(Color.BLACK); - } + // Draw if value is greeter than 0 if(value>0){ + // Ajuste font position String strValue=""+value; int localFontSize=fontSize; if(strValue.length()==3){ @@ -193,12 +196,10 @@ public class MainWindowController implements IObserver { else if(strValue.length()>4){ localFontSize=localFontSize/4; } - gc.setFont(new Font(localFontSize)); + // Write value gc.fillText(strValue, x + (this.squareSize / 2) - ((localFontSize/4)+strValue.length()*localFontSize/5) , y + (this.squareSize / 2) + (localFontSize /3)); } - - } } } diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java index 4d8f9b8..f40c409 100644 --- a/src/main/java/model/Board.java +++ b/src/main/java/model/Board.java @@ -14,12 +14,14 @@ public class Board implements IModel{ private LineAlgorithm lineAlgorithm=new LineAlgorithm(); - public Board(int sizeX, int sizeY){ board=new int[sizeY][sizeX]; this.cleanBoard(); } + /** + * Go up + */ public void goUp() { int [][] lastBoard=this.getCloneOfBoard(); @@ -37,6 +39,9 @@ public class Board implements IModel{ } + /** + * Go down + */ public void goDown() { int [][] lastBoard=this.getCloneOfBoard(); @@ -49,6 +54,9 @@ public class Board implements IModel{ } } + /** + * Go left + */ public void goLeft() { int [][] lastBoard=this.getCloneOfBoard(); @@ -63,7 +71,9 @@ public class Board implements IModel{ } } - + /** + * Go right + */ public void goRight() { int [][] lastBoard=this.getCloneOfBoard(); @@ -75,13 +85,20 @@ public class Board implements IModel{ } } - + /** + * Clean the board (all value to -1) + */ private void cleanBoard(){ for(int i=0;i choices=new ArrayList(); for(int i=0;i= 0; j--) { @@ -48,7 +55,11 @@ public class LineAlgorithm { return line; } - + /** + * Clear a line (all to -1) + * @param line + * @return + */ public static int[] clearLine(int[] line){ for(int i=0;i