aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/model/Board.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/model/Board.java')
-rw-r--r--src/main/java/model/Board.java147
1 files changed, 147 insertions, 0 deletions
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<this.board[0].length;i++){
+
+ int[] column=this.lineAlgorithm.reverseLine(this.getColumn(i));
+
+
+ this.setColumn(this.lineAlgorithm.reverseLine(this.lineAlgorithm.mergeRight(column)), i);
+
+ }
+
+ }
+
+ public void goDown() {
+ for(int i=0;i<this.board[0].length;i++){
+ int[] column=this.getColumn(i);
+ this.setColumn(this.lineAlgorithm.mergeRight(column), i);
+ }
+ }
+
+ public void goLeft() {
+ for(int i=0;i<this.board.length;i++){
+ int[] tmp=this.lineAlgorithm.reverseLine(board[i]);
+ tmp=this.lineAlgorithm.mergeRight(tmp);
+ tmp=this.lineAlgorithm.reverseLine(tmp);
+ this.board[i]=tmp;
+ }
+ }
+
+
+ public void goRight() {
+ for(int i=0;i<this.board.length;i++){
+ this.board[i]=this.lineAlgorithm.mergeRight(board[i]);
+ }
+ }
+
+
+ private void cleanBoard(){
+ for(int i=0;i<this.board.length;i++) {
+ this.board[i]=this.lineAlgorithm.clearLine(this.board[i]);
+ }
+ }
+
+ private int[] getColumn(int index){
+ int[] column=new int[this.board.length];
+ for(int i=0;i<this.board.length;i++){
+ column[i]=this.board[i][index];
+ }
+ return column;
+ }
+
+ private void setColumn(int[] column, int index){
+ for(int i=0;i<this.board[index].length;i++){
+ this.board[i][index]=column[i];
+ }
+ }
+
+
+ public void addRandomNumber(){
+ Collection<Integer[]> choices=new ArrayList<Integer[]>();
+ for(int i=0;i<this.board.length;i++) {
+ for (int j = 0; j < this.board[i].length; j++) {
+ if(this.board[i][j]==-1){
+ choices.add(new Integer[]{i,j});
+ }
+ }
+ }
+ if(choices.size()>0){
+ 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<board1.length;i++){
+ if(!this.lineAlgorithm.linesIsEquals(board1[i],board2[i])){
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int[][] getBoard() {
+ return this.getCloneOfBoard();
+ }
+
+
+ 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;
+ }
+}