-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
240 lines (198 loc) · 4.41 KB
/
Copy pathmain.lua
File metadata and controls
240 lines (198 loc) · 4.41 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
local x = 5
print(x)
print(" ")
print(x)
-- convert number to string
local str = "22"
print(type(str)) --prints string
print(tonumber(str)) -- converts to number
print(type(str)) -- still prints string
print(type(tonumber(str))) -- prints number
print(" ")
print(math.pi)
print(math.random(10))
print(math.random(10, 50))
-- strings
print(" ")
print("---- This is a STRING ----")
print(" ")
local str = "Hello world"
print(str)
print(#str) -- lenght of a string
local x = 22
local y = tostring(x)
-- if statements
print(" ")
print("---- This is a IF STATEMENT ----")
print(" ")
if true then
print("Statment was true")
end
local age = 20
if age > 17 and age < 60 then
print("You may enter")
end
-- loops
print(" ")
print("---- This is a LOOPS ----")
print(" ")
for i = 1, 10, 1 do
print(i)
end
local start_val, end_val, step_val = 1, 10, 1
for i = start_val, end_val, step_val do
print(i)
end
local arr = {1, 5, 98, 365, 24, 3, 2} -- for with array
for i = 1, #arr do
print(arr[i])
end
-- while loop
print(" ")
print("---- This is a WHILE LOOP ----")
print(" ")
print("\n")
local peeps = 10
while peeps > 0 do
peeps = peeps - 1
print("People left at party: " .. peeps)
end
local run = true
local runtime = 0
while run do
print("running")
if runtime == 10 then
run = false
end
runtime = runtime + 1
end
-- repeat
print(" ")
print("---- This is a REPEAT ----")
print(" ")
local x = 1
repeat
print("Hey there")
x = x + 1
until x > 10
-- user input
print("What is 10 + 5?")
local answer = io.read()
if tonumber(answer) == 15 then
print("Correct!")
else
print("Wrong!")
end
-- user input
print(" ")
print("---- This is a USER INPUT ----")
print(" ")
local num1, num2 = 10, 5
local true_ans = num1 + num2
io.write("input " .. num1 .. " + " .. num2 .. ": ")
local ans = io.read()
if tonumber(ans) == true_ans then
print("You are correct!")
else
print("Your answer, " .. ans .. ", is wrong!")
end
-- tables
print(" ")
print("---- This is a TABLES (ARRAYS) ----")
print(" ")
local x = 10
local y = 15
local z = 20
-- is same as
local arr = {10, 15, 20}
print(arr[1])
--
local arr = {
{1, 2, 3},
{6, 10, 100},
{0, 5, 7}
}
for i = 1, #arr do
for j = 1, #arr[i] do
print(arr[i][j])
end
end
-- functions
print(" ")
print("---- This is a FUNCTION ----")
print(" ")
function displayAge(age)
age = age or 36 -- define parameter if nothing will be passed
print("I am " .. age .. " years old")
print("I will be " .. age + 2 .. " years old in two years")
print("I was " .. age - 1 .. " years old last year")
end
displayAge(36)
print(" ")
print("---- This is a FUNCTION 2 ----")
print(" ")
local function sum(num1, num2)
return num1 + num2
end
local x = sum(2, 3)
print(x)
print(" ")
print("---- This is a FUNCTION 3 ----")
print(" ")
local add10 = function(number)
local outcome = 10 + number
return number, outcome
end
local stored, output = add10(20)
print(stored .. " had 10 added to it: " .. output)
-- Recursive function
print(" ")
print("---- This is a RECURSIVE FUNCTION ----")
print(" ")
local function counter(number, end_num)
local count = number + 1
if (count < end_num) then
return counter(count, end_num) -- here we return the function thats why it goes as a loop
end
return count
end
print(counter(10, 15))
print(" ")
print("---- This is a FUNCTION WITH INFINITE ARGUMENTS ----")
print(" ")
local function sum(...) -- you can pas as many arguments as you want
local sums = 0
for key, value in pairs({...}) do -- kei is index, and value is the valuae stored in that index
sums = sums + value
end
return sums
end
print(sum(10, 5, 20, 21, 16, 3))
print(" ")
print("---- This is a CO-ROUTINES ----")
print(" ")
-- example bellow for a visual could be refered to loading bar
local routine_1 = coroutine.create(
function ()
for i = 1, 10, 1 do
print("Routine 1: " .. i)
if i == 5 then
coroutine.yield()
end
end
end
)
local routine_func = function ()
for i = 11, 20, 1 do
print("Routine 2: " .. i)
end
end
local routine_2 = coroutine.create(routine_func)
coroutine.resume(routine_1)
coroutine.resume(routine_2)
if coroutine.status(routine_1) == "suspended" then
coroutine.resume(routine_1)
end
-- print(coroutine.status(routine_1))
-- coroutine.resume(routine_1)
-- print(coroutine.status(routine_1))