Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #/usr/bin/env lua
  2.  
  3. require('strict')
  4.  
  5. board = {
  6. {0,0,0,0},
  7. {0,0,0,0},
  8. {0,0,0,0},
  9. {0,0,0,0},
  10. }
  11.  
  12. function show_board(board)
  13. local r, c
  14.  
  15. print(" UUUU")
  16. print(" +----+")
  17. for r=1,4 do
  18. io.write("L:")
  19. for c=1,4 do
  20. if board[r][c] == 0 then
  21. io.write('.')
  22. else
  23. io.write(string.char(string.byte('a') - 1 + board[r][c]))
  24. end
  25. end
  26. print(":R")
  27. end
  28. print(" +----+")
  29. print(" DDDD")
  30. print()
  31. end
  32.  
  33. function add_blocks_at_random(number)
  34. local function add_block_at_random()
  35. local r = math.random(4)
  36. local c = math.random(4)
  37.  
  38. while board[r][c] ~= 0 do
  39. r = math.random(4)
  40. c = math.random(4)
  41. end
  42.  
  43. board[r][c] = 1
  44. end
  45.  
  46. local c
  47.  
  48. for c=1,number do
  49. add_block_at_random()
  50. end
  51. end
  52.  
  53. function can_make_a_move()
  54. local r, c
  55.  
  56. -- Game over once a 'z' appears
  57. for r=1,4 do
  58. for c=1,4 do
  59. if board[r][c] == 26 then
  60. return false
  61. end
  62. end
  63. end
  64.  
  65. -- Are there any free spaces?
  66. for r=1,4 do
  67. for c=1,4 do
  68. if board[r][c] == 0 then
  69. return true
  70. end
  71. end
  72. end
  73.  
  74. -- Could non empty spaces be merged?
  75. for r=1,3 do
  76. for c=1,3 do
  77. if board[r][c] ~= 0 then
  78. if board[r][c] == board[r][c+1] or board[r][c] == board[r+1][c] then
  79. return true
  80. end
  81. end
  82. end
  83. end
  84.  
  85. for c=1,3 do
  86. if board[4][c] ~= 0 then
  87. if board[4][c] == board[4][c+1] then
  88. return true
  89. end
  90. end
  91. end
  92.  
  93. -- Well that would be a no then
  94. return false
  95. end
  96.  
  97. function get_user_move()
  98. local here, waiting_for_input
  99.  
  100. waiting_for_input = true
  101.  
  102. while waiting_for_input do
  103. waiting_for_input = false
  104.  
  105. print("Enter a direction to move: U, D, L or R")
  106. here = io.read()
  107.  
  108. if here == 'U' or here == 'u' then
  109. here = 'u'
  110. elseif here == 'D' or here == 'd' then
  111. here = 'd'
  112. elseif here == 'L' or here == 'l' then
  113. here = 'l'
  114. elseif here == 'R' or here == 'r' then
  115. here = 'r'
  116. else
  117. waiting_for_input = true
  118. print("That was not a valid instruction")
  119. end
  120. end
  121.  
  122. return here
  123. end
  124.  
  125. function move_board(here)
  126. local function check(r, c, dr, dc)
  127. local moved = 0
  128.  
  129. if board[r][c] == 0 then
  130. if board[r+dr][c+dc] ~= 0 then
  131. board[r][c] = board[r+dr][c+dc]
  132. board[r+dr][c+dc] = 0
  133. moved = moved + 1
  134. end
  135. elseif board[r][c] == board[r+dr][c+dc] then
  136. board[r][c] = board[r][c] + 1
  137. board[r+dr][c+dc] = 0
  138. moved = moved + 1
  139. end
  140.  
  141. return moved
  142. end
  143.  
  144. local function find(row_from,row_to,row_step,column_from,column_to,column_step,row_offset,column_offset)
  145. local r, c
  146.  
  147. local x = 0
  148.  
  149. for r=row_from,row_to,row_step do
  150. for c=column_from,column_to,column_step do
  151. x = x + check(r, c, row_offset, column_offset)
  152. end
  153. end
  154.  
  155. return x > 0
  156. end
  157.  
  158. if here == 'l' then
  159. return find(1,4,1,1,3,1,0,1) and here or 'x'
  160. elseif here == 'r' then
  161. return find(1,4,1,4,2,-1,0,-1) and here or 'x'
  162. elseif here == 'u' then
  163. return find(1,3,1,1,4,1,1,0) and here or 'x'
  164. else
  165. return find(4,2,-1,1,4,1,-1,0) and here or 'x'
  166. end
  167. end
  168.  
  169. function add_new_block(here)
  170. local function pick_one(t)
  171. local x = math.random(#t)
  172. return t[x]
  173. end
  174.  
  175. local function add_to_column(c)
  176. local r
  177. local v = {}
  178.  
  179. for r=1,4 do
  180. if board[r][c] == 0 then
  181. v[#v+1] = r
  182. end
  183. end
  184.  
  185. board[pick_one(v)][c] = 1
  186. end
  187.  
  188. local function add_to_row(r)
  189. local c
  190. local v = {}
  191.  
  192. for c=1,4 do
  193. if board[r][c] == 0 then
  194. v[#v+1] = c
  195. end
  196. end
  197.  
  198. board[r][pick_one(v)] = 1
  199. end
  200.  
  201. if here == 'l' then
  202. add_to_column(4)
  203. elseif here == 'r' then
  204. add_to_column(1)
  205. elseif here == 'u' then
  206. add_to_row(4)
  207. else
  208. add_to_row(1)
  209. end
  210. end
  211.  
  212. function score_game(board)
  213. -- Better scoring values required
  214. local scores = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}
  215.  
  216. local r, c
  217. local score = 0
  218. local max = 0
  219.  
  220. for r=1,4 do
  221. for c=1,4 do
  222. score = score + scores[board[r][c]]
  223. if max < board[r][c] then
  224. max = board[r][c]
  225. end
  226. end
  227. end
  228.  
  229. print("Final score was " .. score .. " The highest letter was '" .. string.char(string.byte('a') + max - 1) .. "'")
  230. end
  231.  
  232. math.randomseed( os.time() )
  233. add_blocks_at_random(2)
  234.  
  235. while can_make_a_move() do
  236. show_board(board)
  237. here = get_user_move()
  238. if move_board(here) == 'x' then
  239. print("Unable to move in that directions (" .. here .. ")")
  240. else
  241. add_new_block(here)
  242. end
  243. end
  244.  
  245. show_board(board)
  246. score_game(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement