2048 Game in R Language
2048 is a popular puzzle game where players use arrow keys to make same-number tiles merge, creating a new tile with a higher value. The game ends when all tiles are filled or no more moves are possible. In this implementation, we will use the R language to create a game that follows the same rules.
Game Structure
The game is structured as follows:
- The game is initialized with a 4x4 matrix of random numbers, with two or four in each cell.
- The player can move the tiles up, down, left, or right using the corresponding arrow keys.
- When two tiles of the same number collide, they merge into a new tile with a value twice the original number.
- The game ends when all tiles are filled or no more moves are possible.
Code Implementation
# Initialize the game
init <- function() {
# Set the stage number
e$stage <- 1
# Initialize the matrix with random numbers
mt <- matrix(c(sample(c(2,4), 1), rep(0,15)), nrow = 4)
e$m <- mt[sample(4), sample(4)]
# Draw the background and numbers
draw_bg()
draw_num()
}
# Remove zeros from the matrix
rm_zero <- function() {
if (e$x == 0) {
if (e$dir == "up") {
for (c in 1:4) {
e$m[, c] <- c(e$m[, c][!which(e$m[, c] == 0)], rep(0, 4-length(e$m[, c][which(e$m[, c] != 0)])))
}
}
if (e$dir == "down") {
for (c in 1:4) {
e$m[, c] <- c(rep(0, 4-length(e$m[, c][which(e$m[, c] == 0)])), e$m[, c][which(e$m[, c] != 0)])
}
}
if (e$dir == "left") {
for (r in 1:4) {
e$m[r,] <- c(e$m[r,][!which(e$m[r,] == 0)], rep(0, 4-length(e$m[r,][which(e$m[r,] != 0)])))
}
}
if (e$dir == "right") {
for (r in 1:4) {
e$m[r,] <- c(rep(0, 4-length(e$m[r,][!which(e$m[r,] == 0)])), e$m[r,][which(e$m[r,] != 0)])
}
}
} else {
if (e$dir == "up") {
c <- e$x
e$m[, c] <- c(e$m[, c][which(e$m[, c] == 0)!], rep(0, 4-length(e$m[, c][which(e$m[, c] != 0)])))
}
if (e$dir == "down") {
c <- e$x
e$m[, c] <- c(rep(0, 4-length(e$m[, c][!which(e$m[, c] == 0)])), e$m[, c][which(e$m[, c] != 0)])
}
if (e$dir == "left") {
r <- e$x
e$m[r,] <- c(e$m[r,][!which(e$m[r,] == 0)], rep(0, 4-length(e$m[r,][which(e$m[r,] != 0)])))
}
if (e$dir == "right") {
r <- e$x
e$m[r,] <- c(rep(0, 4-length(e$m[r,][!which(e$m[r,] == 0)])), e$m[r,][which(e$m[r,] != 0)])
}
}
}
# Add a random number in the space provided
new_mt <- function() {
e$m[sample(which(e$m == 0), 1)] <- sample(c(2,4), 1)
}
# Check if the game fails
fail <- function() {
if (length(e$m[which(e$m == 0)]) == 0) {
e$x = 0
for (r in 1:3) {
for (c in 1:3) {
if (e$m[r, c] == e$m[r, c + 1] | e$m[r, c] == e$m[r + 1, c]) {
e$x = 1
}
}
}
if (e$x == 0) {
stage2()
}
}
}
# Game
stage1 <- function() {
e$stage <- 2
e$x <- 0
rm_zero()
if (e$dir == "left") {
i <- 1
while (i <= 4) {
if (e$m[i, 1] != 0 & e$m[i, 1] == e$m[i, 2] & e$m[i, 1] == e$m[i, 3] & e$m[i, 1] == e$m[i, 4]) {
e$m[i,] <- rep(c(2 * e$m[i, 1], 0), each = 2)
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 3] != 0 & e$m[i, 2] == e$m[i, 1] & e$m[i, 3] == e$m[i, 4]) {
e$m[i,] <- c(2 * e$m[i, 1], 0, 2 * e$m[i, 3], 0)
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 2] == e$m[i, 1]) {
e$m[i,] <- c(2 * e$m[i, 1], 0, e$m[i, 3], e$m[i, 4])
e$x <- 1
} else if (e$m[i, 3] != 0 & e$m[i, 3] == e$m[i, 4]) {
e$m[i,] <- c(e$m[i, 1], e$m[i, 2], 2 * e$m[i, 3], 0)
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 2] == e$m[i, 3]) {
e$m[i,] <- c(e$m[i, 1], 2 * e$m[i, 2], 0, e$m[i, 4])
e$x <- 1
}
i <- i + 1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
if (e$dir == "right") {
i <- 1
while (i <= 4) {
if (e$m[i, 1] != 0 & e$m[i, 1] == e$m[i, 2] & e$m[i, 1] == e$m[i, 3] & e$m[i, 1] == e$m[i, 4]) {
e$m[i,] <- rep(c(0, 2 * e$m[i, 1]), each = 2)
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 3] != 0 & e$m[i, 2] == e$m[i, 1] & e$m[i, 3] == e$m[i, 4]) {
e$m[i,] <- c(0, 2 * e$m[i, 1], 0, 2 * e$m[i, 3])
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 2] == e$m[i, 1]) {
e$m[i,] <- c(0, 2 * e$m[i, 1], e$m[i, 3], e$m[i, 4])
e$x <- 1
} else if (e$m[i, 3] != 0 & e$m[i, 3] == e$m[i, 4]) {
e$m[i,] <- c(e$m[i, 1], e$m[i, 2], 0, 2 * e$m[i, 3])
e$x <- 1
} else if (e$m[i, 2] != 0 & e$m[i, 2] == e$m[i, 3]) {
e$m[i,] <- c(e$m[i, 1], 0, 2 * e$m[i, 2], e$m[i, 4])
e$x <- 1
}
i <- i + 1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
if (e$dir == "up") {
j <- 1
while (j <= 4) {
if (e$m[1, j] != 0 & e$m[1, j] == e$m[2, j] & e$m[1, j] == e$m[3, j] & e$m[1, j] == e$m[4, j]) {
e$m[, j] <- rep(c(2 * e$m[1, j], 0), each = 2)
e$x <- 1
} else if (e$m[2, j] != 0 & e$m[3, j] != 0 & e$m[2, j] == e$m[1, j] & e$m[3, j] == e$m[4, j]) {
e$m[, j] <- c(2 * e$m[1, j], 0, 2 * e$m[3, j], 0)
e$x <- 1
} else if (e$m[2, j] != 0 & e$m[2, j] == e$m[1, j]) {
e$m[, j] <- c(2 * e$m[1, j], 0, e$m[3, j], e$m[4, j])
e$x <- 1
} else if (e$m[3, j] != 0 & e$m[3, j] == e$m[4, j]) {
e$m[, j] <- c(e$m[1, j], e$m[2, j], 2 * e$m[3, j], 0)
e$x <- 1
} else if (e$m[2, j] != 0 & e$m[2, j] == e$m[3, j]) {
e$m[, j] <- c(e$m[1, j], 2 * e$m[2, j], 0, e$m[4, j])
e$x <- 1
}
j <- j + 1
}
rm_zero()
new_mt()
draw_bg()
draw_num()
fail()
}
}
# Draw the background
draw_bg <- function() {
plot(0, 0, xlim = c(0, 0.8), ylim = c(0, 0.8), type = 'n', xaxs = "i", yaxs = "i")
for (i in c(1:4)) {
for (j in c(1:4)) {
points(0.1 + 0.2 * (i-1), 0.9-0.2 * (j), col = "gray", pch = 15, cex = 16)
}
}
}
# Draw the numbers
draw_num <- function() {
for (i in c(1:4)) {
for (j in c(1:4)) {
if (e$m[i, j] != 0) {
text(0.1 + (j-1) * 0.2, 0.7 - (i-1) * 0.2, font = 2, family = "Arial", label = e$m[i, j], cex = 2)
}
}
}
}
# Run the game
init()
while (e$x == 0) {
stage1()
draw_bg()
draw_num()
fail()
}
This code implements the 2048 game in R language. The game is initialized with a 4x4 matrix of random numbers, and the player can move the tiles up, down, left, or right using the corresponding arrow keys. When two tiles of the same number collide, they merge into a new tile with a value twice the original number. The game ends when all tiles are filled or no more moves are possible.