aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/model/Board.java
blob: 8d1586ddcc00b4e96dfde8debeb0673775f669fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 +1) + 0;
            }
            Integer[] xy=(Integer[])choices.toArray()[index];
            int twoOrFour=rand.nextInt(3-0 +1) + 0;
            switch (twoOrFour){
                case 0:
                    this.board[xy[0]][xy[1]]=4;
                    break;
                default:
                    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;
    }
}