Блог им. s_mike |2048

Нашел у себя тексты некогда популярной игрушки 2048, на скорую руку адаптировал к квику. Ловите, улыбайтесь )

local stopped

game = {
	cell = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	best = 0,
--	key = nil,
-- t = nil,
	colors =	{	[0]	= QTABLE_DEFAULT_COLOR,
					[2]	= RGB(248,248,255),
					[4]	= RGB(255,239,213),
					[8]	= RGB(255,222,173),
					[8]	= RGB(255,218,185),
					[16]	= RGB(205,92,92),
					[32]	= RGB(255,69,0),
					[64]	= RGB(238,238,0),
					[128]	= RGB(238,118,33),
					[256]	= RGB(255,160,122),
					[512]	= RGB(255,0,0),
					[1028]= RGB(238,18,137),
					[2048]= RGB(139,0,0)
				},
	
	create_table =
		function(self)
			self.t = AllocTable()
			for i = 1,4 do
				AddColumn(self.t, i, "", true, QTABLE_INT_TYPE, 10)
			end
			CreateWindow(self.t)
			SetWindowPos(self.t,100,100,300,130)
			for i = 1,4 do
				InsertRow(self.t,i)
			end
			SetTableNotificationCallback(self.t, 		
												  function(_t, msg, _p, key)
													if msg == QTABLE_VKEY then
														self.key = key
													elseif msg == QTABLE_CLOSE then
														stopped = true
													end
												  end)
			SetWindowCaption(self.t, "2048 game by www.bot4sale.ru")
		end,
	
	draw = 
		function(self)
			local cell = self.cell
			for row=1,4 do
				for column=1,4 do
					local value = cell[(row-1)*4 + column]
					SetCell(self.t,row,column,value==0 and "" or tostring(value))
					SetColor(self.t,row,column,game.colors[value],0,game.colors[value],0)
				end
			end
	  end,
  
	incr = 
		function(self)
			local t,open = self.cell,{}
			for i=1,16 do 
				if t[i]==0 then 
					open[#open+1]=i 
				end
			end
			t[open[math.random(#open)]] = math.random()<0.1 and 4 or 2
		end,
  
	pack = 
		function(self,ofr,oto,ost,ifr,ito,ist)
			local t = self.cell
			for outer=ofr,oto,ost do
				local skip = 0
				for inner=ifr,ito,ist do
					local i = outer+inner
					if t[i]==0 then 
						skip=skip+1 
					else 
						if skip>0 then 
							t[i-skip*ist],t[i],self.diff = t[i],0,true 
						end 
					end
				end
			end
		end,
  
	comb = 
		function(self,ofr,oto,ost,ifr,ito,ist)
			local t = self.cell
			for outer=ofr,oto,ost do
				for inner=ifr,ito-ist,ist do
					local i,j = outer+inner,outer+inner+ist
					if t[i]>0 and t[i]==t[j] then 
						t[i],t[j],self.diff,self.best = t[i]*2,0,true,math.max(self.best,t[i]*2) 
					end
				end
			end
		end,
  
	move = 
		function(self,dir)
			local loopdata =	{	{0,12,4,1,4,1},
										{0,12,4,4,1,-1},
										{1,4,1,0,12,4},
										{1,4,1,12,0,-4}
									}
			local ofr,oto,ost,ifr,ito,ist = table.unpack(loopdata[dir])
			self:pack(ofr,oto,ost,ifr,ito,ist)
			self:comb(ofr,oto,ost,ifr,ito,ist)
			self:pack(ofr,oto,ost,ifr,ito,ist)
		end,
  
	full = 
		function(self)
			local t = self.cell
			for r=0,12,4 do
				for c=1,4 do
					local i,v = r+c,t[r+c]
					if (v==0) or (c>1 and t[i-1]==v) or (c<4 and t[i+1]==v) or (r>0 and t[i-4]==v) or (r<12 and t[i+4]==v) then
						return false
					end
				end
			end
		return true
	end,
  
	play = 
		function(self)
			math.randomseed(os.time())
			self:incr()
			self:incr()
			while not stopped do
				self:draw()
				if self.best == 2048 then 
					message("Поздравляю!\n\nwww.bot4sale.ru") 
					break 
				end
				if self:full() then 
					message("Game Over!")
					break 
				end
				
				self.diff = false
				if		 self.key==0x25 then 		-- Left
					self:move(1)
				elseif self.key==0x27 then			-- Right
					self:move(2)
				elseif self.key==0x26 then 		-- Up
					self:move(3)
				elseif self.key==0x28 then			-- Down
					self:move(4)
				end
				self.key = nil
				if self.diff then 
					self:incr() 
				end
				sleep(100)
			end
		end,
		
	rules = 
		function()
			message("Правила игры:\n\nВ каждом раунде появляется плитка номинала «2» или «4». Нажатием стрелки игрок может скинуть все плитки игрового поля в одну из 4 сторон. Если при сбрасывании две плитки одного номинала «налетают» одна на другую, то они превращаются в одну, номинал которой равен сумме соединившихся плиток. После каждого хода на свободной секции поля появляется новая плитка номиналом «2» или «4». Если при нажатии кнопки местоположение плиток или их номинал не изменится, то ход не совершается. Если в одной строчке или в одном столбце находится более двух плиток одного номинала, то при сбрасывании они начинают соединяться с той стороны, в которую были направлены.\nЛевый столбец с номерами строк участия в игре не принимает.\n\[email protected]")
		end
}
local game = game

--------------------------------------------------

function main()
	game:create_table()
	game:rules()
	game:play()
end

 

 Оригинал здесь


....все тэги
UPDONW
Новый дизайн