--[[

2048 in CC

by Martmists

Copy this code or use

> pastebin get wb6Mn5N0 2048

NOTE:

This still contains the following bugs:

- Game over when the fiel is full, even if there are still moves left.

- if you have 4,4,8 (or similar, e.g. 2,2,4 or 256,256,512) it becomes 16 automatically. --FIXED

- the colors need to be better (not really a bug)

Controls:

Arrow keys: Move

Backspace: Quit

Field:

[1][1] || [2][1] || [3][1] || [4][1]

=======##========##========##=======

[1][2] || [2][2] || [3][2] || [4][2]

=======##========##========##=======

[1][3] || [2][3] || [3][3] || [4][3]

=======##========##========##=======

[1][4] || [2][4] || [3][4] || [4][4]

--]]

function InitField ( )

shell . run ( "clear" )

Field = { }

for i = 1 , 4 do

Field [ i ] = { 0 , 0 , 0 , 0 }

end

term . setBackgroundColor ( colors . gray )

for J = 2 , 25 do

for I = 2 , 25 do

term . setCursorPos ( J , I )

print ( " " )

end

end

term . setBackgroundColor ( colors . lightGray )

for J = 1 , 25 , 6 do

for I = 1 , 17 do

term . setCursorPos ( J , I )

print ( " " )

end

end

for J = 1 , 17 , 4 do

for I = 1 , 25 do

term . setCursorPos ( I , J )

print ( " " )

end

end

term . setBackgroundColor ( colors . gray )

end

function GetMove ( ) -- Returns what key you pressed

event , key = os . pullEvent ( "key" )

end

function Move ( ) -- Initializes movement

if key == 200 then

Direction = "Left"

elseif key == 208 then

Direction = "Right"

elseif key == 205 then

Direction = "Down"

elseif key == 203 then

Direction = "Up"

elseif key == 14 then

Quit ( )

end

MoveDir ( )

end

function MoveDir ( ) -- Moves

for X = 1 , 4 do

-- Check direction

if Direction == "Up" then

D = Field [ X ] [ 1 ] -- Initialize variables

C = Field [ X ] [ 2 ]

B = Field [ X ] [ 3 ]

A = Field [ X ] [ 4 ]

elseif Direction == "Down" then

A = Field [ X ] [ 1 ] -- Initialize variables

B = Field [ X ] [ 2 ]

C = Field [ X ] [ 3 ]

D = Field [ X ] [ 4 ]

elseif Direction == "Left" then

D = Field [ 1 ] [ X ] -- Initialize variables

C = Field [ 2 ] [ X ]

B = Field [ 3 ] [ X ]

A = Field [ 4 ] [ X ]

else

A = Field [ 1 ] [ X ] -- Initialize variables

B = Field [ 2 ] [ X ]

C = Field [ 3 ] [ X ]

D = Field [ 4 ] [ X ]

end

if D ~= 0 then

if C ~= 0 then

if D == C then

D = 2 * D

if A == 0 then

if B == 0 then

C = 0

else

C = B

B = 0

end

else

if B == 0 then

C = A

A = 0

else

if A == B then

C = 2 * B

B = 0

else

C = B

B = A

end

A = 0

end

end

else

if C == B then

C = 2 * C

B = 0

else

if B == A then

B = 2 * B

A = 0

else

if B == 0 then

B = A

A = 0

end

end

end

end

else

if D == B then

D = 2 * D

C = A

B = 0

A = 0

else

if A == B then

C = 2 * B

B = 0

else

C = B

B = A

end

A = 0

end

end

else

if C ~= 0 then

if C == B then

D = 2 * C

B = 0

if A ~= 0 then

C = A

A = 0

end

else

D = C

if B == A then

C = 2 * B

B = 0

else

C = B

B = A

end

A = 0

end

else

if A == B then

D = 2 * B

A = 0

B = 0

else

D = B

C = A

B = 0

A = 0

end

end

end

-- Check Direction

if Direction == "Up" then

Field [ X ] [ 1 ] = D -- Return variables

Field [ X ] [ 2 ] = C

Field [ X ] [ 3 ] = B

Field [ X ] [ 4 ] = A

elseif Direction == "Down" then

Field [ X ] [ 1 ] = A -- Return variables

Field [ X ] [ 2 ] = B

Field [ X ] [ 3 ] = C

Field [ X ] [ 4 ] = D

elseif Direction == "Left" then

Field [ 1 ] [ X ] = D -- Return variables

Field [ 2 ] [ X ] = C

Field [ 3 ] [ X ] = B

Field [ 4 ] [ X ] = A

else

Field [ 1 ] [ X ] = A -- Return variables

Field [ 2 ] [ X ] = B

Field [ 3 ] [ X ] = C

Field [ 4 ] [ X ] = D

end

end

end

function Quit ( )

Loop = false

end

function Gen ( )

RandGen ( )

while Field [ S ] [ T ] ~= 0 do

RandGen ( )

sleep ( 0.1 )

end

Field [ S ] [ T ] = 2

end

function RandGen ( )

S = math.random ( 1 , 4 )

T = math.random ( 1 , 4 )

end

function CheckStuck ( )

StuckCount = 0

for I = 1 , 4 do

for J = 1 , 4 do

if Field [ I ] [ J ] ~= 0 then

StuckCount = StuckCount + 1

end

end

end

if StuckCount == 16 then

Loop = false

GameOver = true

end

end

function GetScore ( )

Score = 0 -- Reset Score

for I = 1 , 4 do

for J = 1 , 4 do

if Field [ I ] [ J ] ~= 2 then -- Don't count 2

Score = Score + Field [ I ] [ J ] -- Add every tile to Score

end

end

end

term . setBackgroundColor ( colors . white )

term . setTextColor ( colors . black )

term . setCursorPos ( 1 , 1 )

print ( Score )

end

function ResetDisp ( )

term . setBackgroundColor ( colors . lightGray )

for J = 1 , 25 , 6 do

for I = 1 , 17 do

term . setCursorPos ( J , I )

print ( " " )

end

end

for J = 1 , 17 , 4 do

for I = 1 , 25 do

term . setCursorPos ( I , J )

print ( " " )

end

end

term . setBackgroundColor ( colors . gray )

term . setTextColor ( colors . white )

-- Row 1

X , Y = 4 , 3

printslot ( 0 )

X , Y = 4 , 7

printslot ( 0 )

X , Y = 4 , 11

printslot ( 0 )

X , Y = 4 , 15

printslot ( 0 )

-- Row 2

X , Y = 10 , 3

printslot ( 0 )

X , Y = 10 , 7

printslot ( 0 )

X , Y = 10 , 11

printslot ( 0 )

X , Y = 10 , 15

printslot ( 0 )

-- Row 3

X , Y = 16 , 3

printslot ( 0 )

X , Y = 16 , 7

printslot ( 0 )

X , Y = 16 , 11

printslot ( 0 )

X , Y = 16 , 15

printslot ( 0 )

-- Row 4

X , Y = 22 , 3

printslot ( 0 )

X , Y = 22 , 7

printslot ( 0 )

X , Y = 22 , 11

printslot ( 0 )

X , Y = 22 , 15

printslot ( 0 )

end

function Display ( )

-- Row 1

X , Y = 4 , 3

printslot ( Field [ 1 ] [ 1 ] )

X , Y = 4 , 7

printslot ( Field [ 2 ] [ 1 ] )

X , Y = 4 , 11

printslot ( Field [ 3 ] [ 1 ] )

X , Y = 4 , 15

printslot ( Field [ 4 ] [ 1 ] )

-- Row 2

X , Y = 10 , 3

printslot ( Field [ 1 ] [ 2 ] )

X , Y = 10 , 7

printslot ( Field [ 2 ] [ 2 ] )

X , Y = 10 , 11

printslot ( Field [ 3 ] [ 2 ] )

X , Y = 10 , 15

printslot ( Field [ 4 ] [ 2 ] )

-- Row 3

X , Y = 16 , 3

printslot ( Field [ 1 ] [ 3 ] )

X , Y = 16 , 7

printslot ( Field [ 2 ] [ 3 ] )

X , Y = 16 , 11

printslot ( Field [ 3 ] [ 3 ] )

X , Y = 16 , 15

printslot ( Field [ 4 ] [ 3 ] )

-- Row 4

X , Y = 22 , 3

printslot ( Field [ 1 ] [ 4 ] )

X , Y = 22 , 7

printslot ( Field [ 2 ] [ 4 ] )

X , Y = 22 , 11

printslot ( Field [ 3 ] [ 4 ] )

X , Y = 22 , 15

printslot ( Field [ 4 ] [ 4 ] )

end

function printslot ( slot )

if slot == 0 then

term . setBackgroundColor ( colors . gray )

elseif slot == 2 then

term . setBackgroundColor ( colors . yellow )

elseif slot == 4 then

term . setBackgroundColor ( colors . orange )

elseif slot == 8 then

term . setBackgroundColor ( colors . red )

elseif slot == 16 then

term . setBackgroundColor ( colors . purple )

elseif slot == 32 then

term . setBackgroundColor ( colors . blue )

else

term . setBackgroundColor ( colors . black )

end

for i = - 2 , 2 do

for j = - 1 , 1 do

term . setCursorPos ( X + i , Y + j )

print ( " " )

end

end

if string.len ( slot ) >= 3 then

term . setCursorPos ( X - 1 , Y )

else

term . setCursorPos ( X , Y )

end

print ( slot )

end

function Main ( )

term . clear ( )

InitField ( )

Loop = true

GameOver = false

Gen ( )

while Loop do

Gen ( )

ResetDisp ( )

Display ( )

GetScore ( )

CheckStuck ( )

if Loop then

GetMove ( )

Move ( )

end

end

term . setBackgroundColor ( colors . black )

term . setTextColor ( colors . white )

term . clear ( )

if GameOver then

print ( "Game Over!" )

end

end