I am making a 2048 game without the use of classes. One of my most major problems is that once the array is full with values, the program stops working. I tried creating a Boolean to change screens before the array is full, but nothing seems to work. I would appreciate if someone could help me. Here is my code so far:
int score =0;
int screen=0;
PFont f;
int[][] tiles= new int[4][4];
int tileWidth = 200;
int startTime;
int a =0;
void setup () {
fullScreen();
fill(0);
startTime = frameCount;
f= createFont("Arial", 12, true);
for (int loop = 0; loop<2; loop++) {
tiles[int(random(0, 4))][int(random(0, 4))]=2;
}
}
void draw () {
textFont(f, 24);
if (screen==0) {
background(255);
fill(175, 170, 170);
rect(200, 200, 200, 100);
rect(200, 300, 200, 100);
rect(200, 400, 200, 100);
fill(0);
text("2048", 275, 100);
text("Game", 275, 255);
text("High Score", 255, 355);
text("Rules", 275, 455);
if (isMouseinArea(200, 400, 200, 300) && screen == 0) {
mousePressed=false;
screen = 1;
}
if (isMouseinArea(200, 400, 300, 400) && screen == 0) {
mousePressed=false;
screen = 2;
}
if (isMouseinArea(200, 400, 400, 500) && screen == 0) {
mousePressed=false;
screen = 3;
}
}
if (screen==1) {
background(255);
fill(175, 170, 170);
rect(200, 200, 200, 100);
rect(200, 300, 200, 100);
fill(0);
text("Game Mode", 255, 100);
text("Timed", 275, 255);
text("Free Play", 255, 355);
if (isMouseinArea(200, 400, 200, 300) && screen == 1) {
mousePressed=false;
screen = 4;
}
if (isMouseinArea(200, 400, 300, 400) && screen == 1) {
mousePressed=false;
screen = 5;
}
}
if (screen==4) {
int timer = (frameCount-startTime)/60;
while (a==0) {
if (keyCode == SHIFT) {
tiles= new int[4][4];
score=0;
startTime = frameCount;
for (int loop = 0; loop<2; loop++) {
tiles[int(random(0, 4))][int(random(0, 4))]=2;
}
}
a++;
}
background(255);
noFill();
textSize(50);
textAlign(CENTER, CENTER);
fill(0);
text("Score:", 900, 50);
text(score, 1050, 50);
text("Time:", 900, 100);
text(timer, 1050, 100);
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
fill(255);
rect(i*tileWidth, j*tileWidth, tileWidth, tileWidth);
if (tiles[j][i] == 2) {
fill(20, 200, 100);
} else if (tiles[j][i] == 4) {
fill(40, 100, 200);
} else if (tiles[j][i] == 8) {
fill(255, 100, 50);
} else if (tiles[j][i] == 16) {
fill(0, 150, 150);
} else if (tiles[j][i] == 32) {
fill(255, 0, 100);
} else if (tiles[j][i] == 64) {
fill(55, 155, 255);
} else if (tiles[j][i] ==128) {
fill(150, 50, 250);
} else if (tiles[j][i] == 256) {
fill(0, 255, 255);
} else if (tiles[j][i] == 512) {
fill(0, 150, 150);
} else if (tiles[j][i] == 1024) {
fill(0, 150, 150);
} else if (tiles[j][i] == 2048) {
fill(0, 150, 150);
}
if (tiles[j][i] != 0) text(Integer.toString(tiles[j][i]), i*tileWidth, j*tileWidth, tileWidth, tileWidth);
}
}
if (full==true) {
println("full");
}
if (timer==60) {
screen=6;
}
}
if (screen==5) {
while (a==0) {
if (keyCode == SHIFT) {
tiles= new int[4][4];
score=0;
for (int loop = 0; loop<2; loop++) {
tiles[int(random(0, 4))][int(random(0, 4))]=2;
}
}
a++;
}
background(255);
noFill();
textSize(50);
textAlign(CENTER, CENTER);
fill(0);
text("Score:", 900, 50);
text(score, 1050, 50);
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
fill(255);
rect(i*tileWidth, j*tileWidth, tileWidth, tileWidth);
if (tiles[j][i] == 2) {
fill(20, 200, 100);
} else if (tiles[j][i] == 4) {
fill(40, 100, 200);
} else if (tiles[j][i] == 8) {
fill(255, 100, 50);
} else if (tiles[j][i] == 16) {
fill(0, 150, 150);
} else if (tiles[j][i] == 32) {
fill(255, 0, 100);
} else if (tiles[j][i] == 64) {
fill(55, 155, 255);
} else if (tiles[j][i] ==128) {
fill(150, 50, 250);
} else if (tiles[j][i] == 256) {
fill(0, 255, 255);
} else if (tiles[j][i] == 512) {
fill(0, 150, 150);
} else if (tiles[j][i] == 1024) {
fill(0, 150, 150);
} else if (tiles[j][i] == 2048) {
fill(0, 150, 150);
}
if (tiles[j][i] != 0) text(Integer.toString(tiles[j][i]), i*tileWidth, j*tileWidth, tileWidth, tileWidth);
}
}
if (full==true) {
println("full");
}
}
if (screen==2) {
}
if (screen==3) {
background(255);
text("Rules", width/2-25, 100);
text("2048 is played on a gray 4×4 grid, with numbered tiles that slide smoothly when a player moves them using the four arrow keys. Every turn, a new tile will randomly appear in an empty spot on the board with a value of either 2 or 4. There is a time limit of 60 seconds and players must get the highest score in that time frame", 100, 200, width-200, height);
text("BACK", 520, 600);
if (isMouseinArea(520, 600, 500, 600) && screen == 3) {
mousePressed=false;
screen = 0;
}
}
}
void keyPressed() {
if (keyCode == RIGHT) moveRight();
if (keyCode == LEFT) moveLeft();
if (keyCode == UP) moveUp();
if (keyCode == DOWN) moveDown();
if (full == false) {
generateNewBlock();
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
score += tiles[i][j];
}
}
}
if (keyCode == SHIFT) {
a=0;
}
}
void generateNewBlock() {
boolean foundAnEmptySpace = false;
while (!foundAnEmptySpace) {
int xcor = floor(random(0, 4));
int ycor = floor(random(0, 4));
if (tiles[ycor][xcor] == 0) {
tiles[ycor][xcor] = 2;
foundAnEmptySpace = true;
}
}
while(foundAnEmptySpace==false){
ellipse(100,100,100,100);
}
}
**
void moveRight() {
for (int loop = 0; loop<3; loop++) {
for (int i=0; i<4; i++) {
for (int j=2; j>=0; j--) {
if (tiles[i][j+1] == 0) {
int temp = tiles[i][j];
tiles[i][j+1] = temp;
tiles[i][j] = 0;
}
if (tiles[i][j] == tiles[i][j+1]) {
int temp = 2*tiles[i][j];
tiles[i][j+1] = temp;
tiles[i][j] = 0;
}
}
}
}
}
void moveLeft() {
for (int loop = 0; loop<3; loop++) {
for (int i=0; i<4; i++) {
for (int j=1; j<4; j++) {
if (tiles[i][j-1] == 0) {
int temp = tiles[i][j];
tiles[i][j-1] = temp;
tiles[i][j] = 0;
}
if (tiles[i][j] == tiles[i][j-1]) {
int temp = 2*tiles[i][j];
tiles[i][j-1] = temp;
tiles[i][j] = 0;
}
}
}
}
}
void moveUp() {
for (int loop = 0; loop<3; loop++) {
for (int i=1; i<4; i++) {
for (int j=0; j<4; j++) {
if (tiles[i-1][j] == 0) {
int temp = tiles[i][j];
tiles[i-1][j] = temp;
tiles[i][j] = 0;
}
if (tiles[i][j] == tiles[i-1][j]) {
int temp = 2*tiles[i][j];
tiles[i-1][j] = temp;
tiles[i][j] = 0;
}
}
}
}
}
void moveDown() {
for (int loop = 0; loop<3; loop++) {
for (int i=2; i>=0; i--) {
for (int j=0; j<4; j++) {
if (tiles[i+1][j] == 0) {
int temp = tiles[i][j];
tiles[i+1][j] = temp;
tiles[i][j] = 0;
}
if (tiles[i][j] == tiles[i+1][j]) {
int temp = 2*tiles[i][j];
tiles[i+1][j] = temp;
tiles[i][j] = 0;
}
}
}
}
}
boolean isMouseinArea(int x1, int x2, int y1, int y2) {
return mousePressed && mouseX > x1 && mouseX < x2 && mouseY > y1 && mouseY <y2;
//boolean for clicking in areas
}
boolean full;
boolean arrayfull() {
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
if (tiles[i][j] == 0) full = false;
if (tiles[i][j] != 0) full = true;
}
}
return full;
}