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 choices=new ArrayList(); for(int i=0;i0){ 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