Untitled


SUBMITTED BY: Guest

DATE: July 28, 2015, 3:12 p.m.

FORMAT: Text only

SIZE: 15.7 kB

HITS: 441

  1. #include <array.au3>
  2. #include <Misc.au3>
  3. Global $hDLL = DllOpen("user32.dll")
  4. Global $Title[4][4]
  5. Global $Value[4][4] = [['1','2','3','4'],['5','6','7','8'],['9','10','11','12'],['13','14','15','16']]
  6. Global $Map[4][4] = [['1','2','3','4'],['5','6','7','8'],['9','10','11','12'],['13','14','15','16']]
  7. Global $Score = 0
  8. Global $Win = False
  9. Global $IsNewGame = False
  10. Global $key_down_delays = 200
  11. Global $Key = 'HKEY_CURRENT_USER\Software\AutoIt v3\2048'
  12. $hGUI = GUICreate('2048',300,410)
  13. GUISetFont(8.5,400,-1,'Arial')
  14. GUISetBkColor(0xFAf8EF)
  15. GUICtrlCreateLabel('2048',14,8,87,37)
  16. GUICtrlSetFont(-1,30,800)
  17. GUICtrlSetColor(-1,0x776E65)
  18. GUICtrlCreateLabel('',115,12.5,85,35,0x1000)
  19. GUICtrlSetBkColor(-1,0xBBADA0)
  20. GUICtrlCreateLabel('SCORE',114,15,85,15,0x01)
  21. GUICtrlSetColor(-1,0xEEE4CF)
  22. GUICtrlSetFont(-1,8,600)
  23. GUICtrlSetBkColor(-1,-2)
  24. $Current_Score_show = GUICtrlCreateLabel('0',114,28.5,85,15,0x01)
  25. GUICtrlSetColor(-1,0xf0f0f0)
  26. GUICtrlSetFont(-1,10,600)
  27. GUICtrlSetBkColor(-1,-2)
  28. $Add_score = GUICtrlCreateLabel('',114,28.5,85,15,0x01)
  29. GUICtrlSetColor(-1,0x776E65)
  30. GUICtrlSetFont(-1,10,600)
  31. GUICtrlSetBkColor(-1,-2)
  32. GUICtrlCreateLabel('',205,12.5,85,35,0x1000)
  33. GUICtrlSetBkColor(-1,0xBBADA0)
  34. GUICtrlCreateLabel('BEST',204,15,85,15,0x01)
  35. GUICtrlSetColor(-1,0xEEE4CF)
  36. GUICtrlSetFont(-1,8,600)
  37. GUICtrlSetBkColor(-1,-2)
  38. $Best_Score_show = GUICtrlCreateLabel(RegRead($key,'best'),204,28.5,85,15,0x01)
  39. GUICtrlSetColor(-1,0xf0f0f0)
  40. GUICtrlSetFont(-1,10,600)
  41. GUICtrlSetBkColor(-1,-2)
  42. GUICtrlCreateLabel('Join the numbers and get to the',15,65,133,15)
  43. GUICtrlSetFont(-1,7,400)
  44. GUICtrlSetColor(-1,0x776E65)
  45. GUICtrlCreateLabel('2048 tile!',152,65,50,15)
  46. GUICtrlSetFont(-1,7,600)
  47. GUICtrlSetColor(-1,0x776E65)
  48. $NewGame_button = GUICtrlCreateButton('New Game',205,57.5,85,25)
  49. GUICtrlSetBKColor(-1,0x8F7A66)
  50. GUICtrlSetColor(-1,0xf0f0f0)
  51. GUICtrlSetFont(-1,-1,600)
  52. $Author_button = GUICtrlCreateButton('Author',10,90,85,25)
  53. GUICtrlSetBKColor(-1,0x8F7A66)
  54. GUICtrlSetColor(-1,0xf0f0f0)
  55. GUICtrlSetFont(-1,-1,600)
  56. $Auto_button = GUICtrlCreateButton('Start AI',205,90,85,25)
  57. GUICtrlSetBKColor(-1,0x8F7A66)
  58. GUICtrlSetColor(-1,0xf0f0f0)
  59. GUICtrlSetFont(-1,-1,600)
  60. GUICtrlCreateButton('',10,120,280,280)
  61. GUICtrlSetBKColor(-1,0xafa8a2)
  62. GUICtrlSetState(-1,128)
  63. $x = 20
  64. $y = 130
  65. For $i = 0 to 3
  66. For $u = 0 to 3
  67. $Title[$i][$u] = GUICtrlCreateLabel($value[$i][$u],$x+$u*67.5,$y+$i*67.5,57.5,57.5,0x1201)
  68. GUICtrlSetBKColor(-1,0xCCC0B3)
  69. GUICtrlSetState(-1,64)
  70. GUICtrlSetFont(-1,12,800)
  71. Next
  72. Next
  73. $Win_label = GUICtrlCreateLabel('You win!',11,211,280,40,0x01)
  74. GUICtrlSetBKColor(-1,-2)
  75. GUICtrlSetFont(-1,30,900)
  76. GUICtrlSetColor(-1,0xffff00)
  77. GUICtrlSetState(-1,32+128)
  78. $Win_label_b = GUICtrlCreateLabel('You win!',10,210,280,40,0x01)
  79. GUICtrlSetBKColor(-1,-2)
  80. GUICtrlSetFont(-1,30,900)
  81. GUICtrlSetState(-1,32+128)
  82. $KeepGoing_button = GUICtrlCreateButton('Keep going',55,260,90,30)
  83. GUICtrlSetBKColor(-1,0x8F7A66)
  84. GUICtrlSetColor(-1,0xf0f0f0)
  85. GUICtrlSetFont(-1,-1,600)
  86. GUICtrlSetState(-1,32+128)
  87. $NewG_button = GUICtrlCreateButton('New game',155,260,90,30)
  88. GUICtrlSetBKColor(-1,0x8F7A66)
  89. GUICtrlSetColor(-1,0xf0f0f0)
  90. GUICtrlSetFont(-1,-1,600)
  91. GUICtrlSetState(-1,32+128)
  92. $GameOver_label = GUICtrlCreateLabel('Game over!',11,211,280,40,0x01)
  93. GUICtrlSetBKColor(-1,-2)
  94. GUICtrlSetFont(-1,30,900)
  95. GUICtrlSetColor(-1,0xff0000)
  96. GUICtrlSetState(-1,32+128)
  97. $GameOver_label_b = GUICtrlCreateLabel('Game over!',10,210,280,40,0x01)
  98. GUICtrlSetBKColor(-1,-2)
  99. GUICtrlSetFont(-1,30,900)
  100. GUICtrlSetState(-1,32+128)
  101. $TryAgain_button = GUICtrlCreateButton('Try again',105,260,90,30)
  102. GUICtrlSetBKColor(-1,0x8F7A66)
  103. GUICtrlSetColor(-1,0xf0f0f0)
  104. GUICtrlSetFont(-1,-1,600)
  105. GUICtrlSetState(-1,32+128)
  106. GUISetState()
  107. newgame()
  108. While True
  109. $msg = GUIGetMsg()
  110. Switch $msg
  111. case -3
  112. if MsgBox(4,'2048','Do you really want to quit?',0,$hGUI) == 6 then exit
  113. case $Auto_button
  114. MsgBox(4096,'2048','Comming Soon...',0,$hGUI)
  115. case $Author_button
  116. MsgBox(4096,'2048','Facebook: http://fb.com/955610707797621' & @LF & 'Email: anonangel2001@gmail.com' & @LF & 'Blog: http://anonangelteam.blogspot.com',0,$hGUI)
  117. case $TryAgain_button
  118. newgame()
  119. case $NewGame_button
  120. newgame()
  121. case $NewG_button
  122. newgame()
  123. case $KeepGoing_button
  124. set(64)
  125. GUICtrlSetState($Win_label,32+128)
  126. GUICtrlSetState($Win_label_b,32+128)
  127. GUICtrlSetState($KeepGoing_button,32+128)
  128. GUICtrlSetState($NewG_button,32+128)
  129. AdlibRegister('getkey',1)
  130. EndSwitch
  131. WEnd
  132. func checkwin()
  133. For $g = 0 To 3
  134. for $gg = 0 to 3
  135. if $value[$g][$gg] == 2048 then Return True
  136. Next
  137. Next
  138. return false
  139. EndFunc
  140. Func getkey()
  141. if WinActive($hGUI) Then
  142. Global $map = coppy($value)
  143. if _IsPressed("25") Then
  144. $re = moveleft()
  145. EndIf
  146. if _IsPressed("26") Then
  147. $re = moveup()
  148. EndIf
  149. if _IsPressed("27") Then
  150. $re = moveright()
  151. EndIf
  152. if _IsPressed("28") Then
  153. $re = movedown()
  154. EndIf
  155. if isEqualMatrix($map,$value) == false Then
  156. $value = coppy($map)
  157. redraw()
  158. if $re > 0 Then
  159. $score += $re
  160. ;~ addscore($re)
  161. Sleep($key_down_delays)
  162. EndIf
  163. addtitle()
  164. if $Win = False then
  165. if checkwin() == True then
  166. set(128)
  167. GUICtrlSetState($Win_label,64+16)
  168. GUICtrlSetState($Win_label_b,64+16)
  169. GUICtrlSetState($KeepGoing_button,64+16)
  170. GUICtrlSetState($NewG_button,64+16)
  171. AdlibUnRegister('getkey')
  172. $Win = True
  173. EndIf
  174. EndIf
  175. EndIf
  176. EndIf
  177. EndFunc
  178. Func addscore($sco)
  179. $x = Random(0,15,1)*-1 + 114
  180. GUICtrlSetData($add_score,'+'&$sco)
  181. $len = stringlen($Sco)
  182. for $y = 28.5 to 0 step -1
  183. GUICtrlSetPos($add_score,$x+(85-($len+1)*10)/2,$y,($len+1)*10,13)
  184. Sleep($key_down_delays/28.5)
  185. Next
  186. GUICtrlSetData($add_score,'')
  187. EndFunc
  188. func set($stt)
  189. For $g = 0 To 3
  190. for $gg = 0 to 3
  191. GUICtrlSetState($title[$g][$gg],$stt)
  192. Next
  193. Next
  194. endfunc
  195. Func newgame()
  196. set(64)
  197. GUICtrlSetState($GameOver_label,32+128)
  198. GUICtrlSetState($GameOver_label_b,32+128)
  199. GUICtrlSetState($TryAgain_button,32+128)
  200. GUICtrlSetState($Win_label,32+128)
  201. GUICtrlSetState($Win_label_b,32+128)
  202. GUICtrlSetState($KeepGoing_button,32+128)
  203. GUICtrlSetState($NewG_button,32+128)
  204. Global $Value[4][4] = [['0','0','0','0'],['0','0','0','0'],['0','0','0','0'],['0','0','0','0']]
  205. $Win = False
  206. $IsNewGame = True
  207. addtitle()
  208. addtitle()
  209. $IsNewGame = False
  210. AdlibRegister('getkey',1)
  211. EndFunc
  212. Func checkmove()
  213. $NoMove = True
  214. for $cm = 0 to 3
  215. Global $map = coppy($value)
  216. $re = move($cm)
  217. if isEqualMatrix($map,$value) == false then
  218. $NoMove = False
  219. EndIf
  220. Next
  221. if $NoMove == True and $IsNewGame == False Then
  222. set(128)
  223. GUICtrlSetState($GameOver_label,64+16)
  224. GUICtrlSetState($GameOver_label_b,64+16)
  225. GUICtrlSetState($TryAgain_button,64+16)
  226. AdlibUnRegister('getkey')
  227. EndIf
  228. EndFunc
  229. func redraw()
  230. For $i = 0 to 3
  231. For $u = 0 to 3
  232. If GUICtrlRead($Title[$i][$u]) <> $value[$i][$u] and $value[$i][$u] <> 0 then
  233. GUICtrlSetData($Title[$i][$u],$value[$i][$u])
  234. color($i,$u)
  235. ElseIf GUICtrlRead($Title[$i][$u]) <> '' and $value[$i][$u] == 0 then
  236. GUICtrlSetData($Title[$i][$u],'')
  237. color($i,$u)
  238. EndIf
  239. If GUICtrlRead($Current_Score_show) <> $Score then GUICtrlSetData($Current_Score_show,$Score)
  240. If GUICtrlRead($Best_Score_show) < $Score then
  241. GUICtrlSetData($Best_Score_show,$Score)
  242. RegWrite($key,'best','REG_SZ',$Score)
  243. EndIf
  244. Next
  245. Next
  246. endfunc
  247. Func color($i,$u)
  248. Switch $value[$i][$u]
  249. Case 0
  250. GUICtrlSetColor($Title[$i][$u],0x776E65)
  251. GUICtrlSetBkColor($Title[$i][$u],0xCCC0B3)
  252. Case 2
  253. GUICtrlSetColor($Title[$i][$u],0x776E65)
  254. GUICtrlSetBkColor($Title[$i][$u],0xEEE4DA)
  255. Case 4
  256. GUICtrlSetColor($Title[$i][$u],0x776E65)
  257. GUICtrlSetBkColor($Title[$i][$u],0xEDE0C8)
  258. Case 8
  259. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  260. GUICtrlSetBkColor($Title[$i][$u],0xF2B179)
  261. Case 16
  262. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  263. GUICtrlSetBkColor($Title[$i][$u],0xF59563)
  264. Case 32
  265. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  266. GUICtrlSetBkColor($Title[$i][$u],0xF67C5F)
  267. Case 64
  268. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  269. GUICtrlSetBkColor($Title[$i][$u],0xF65E3B)
  270. Case 128
  271. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  272. GUICtrlSetBkColor($Title[$i][$u],0xEDCF72)
  273. Case 256
  274. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  275. GUICtrlSetBkColor($Title[$i][$u],0xEDCC61)
  276. Case 512
  277. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  278. GUICtrlSetBkColor($Title[$i][$u],0xEDC850)
  279. Case 1024
  280. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  281. GUICtrlSetBkColor($Title[$i][$u],0xEDC53F)
  282. Case 2048
  283. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  284. GUICtrlSetBkColor($Title[$i][$u],0xEDC22E)
  285. Case Else
  286. GUICtrlSetColor($Title[$i][$u],0xF9F6F2)
  287. GUICtrlSetBkColor($Title[$i][$u],0x3C3A32)
  288. EndSwitch
  289. EndFunc
  290. Func addtitle()
  291. redraw()
  292. checkmove()
  293. Do
  294. Local $ran_1 = Random(0,3,1)
  295. Local $ran_2 = Random(0,3,1)
  296. If $Value[$ran_1][$ran_2] == '0' Then
  297. $ten_percent = Random(0,9,1)
  298. if $ten_percent = 0 Then
  299. $Value[$ran_1][$ran_2] = 4
  300. else
  301. $Value[$ran_1][$ran_2] = 2
  302. endif
  303. ExitLoop
  304. EndIf
  305. Until False == True
  306. redraw()
  307. checkmove()
  308. endfunc
  309. Func isEqualMatrix($m1,$m2)
  310. for $i = 0 to 3
  311. for $j = 0 to 3
  312. if $m1[$i][$j] <> $m2[$i][$j] then return false
  313. next
  314. next
  315. return true
  316. EndFunc
  317. Func coppy($matrix)
  318. Local $ret[4][4] = [['0','0','0','0'],['0','0','0','0'],['0','0','0','0'],['0','0','0','0']]
  319. For $g = 0 To 3
  320. for $gg = 0 to 3
  321. $ret[$g][$gg] = $matrix[$g][$gg]
  322. Next
  323. Next
  324. Return $ret
  325. EndFunc
  326. func move($direct)
  327. Local $Rets = 0
  328. If $direct = 0 Then
  329. $Rets = moveup()
  330. ElseIf $direct = 1 Then
  331. $Rets = moveright()
  332. ElseIf $direct = 2 Then
  333. $Rets = movedown()
  334. ElseIf $direct = 3 Then
  335. $Rets = moveleft()
  336. EndIf
  337. Return $Rets
  338. endfunc
  339. Func movedown()
  340. Local $score = 0
  341. For $j = 0 To 3
  342. For $i = 3 To 0 step -1
  343. Local $max = 0
  344. For $k = 1 to 3
  345. If $map[$i][$j] <> 0 Then
  346. If $i+$k < 4 Then
  347. If $map[$i+$k][$j] == 0 Then
  348. $max += 1
  349. Else
  350. ExitLoop
  351. EndIf
  352. EndIf
  353. EndIf
  354. Next
  355. If $max <> 0 then
  356. $map[$i+$max][$j] = $map[$i][$j]
  357. $map[$i][$j] = 0
  358. EndIf
  359. Next
  360. For $i = 3 To 0 step -1
  361. If $map[$i][$j] <> 0 Then
  362. If $i+1 < 4 Then
  363. If $map[$i+1][$j] == $map[$i][$j] Then
  364. $map[$i+1][$j] = $map[$i][$j]*2
  365. $score += ($map[$i][$j]*2)
  366. $map[$i][$j] = 0
  367. EndIf
  368. EndIf
  369. EndIf
  370. Next
  371. For $i = 3 To 0 step -1
  372. Local $max = 0
  373. For $k = 1 to 3
  374. If $map[$i][$j] <> 0 Then
  375. If $i+$k < 4 Then
  376. If $map[$i+$k][$j] == 0 Then
  377. $max += 1
  378. Else
  379. ExitLoop
  380. EndIf
  381. EndIf
  382. EndIf
  383. Next
  384. If $max <> 0 then
  385. $map[$i+$max][$j] = $map[$i][$j]
  386. $map[$i][$j] = 0
  387. EndIf
  388. Next
  389. Next
  390. Return $score
  391. EndFunc
  392. Func moveup()
  393. Local $score = 0
  394. For $j = 0 To 3
  395. Local $betrue = 0
  396. For $i = 0 To 3
  397. If $map[$i][$j] <> 0 Then
  398. If $i-1 > -1 Then
  399. If $map[$i-1][$j] == $map[$i][$j] Then
  400. $map[$i-1][$j] = $map[$i][$j]*2
  401. $score += ($map[$i][$j]*2)
  402. $map[$i][$j] = 0
  403. EndIf
  404. EndIf
  405. EndIf
  406. Next
  407. For $i = 0 To 3
  408. Local $max = 0
  409. For $k = 1 to 3
  410. If $map[$i][$j] <> 0 Then
  411. If $i-$k > -1 Then
  412. If $map[$i-$k][$j] == 0 Then
  413. $max -= 1
  414. Else
  415. ExitLoop
  416. EndIf
  417. EndIf
  418. EndIf
  419. Next
  420. If $max <> 0 then
  421. $map[$i+$max][$j] = $map[$i][$j]
  422. $map[$i][$j] = 0
  423. $betrue = 1
  424. EndIf
  425. Next
  426. If $betrue = 1 Then
  427. For $i = 3 To 0 step -1
  428. If $map[$i][$j] <> 0 Then
  429. If $i-1 > -1 Then
  430. If $map[$i-1][$j] == $map[$i][$j] Then
  431. $map[$i-1][$j] = $map[$i][$j]*2
  432. $score += ($map[$i][$j]*2)
  433. $map[$i][$j] = 0
  434. EndIf
  435. EndIf
  436. EndIf
  437. Next
  438. EndIf
  439. Next
  440. Return $score
  441. EndFunc
  442. Func moveleft()
  443. Local $score = 0
  444. For $i = 0 To 3
  445. For $j = 0 To 3
  446. Local $max = 0
  447. For $k = 1 to 3
  448. If $map[$i][$j] <> 0 Then
  449. If $j-$k > -1 Then
  450. If $map[$i][$j-$k] == 0 Then
  451. $max -= 1
  452. Else
  453. ExitLoop
  454. EndIf
  455. EndIf
  456. EndIf
  457. Next
  458. If $max <> 0 then
  459. $map[$i][$j+$max] = $map[$i][$j]
  460. $map[$i][$j] = 0
  461. EndIf
  462. Next
  463. For $j = 0 To 3
  464. If $map[$i][$j] <> 0 Then
  465. If $j-1 > -1 Then
  466. If $map[$i][$j-1] == $map[$i][$j] Then
  467. $map[$i][$j-1] = $map[$i][$j]*2
  468. $score += ($map[$i][$j]*2)
  469. $map[$i][$j] = 0
  470. EndIf
  471. EndIf
  472. EndIf
  473. Next
  474. For $j = 0 To 3
  475. Local $max = 0
  476. For $k = 1 to 3
  477. If $map[$i][$j] <> 0 Then
  478. If $j-$k > -1 Then
  479. If $map[$i][$j-$k] == 0 Then
  480. $max -= 1
  481. Else
  482. ExitLoop
  483. EndIf
  484. EndIf
  485. EndIf
  486. Next
  487. If $max <> 0 then
  488. $map[$i][$j+$max] = $map[$i][$j]
  489. $map[$i][$j] = 0
  490. EndIf
  491. Next
  492. Next
  493. Return $score
  494. EndFunc
  495. Func moveright()
  496. Local $score = 0
  497. For $i = 0 To 3
  498. Local $betrue = 0
  499. For $j = 3 To 0 step -1
  500. If $map[$i][$j] <> 0 Then
  501. If $j+1 < 4 Then
  502. If $map[$i][$j+1] == $map[$i][$j] Then
  503. $map[$i][$j+1] = $map[$i][$j]*2
  504. $score += ($map[$i][$j]*2)
  505. $map[$i][$j] = 0
  506. EndIf
  507. EndIf
  508. EndIf
  509. Next
  510. For $j = 3 To 0 step -1
  511. Local $max = 0
  512. For $k = 1 to 3
  513. If $map[$i][$j] <> 0 Then
  514. If $j+$k < 4 Then
  515. If $map[$i][$j+$k] == 0 Then
  516. $max += 1
  517. Else
  518. ExitLoop
  519. EndIf
  520. EndIf
  521. EndIf
  522. Next
  523. If $max <> 0 then
  524. $map[$i][$j+$max] = $map[$i][$j]
  525. $map[$i][$j] = 0
  526. $betrue = 1
  527. EndIf
  528. Next
  529. If $betrue = 1 Then
  530. For $j = 3 To 0 step -1
  531. If $map[$i][$j] <> 0 Then
  532. If $j+1 < 4 Then
  533. If $map[$i][$j+1] == $map[$i][$j] Then
  534. $map[$i][$j+1] = $map[$i][$j]*2
  535. $score += ($map[$i][$j]*2)
  536. $map[$i][$j] = 0
  537. EndIf
  538. EndIf
  539. EndIf
  540. Next
  541. EndIf
  542. Next
  543. Return $score
  544. EndFunc

comments powered by Disqus