Advertisement
uriid1

Game: Guess The Number | lua

Sep 29th, 2022 (edited)
1,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | Gaming | 0 0
  1. --[[
  2.     ####--------------------------------####
  3.     #--# Author:   by uriid1            #--#
  4.     #--# License:  GNU GPLv3            #--#
  5.     #--# Telegram: @main_moderator      #--#
  6.     #--# E-mail:   [email protected]   #--#
  7.     ####--------------------------------####
  8. --]]
  9.  
  10. local function input(text)
  11.     io.stdout:write(text)
  12.     return io.stdin:read()
  13. end
  14.  
  15. local function lprint(text)
  16.     io.stdout:write(text)
  17. end
  18.  
  19. local current_num = 0
  20. local function gen_new_num()
  21.     math.randomseed(os.time())
  22.     return math.random(1, 3)
  23. end
  24.  
  25. local function clear()
  26.     io.stdout:write('\027\091\072\027\091\050\074\027\091\051\074')
  27. end
  28.  
  29. local user = {
  30.     name  = '';
  31.     money = 10000;
  32.     score = 0;
  33.     bet   = 0;
  34. }
  35.  
  36. local states = {
  37.     main_menu = 0;
  38.     info      = 1;
  39.     game      = 2;
  40.     exit      = 3;
  41. }
  42. local state = states.main_menu
  43.  
  44. local info_text = [[
  45. Guess the number game.
  46. Written for fun.
  47. Your task is to make a delivery and guess -
  48. hidden number from 1 to 3.
  49. Each win gives X2 to the bet.
  50. ]]
  51.  
  52. -- Game loop
  53. while state ~= states.exit do
  54.     repeat
  55.         -- State Menu
  56.         if state == states.main_menu then
  57.             clear()
  58.  
  59.             print('\t\tMain Menu')
  60.             print('\t\t1 New Game')
  61.             print('\t\t2 Info')
  62.             print('\t\t3 Exit')
  63.  
  64.             local res = tonumber(input('Enter state: '))
  65.             if res == 1 then
  66.                 state = states.game
  67.             elseif res == 2 then
  68.                 state = states.info
  69.             elseif res == 3 then
  70.                 state = states.exit
  71.             end
  72.  
  73.         -- State game
  74.         elseif state == states.game then
  75.             clear()
  76.  
  77.             -- Print info
  78.             lprint('Score: ' .. user.score)
  79.             lprint(('\t\t\tMoney: $%s'):format(user.money))
  80.             lprint('\n')
  81.  
  82.             -- Get user name
  83.             if user.name == '' then
  84.                 user.name = input("What is your name?: ")
  85.                 break
  86.             end
  87.  
  88.             -- Get number
  89.             if user.bet <= 0 then
  90.                 user.bet = tonumber(input("Your bet: "))
  91.  
  92.                 -- Check nil
  93.                 if not user.bet then
  94.                     user.bet = 0
  95.                     break
  96.                 end
  97.  
  98.                 --
  99.                 if user.bet > user.money then
  100.                     user.bet = 0
  101.                     lprint('Over money bet')
  102.                     lprint('\n')
  103.                     input('Any key to new bet: ')
  104.                 end
  105.  
  106.                 break
  107.             end
  108.  
  109.             -- Print bet
  110.             lprint('\t\tBet: ' .. user.bet)
  111.             lprint('\n')
  112.             lprint('\t\tWin: $' .. user.bet*2)
  113.             lprint('\n')
  114.             lprint('\t\tPlayer: ' .. user.name)
  115.             lprint('\n\n\n')
  116.  
  117.             local user_num = tonumber(input('Your number: '))
  118.             if user_num then
  119.  
  120.                 if user_num > 3 or user_num < 1 then
  121.                     lprint('\n')
  122.                     lprint('Enter a number from 1 to 3')
  123.                     lprint('\n')
  124.                     input('Any key to continue: ')
  125.                     break
  126.                 end
  127.  
  128.                 current_num = gen_new_num()
  129.  
  130.                 -- User is win
  131.                 if user_num == current_num then
  132.                     lprint("\t\tYou Win! +$" .. user.bet*2)
  133.                     lprint('\n')
  134.                     lprint('\t\tNumber is: ' .. current_num)
  135.                     lprint("\n\n")
  136.                     user.score = user.score + 1
  137.                     user.money = user.money + user.bet*2
  138.                     user.bet = 0
  139.                     input('Any key to new game: ')
  140.                     break
  141.                 end
  142.  
  143.                 -- User is loose
  144.                 lprint('\t\tYou Loose! -$'..user.bet)
  145.                 lprint('\n')
  146.                 lprint('\t\tNumber is: ' .. current_num)
  147.                 lprint('\n\n')
  148.                 input('Any key to new game: ')
  149.                 user.money = user.money - user.bet
  150.                 user.bet = 0
  151.                 break
  152.             end
  153.  
  154.         -- State info
  155.         elseif state == states.info then
  156.             clear()
  157.             lprint(info_text)
  158.             lprint('\n')
  159.             input('Any key to back: ')
  160.             state = states.main_menu
  161.         end
  162.     until true
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement