Clean code

This commit is contained in:
loic 2016-09-23 09:37:06 +02:00
parent 8f45d6ced9
commit cb117797a6
10 changed files with 182 additions and 147 deletions

View file

@ -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();
}

View file

@ -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);

View file

@ -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();
}
}

View file

@ -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();
}

View file

@ -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));
}
}
}
}

View file

@ -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,6 +147,49 @@ public class Board implements IModel{
}
}
/**
* Return true if the game is loose, false else
* @return
*/
public boolean isLoosed() {
int[][] copyBoard=this.getCloneOfBoard();
this.goDown();
if(boardsIsEquals(copyBoard, this.board)) {
this.goUp();
if (boardsIsEquals(copyBoard, this.board)) {
this.goRight();
if (boardsIsEquals(copyBoard, this.board)) {
this.goLeft();
if (boardsIsEquals(copyBoard, this.board)) {
return true;
}
}
}
}
this.board=copyBoard;
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++){
copyBoard[i]=this.board[i].clone();
}
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;
@ -148,35 +215,4 @@ public class Board implements IModel{
}
public boolean isLoosed() {
int[][] copyBoard=this.getCloneOfBoard();
this.goDown();
if(boardsIsEquals(copyBoard, this.board)) {
this.goUp();
if (boardsIsEquals(copyBoard, this.board)) {
this.goRight();
if (boardsIsEquals(copyBoard, this.board)) {
this.goLeft();
if (boardsIsEquals(copyBoard, this.board)) {
return true;
}
}
}
}
this.board=copyBoard;
return false;
}
private int[][] getCloneOfBoard(){
int[][] copyBoard=new int[this.board.length][this.board[0].length];
for(int i=0;i<this.board.length;i++){
copyBoard[i]=this.board[i].clone();
}
return copyBoard;
}
}

View file

@ -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();
}

View file

@ -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;
}

View file

@ -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();
}

View file

@ -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();
}