xxxxxxxxxx
let rect_size = 90;
let grid;
let level = 0;
let direction = "";
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
grid = [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
]
addNumber();
addNumber();
}
function draw() {
drawGrid();
drawNumbers();
}
function drawGrid(){
for (let y=-2; y<=1; y++){
for (let x=-2; x<=1;x++){
rect(windowWidth/2+rect_size*x, windowHeight/2+rect_size*y, rect_size, rect_size)
}
}
}
function drawNumbers(){
console.log(grid)
for (let r=0; r<4; r++){
for (let c=0;c<4;c++){
if (grid[r][c] != 0){
textSize(40)
text(grid[r][c], windowWidth/2+rect_size*(r-2) + rect_size/2 -10, windowHeight/2+rect_size*(c-2) + rect_size/2 +10)
}
}
}
}
function addNumber(){
let options = []
for (let r=0; r<4; r++){
for (let c=0;c<4;c++){
if (grid[r][c]==0){
options.push({
x:r,
y:c
})
}
}
}
console.log(options)
if (options.length >0){
let spot = random(options)
let r = random(1)
grid[spot.x][spot.y] = r >0.3 ? 2 : 4
}
}
function moveUp(r,c,start){
if (start < 4){
//check if the row you are moving and the row you are moving to are not the same, if the same, don't move
if (c != start) {
//if first row is empty
if (grid[r][start] == 0){
grid[r][start] = grid[r][c]
grid[r][c] = 0
}
//else,
else {
//if same value and next to each other, add it up
if (grid[r][start] == grid[r][c] && Math.abs(start-c)==1){
grid[r][start] += grid[r][c]
grid[r][c] = 0
}
//else, move to second row
else {
moveUp(r,c,start+1)
}
}
}
}
}
function moveDown(r,c,end){
if (end >=0){
//check if the row you are moving and the row you are moving to are not the same, if the same, don't move
if (c != end) {
//if last row is empty
if (grid[r][end] == 0){
grid[r][end] = grid[r][c]
grid[r][c] = 0
}
//else,
else {
//if same, add it up
if (grid[r][end] == grid[r][c] && Math.abs(end-c)==1){
grid[r][end] += grid[r][c]
grid[r][c] = 0
}
//else, move to second row
else {
moveDown(r,c,end-1)
}
}
}
}
}
function moveRight(r,c,end){
if (end >=0){
//check if the col you are moving and the col you are moving to are not the same, if the same, don't move
if (r != end) {
//if last col is empty
if (grid[end][c] == 0){
grid[end][c] = grid[r][c]
grid[r][c] = 0
}
//else,
else {
//if same, add it up
if (grid[end][c] == grid[r][c] && Math.abs(end-r)==1){
grid[end][c] += grid[r][c]
grid[r][c] = 0
}
//else, move to second row
else {
moveRight(r,c,end-1)
}
}
}
}
}
function moveLeft(r,c,start){
if (start < 4){
//check if the col you are moving and the col you are moving to are not the same, if the same, don't move
if (r != start) {
//if first col is empty
if (grid[start][c] == 0){
grid[start][c] = grid[r][c]
grid[r][c] = 0
}
//else,
else {
//if same, add it up
if (grid[start][c] == grid[r][c] && Math.abs(start-r)==1){
grid[start][c] += grid[r][c]
grid[r][c] = 0
}
//else, move to second row
else {
moveLeft(r,c,start+1)
}
}
}
}
}
function keyPressed(){
switch(keyCode){
case UP_ARROW:
direction = "up"
addNumber();
break;
case DOWN_ARROW:
direction = "down"
addNumber();
break;
case RIGHT_ARROW:
direction = "right"
addNumber();
break;
case LEFT_ARROW:
direction = "left"
addNumber();
break;
}
for (let r=0; r<4; r++){
for (let c=0;c<4;c++){
if (grid[r][c] != 0){
if (direction == "up"){
moveUp(r,c,0)
}
if (direction == "down"){
moveDown(r,c,3)
}
if (direction == "left"){
moveLeft(r,c,0)
}
if (direction == "right"){
moveRight(r,c,3)
}
}
}
}
}