aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Game.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Gamestates/Game.lua')
-rw-r--r--Gamestates/Game.lua65
1 files changed, 65 insertions, 0 deletions
diff --git a/Gamestates/Game.lua b/Gamestates/Game.lua
new file mode 100644
index 0000000..3bd870a
--- /dev/null
+++ b/Gamestates/Game.lua
@@ -0,0 +1,65 @@
+--[[
+ Gamestates/Game.lua
+ Shared game functions between Solo and Multiplayer mode
+ by piernov
+
+ Comment: this should even get more functions shared like mousepressed.
+]]--
+
+local Game = {}
+
+function Game.checkKeys(received, answer) -- Check the submitter answer against the real one
+ local hints = {}
+
+ for id, num in ipairs(received) do
+ if tonumber(num) == answer[id] then -- Right
+ table.insert(hints, 1)
+ else -- Misplaced or Wrong
+ for k,v in ipairs(answer) do
+ if tonumber(num) == v then -- Misplaced
+ table.insert(hints, 0)
+ end
+ end
+ end
+ end
+
+ return hints
+end
+
+function Game.addHints(hints, lineNumber) -- Generate hints rectangles from the checkKeys()-returned table
+ local polygons = {}
+ for i=1,#hints do
+ if tonumber(hints:sub(i, i)) == 1 then -- Show "misplaced" hint
+ table.insert(polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill",
+ Position = { x = 0.795+((#polygons-1)%2)*0.0325, y = 0.12+math.floor(#polygons/2)*0.0325+(lineNumber-1)*0.075},
+ Dimension = {width = 0.0225, height = 0.025}, Colors = {255, 0, 171, 255} })
+ else -- Show "well placed" hint
+ table.insert(polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill",
+ Position = { x = 0.795+((#polygons-1)%2)*0.0325, y = 0.12+math.floor(#polygons/2)*0.0325+(lineNumber-1)*0.075},
+ Dimension = {width = 0.0225, height = 0.025}, Colors = {171, 171, 171, 255} })
+ end
+ end
+ return polygons
+end
+
+
+function Game.genAnswer() -- Generate a random answer
+ local allowedNumbers = { 1, 2, 3, 4, 5, 6 } -- Select between those numbers, only 6 possibility
+ local tempAnswer = {}
+
+ while #tempAnswer < 4 do -- Pick 4 numbers
+ local n = love.math.random(#allowedNumbers) -- Randomly in allowedNumbers
+ table.insert(tempAnswer, allowedNumbers[n]) -- Add the number to the answer
+ table.remove(allowedNumbers, n) -- And delete it from allowedNumbers to prevent picking the same number a second time
+ end
+
+ return tempAnswer
+end
+
+function Game.init()
+ if not love.getVersion then -- Check if LÖVE is older than 0.9.1, getVersion() introduced in version 0.9.1
+ love.math.random() -- Throw first value since it's not random in LÖVE 0.9.0
+ end
+end
+
+return Game