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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
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=new LineAlgorithm();
public Board(int sizeX, int sizeY){
board=new int[sizeY][sizeX];
this.cleanBoard();
}
/**
* Go up
*/
public void goUp() {
int [][] lastBoard=this.getCloneOfBoard();
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);
}
if(!boardsIsEquals(this.board,lastBoard)){
this.addRandomNumber();
}
}
/**
* Go down
*/
public void goDown() {
int [][] lastBoard=this.getCloneOfBoard();
for(int i=0;i<this.board[0].length;i++){
int[] column=this.getColumn(i);
this.setColumn(this.lineAlgorithm.mergeRight(column), i);
}
if(!boardsIsEquals(this.board,lastBoard)){
this.addRandomNumber();
}
}
/**
* Go left
*/
public void goLeft() {
int [][] lastBoard=this.getCloneOfBoard();
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;
}
if(!boardsIsEquals(this.board,lastBoard)){
this.addRandomNumber();
}
}
/**
* Go right
*/
public void goRight() {
int [][] lastBoard=this.getCloneOfBoard();
for(int i=0;i<this.board.length;i++){
this.board[i]=this.lineAlgorithm.mergeRight(board[i]);
}
if(!boardsIsEquals(this.board,lastBoard)){
this.addRandomNumber();
}
}
/**
* 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++){
column[i]=this.board[i][index];
}
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++) {
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(5-0 +1) + 0;
switch (twoOrFour){
case 0:
this.board[xy[0]][xy[1]]=4;
break;
default:
this.board[xy[0]][xy[1]]=2;
}
}
}
/**
* 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;
}
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();
}
}
|