Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local boardWidth, boardHeight = 4, 4
- local board = {}
- local score = 0
- -- Initialize the board
- for x = 1, boardWidth do
- board[x] = {}
- for y = 1, boardHeight do
- board[x][y] = 0
- end
- end
- -- Function to prompt the user for monochrome mode
- local function promptMonochromeMode()
- term.clear()
- term.setTextColor(colors.white)
- print("Do you want to play in monochrome mode? (y/n)")
- local response = string.lower(read())
- return response == "y"
- end
- -- Function to draw the board
- local function drawBoard(monochromeMode)
- term.clear()
- for y = 1, boardHeight do
- for x = 1, boardWidth do
- -- Set color and background based on tile value
- local value = board[x][y]
- local textColor, bgColor = colors.black, colors.gray
- if monochromeMode then
- textColor, bgColor = colors.white, colors.gray
- else
- if value == 2 then
- bgColor = colors.yellow
- elseif value == 4 then
- bgColor = colors.orange
- elseif value == 8 then
- bgColor = colors.red
- elseif value == 16 then
- bgColor = colors.magenta
- elseif value == 32 then
- bgColor = colors.purple
- elseif value == 64 then
- bgColor = colors.blue
- elseif value == 128 then
- bgColor = colors.lightBlue
- elseif value == 256 then
- bgColor = colors.cyan
- elseif value == 512 then
- bgColor = colors.lime
- elseif value == 1024 then
- bgColor = colors.green
- elseif value == 2048 then
- bgColor = colors.white
- end
- end
- term.setBackgroundColor(bgColor)
- term.setTextColor(textColor)
- term.setCursorPos(x * 7 - 6, y * 2)
- term.write(string.format("%6d", value))
- end
- end
- -- Display score
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, boardHeight * 2 + 1)
- print("Score: " .. score)
- -- Display "nono2048" in monochrome colors if in monochrome mode
- if monochromeMode then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- local text = "nono2048"
- local xPos = math.floor((boardWidth * 7 - #text) / 2) + 1
- term.setCursorPos(xPos, boardHeight * 2 + 2)
- term.write(text)
- else
- -- Display "nono2048" in rainbow colors if not in monochrome mode
- local rainbowColors = {
- colors.red,
- colors.orange,
- colors.yellow,
- colors.lime,
- colors.green,
- colors.blue,
- colors.purple
- }
- local text = "nono2048"
- local xPos = math.floor((boardWidth * 7 - #text) / 2) + 1
- for i = 1, #text do
- term.setTextColor(rainbowColors[(i - 1) % #rainbowColors + 1])
- term.setBackgroundColor(colors.black)
- term.setCursorPos(xPos + i - 1, boardHeight * 2 + 2)
- term.write(text:sub(i, i))
- end
- end
- end
- -- Function to add a new tile (either 2 or 4) randomly to the board
- local function addNewTile()
- local emptyTiles = {}
- for x = 1, boardWidth do
- for y = 1, boardHeight do
- if board[x][y] == 0 then
- table.insert(emptyTiles, {x, y})
- end
- end
- end
- if #emptyTiles > 0 then
- local tile = emptyTiles[math.random(1, #emptyTiles)]
- board[tile[1]][tile[2]] = math.random() < 0.9 and 2 or 4
- end
- end
- -- Function to move tiles up
- local function moveUp()
- -- Implementation for moving tiles up
- -- Iterate over each column
- for x = 1, boardWidth do
- local lastMerged = false -- Track if a merge has occurred in this column
- for y = 2, boardHeight do
- local value = board[x][y]
- if value ~= 0 then
- local newY = y
- while newY > 1 do
- if board[x][newY - 1] == 0 then
- board[x][newY - 1] = value
- board[x][newY] = 0
- newY = newY - 1
- elseif board[x][newY - 1] == value and not lastMerged then
- board[x][newY - 1] = value * 2
- board[x][newY] = 0
- score = score + value * 2 -- Increment score by merged tile value
- lastMerged = true -- Set lastMerged to true
- break
- else
- break
- end
- end
- end
- end
- end
- end
- -- Function to move tiles down
- local function moveDown()
- -- Implementation for moving tiles down
- -- Iterate over each column
- for x = 1, boardWidth do
- local lastMerged = false -- Track if a merge has occurred in this column
- for y = boardHeight - 1, 1, -1 do -- Iterate from bottom to top
- local value = board[x][y]
- if value ~= 0 then
- local newY = y
- while newY < boardHeight do
- if board[x][newY + 1] == 0 then
- board[x][newY + 1] = value
- board[x][newY] = 0
- newY = newY + 1
- elseif board[x][newY + 1] == value and not lastMerged then
- board[x][newY + 1] = value * 2
- board[x][newY] = 0
- score = score + value * 2 -- Increment score by merged tile value
- lastMerged = true -- Set lastMerged to true
- break
- else
- break
- end
- end
- end
- end
- end
- end
- -- Function to move tiles left
- local function moveLeft()
- -- Implementation for moving tiles left
- -- Iterate over each row
- for y = 1, boardHeight do
- local lastMerged = false -- Track if a merge has occurred in this row
- for x = 2, boardWidth do -- Iterate from left to right
- local value = board[x][y]
- if value ~= 0 then
- local newX = x
- while newX > 1 do
- if board[newX - 1][y] == 0 then
- board[newX - 1][y] = value
- board[newX][y] = 0
- newX = newX - 1
- elseif board[newX - 1][y] == value and not lastMerged then
- board[newX - 1][y] = value * 2
- board[newX][y] = 0
- score = score + value * 2 -- Increment score by merged tile value
- lastMerged = true -- Set lastMerged to true
- break
- else
- break
- end
- end
- end
- end
- end
- end
- -- Function to move tiles right
- local function moveRight()
- -- Implementation for moving tiles right
- -- Iterate over each row
- for y = 1, boardHeight do
- local lastMerged = false -- Track if a merge has occurred in this row
- for x = boardWidth - 1, 1, -1 do -- Iterate from right to left
- local value = board[x][y]
- if value ~= 0 then
- local newX = x
- while newX < boardWidth do
- if board[newX + 1][y] == 0 then
- board[newX + 1][y] = value
- board[newX][y] = 0
- newX = newX + 1
- elseif board[newX + 1][y] == value and not lastMerged then
- board[newX + 1][y] = value * 2
- board[newX][y] = 0
- score = score + value * 2 -- Increment score by merged tile value
- lastMerged = true -- Set lastMerged to true
- break
- else
- break
- end
- end
- end
- end
- end
- end
- -- Function to handle player input
- local function handleInput()
- local event, key = os.pullEvent("key")
- if key == keys.up then
- moveUp()
- elseif key == keys.down then
- moveDown()
- elseif key == keys.left then
- moveLeft()
- elseif key == keys.right then
- moveRight()
- end
- end
- -- Function to check for game over condition
- local function checkGameOver()
- -- Check for any empty tiles
- for x = 1, boardWidth do
- for y = 1, boardHeight do
- if board[x][y] == 0 then
- return false -- Game is not over
- end
- end
- end
- -- Check for adjacent tiles with the same value horizontally
- for y = 1, boardHeight do
- for x = 1, boardWidth - 1 do
- if board[x][y] == board[x + 1][y] then
- return false -- Game is not over
- end
- end
- end
- -- Check for adjacent tiles with the same value vertically
- for x = 1, boardWidth do
- for y = 1, boardHeight - 1 do
- if board[x][y] == board[x][y + 1] then
- return false -- Game is not over
- end
- end
- end
- return true -- Game is over
- end
- -- Function to display game over screen
- local function displayGameOver(monochromeMode)
- term.clear()
- term.setCursorPos(1, 1)
- if monochromeMode then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.gray)
- else
- term.setTextColor(colors.red)
- term.setBackgroundColor(colors.black)
- end
- print("Game Over!")
- print("Score: " .. score)
- print("Press any key to exit.")
- os.pullEvent("key")
- error("Game Over")
- end
- -- Main game loop
- while true do
- local monochromeMode = promptMonochromeMode() -- Prompt for monochrome mode
- while true do
- drawBoard(monochromeMode)
- handleInput()
- addNewTile()
- -- Check for game over condition
- if checkGameOver() then
- displayGameOver(monochromeMode)
- break
- end
- -- Implement tile movement logic
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement