Advertisement
duck702702

Same Game for CraftOS 1.0.0

Oct 13th, 2016
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 KB | None | 0 0
  1. --Same Game for CraftOS 1.0.0 (ShinyCube) (Advanced Computer)
  2. local monitor, screen
  3. local score, A, B, C, D, E
  4. local board = {}
  5. local selected = {}
  6. local backup_board = {}
  7. local backup_score
  8. local backup_exists = false
  9. local cnt_selected
  10. local selected_color
  11. local is_gameover
  12. local best_scores = {}
  13. local best_score_names = {}
  14. local best_score_view = false
  15. function init()
  16.     loadScore()
  17.     detectScreen()
  18.     term.setBackgroundColor(colors.black)
  19.     term.setTextColor(colors.white)
  20.     term.clear()
  21.     for i = 1, 10 do
  22.         board[i] = {}
  23.         selected[i] = {}
  24.         backup_board[i] = {}
  25.     end
  26.     newGame()
  27.     eventLoop()
  28. end
  29. function eventLoop()
  30.     while true do
  31.         local event, button, x, y = os.pullEvent()
  32.         if event == "mouse_click" or event == "monitor_touch" then
  33.             if event == "monitor_touch" then
  34.                 local l,t = screen.getPosition();
  35.                 x = x - l + 1
  36.                 y = y - t + 1
  37.             end
  38.             if best_score_view then
  39.                 if y == 1 and 44 <= x and x <= 51 then
  40.                     best_score_view = false
  41.                     redraw()
  42.                 end
  43.             else
  44.                 if x >= 7 and x <= 46 and y >=8 and y <= 17 then
  45.                     local j = math.floor((x-7)/2) + 1
  46.                     local i = (y-8) + 1
  47.                     clicked(i,j)
  48.                 end
  49.                 if y == 1 and 1 <= x and x <= 7 then
  50.                     newGame()
  51.                 end
  52.                 if y == 1 and 9 <= x and x <= 16 then
  53.                     undo()
  54.                     redraw()
  55.                 end
  56.                 if y == 1 and 18 <= x and x <= 31 then
  57.                     showBestScore()
  58.                 end
  59.                 if y == 1 and 33 <= x and x <= 42 then
  60.                     detectScreen()
  61.                     redraw()
  62.                 end
  63.                 if y == 1 and 44 <= x and x <= 51 then
  64.                     term.clear()
  65.                     term.setCursorPos(1,1)
  66.                     break
  67.                 end
  68.             end
  69.         end
  70.     end
  71. end
  72. function newGame()
  73.     score = 0
  74.     A = 0
  75.     B = 0
  76.     C = 0
  77.     D = 0
  78.     E = 0
  79.     cnt_selected = 0
  80.     is_gameover = false
  81.     backup_exists = false
  82.     for i = 1, 10 do
  83.         for j = 1, 20 do
  84.             board[i][j] = math.random(5)
  85.             if(board[i][j] == 1) then A = A + 1 end
  86.             if(board[i][j] == 2) then B = B + 1 end
  87.             if(board[i][j] == 3) then C = C + 1 end
  88.             if(board[i][j] == 4) then D = D + 1 end
  89.             if(board[i][j] == 5) then E = E + 1 end
  90.             selected[i][j] = false
  91.         end
  92.     end
  93.     redraw()
  94. end
  95. function redraw()
  96.     redraw_term(term)
  97.     if screen then redraw_term(screen) end
  98. end
  99. function redraw_term(term)
  100.     if best_score_view then
  101.         term.setCursorPos(1,1) term.write("                                           [ BACK ]")
  102.     else
  103.         term.setCursorPos(1,1) term.write("[ NEW ] [ UNDO ] [ HIGH SCORE ] [ SCREEN ] [ EXIT ]")
  104.     end
  105.     term.setCursorPos(16,3) term.write("Same Game for Craft OS")
  106.     term.setCursorPos(15,5) term.write("Implemented by ShinyCube")
  107.     term.setCursorPos(3,19) term.write("Score:            A:    B:    C:    D:    E:   ")
  108.     if best_score_view then
  109.         for i = 1, 10 do
  110.             term.setTextColor(colors.white)
  111.             term.setBackgroundColor(colors.black)
  112.             term.setCursorPos(7,8+(i-1))
  113.             term.write(string.format("%2d. ...............................%5d",i,best_scores[i]))
  114.             term.setCursorPos(11,8+(i-1))
  115.             term.write(best_score_names[i])
  116.         end
  117.     else
  118.         for i = 1, 10 do
  119.             for j = 1, 20 do
  120.                 term.setCursorPos(7+(j-1)*2,8+(i-1))
  121.                 if board[i][j] == 0 then
  122.                     term.blit(". ","00","ff")
  123.                 elseif board[i][j] == 1 then
  124.                     if selected[i][j] then
  125.                         term.blit("A ","aa","00")
  126.                     else
  127.                         term.blit("A ","00","aa")
  128.                     end
  129.                 elseif board[i][j] == 2 then
  130.                     if selected[i][j] then
  131.                         term.blit("B ","bb","00")
  132.                     else
  133.                         term.blit("B ","00","bb")
  134.                     end
  135.                 elseif board[i][j] == 3 then
  136.                     if selected[i][j] then
  137.                         term.blit("C ","cc","00")
  138.                     else
  139.                         term.blit("C ","00","cc")
  140.                     end
  141.                 elseif board[i][j] == 4 then
  142.                     if selected[i][j] then
  143.                         term.blit("D ","dd","00")
  144.                     else
  145.                         term.blit("D ","00","dd")
  146.                     end
  147.                 elseif board[i][j] == 5 then
  148.                     if selected[i][j] then
  149.                         term.blit("E ","ee","00")
  150.                     else
  151.                         term.blit("E ","00","ee")
  152.                     end
  153.                 end
  154.             end
  155.         end
  156.     end
  157.     term.setTextColor(colors.white)
  158.     term.setBackgroundColor(colors.black)
  159.     term.setCursorPos(22,7)
  160.     if is_gameover then
  161.         term.write("GAME OVER")
  162.     else
  163.         term.write("         ")
  164.     end
  165.     term.setCursorPos(9,19)
  166.     term.write("           ")
  167.     term.setCursorPos(9,19)
  168.     term.write(score)
  169.     if cnt_selected > 0 then
  170.         term.write("+" .. cnt_selected*cnt_selected-3*cnt_selected+4)
  171.     end
  172.     term.setCursorPos(23,19)
  173.     term.write(A)
  174.     term.setCursorPos(29,19)
  175.     term.write(B)
  176.     term.setCursorPos(35,19)
  177.     term.write(C)
  178.     term.setCursorPos(41,19)
  179.     term.write(D)
  180.     term.setCursorPos(47,19)
  181.     term.write(E)
  182. end
  183. function deselectAll()
  184.     for i = 1, 10 do
  185.         for j = 1, 20 do
  186.             selected[i][j] = false
  187.         end
  188.     end
  189.     cnt_selected = 0
  190. end
  191. function rec_selection(i,j)
  192.     if not selected[i][j] then
  193.         selected[i][j] = true
  194.         cnt_selected = cnt_selected + 1
  195.         if i-1 >= 1 and board[i][j] == board[i-1][j] then rec_selection(i-1,j) end
  196.         if i+1 <= 10 and board[i][j] == board[i+1][j] then rec_selection(i+1,j) end
  197.         if j-1 >= 1 and board[i][j] == board[i][j-1] then rec_selection(i,j-1) end
  198.         if j+1 <= 20 and board[i][j] == board[i][j+1] then rec_selection(i,j+1) end
  199.     end
  200. end
  201. function backup()
  202.     for i = 1, 10 do
  203.         for j = 1, 20 do
  204.             backup_board[i][j] = board[i][j]
  205.             backup_score = score
  206.         end
  207.     end
  208.     backup_exists = true
  209. end
  210. function removeSelected()
  211.     local di, dj
  212.     dj = 1
  213.     for sj = 1, 20 do
  214.         di = 10
  215.         for si = 10, 1, -1 do
  216.             if not selected[si][sj] then
  217.                 board[di][dj] = board[si][sj]
  218.                 di = di - 1
  219.             end
  220.         end
  221.         for di = di, 1, -1 do
  222.             board[di][dj] = 0
  223.         end
  224.         if board[10][dj] ~= 0 then dj = dj + 1 end
  225.     end
  226.     for dj = dj, 20 do
  227.         for di = 1, 10 do
  228.             board[di][dj] = 0
  229.         end
  230.     end
  231. end
  232. function checkGameOver()
  233.     for i = 1, 10 do
  234.         for j = 1, 20 do
  235.             if i-1>=1 and board[i][j] > 0 and board[i][j] == board[i-1][j] then return false end
  236.             if i+1<=10 and board[i][j] > 0 and board[i][j] == board[i+1][j] then return false end
  237.             if j-1>=1 and board[i][j] > 0 and board[i][j] == board[i][j-1] then return false end
  238.             if j+1<=20 and board[i][j] > 0 and board[i][j] == board[i][j+1] then return false end
  239.         end
  240.     end
  241.     return true
  242. end
  243. function loadScore()
  244.     local file = fs.open("same.dat","r")
  245.     if file then
  246.         for i = 1, 10 do
  247.             best_score_names[i] = file.readLine() or "NONAME"
  248.             best_scores[i] = tonumber(file.readLine()) or 0
  249.         end
  250.         file.close()
  251.     else
  252.         for i = 1, 10 do
  253.             best_score_names[i] = "NONAME"
  254.             best_scores[i] = 0
  255.         end
  256.     end
  257. end
  258. function saveScore()
  259.     local file = fs.open("same.dat","w")
  260.     if file then
  261.         for i = 1, 10 do
  262.             file.writeLine(best_score_names[i])
  263.             file.writeLine(best_scores[i])
  264.         end
  265.         file.flush()
  266.     end
  267. end
  268. function updateScore()
  269.     local rank = 1
  270.     for i = 10, 1, -1 do
  271.         if best_scores[i] < score then
  272.             best_score_names[i+1] = best_score_names[i]
  273.             best_scores[i+1] = best_scores[i]
  274.         else
  275.             rank = i + 1
  276.             break
  277.         end
  278.     end
  279.     if rank <= 10 then
  280.         best_score_names[rank] = getName(rank, score)
  281.         best_scores[rank] = score
  282.         saveScore()
  283.         best_score_view = true
  284.         redraw()
  285.     end
  286. end
  287. function getName(rank, score)
  288.     term.setTextColor(colors.white)
  289.     term.setBackgroundColor(colors.black)
  290.     term.clear()
  291.     term.setCursorPos(1,1)
  292.     print("Congratulation!")
  293.     print("You got a high score!")
  294.     print("Your score: " .. score)
  295.     print("Your rank: " .. rank)
  296.     print("Type your name. >")
  297.     local name = read()
  298.     name = string.sub(name, 1, 30)
  299.     term.setTextColor(colors.white)
  300.     term.setBackgroundColor(colors.black)
  301.     term.clear()
  302.     return name
  303. end
  304. function undo()
  305.     if backup_exists then
  306.         deselectAll()
  307.         backup_exists = false
  308.         score = backup_score
  309.         for i = 1, 10 do
  310.             for j = 1, 20 do
  311.                 board[i][j] = backup_board[i][j]
  312.             end
  313.         end
  314.     end
  315. end
  316. function showBestScore()
  317.     best_score_view = true
  318.     redraw()
  319. end
  320. function detectScreen()
  321.     monitor = peripheral.find("monitor")
  322.     if monitor then
  323.         local scale, w, h, left, top
  324.         for scale = 2, 0.5, -0.5 do
  325.             monitor.setTextScale(scale)
  326.             w, h = monitor.getSize()
  327.             if w >= 51 and h >= 19 then break end
  328.         end
  329.         left = math.floor((w-51)/2)+1
  330.         top = math.floor((h-19)/2)+1
  331.         screen = window.create(monitor, left, top, 51, 19)
  332.     end
  333. end
  334. function clicked(ci,cj)
  335.     if selected[ci][cj] then
  336.         backup()
  337.         score = score + cnt_selected*cnt_selected-3*cnt_selected+4
  338.         if selected_color == 1 then A = A - cnt_selected
  339.         elseif selected_color == 2 then B = B - cnt_selected
  340.         elseif selected_color == 3 then C = C - cnt_selected
  341.         elseif selected_color == 4 then D = D - cnt_selected
  342.         elseif selected_color == 5 then E = E - cnt_selected
  343.         end
  344.         removeSelected()
  345.         deselectAll()
  346.         if checkGameOver() then
  347.             is_gameover = true
  348.             backup_exists = false
  349.             updateScore()
  350.             redraw()
  351.         else
  352.             redraw()
  353.         end
  354.     else
  355.         if cnt_selected > 0 then
  356.             deselectAll()
  357.             redraw()
  358.         else
  359.             if board[ci][cj] > 0 then
  360.                 selected_color = board[ci][cj]
  361.                 rec_selection(ci,cj)
  362.                 if cnt_selected == 1 then
  363.                     deselectAll()
  364.                 end
  365.                 redraw()
  366.             end
  367.         end
  368.     end
  369. end
  370. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement