2048-javafx/src/main/java/model/Board.java
2016-09-22 20:09:07 +02:00

154 lines
4 KiB
Java

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