aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorloic <git-account@loicguegan.fr>2016-09-23 09:37:06 +0200
committerloic <git-account@loicguegan.fr>2016-09-23 09:37:06 +0200
commitcb117797a68cf912b2c58d6e61b1e12025baace2 (patch)
treeabab09d1403d32747563693a854083d501d1817b /src/main
parent8f45d6ced9e75ab382ea302050fd8fa86ea55468 (diff)
Clean codeHEADmaster
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/adapter/IModelAdapter.java12
-rw-r--r--src/main/java/adapter/ModelAdapter.java46
-rw-r--r--src/main/java/app/Application.java17
-rw-r--r--src/main/java/controller/ConsoleController.java8
-rw-r--r--src/main/java/controller/MainWindowController.java61
-rw-r--r--src/main/java/model/Board.java100
-rw-r--r--src/main/java/model/IModel.java10
-rw-r--r--src/main/java/model/LineAlgorithm.java43
-rw-r--r--src/main/java/view/ConsoleView.java13
-rw-r--r--src/main/java/view/IView.java13
10 files changed, 179 insertions, 144 deletions
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<IObserver> observers;
@@ -21,41 +17,55 @@ public class ModelAdapter implements IModelAdapter, IObservable{
observers = new ArrayList<IObserver>();
}
- @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<KeyEvent>() {
@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; i<board.length;i++) {
for (int j = 0; j < board[i].length; j++) {
+ // Get value
int value=board[i][j];
+
+ // Apply theme
switch (value){
case -1:
gc.setFill(Color.rgb(202,192,180));
@@ -164,25 +166,26 @@ public class MainWindowController implements IObserver {
break;
}
-
+ // Build coordinates
int x=this.boardPosition[0] + (j*this.squareSize);
x+=(j+1)*squarePadding;
-
int y=this.boardPosition[1] + (i*this.squareSize);
y+=(i+1)*squarePadding;
-
+ // Draw cell
gc.fillRect(x,y, this.squareSize, this.squareSize);
+ // Build number font color
if(value>2048) {
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<this.board.length;i++) {
this.board[i]=this.lineAlgorithm.clearLine(this.board[i]);
}
}
+ /**
+ * Get a column of the board
+ * @param index which column ?
+ * @return
+ */
private int[] getColumn(int index){
int[] column=new int[this.board.length];
for(int i=0;i<this.board.length;i++){
@@ -90,13 +107,20 @@ public class Board implements IModel{
return column;
}
+ /**
+ * Set a column of the board to a list
+ * @param column the new column value
+ * @param index
+ */
private void setColumn(int[] column, int index){
for(int i=0;i<this.board[index].length;i++){
this.board[i][index]=column[i];
}
}
-
+ /**
+ * Add a random number at a random place on the board
+ */
public void addRandomNumber(){
Collection<Integer[]> choices=new ArrayList<Integer[]>();
for(int i=0;i<this.board.length;i++) {
@@ -123,31 +147,10 @@ public class Board implements IModel{
}
}
- private boolean boardsIsEquals(int[][] board1, int[][] board2){
- if(board1.length!=board1.length){
- return false;
- }
- else {
- for (int i=0;i<board1.length;i++){
- if(!this.lineAlgorithm.linesIsEquals(board1[i],board2[i])){
- return false;
- }
- }
- }
- return true;
- }
-
- @Override
- public int[][] getBoard() {
- return this.getCloneOfBoard();
- }
-
- @Override
- public int getScore() {
- return this.lineAlgorithm.getScore();
- }
-
-
+ /**
+ * Return true if the game is loose, false else
+ * @return
+ */
public boolean isLoosed() {
int[][] copyBoard=this.getCloneOfBoard();
@@ -169,9 +172,10 @@ public class Board implements IModel{
return false;
}
-
-
-
+ /**
+ * Return a copy of the board
+ * @return
+ */
private int[][] getCloneOfBoard(){
int[][] copyBoard=new int[this.board.length][this.board[0].length];
for(int i=0;i<this.board.length;i++){
@@ -179,4 +183,36 @@ public class Board implements IModel{
}
return copyBoard;
}
+
+ /**
+ * Return true if the two board are equals
+ * @param board1
+ * @param board2
+ * @return
+ */
+ private boolean boardsIsEquals(int[][] board1, int[][] board2){
+ if(board1.length!=board1.length){
+ return false;
+ }
+ else {
+ for (int i=0;i<board1.length;i++){
+ if(!this.lineAlgorithm.linesIsEquals(board1[i],board2[i])){
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int[][] getBoard() {
+ return this.getCloneOfBoard();
+ }
+
+ @Override
+ public int getScore() {
+ return this.lineAlgorithm.getScore();
+ }
+
+
}
diff --git a/src/main/java/model/IModel.java b/src/main/java/model/IModel.java
index 6e0a076..6287c68 100644
--- a/src/main/java/model/IModel.java
+++ b/src/main/java/model/IModel.java
@@ -4,6 +4,16 @@ package model;
* Created by loic on 21/09/16.
*/
public interface IModel {
+
+ /**
+ * Get a copy of the board
+ * @return
+ */
int[][] getBoard();
+
+ /**
+ * Get the score
+ * @return
+ */
int getScore();
}
diff --git a/src/main/java/model/LineAlgorithm.java b/src/main/java/model/LineAlgorithm.java
index 0d0364b..a15dd68 100644
--- a/src/main/java/model/LineAlgorithm.java
+++ b/src/main/java/model/LineAlgorithm.java
@@ -5,10 +5,13 @@ package model;
*/
public class LineAlgorithm {
-
-
private int score=0;
+ /**
+ * Do a right move on the line
+ * @param line
+ * @return
+ */
public int[] mergeRight(int[] line){
line=gravityRight(line);
@@ -31,7 +34,11 @@ public class LineAlgorithm {
return line;
}
-
+ /**
+ * Push all entry to the right side
+ * @param line
+ * @return
+ */
private static int[] gravityRight(int[] line){
for(int i=0;i<line.length;i++) {
for (int j = (line.length - 1); j >= 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<line.length;i++){
line[i]=-1;
@@ -56,6 +67,11 @@ public class LineAlgorithm {
return line;
}
+ /**
+ * Reverse the line passed in parameters
+ * @param line
+ * @return
+ */
public static int[] reverseLine(int[] line){
int[] reversedLine=new int[line.length];
@@ -68,6 +84,12 @@ public class LineAlgorithm {
return reversedLine;
}
+ /**
+ * Return true if two line are equals
+ * @param line1
+ * @param line2
+ * @return
+ */
public static boolean linesIsEquals(int[] line1, int[] line2){
if(line1.length!=line2.length){
return false;
@@ -83,15 +105,10 @@ public class LineAlgorithm {
}
- public static void printLine(int[] line){
- System.out.println("----------");
- for(int i=0;i<line.length;i++){
- System.out.print(line[i]);
- }
- System.out.println("\n----------");
-
- }
-
+ /**
+ * Get the current score
+ * @return
+ */
public int getScore() {
return score;
}
diff --git a/src/main/java/view/ConsoleView.java b/src/main/java/view/ConsoleView.java
index 68e25f3..0b317ca 100644
--- a/src/main/java/view/ConsoleView.java
+++ b/src/main/java/view/ConsoleView.java
@@ -1,7 +1,7 @@
package view;
+import adapter.ModelAdapter;
import controller.ConsoleController;
-import adapter.IModelAdapter;
import model.IModel;
import observer.IObserver;
@@ -12,21 +12,18 @@ import java.io.InputStreamReader;
/**
* Created by loic on 21/09/16.
*/
-public class ConsoleView implements IView, IObserver{
+public class ConsoleView implements IObserver{
private ConsoleController controller;
private IModel model;
- @Override
- public void loadComponent(IModelAdapter controller, IModel model) {
- this.controller= (ConsoleController) controller;
+
+ public void loadComponent(ConsoleController controller, IModel model) {
+ this.controller= controller;
this.model=model;
}
-
-
- @Override
public void show() {
this.controller.startGame();
}
diff --git a/src/main/java/view/IView.java b/src/main/java/view/IView.java
deleted file mode 100644
index e0c3671..0000000
--- a/src/main/java/view/IView.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package view;
-
-import adapter.IModelAdapter;
-import model.IModel;
-
-/**
- * Created by loic on 21/09/16.
- */
-public interface IView {
-
- void loadComponent(IModelAdapter controller, IModel model);
- void show();
-}