#include #include Global $hDLL = DllOpen("user32.dll") Global $Title[4][4] Global $Value[4][4] = [['1','2','3','4'],['5','6','7','8'],['9','10','11','12'],['13','14','15','16']] Global $Map[4][4] = [['1','2','3','4'],['5','6','7','8'],['9','10','11','12'],['13','14','15','16']] Global $Score = 0 Global $Win = False Global $IsNewGame = False Global $key_down_delays = 200 Global $Key = 'HKEY_CURRENT_USER\Software\AutoIt v3\2048' $hGUI = GUICreate('2048',300,410) GUISetFont(8.5,400,-1,'Arial') GUISetBkColor(0xFAf8EF) GUICtrlCreateLabel('2048',14,8,87,37) GUICtrlSetFont(-1,30,800) GUICtrlSetColor(-1,0x776E65) GUICtrlCreateLabel('',115,12.5,85,35,0x1000) GUICtrlSetBkColor(-1,0xBBADA0) GUICtrlCreateLabel('SCORE',114,15,85,15,0x01) GUICtrlSetColor(-1,0xEEE4CF) GUICtrlSetFont(-1,8,600) GUICtrlSetBkColor(-1,-2) $Current_Score_show = GUICtrlCreateLabel('0',114,28.5,85,15,0x01) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,10,600) GUICtrlSetBkColor(-1,-2) $Add_score = GUICtrlCreateLabel('',114,28.5,85,15,0x01) GUICtrlSetColor(-1,0x776E65) GUICtrlSetFont(-1,10,600) GUICtrlSetBkColor(-1,-2) GUICtrlCreateLabel('',205,12.5,85,35,0x1000) GUICtrlSetBkColor(-1,0xBBADA0) GUICtrlCreateLabel('BEST',204,15,85,15,0x01) GUICtrlSetColor(-1,0xEEE4CF) GUICtrlSetFont(-1,8,600) GUICtrlSetBkColor(-1,-2) $Best_Score_show = GUICtrlCreateLabel(RegRead($key,'best'),204,28.5,85,15,0x01) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,10,600) GUICtrlSetBkColor(-1,-2) GUICtrlCreateLabel('Join the numbers and get to the',15,65,133,15) GUICtrlSetFont(-1,7,400) GUICtrlSetColor(-1,0x776E65) GUICtrlCreateLabel('2048 tile!',152,65,50,15) GUICtrlSetFont(-1,7,600) GUICtrlSetColor(-1,0x776E65) $NewGame_button = GUICtrlCreateButton('New Game',205,57.5,85,25) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) $Author_button = GUICtrlCreateButton('Author',10,90,85,25) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) $Auto_button = GUICtrlCreateButton('Start AI',205,90,85,25) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) GUICtrlCreateButton('',10,120,280,280) GUICtrlSetBKColor(-1,0xafa8a2) GUICtrlSetState(-1,128) $x = 20 $y = 130 For $i = 0 to 3 For $u = 0 to 3 $Title[$i][$u] = GUICtrlCreateLabel($value[$i][$u],$x+$u*67.5,$y+$i*67.5,57.5,57.5,0x1201) GUICtrlSetBKColor(-1,0xCCC0B3) GUICtrlSetState(-1,64) GUICtrlSetFont(-1,12,800) Next Next $Win_label = GUICtrlCreateLabel('You win!',11,211,280,40,0x01) GUICtrlSetBKColor(-1,-2) GUICtrlSetFont(-1,30,900) GUICtrlSetColor(-1,0xffff00) GUICtrlSetState(-1,32+128) $Win_label_b = GUICtrlCreateLabel('You win!',10,210,280,40,0x01) GUICtrlSetBKColor(-1,-2) GUICtrlSetFont(-1,30,900) GUICtrlSetState(-1,32+128) $KeepGoing_button = GUICtrlCreateButton('Keep going',55,260,90,30) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) GUICtrlSetState(-1,32+128) $NewG_button = GUICtrlCreateButton('New game',155,260,90,30) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) GUICtrlSetState(-1,32+128) $GameOver_label = GUICtrlCreateLabel('Game over!',11,211,280,40,0x01) GUICtrlSetBKColor(-1,-2) GUICtrlSetFont(-1,30,900) GUICtrlSetColor(-1,0xff0000) GUICtrlSetState(-1,32+128) $GameOver_label_b = GUICtrlCreateLabel('Game over!',10,210,280,40,0x01) GUICtrlSetBKColor(-1,-2) GUICtrlSetFont(-1,30,900) GUICtrlSetState(-1,32+128) $TryAgain_button = GUICtrlCreateButton('Try again',105,260,90,30) GUICtrlSetBKColor(-1,0x8F7A66) GUICtrlSetColor(-1,0xf0f0f0) GUICtrlSetFont(-1,-1,600) GUICtrlSetState(-1,32+128) GUISetState() newgame() While True $msg = GUIGetMsg() Switch $msg case -3 if MsgBox(4,'2048','Do you really want to quit?',0,$hGUI) == 6 then exit case $Auto_button MsgBox(4096,'2048','Comming Soon...',0,$hGUI) case $Author_button MsgBox(4096,'2048','Facebook: http://fb.com/955610707797621' & @LF & 'Email: anonangel2001@gmail.com' & @LF & 'Blog: http://anonangelteam.blogspot.com',0,$hGUI) case $TryAgain_button newgame() case $NewGame_button newgame() case $NewG_button newgame() case $KeepGoing_button set(64) GUICtrlSetState($Win_label,32+128) GUICtrlSetState($Win_label_b,32+128) GUICtrlSetState($KeepGoing_button,32+128) GUICtrlSetState($NewG_button,32+128) AdlibRegister('getkey',1) EndSwitch WEnd func checkwin() For $g = 0 To 3 for $gg = 0 to 3 if $value[$g][$gg] == 2048 then Return True Next Next return false EndFunc Func getkey() if WinActive($hGUI) Then Global $map = coppy($value) if _IsPressed("25") Then $re = moveleft() EndIf if _IsPressed("26") Then $re = moveup() EndIf if _IsPressed("27") Then $re = moveright() EndIf if _IsPressed("28") Then $re = movedown() EndIf if isEqualMatrix($map,$value) == false Then $value = coppy($map) redraw() if $re > 0 Then $score += $re ;~ addscore($re) Sleep($key_down_delays) EndIf addtitle() if $Win = False then if checkwin() == True then set(128) GUICtrlSetState($Win_label,64+16) GUICtrlSetState($Win_label_b,64+16) GUICtrlSetState($KeepGoing_button,64+16) GUICtrlSetState($NewG_button,64+16) AdlibUnRegister('getkey') $Win = True EndIf EndIf EndIf EndIf EndFunc Func addscore($sco) $x = Random(0,15,1)*-1 + 114 GUICtrlSetData($add_score,'+'&$sco) $len = stringlen($Sco) for $y = 28.5 to 0 step -1 GUICtrlSetPos($add_score,$x+(85-($len+1)*10)/2,$y,($len+1)*10,13) Sleep($key_down_delays/28.5) Next GUICtrlSetData($add_score,'') EndFunc func set($stt) For $g = 0 To 3 for $gg = 0 to 3 GUICtrlSetState($title[$g][$gg],$stt) Next Next endfunc Func newgame() set(64) GUICtrlSetState($GameOver_label,32+128) GUICtrlSetState($GameOver_label_b,32+128) GUICtrlSetState($TryAgain_button,32+128) GUICtrlSetState($Win_label,32+128) GUICtrlSetState($Win_label_b,32+128) GUICtrlSetState($KeepGoing_button,32+128) GUICtrlSetState($NewG_button,32+128) Global $Value[4][4] = [['0','0','0','0'],['0','0','0','0'],['0','0','0','0'],['0','0','0','0']] $Win = False $IsNewGame = True addtitle() addtitle() $IsNewGame = False AdlibRegister('getkey',1) EndFunc Func checkmove() $NoMove = True for $cm = 0 to 3 Global $map = coppy($value) $re = move($cm) if isEqualMatrix($map,$value) == false then $NoMove = False EndIf Next if $NoMove == True and $IsNewGame == False Then set(128) GUICtrlSetState($GameOver_label,64+16) GUICtrlSetState($GameOver_label_b,64+16) GUICtrlSetState($TryAgain_button,64+16) AdlibUnRegister('getkey') EndIf EndFunc func redraw() For $i = 0 to 3 For $u = 0 to 3 If GUICtrlRead($Title[$i][$u]) <> $value[$i][$u] and $value[$i][$u] <> 0 then GUICtrlSetData($Title[$i][$u],$value[$i][$u]) color($i,$u) ElseIf GUICtrlRead($Title[$i][$u]) <> '' and $value[$i][$u] == 0 then GUICtrlSetData($Title[$i][$u],'') color($i,$u) EndIf If GUICtrlRead($Current_Score_show) <> $Score then GUICtrlSetData($Current_Score_show,$Score) If GUICtrlRead($Best_Score_show) < $Score then GUICtrlSetData($Best_Score_show,$Score) RegWrite($key,'best','REG_SZ',$Score) EndIf Next Next endfunc Func color($i,$u) Switch $value[$i][$u] Case 0 GUICtrlSetColor($Title[$i][$u],0x776E65) GUICtrlSetBkColor($Title[$i][$u],0xCCC0B3) Case 2 GUICtrlSetColor($Title[$i][$u],0x776E65) GUICtrlSetBkColor($Title[$i][$u],0xEEE4DA) Case 4 GUICtrlSetColor($Title[$i][$u],0x776E65) GUICtrlSetBkColor($Title[$i][$u],0xEDE0C8) Case 8 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xF2B179) Case 16 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xF59563) Case 32 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xF67C5F) Case 64 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xF65E3B) Case 128 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xEDCF72) Case 256 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xEDCC61) Case 512 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xEDC850) Case 1024 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xEDC53F) Case 2048 GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0xEDC22E) Case Else GUICtrlSetColor($Title[$i][$u],0xF9F6F2) GUICtrlSetBkColor($Title[$i][$u],0x3C3A32) EndSwitch EndFunc Func addtitle() redraw() checkmove() Do Local $ran_1 = Random(0,3,1) Local $ran_2 = Random(0,3,1) If $Value[$ran_1][$ran_2] == '0' Then $ten_percent = Random(0,9,1) if $ten_percent = 0 Then $Value[$ran_1][$ran_2] = 4 else $Value[$ran_1][$ran_2] = 2 endif ExitLoop EndIf Until False == True redraw() checkmove() endfunc Func isEqualMatrix($m1,$m2) for $i = 0 to 3 for $j = 0 to 3 if $m1[$i][$j] <> $m2[$i][$j] then return false next next return true EndFunc Func coppy($matrix) Local $ret[4][4] = [['0','0','0','0'],['0','0','0','0'],['0','0','0','0'],['0','0','0','0']] For $g = 0 To 3 for $gg = 0 to 3 $ret[$g][$gg] = $matrix[$g][$gg] Next Next Return $ret EndFunc func move($direct) Local $Rets = 0 If $direct = 0 Then $Rets = moveup() ElseIf $direct = 1 Then $Rets = moveright() ElseIf $direct = 2 Then $Rets = movedown() ElseIf $direct = 3 Then $Rets = moveleft() EndIf Return $Rets endfunc Func movedown() Local $score = 0 For $j = 0 To 3 For $i = 3 To 0 step -1 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $i+$k < 4 Then If $map[$i+$k][$j] == 0 Then $max += 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i+$max][$j] = $map[$i][$j] $map[$i][$j] = 0 EndIf Next For $i = 3 To 0 step -1 If $map[$i][$j] <> 0 Then If $i+1 < 4 Then If $map[$i+1][$j] == $map[$i][$j] Then $map[$i+1][$j] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next For $i = 3 To 0 step -1 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $i+$k < 4 Then If $map[$i+$k][$j] == 0 Then $max += 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i+$max][$j] = $map[$i][$j] $map[$i][$j] = 0 EndIf Next Next Return $score EndFunc Func moveup() Local $score = 0 For $j = 0 To 3 Local $betrue = 0 For $i = 0 To 3 If $map[$i][$j] <> 0 Then If $i-1 > -1 Then If $map[$i-1][$j] == $map[$i][$j] Then $map[$i-1][$j] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next For $i = 0 To 3 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $i-$k > -1 Then If $map[$i-$k][$j] == 0 Then $max -= 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i+$max][$j] = $map[$i][$j] $map[$i][$j] = 0 $betrue = 1 EndIf Next If $betrue = 1 Then For $i = 3 To 0 step -1 If $map[$i][$j] <> 0 Then If $i-1 > -1 Then If $map[$i-1][$j] == $map[$i][$j] Then $map[$i-1][$j] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next EndIf Next Return $score EndFunc Func moveleft() Local $score = 0 For $i = 0 To 3 For $j = 0 To 3 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $j-$k > -1 Then If $map[$i][$j-$k] == 0 Then $max -= 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i][$j+$max] = $map[$i][$j] $map[$i][$j] = 0 EndIf Next For $j = 0 To 3 If $map[$i][$j] <> 0 Then If $j-1 > -1 Then If $map[$i][$j-1] == $map[$i][$j] Then $map[$i][$j-1] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next For $j = 0 To 3 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $j-$k > -1 Then If $map[$i][$j-$k] == 0 Then $max -= 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i][$j+$max] = $map[$i][$j] $map[$i][$j] = 0 EndIf Next Next Return $score EndFunc Func moveright() Local $score = 0 For $i = 0 To 3 Local $betrue = 0 For $j = 3 To 0 step -1 If $map[$i][$j] <> 0 Then If $j+1 < 4 Then If $map[$i][$j+1] == $map[$i][$j] Then $map[$i][$j+1] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next For $j = 3 To 0 step -1 Local $max = 0 For $k = 1 to 3 If $map[$i][$j] <> 0 Then If $j+$k < 4 Then If $map[$i][$j+$k] == 0 Then $max += 1 Else ExitLoop EndIf EndIf EndIf Next If $max <> 0 then $map[$i][$j+$max] = $map[$i][$j] $map[$i][$j] = 0 $betrue = 1 EndIf Next If $betrue = 1 Then For $j = 3 To 0 step -1 If $map[$i][$j] <> 0 Then If $j+1 < 4 Then If $map[$i][$j+1] == $map[$i][$j] Then $map[$i][$j+1] = $map[$i][$j]*2 $score += ($map[$i][$j]*2) $map[$i][$j] = 0 EndIf EndIf EndIf Next EndIf Next Return $score EndFunc