This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rich73's profile picture

2048 for Pocket Computers

Started by rich73, 11 April 2014 - 01:34 PM
rich73 #1
Posted 11 April 2014 - 03:34 PM
No doubt it's been done before, but here's a quick clone I made of the infamous 2048 game! Got a little bored last night so this is what I managed to hack together. Not perfect code by any means but it seems to work okay… might tidy and comment it later. Feel free to modify/improve/share but if you could give me a little bit of credit that would be awesome. :)/> Video: https://www.youtube....h?v=UD2SOicH9L4 Source: http://pastebin.com/ZDQ59nLN
Spoiler

-- 2048
-- Clone by Richard (Rich73)
-- Hacked together 10/04/2014
g = { }
tiles = { }
tiles[1]  = {"	", 1}
tiles[2]  = {" 2  ", 2}
tiles[3]  = {" 4  ", 4}
tiles[4]  = {" 8  ", 8}
tiles[5]  = {" 16 ", 16}
tiles[6]  = {" 32 ", 32}
tiles[7]  = {" 64 ", 64}
tiles[8]  = {"128 ", 128}
tiles[9]  = {"256 ", 256}
tiles[10] = {"512 ", 512}
tiles[11] = {"1024", 1024}
tiles[12] = {"2048", 2048}
bg = "icon"
size = 4
score = 0
hiscore = 0
function createBoard()
  for i=1, size do
	g[i] = { }
	for j=1, size do
	  g[i][j] = 0
	end
  end
  for j=1, 2 do
	local x, y = findEmptyPos()
	g[y][x] = 1
  end
end
function getRandomPos()
  return math.random(size), math.random(size)
end
function findEmptyPos()
  while true do
	x, y = getRandomPos()
	if g[y][x] == 0 then return x, y end
  end
end
function isFull()
  local full = true
  for i=1, size do
	for j=1, size do
	  if g[i][j] == 0 then full = false end
	end
  end
  return full
end
function canMove()
  if not isFull() then return true end
  local pr = nil
  for i=1, size do
	local k = 1
	while k <= size do
	  if k~=size and g[i][k] == g[i][k+1] or false then break end
	  if pr and g[i][k] == pr[k] or false then break end
	  k = k + 1
	end
	if k ~= size+1 then return true end
	pr = g[i]
  end
  return false
end
function moveLeft()
  for i=1, size do
	for j=2, size do
	  local k = j
	  while k > 1 do
		if g[i][k] == 0 then break
		elseif g[i][k] == g[i][k-1] then
		  g[i][k-1] = g[i][k] + 1
		  g[i][k] = 0
		  score = score + math.pow(2,g[i][k-1])
		  k = k-1
		elseif g[i][k-1] == 0 then
		  g[i][k-1] = g[i][k]
		  g[i][k] = 0
		else break end
		k = k-1
	  end
	end
  end
end
function moveUp()
  for j=1, size do
	for i=2, size do
	  local k = i
	  while k > 1 do
		if g[k][j] == 0 then break
		elseif g[k][j] == g[k-1][j] then
		  g[k-1][j] = g[k][j] + 1
		  g[k][j] = 0
		  score = score + math.pow(2,g[k-1][j])
		  k = k-1
		elseif g[k-1][j] == 0 then
		  g[k-1][j] = g[k][j]
		  g[k][j] = 0
		else break end
		k = k-1
	  end
	end
  end
end
function moveRight()
  flipX()
  moveLeft()
  flipX()
end
function moveDown()
  flipY()
  moveUp()
  flipY()
end
function drawBoard()
term.setBackgroundColor(colors.black)
term.clear()
term.setTextColor(colors.white)
  for x=1, size do
	for y=1, size do
	  term.setCursorPos(x*5-1,y*4-2)
	  term.setBackgroundColor(tiles[g[y][x]+1][2])
	  term.write("	")
	  term.setCursorPos(x*5-1,y*4-1)
	  term.write(tiles[g[y][x]+1][1])
	  term.setCursorPos(x*5-1,y*4)
	  term.write("	")
	end
  end
  term.setCursorPos(4,18)
  term.setBackgroundColor(colors.black)
  term.write("Score: "..score)
  drawScores()
end
function drawScores()
  term.setCursorPos(4,18)
  term.setBackgroundColor(colors.black)
  term.write("  Score: "..score)
  term.setCursorPos(4,19)
  term.write("HiScore: "..hiscore)
end
function drawHome()
  term.clear()
  im = paintutils.loadImage(bg)
  paintutils.drawImage(im,2,2)
end
function table.reverse(tab)
  local newtab = { }
  for i=1, #tab do
	newtab[#tab+1-i] = tab[i]
  end
  return newtab
end
function flipX()
  for i=1, size do
	g[i] = table.reverse(g[i])
  end
end
function flipY()
  g = table.reverse(g)
end
function update()
  drawBoard()
  if not isFull() then
	local x, y = findEmptyPos()
	g[y][x] = 1
  end
  os.sleep(0.075)
end
function newGame()
  if score > hiscore then
	hiscore = score
  end
  score = 0
  term.setCursorPos(9,18)
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.clearLine()
  term.write("GAME OVER!")
  term.setCursorPos(8,19)
  term.clearLine()
  term.write("New game? y/n")
  while true do
	local event, key = os.pullEvent("key")
	if event=="key" then
	  if key==keys.y then return true end
	  if key==keys.n then return false end
	end
  end
end
drawHome()
os.sleep(2)
while true do
  createBoard()
  while canMove() do
	drawBoard()
	event, key = os.pullEvent("key")
	if event == "key" then
	  if key == keys.left then moveLeft()
	  elseif key == keys.right then
		moveRight()
		update()
	  elseif key == keys.up then
		moveUp()
		update()
	  elseif key == keys.down then
		moveDown()
		update()
	  elseif key == keys.q then
		break
	  end
	end
  end
  drawBoard()
  if not newGame() then
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	term.clear()
	term.setCursorPos(1,1)
	break
  end
end
Icon file for startup
Spoiler

44444444444444444444444
44444444444444444444444
44455545554544445555444
44444545454544445445444
44455545454544445555444
44454445454555545445444
44455545554445445555444
44444444444444444444444
44455555555555555555444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
44444444444444444444444
biggest yikes #2
Posted 14 April 2014 - 02:04 AM
It's good if you put the proper appliances to it (like 1 fix and add the file 'bg'):
But I have a few suggestions.
1. Make it so at the start there's an fs.exists() check and if it's false that BG exists run a pastebin to download the bg file.
2. At line 160, it runs a load image to the VARIABLE bg. Make sure you use quotations to make it a string for the file, not a variable!
3. Sometimes you have to move it twice for another 2 to generate (however, the move is still executed, just not a 2 generating). It would be greatly improved if this was fixed.
Overall, it's a nice concept!
Edited on 14 April 2014 - 12:06 AM
Hinds #3
Posted 01 May 2014 - 07:11 PM
Pretty nice, managed to get to the 1048 tile on my first turn :P/>
Although, I would probably bring up the same points as minimite made, as there are a few quirks with actually getting the program to work. If those were fixed, it would be pretty much perfect.
Edited on 01 May 2014 - 05:14 PM
Hinds #4
Posted 02 May 2014 - 06:26 PM
I win! :3

Spoiler
Xenthera #5
Posted 06 May 2014 - 04:01 AM
If i'm not mistaken, it still adds a new tile even if the move wasn't valid (I.E. No tiles were combined or moved)
GreenByteSoftware #6
Posted 17 May 2014 - 04:44 PM
I'm creating my own operating system called "PockyOS", and the next update will include "CraftStore"(like appstore on ios), so could I add this program into the store? You will get a credit.
civilwargeeky #7
Posted 22 May 2014 - 09:47 PM
Hi. I just added a line that installs the icon if it doesn't exist: http://pastebin.com/xyGMBBBk
I improved it a bit more, added persistent high score and a notifier that you can press q to quit. http://pastebin.com/RHhdEZ52
Edited on 26 May 2014 - 04:29 AM
TableCraft0R #8
Posted 15 June 2014 - 05:33 AM
I Added online scores: pastebin run e14LSbMC
SCORE VIEWER
http://captainhandsome.byethost.com/scores-v2/
theoriginalbit #9
Posted 15 June 2014 - 06:57 AM
I Added online scores: pastebin run e14LSbMC
SCORE VIEWER
http://captainhandso....com/scores-v2/
I don't think your PHP works. my highscore isn't showing up.
Bomb Bloke #10
Posted 15 June 2014 - 07:22 AM
Ditto. Nor are any scores, for that matter.
TableCraft0R #11
Posted 16 June 2014 - 10:27 AM
I Added online scores: pastebin run e14LSbMC
SCORE VIEWER
http://captainhandso....com/scores-v2/
I don't think your PHP works. my highscore isn't showing up.

<?php
$file = 'feedcreep.txt';
$score = $_POST['score'];
$current = file_get_contents($file);
file_put_contents( $file,'<hr>' . $score . $current )
echo 'Success.'
?>
theoriginalbit #12
Posted 16 June 2014 - 10:41 AM

<?php
$file = 'feedcreep.txt';
		$score = $_POST['score'];
$current = file_get_contents($file);
file_put_contents( $file,'<hr>' . $score . $current )
echo 'Success.'
?>
nah you've definitely got a problem, just checked with this

http.request("http://captainhandsome.byethost.com/scores-v2/scorepost.php", "score=0")

while true do
  local e = { os.pullEvent() }
  if e[1]:find("http_") then
    print(e[1])
    if e[2] then
      print(e[2].readAll())
    end
    break
  end
end
and its coming back with http_failure.
KatanaKid #13
Posted 24 July 2014 - 01:33 AM
I made this game also :D/>
danielsv03 #14
Posted 25 July 2014 - 12:22 PM
cool script did it take long for you?? its amazing dude :)/>
Uncertified Robot #15
Posted 23 March 2015 - 07:54 PM
2048:160: Expected image, x, y
civilwargeeky #16
Posted 23 March 2015 - 10:14 PM
2048:160: Expected image, x, y
I'm going to guess You didn't download the picture.
Try this version that does it automatically
http://pastebin.com/RHhdEZ52
magiczocker #17
Posted 31 December 2015 - 11:56 AM
It's very nice, I like to play it.
scopeFX #18
Posted 16 February 2016 - 09:42 PM
Nice Game!
Vex #19
Posted 23 May 2016 - 07:21 AM
My phone addiction has just come to Minecraft, I guess I can never escape 2048.