public static int EncontrarCadena(char arrayTriangulo[][]) {
int aux = 0;
int area = 0;
int cantidadFilas = arrayTriangulo.length;
for (int i = 0; i < cantidadFilas; i++) {
int cantidadColumnas = arrayTriangulo[i].length;
for (int k = 0; k < cantidadColumnas; k++) {
if (arrayTriangulo[i][k] == '-') {
aux++;
} else if (arrayTriangulo[i][k] == '#') {
aux = 0;
}
if (aux%2!=0) {
int aux2 = ((aux + 1) / 2) - 1;
int aux4 = ((aux + 1) / 2) - 1;
int aux3 = aux;
int aux5 = aux3;
int a = 0;
int k2 = k;
while (aux2 != 0) {
for (int i2 = i + 1; i2 < cantidadFilas; i2++) {
for (int j = k2 - aux5 + 1; j < 2 * aux2 - 1; j++) {
if (arrayTriangulo[i2][j] == '-') {
aux3++;
} else if (arrayTriangulo[i2][j] == '#') {
break;
}
}
if (aux3 == aux + 2 * aux4 - 1) {
a++;
k2 = k2 + aux3 + 1;
aux2 = aux2 - 1;
aux4 = aux4 + 2 * ((aux + 1) / 2) - a;
aux5 = aux3;
}
}
}
if (aux2 == 0 && area < aux3) {
area = aux3;
}
}
}
aux = 0;
}
return area;
}
public static void main(String[] args) {
int filas = 5;
int columnas = 2 * filas - 1;
char arrayTriangulo[][] = new char[filas][columnas];
for (int i = (filas - 1); i > -1; i--) {
int aux = 2 * (filas - i) - 1;
for (int k = 0; k < columnas; k++) {
if (aux < 2 * filas - 1) {
if (k > (2 * filas - (aux)) / 2 && k < (columnas - (aux)) / 2 + aux) {
arrayTriangulo[i][k] = '-';
} else {
arrayTriangulo[i][k] = '-';
}
}
}
}
int area = EncontrarCadena(arrayTriangulo);
System.out.println(area);
}
So, This code get a Triangle made inside a 2d Array with the Following Format
---------
-------
-----
---
-
It's Supposed to count the area of smaller Triangles Inside of it and then return the are of the biguest triangle wich would be 25 (The sum of all the '-'). thing is the code isn't working and i have no idea why.
while (aux2 != 0) {
for (int i2 = i + 1; i2 < cantidadFilas; i2++) {
for (int j = k2 - aux5 + 1; j < 2 * aux2 - 1; j++) {
if (arrayTriangulo[i2][j] == '-') {
aux3++;
} else if (arrayTriangulo[i2][j] == '#') {
break;
}
}
if (aux3 == aux + 2 * aux4 - 1) {
a++;
k2 = k2 + aux3 + 1;
aux2 = aux2 - 1;
aux4 = aux4 + 2 * ((aux + 1) / 2) - a;
aux5 = aux3;
}
}
}
fails arround this part if i'm not mistaken
Related
I'm trying to solve this problem given a number 5, we display:
*
*****
*********
*****
*
And so on. So you give it a number and it format it for you like above. I tried to solve it using this code bellow and I can't see where the problem is in my code.
public class Exe01 {
public static String space = "";//global space var
public static String ast = "";//global * var
public static String adjustAst(int numOfAst) {
Exe01.ast = "";
for (int i = numOfAst; i > 0; i--) {
Exe01.ast+="*";
}
return Exe01.ast;
}
public static String adjustSpaces(int numOfSpaces) {
Exe01.space = "";
for (int i = numOfSpaces; i > 0; i--) {
Exe01.space = Exe01.space + " ";
}
return Exe01.space;
}
public static void showAst(int num) {
if (num <= 0 || num % 2 == 0)
System.out.println("arg to the function need to be positive and odd");
else if (num == 1)
System.out.println("*");
else {
int mid = (int) (num / 2);
int numberOfSpaces = num - 1;
for (int i = 0; i < num; i++) {
int k = 0;
if (i < mid) {
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces - 2;
} else if (i == mid) {
numberOfSpaces = 0;
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
} else {
k = k - 4;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
}
}
}
}
public static void main(String args[]) {
Exe01.showAst(5);
}
}
At compilation time it gives me this:
*
*
*
I am trying to find the maximum product of two non overlapping palindromic sub-sequences of string s that we'll refer to as a and b. I came up with below code but it's not giving correct output:
public static int max(String s) {
int[][] dp = new int[s.length()][s.length()];
for (int i = s.length() - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i+1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][s.length()-1];
}
For input string "acdapmpomp", we can choose a = "aca" and b ="pmpmp" to get a maximal product of score 3 * 5 = 15. But my program gives output as 5.
Firstly you should traverse the dp table to find out the length of longest palindromic subsequences using bottom up approach, then you can calculate the max product by multiplying dp[i][j] with dp[j+1][n-1] : Given below is the code in C++;
int longestPalindromicSubsequenceProduct(string x){
int n = x.size();
vector<vector<int>> dp(n,vector<int>(n,0));
for(int i=0;i<n;i++){
dp[i][i] = 1;
}
for(int k=1;k<n;k++){
for(int i=0;i<n-k;i++){
int j = i + k;
if(x[i]==x[j]){
dp[i][j] = 2 + dp[i+1][j-1];
} else{
dp[i][j] = max(dp[i][j-1],dp[i+1][j]);
}
}
}
int maxProd = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n-1;j++){
maxProd = max(maxProd,dp[i][j]*dp[j+1][n-1]);
}
}
return maxProd;
}
int multiplyPalindrome(string s) {
int n=s.size(),m=0;
vector<vector<int>> dp(n, vector<int> (n));
for(int i=0;i<n;i++) dp[i][i]=1;
for (int cl=2; cl<=n; cl++) {
for (int i=0; i<n-cl+1; i++){
int j = i+cl-1;
if (s[i] == s[j] && cl == 2) dp[i][j] = 2;
else if (s[i] == s[j]) dp[i][j] = dp[i+1][j-1] + 2;
else dp[i][j] = max(dp[i][j-1], dp[i+1][j]);
}
}
for(int i=0;i<n-1;i++){
m = max( m, dp[0][i]*dp[i+1][n-1] );
}
return m;
}
int palSize(string &s, int mask) {
int p1 = 0, p2 = s.size(), res = 0;
while (p1 <= p2) {
if ((mask & (1 << p1)) == 0)
++p1;
else if ((mask & (1 << p2)) == 0)
--p2;
else if (s[p1] != s[p2])
return 0;
else
res += 1 + (p1++ != p2--);
}
return res;
}
int maxProduct(string s) {
int mask[4096] = {}, res = 0;
for (int m = 1; m < (1 << s.size()); ++m)
mask[m] = palSize(s, m);
for (int m1 = 1; m1 < (1 << s.size()); ++m1)
if (mask[m1])
for (int m2 = 1; m2 < (1 << s.size()); ++m2)
if ((m1 & m2) == 0)
res = max(res, mask[m1] * mask[m2]);
return res;
}
You can loop through all non-overlapping palindromic subsequences and return the maximum value.
public int longestPalindromicSubsequenceProduct(String str) {
int maxProduct = 0;
for (int k = 0; k < str.length(); k++) {
String left = str.substring(0, k);
String right = str.substring(k);
int currProduct = longestPalindromicSubsequence(left) * longestPalindromicSubsequence(right);
maxProduct = Math.max(maxProduct, currProduct);
}
return maxProduct;
}
private int longestPalindromicSubsequence(String org) {
String rev = new StringBuilder(org).reverse().toString();
return longestCommonSubsequence(org, rev);
}
private int longestCommonSubsequence(String str1, String str2) {
int rows = str1.length();
int cols = str2.length();
int[][] dp = new int[rows + 1][cols + 1];
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
if (str1.charAt(r - 1) == str2.charAt(c - 1)) dp[r][c] = 1 + dp[r - 1][c - 1];
else dp[r][c] = Math.max(dp[r - 1][c], dp[r][c - 1]);
}
}
return dp[rows][cols];
}
Your algorithm returns the maximum length of a palyndrome, not the maximum of the product of two lengths.
UPDATE
Here's a possible solution:
public static int max(String s) {
int max = 0;
for (int i = 1; i < s.length()-1; ++i) {
String p1 = bestPalyndrome(s, 0, i);
String p2 = bestPalyndrome(s, i, s.length());
int prod = p1.length()*p2.length();
if (prod > max) {
System.out.println(p1 + " " + p2 + " -> " + prod);
max = prod;
}
}
return max;
}
private static String bestPalyndrome(String s, int start, int end) {
if (start >= end) {
return "";
} else if (end-start == 1) {
return s.substring(start, end);
} else if (s.charAt(start) == s.charAt(end-1)) {
return s.charAt(start) + bestPalyndrome(s, start+1, end-1)
+ s.charAt(end-1);
} else {
String s1 = bestPalyndrome(s, start, end-1);
String s2 = bestPalyndrome(s, start+1, end);
return s2.length() > s1.length() ? s2 : s1;
}
}
I want to make the diamond pattern with asterisks and the lines of it have to be given by the user. I have problem to make the diamond when the number of lines is even. This is all the code:
import java.util.Scanner;
public class DrawDiam {
public static void main(String[] args) {
System.out.println("Please give the number of lines");
Scanner in = new Scanner(System. in );
int L = in .nextInt();
if (L < 4) {
System.exit(0);
} else {
if ((L % 2) != 0) {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 1; i <= L; i++) {
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add; j++) {
System.out.print("*");
}
System.out.println();
if (i < (L / 2 + 1)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
} else {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
}
} else {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 0; i <= L + 1; i++) {
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add - 2; j++) {
System.out.print("*");
}
System.out.println();
if (i < (L / 2 + 1)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
} else {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
}
}
}
}
}
So the program asks for a number >=4.The odd number part is running perfectly but the even number part for L = 6 is coming out like this:
*
***
*****
*******
*****
***
*
for example L = 6 should show this:
*
***
*****
*******
*****
***
*
Here is my solution. You need to test if you have passed the middle of diamond. If you are at i==L/2 your in the middle and don't increment add or numOfSpaces
public static void main(String[] args) {
System.out.println("Please give the number of lines");
Scanner in = new Scanner(System.in);
int L = in.nextInt();
if (L < 4) {
System.exit(0);
} else {
if ((L % 2) != 0) {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 1; i <= L; i++) {
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add; j++) {
System.out.print("*");
}
System.out.println();
if (i < (L / 2 + 1)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
} else {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
}
} else {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 0; i < L + 1; i++) {
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add - 2; j++) {
System.out.print("*");
}
System.out.println();
if (i < (L / 2)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
}
// Edit your else block here:
else if (i > (L / 2)) {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
// End of edit!
}
}
}
}
Don't forget to close the Scanner at the end, you need to detect when "i" is the middle of "L", and remove one of the two lines around the middle (cause by example 4/2=2, so the middle is 2 and 3) of it.
This code is working:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Please give the number of lines");
Scanner in = new Scanner(System. in );
int L = in .nextInt();
if (L < 4) {
System.exit(0);
} else {
int midp = (L/2)+1;
int midm = (L/2)-1;
if ((L % 2) != 0) {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 1; i <= L; i++) {
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add; j++) {
System.out.print("*");
}
System.out.println();
if (i < (L / 2 + 1)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
} else {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
}
} else {
int add = 1;
int numOfSpaces = L / 2;
for (int i = 0; i <= L + 1; i++) {
if(i != midm){
for (int j = numOfSpaces; j >= 1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= add - 2; j++) {
System.out.print("*");
}
System.out.println();
if(i == midp){
} else if (i < (L / 2 + 1)) {
add = add + 2;
numOfSpaces = numOfSpaces - 1;
} else {
add = add - 2;
numOfSpaces = numOfSpaces + 1;
}
}
}
}
}
in.close();
}
}
I have used Libgdx to generate a dungeon out of keyboard characters. I have decided to print out the array as a text file.
However this is what I got:
Furthermore, It isn't consistent
I don't get what is wrong?
I checked over my algorithm and didn't find anything wrong.
Here is my code:
public class test1 extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
int X = 50;
int Y = 25;
////////// # wall
////////// . ground
char[][][] mapChars = new char[1000][1000][1000];
private void genDung() {
int clearance = 4;
for (int i = 0; i < Y; i++) {
for (int j = 0; j <= X; j++) {
if(j == X)
mapChars[0][i][j] = '\n';
else
mapChars[0][i][j] = '#';
}
}
int roomCount = MathUtils.random(2, 2);
int[] roomPosX = new int[roomCount];
int[] roomPosY = new int[roomCount];
int[] roomCenterPosX = new int[roomCount];
int[] roomCenterPosY = new int[roomCount];
int[] roomSizeX = new int[roomCount];
int[] roomSizeY = new int[roomCount];
for (int i = 0; i < roomCount; i++) {
int attempts = 0;
while(true) {
boolean rePosition = false;
roomPosX[i] = MathUtils.random(1, X-1);
roomPosY[i] = MathUtils.random(1, Y-1);
roomSizeX[i] = MathUtils.random(2, 12);
roomSizeY[i] = MathUtils.random(2, 8);
for(int j = 0; j <= i; j++) {
if(i != j) {
if(roomPosX[i] >= roomPosX[j] && roomPosX[i] <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if(roomPosY[i] >= roomPosY[j] && roomPosY[i] <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]+roomSizeX[i]) >= roomPosX[j] && (roomPosX[i]+roomSizeX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]+roomSizeY[i]) >= roomPosY[j] && (roomPosY[i]+roomSizeY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]) >= roomPosX[j] && (roomPosX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]+roomSizeY[i]) >= roomPosY[j] && (roomPosY[i]+roomSizeY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]+roomSizeX[i]) >= roomPosX[j] && (roomPosX[i]+roomSizeX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]) >= roomPosY[j] && (roomPosY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
}
else if(roomPosX[j] + roomSizeX[j] >= X-1){
rePosition = true;
}
else if(roomPosY[j] + roomSizeY[j] >= Y-1){
rePosition = true;
}
}
attempts++;
if(attempts >= 10000) break;
if(!rePosition) break;
}
}
for(int r = 0; r < roomCount; r++) {
for (int a = roomPosX[r]; a <= (roomPosX[r] + roomSizeX[r]); a++) {
for (int b = roomPosY[r]; b <= (roomPosY[r] + roomSizeY[r]); b++) {
mapChars[0][b][a] = '.';
}
}
}
Gdx.app.log("roomCount", String.valueOf(roomCount)+"\n\n\n");
for(int i =0; i< roomCount; i++) {
roomCenterPosX[i] = roomPosX[i] + roomSizeX[i]/2;
roomCenterPosY[i] = roomPosY[i] + roomSizeY[i]/2;
Gdx.app.log("room", String.valueOf(i)+"\n");
Gdx.app.log("roomPosX", String.valueOf(roomPosX[i]));
Gdx.app.log("roomPosY", String.valueOf(roomPosY[i]));
Gdx.app.log("roomSizeX", String.valueOf(roomSizeX[i]));
Gdx.app.log("roomSizeY", String.valueOf(roomSizeY[i])+"\n");
Gdx.app.log("RoomCenterPosX", String.valueOf(roomCenterPosX[i]));
Gdx.app.log("RoomCenterPosY", String.valueOf(roomCenterPosY[i])+"\n\n");
}
int difference = X;
int[] roomNum = new int[2];
for(int i = 0; i < roomCount; i++) {
for(int j = 0; j < roomCount; j++) {
if(i != j) {
if(abs(roomCenterPosX[i] - roomCenterPosX[j]) < difference) {
difference = abs(roomCenterPosX[i] - roomCenterPosX[j]);
roomNum[0] = i;
roomNum[1] = j;
}
}
}
}
Gdx.app.log("FarthestRooms", String.valueOf(roomNum[0]));
Gdx.app.log("FarthestRooms", String.valueOf(roomNum[1]));
int differenceX = X;
int differenceY = Y;
int[] connectRooms = new int[2];
// int[] roomsConnected = new int[roomCount];
connectRooms[0] = MathUtils.random(0, roomCount - 1);
// roomsConnected[0] = connectRooms[0];
int count;
for(int i = 0; i < roomCount-1; i++) {
int j;
while(true) {
connectRooms[1] = MathUtils.random(0, roomCount - 1);
/* while (true) {
connectRooms[1] = MathUtils.random(0, roomCount - 1);
count = 0;
for (j = 0; j < i; j++) {
if (connectRooms[1] != roomsConnected[j] && connectRooms[0] != roomsConnected[j]){
count++;
}
}
if(count >= i-2)
break;
}*/
if(connectRooms[0] != connectRooms[1])
break;
}
// roomsConnected[i+1] = connectRooms[1];
differenceX = roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]];
differenceY = roomCenterPosY[connectRooms[0]] - roomCenterPosY[connectRooms[1]];
if(roomCenterPosX[connectRooms[0]] < roomCenterPosX[connectRooms[1]])
differenceX *= -1;
if(roomCenterPosY[connectRooms[0]] < roomCenterPosY[connectRooms[1]])
differenceY *= -1;
int k;
try {
if (differenceX > 0) {
for (k = 0; k < differenceX; k++) {
mapChars[0][roomCenterPosY[i]][roomCenterPosX[i] + k] = '.';
}
} else if (differenceX < 0) {
for (k = 0; k > differenceX; k--) {
mapChars[0][roomCenterPosY[i]][roomCenterPosX[i] + k] = '.';
}
} else k = 0;
if (differenceY < 0) {
for (int z = 0; z > differenceY; z--) {
mapChars[0][roomCenterPosY[i] + z][roomCenterPosX[i] + k] = '.';
}
} else if (differenceY > 0) {
for (int z = 0; z < differenceY; z++) {
mapChars[0][roomCenterPosY[i] + z][roomCenterPosX[i] + k] = '.';
}
} else {
}
}
catch (ArrayIndexOutOfBoundsException e) {
Gdx.app.log("Non Fatal Exception", String.valueOf(e));
}
Gdx.app.log("Connect", String.valueOf(connectRooms[0]));
Gdx.app.log("Connect", String.valueOf(connectRooms[1]));
Gdx.app.log("DifferenceX", String.valueOf(differenceX));
Gdx.app.log("DifferenceY", String.valueOf(differenceY)+"\n");
}
for(int q = 0; q < Y; q++) {
mapChars[0][q][X] = '\n';
}
for(int w = 0; w < Y; w++) {
mapChars[0][w][X-1] = '#';
}
for(int e = 0; e < Y; e++) {
mapChars[0][Y-1][e] = '#';
}
}
private void export() {
if(Gdx.files.isLocalStorageAvailable()) {
FileHandle fileHandle = Gdx.files.local("map.txt");
if(Gdx.files.local("map.txt").exists())
fileHandle.writeString("", false);
for(int i = 0; i<= Y; i++) {
for (int j = 0; j <= X; j++) {
fileHandle.writeString(""+mapChars[0][i][j] , true);
}
}
}
}
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
// genMap();
// for(int i = 0; i< 4; i++)
// refineMap();
genDung();
export();
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
if(Gdx.input.isTouched()) {
// genMap();
// for(int i = 0; i< 4; i++)
// refineMap();
genDung();
export();
}
}
}
I want the two rooms to connect properly every time.
As you can see, one of the time, the rooms connected
The other time the rooms don't connect.
Thanks in advance
The whole thing can be simplified and definitely needs refactoring. Regarding the 2 room connection - check this piece carefully
differenceX = roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]];
differenceY = roomCenterPosY[connectRooms[0]] - roomCenterPosY[connectRooms[1]];
if(roomCenterPosX[connectRooms[0]] < roomCenterPosX[connectRooms[1]])
differenceX *= -1;
if(roomCenterPosY[connectRooms[0]] < roomCenterPosY[connectRooms[1]])
differenceY *= -1;
As #kiheru pointed out it is equivalent to Math.abs(roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]]) (same for Y). So you always "dig" to right and down, never up or left.
Drop that invertion and have fun with the rest of your algorithm :)
I am working on compacting some code, right now I have 4 methods that all do almost the exact same thing except the for loops are modeled a bit differently. I'm passing int's up, down, right, and left as parameters to this compact method, which coincides with the 4 methods I used to have.
By passing 1 for one of the parameters and 0 for the rest, I am able to do different this in a single loop, I'm just having trouble with switching a < or > sign.
This is the line of code I'm trying to work with:
for (int i = (right*3)+(up*3); i <= (left*3)+(down*3); i= i + (left) + (down) - (up) - (right)) {
Everything works except I need to switch the i <= (left... part to i >= (left... if right == 1, is there a way in Java to do this in for for loop?
If this isn't possible I could make 2 different for loops, I just don't like having almost identical code being repeated.
Thanks!
Edit: Here are the 4 methods:
public boolean moveRight() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 3; i >= 0; i--) {
for (int j = 0; j < 4; j++) {
if (i != 3) {
if (valueArray[i + 1][j] == 0 && valueArray[i][j] != 0) {
valueArray[i + 1][j] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i + 1][j] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i + 1][j] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}
public boolean moveLeft() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j < 4; j++) {
if (i != 0) {
if (valueArray[i - 1][j] == 0 && valueArray[i][j] != 0) {
valueArray[i - 1][j] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i - 1][j] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i - 1][j] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}
public boolean moveDown() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i < 4; i++) {
for (int j = 3; j >= 0; j--) {
if (j != 3) {
if (valueArray[i][j + 1] == 0 && valueArray[i][j] != 0) {
valueArray[i][j + 1] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i][j + 1] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i][j + 1] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}
public boolean moveUp() {
boolean didMove = false;
for (int a = 0; a <= 3; a++) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= 3; j++) {
if (j != 0) {
if (valueArray[i][j - 1] == 0 && valueArray[i][j] != 0) {
valueArray[i][j - 1] = valueArray[i][j];
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
} else if (valueArray[i][j - 1] == valueArray[i][j] && valueArray[i][j] != 0) {
valueArray[i][j - 1] = valueArray[i][j] * 2;
Score += valueArray[i][j] * 2;
valueArray[i][j] = 0;
didMove = true;
button[i][j].setSize(80, 80);
}
}
}
}
}
return didMove;
}
Math; multiplying both sides of the comparison with -1, turns <= into >=.
int sw = Math.abs(Math.signum(right - 1)) * 2 - 1; // -1 when right == 1 else 1
sw*i <= sw*((left*3)+(down*3))
Whether this is more optimal or rather cryptoprogramming...