2048-javafx/src/main/java/model/LineAlgorithm.java
2016-09-22 22:14:50 +02:00

99 lines
2.1 KiB
Java

package model;
/**
* Created by loic on 21/09/16.
*/
public class LineAlgorithm {
private int score=0;
public int[] mergeRight(int[] line){
line=gravityRight(line);
for(int i=(line.length-1);i>=0;i--){
if(i>0){
int a=line[i];
int b=line[i-1];
if(a==b && a!=-1){
line[i]=a+b;
line[i-1]=-1;
i--;
this.score+=(a+b);
}
}
}
line=gravityRight(line);
return line;
}
private static int[] gravityRight(int[] line){
for(int i=0;i<line.length;i++) {
for (int j = (line.length - 1); j >= 0; j--) {
int a = line[j];
if (j > 0) {
int b = line[j - 1];
if (a == -1) {
line[j] = b;
line[j - 1] = -1;
}
}
}
}
return line;
}
public static int[] clearLine(int[] line){
for(int i=0;i<line.length;i++){
line[i]=-1;
}
return line;
}
public static int[] reverseLine(int[] line){
int[] reversedLine=new int[line.length];
int j=0;
for(int i=(line.length-1);i>=0;i--){
reversedLine[j]=line[i];
j++;
}
return reversedLine;
}
public static boolean linesIsEquals(int[] line1, int[] line2){
if(line1.length!=line2.length){
return false;
}
else {
for (int i=0;i<line1.length;i++){
if(line1[i]!=line2[i]){
return false;
}
}
}
return true;
}
public static void printLine(int[] line){
System.out.println("----------");
for(int i=0;i<line.length;i++){
System.out.print(line[i]);
}
System.out.println("\n----------");
}
public int getScore() {
return score;
}
}