aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Game.lua
blob: 3bd870a02b61608184c90f23a31b48c70d178922 (plain)
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
--[[
	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