diff options
Diffstat (limited to 'src/main/java/model')
| -rw-r--r-- | src/main/java/model/Board.java | 100 | ||||
| -rw-r--r-- | src/main/java/model/IModel.java | 10 | ||||
| -rw-r--r-- | src/main/java/model/LineAlgorithm.java | 43 |
3 files changed, 108 insertions, 45 deletions
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; } |
