Advertisement
nonogamer9

nono2048 (ComputerCraft Port)

Mar 17th, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.44 KB | Gaming | 0 0
  1. local boardWidth, boardHeight = 4, 4
  2. local board = {}
  3. local score = 0
  4.  
  5. -- Initialize the board
  6. for x = 1, boardWidth do
  7.     board[x] = {}
  8.     for y = 1, boardHeight do
  9.         board[x][y] = 0
  10.     end
  11. end
  12.  
  13. -- Function to prompt the user for monochrome mode
  14. local function promptMonochromeMode()
  15.     term.clear()
  16.     term.setTextColor(colors.white)
  17.     print("Do you want to play in monochrome mode? (y/n)")
  18.     local response = string.lower(read())
  19.     return response == "y"
  20. end
  21.  
  22. -- Function to draw the board
  23. local function drawBoard(monochromeMode)
  24.     term.clear()
  25.     for y = 1, boardHeight do
  26.         for x = 1, boardWidth do
  27.             -- Set color and background based on tile value
  28.             local value = board[x][y]
  29.             local textColor, bgColor = colors.black, colors.gray
  30.             if monochromeMode then
  31.                 textColor, bgColor = colors.white, colors.gray
  32.             else
  33.                 if value == 2 then
  34.                     bgColor = colors.yellow
  35.                 elseif value == 4 then
  36.                     bgColor = colors.orange
  37.                 elseif value == 8 then
  38.                     bgColor = colors.red
  39.                 elseif value == 16 then
  40.                     bgColor = colors.magenta
  41.                 elseif value == 32 then
  42.                     bgColor = colors.purple
  43.                 elseif value == 64 then
  44.                     bgColor = colors.blue
  45.                 elseif value == 128 then
  46.                     bgColor = colors.lightBlue
  47.                 elseif value == 256 then
  48.                     bgColor = colors.cyan
  49.                 elseif value == 512 then
  50.                     bgColor = colors.lime
  51.                 elseif value == 1024 then
  52.                     bgColor = colors.green
  53.                 elseif value == 2048 then
  54.                     bgColor = colors.white
  55.                 end
  56.             end
  57.            
  58.             term.setBackgroundColor(bgColor)
  59.             term.setTextColor(textColor)
  60.             term.setCursorPos(x * 7 - 6, y * 2)
  61.             term.write(string.format("%6d", value))
  62.         end
  63.     end
  64.     -- Display score
  65.     term.setTextColor(colors.white)
  66.     term.setBackgroundColor(colors.black)
  67.     term.setCursorPos(1, boardHeight * 2 + 1)
  68.     print("Score: " .. score)
  69.    
  70.     -- Display "nono2048" in monochrome colors if in monochrome mode
  71.     if monochromeMode then
  72.         term.setTextColor(colors.white)
  73.         term.setBackgroundColor(colors.black)
  74.         local text = "nono2048"
  75.         local xPos = math.floor((boardWidth * 7 - #text) / 2) + 1
  76.         term.setCursorPos(xPos, boardHeight * 2 + 2)
  77.         term.write(text)
  78.     else
  79.         -- Display "nono2048" in rainbow colors if not in monochrome mode
  80.         local rainbowColors = {
  81.             colors.red,
  82.             colors.orange,
  83.             colors.yellow,
  84.             colors.lime,
  85.             colors.green,
  86.             colors.blue,
  87.             colors.purple
  88.         }
  89.         local text = "nono2048"
  90.         local xPos = math.floor((boardWidth * 7 - #text) / 2) + 1
  91.         for i = 1, #text do
  92.             term.setTextColor(rainbowColors[(i - 1) % #rainbowColors + 1])
  93.             term.setBackgroundColor(colors.black)
  94.             term.setCursorPos(xPos + i - 1, boardHeight * 2 + 2)
  95.             term.write(text:sub(i, i))
  96.         end
  97.     end
  98. end
  99.  
  100. -- Function to add a new tile (either 2 or 4) randomly to the board
  101. local function addNewTile()
  102.     local emptyTiles = {}
  103.     for x = 1, boardWidth do
  104.         for y = 1, boardHeight do
  105.             if board[x][y] == 0 then
  106.                 table.insert(emptyTiles, {x, y})
  107.             end
  108.         end
  109.     end
  110.     if #emptyTiles > 0 then
  111.         local tile = emptyTiles[math.random(1, #emptyTiles)]
  112.         board[tile[1]][tile[2]] = math.random() < 0.9 and 2 or 4
  113.     end
  114. end
  115.  
  116. -- Function to move tiles up
  117. local function moveUp()
  118.     -- Implementation for moving tiles up
  119.     -- Iterate over each column
  120.     for x = 1, boardWidth do
  121.         local lastMerged = false -- Track if a merge has occurred in this column
  122.         for y = 2, boardHeight do
  123.             local value = board[x][y]
  124.             if value ~= 0 then
  125.                 local newY = y
  126.                 while newY > 1 do
  127.                     if board[x][newY - 1] == 0 then
  128.                         board[x][newY - 1] = value
  129.                         board[x][newY] = 0
  130.                         newY = newY - 1
  131.                     elseif board[x][newY - 1] == value and not lastMerged then
  132.                         board[x][newY - 1] = value * 2
  133.                         board[x][newY] = 0
  134.                         score = score + value * 2 -- Increment score by merged tile value
  135.                         lastMerged = true -- Set lastMerged to true
  136.                         break
  137.                     else
  138.                         break
  139.                     end
  140.                 end
  141.             end
  142.         end
  143.     end
  144. end
  145.  
  146. -- Function to move tiles down
  147. local function moveDown()
  148.     -- Implementation for moving tiles down
  149.     -- Iterate over each column
  150.     for x = 1, boardWidth do
  151.         local lastMerged = false -- Track if a merge has occurred in this column
  152.         for y = boardHeight - 1, 1, -1 do -- Iterate from bottom to top
  153.             local value = board[x][y]
  154.             if value ~= 0 then
  155.                 local newY = y
  156.                 while newY < boardHeight do
  157.                     if board[x][newY + 1] == 0 then
  158.                         board[x][newY + 1] = value
  159.                         board[x][newY] = 0
  160.                         newY = newY + 1
  161.                     elseif board[x][newY + 1] == value and not lastMerged then
  162.                         board[x][newY + 1] = value * 2
  163.                         board[x][newY] = 0
  164.                         score = score + value * 2 -- Increment score by merged tile value
  165.                         lastMerged = true -- Set lastMerged to true
  166.                         break
  167.                     else
  168.                         break
  169.                     end
  170.                 end
  171.             end
  172.         end
  173.     end
  174. end
  175.  
  176. -- Function to move tiles left
  177. local function moveLeft()
  178.     -- Implementation for moving tiles left
  179.     -- Iterate over each row
  180.     for y = 1, boardHeight do
  181.         local lastMerged = false -- Track if a merge has occurred in this row
  182.         for x = 2, boardWidth do -- Iterate from left to right
  183.             local value = board[x][y]
  184.             if value ~= 0 then
  185.                 local newX = x
  186.                 while newX > 1 do
  187.                     if board[newX - 1][y] == 0 then
  188.                         board[newX - 1][y] = value
  189.                         board[newX][y] = 0
  190.                         newX = newX - 1
  191.                     elseif board[newX - 1][y] == value and not lastMerged then
  192.                         board[newX - 1][y] = value * 2
  193.                         board[newX][y] = 0
  194.                         score = score + value * 2 -- Increment score by merged tile value
  195.                         lastMerged = true -- Set lastMerged to true
  196.                         break
  197.                     else
  198.                         break
  199.                     end
  200.                 end
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206. -- Function to move tiles right
  207. local function moveRight()
  208.     -- Implementation for moving tiles right
  209.     -- Iterate over each row
  210.     for y = 1, boardHeight do
  211.         local lastMerged = false -- Track if a merge has occurred in this row
  212.         for x = boardWidth - 1, 1, -1 do -- Iterate from right to left
  213.             local value = board[x][y]
  214.             if value ~= 0 then
  215.                 local newX = x
  216.                 while newX < boardWidth do
  217.                     if board[newX + 1][y] == 0 then
  218.                         board[newX + 1][y] = value
  219.                         board[newX][y] = 0
  220.                         newX = newX + 1
  221.                     elseif board[newX + 1][y] == value and not lastMerged then
  222.                         board[newX + 1][y] = value * 2
  223.                         board[newX][y] = 0
  224.                         score = score + value * 2 -- Increment score by merged tile value
  225.                         lastMerged = true -- Set lastMerged to true
  226.                         break
  227.                     else
  228.                         break
  229.                     end
  230.                 end
  231.             end
  232.         end
  233.     end
  234. end
  235.  
  236. -- Function to handle player input
  237. local function handleInput()
  238.     local event, key = os.pullEvent("key")
  239.     if key == keys.up then
  240.         moveUp()
  241.     elseif key == keys.down then
  242.         moveDown()
  243.     elseif key == keys.left then
  244.         moveLeft()
  245.     elseif key == keys.right then
  246.         moveRight()
  247.     end
  248. end
  249.  
  250. -- Function to check for game over condition
  251. local function checkGameOver()
  252.     -- Check for any empty tiles
  253.     for x = 1, boardWidth do
  254.         for y = 1, boardHeight do
  255.             if board[x][y] == 0 then
  256.                 return false -- Game is not over
  257.             end
  258.         end
  259.     end
  260.  
  261.     -- Check for adjacent tiles with the same value horizontally
  262.     for y = 1, boardHeight do
  263.         for x = 1, boardWidth - 1 do
  264.             if board[x][y] == board[x + 1][y] then
  265.                 return false -- Game is not over
  266.             end
  267.         end
  268.     end
  269.  
  270.     -- Check for adjacent tiles with the same value vertically
  271.     for x = 1, boardWidth do
  272.         for y = 1, boardHeight - 1 do
  273.             if board[x][y] == board[x][y + 1] then
  274.                 return false -- Game is not over
  275.             end
  276.         end
  277.     end
  278.  
  279.     return true -- Game is over
  280. end
  281.  
  282. -- Function to display game over screen
  283. local function displayGameOver(monochromeMode)
  284.     term.clear()
  285.     term.setCursorPos(1, 1)
  286.     if monochromeMode then
  287.         term.setTextColor(colors.white)
  288.         term.setBackgroundColor(colors.gray)
  289.     else
  290.         term.setTextColor(colors.red)
  291.         term.setBackgroundColor(colors.black)
  292.     end
  293.     print("Game Over!")
  294.     print("Score: " .. score)
  295.     print("Press any key to exit.")
  296.     os.pullEvent("key")
  297.     error("Game Over")
  298. end
  299.  
  300. -- Main game loop
  301. while true do
  302.     local monochromeMode = promptMonochromeMode() -- Prompt for monochrome mode
  303.     while true do
  304.         drawBoard(monochromeMode)
  305.         handleInput()
  306.         addNewTile()
  307.  
  308.         -- Check for game over condition
  309.         if checkGameOver() then
  310.             displayGameOver(monochromeMode)
  311.             break
  312.         end
  313.         -- Implement tile movement logic
  314.     end
  315. end
  316.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement