Skip to content

Instantly share code, notes, and snippets.

@keshavsaharia
Last active August 29, 2015 14:15
Show Gist options
  • Save keshavsaharia/50135e27c71a2ef71ac2 to your computer and use it in GitHub Desktop.
Save keshavsaharia/50135e27c71a2ef71ac2 to your computer and use it in GitHub Desktop.
Play2048.java
import zen.core.Zen;
public class Play2048 {
static int[][] board;
private static final int UP = 0;
private static final int DOWN = 1;
private static final int LEFT = 2;
private static final int RIGHT = 3;
public static void main(String[] args) {
Zen.create(410, 410);
setup();
draw();
while (true) {
}
}
private static void setup() {
board = new int[4][4];
board[Zen.getRandomNumber(0, 3)][Zen.getRandomNumber(0, 3)] = 2;
}
private static void draw() {
Zen.setBackground("tan");
for (int x = 0 ; x < 4 ; x++) {
for (int y = 0 ; y < 4 ; y++) {
int offset = 0;
switch(board[x][y]) {
case 0: Zen.setColor("light goldenrod"); break;
case 2: Zen.setColor("light gray"); break;
case 4: Zen.setColor("light green"); break;
case 8: Zen.setColor("light blue"); break;
case 16: Zen.setColor("light salmon"); break;
case 32: Zen.setColor("light sky blue"); break;
case 64: Zen.setColor("light pink"); break;
default: Zen.setColor("yellow"); break;
}
Zen.fillRect(10 + x * 100, 10 + y * 100, 90, 90);
if (board[x][y] > 0 && board[x][y] < 10) {
Zen.setFont("Arial", 50);
offset = 20;
}
else if (board[x][y] > 10 && board[x][y] < 100) {
Zen.setFont("Arial", 40);
offset = 10;
}
else if (board[x][y] > 100 && board[x][y] < 1000) {
Zen.setFont("Arial", 30);
offset = 5;
}
else if (board[x][y] > 1000) {
Zen.setFont("Arial", 25);
offset = 0;
}
if (board[x][y] > 0) {
Zen.setColor("white");
Zen.drawText("" + board[x][y], 20 + offset + x * 100, 70 + y * 100);
}
}
}
Zen.buffer(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment