aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2014-04-16 22:59:56 +0200
committerpiernov <piernov@piernov.org>2014-04-16 22:59:56 +0200
commit7ae446b661c47b41ed38d7f88b8f54416575e47f (patch)
tree4e95b54230b5e468f16a22bbe3bc2174a1d622d8 /Gamestates
parent8ce1e6f75a68f5d92fe4bf27263a861c16e71f35 (diff)
downloadMastermind-7ae446b661c47b41ed38d7f88b8f54416575e47f.tar.gz
Mastermind-7ae446b661c47b41ed38d7f88b8f54416575e47f.tar.bz2
Mastermind-7ae446b661c47b41ed38d7f88b8f54416575e47f.tar.xz
Mastermind-7ae446b661c47b41ed38d7f88b8f54416575e47f.zip
Split file + Working Solo mode :
Add hints + Disallow multiple identical colors in same line + Correctly randomize answer
Diffstat (limited to 'Gamestates')
-rw-r--r--Gamestates/Init.lua4
-rw-r--r--Gamestates/Menu.lua21
-rw-r--r--Gamestates/Multiplayer.lua0
-rw-r--r--Gamestates/Solo.lua106
4 files changed, 131 insertions, 0 deletions
diff --git a/Gamestates/Init.lua b/Gamestates/Init.lua
new file mode 100644
index 0000000..5f42661
--- /dev/null
+++ b/Gamestates/Init.lua
@@ -0,0 +1,4 @@
+return {
+ Solo = require("Gamestates/Solo"),
+ Multiplayer = require("Gamestates/Multiplayer")
+}
diff --git a/Gamestates/Menu.lua b/Gamestates/Menu.lua
new file mode 100644
index 0000000..7e82857
--- /dev/null
+++ b/Gamestates/Menu.lua
@@ -0,0 +1,21 @@
+local Menu = {}
+local Gui = require "Quickie"
+local Utils = require "Utils"
+local GUI = { Menu = require("GUI/Menu")}
+local Gamestates = require "Gamestates/Init"
+
+function Menu:update(dt)
+ Gui.group{grow = "down", pos = {Utils.percentCoordinates(10, 10)}, function()
+ for _, name in ipairs(GUI.Menu.Buttons) do
+ if Gui.Button{text = name, size = {Utils.percentCoordinates(80, 10)}} then
+ Gamestate.switch(Gamestates[name])
+ end
+ end
+ end}
+end
+
+function Menu:draw()
+ Gui.core.draw()
+end
+
+return Menu
diff --git a/Gamestates/Multiplayer.lua b/Gamestates/Multiplayer.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Gamestates/Multiplayer.lua
diff --git a/Gamestates/Solo.lua b/Gamestates/Solo.lua
new file mode 100644
index 0000000..8d50ad6
--- /dev/null
+++ b/Gamestates/Solo.lua
@@ -0,0 +1,106 @@
+local GUI = { InGame = require("GUI/InGame")}
+
+local Solo = {}
+
+Solo.Keypressed = {{}}
+Solo.Answer = {}
+Solo.Hints = {}
+
+
+function Solo:checkKeys()
+ local found = true
+ Solo.Hints[#Solo.Hints+1] = {}
+
+ for id, num in ipairs(Solo.Keypressed[#Solo.Keypressed]) do
+ if num == Solo.Answer[id] then -- Right
+ table.insert(Solo.Hints[#Solo.Hints], { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill",
+ Position = { x = 0.795+((#Solo.Hints[#Solo.Hints]-1)%2)*0.0325, y = 0.12+math.floor(#Solo.Hints[#Solo.Hints]/2)*0.0325+(#Solo.Keypressed-1)*0.075},
+ Dimension = {width = 0.0225, height = 0.025}, Colors = {0, 0, 171, 255} })
+ else -- Misplaced or Wrong
+ found = false
+
+ for k,v in ipairs(Solo.Answer) do
+ if num == v then -- Misplaced
+ table.insert(Solo.Hints[#Solo.Hints], { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill",
+ Position = { x = 0.795+((#Solo.Hints[#Solo.Hints]-1)%2)*0.0325, y = 0.12+math.floor(#Solo.Hints[#Solo.Hints]/2)*0.0325+(#Solo.Keypressed-1)*0.075},
+ Dimension = {width = 0.0225, height = 0.025}, Colors = {171, 171, 171, 255} })
+ end
+ end
+ end
+ end
+
+ return found
+end
+
+function Solo:init()
+ GUI.InGame.loadInterface()
+ tmpanswer = { 1, 2, 3, 4, 5, 6 }
+
+ 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
+
+ while #Solo.Answer < 4 do
+ local n = love.math.random(#tmpanswer)
+ table.insert(Solo.Answer, tmpanswer[n])
+ table.remove(tmpanswer, n) -- table.insert(GUI.InGame.Polygons, { Type = "print", Text = n, Position = {x = 0.1+i*0.01, y = 0.5}, Colors = {255, 255, 255, 255}})
+ end
+
+ -- Debug
+ for k,v in ipairs(Solo.Answer) do
+ print(k,v)
+ table.insert(GUI.InGame.Polygons, { Type = "print", Text = v, Position = {x = 0.1+k*0.01, y = 0.5}, Colors = {255, 255, 255, 255}})
+ end
+end
+
+function Solo:draw()
+ for id, polygon in ipairs(GUI.InGame.Polygons) do
+ GUI.InGame.drawPolygon(polygon)
+ end
+
+ for num, line in ipairs(Solo.Hints) do
+ for k, poly in ipairs(line) do
+ GUI.InGame.drawPolygon(poly)
+ end
+ end
+
+ for line, keys in ipairs(Solo.Keypressed) do
+ for id, num in ipairs(keys) do
+ GUI.InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.50+(id-1)*0.075, y = 0.15+(line-1)*0.075-0.025}, Dimension = {width = 0.04, height = 0.04}, Colors = GUI.InGame.Colors[num] })
+ end
+ end
+end
+
+function Solo:mousepressed(x, y, b)
+ x = x/Screen.width
+ y = y/Screen.height
+ if 0.875 < y and y < 0.915 then
+ if x > 0.45 then
+ for i = 0,5 do
+ if 0.50+i*0.075 < x and x < 0.50+i*0.075+0.04 then
+ if #Solo.Keypressed > 7 or #Solo.Keypressed[#Solo.Keypressed] >= 4 then
+ return
+ else
+ for k,v in pairs(Solo.Keypressed[#Solo.Keypressed]) do
+ if v == i+1 then
+ return -- Disallow multiple identical colors in same line
+ end
+ end
+
+ table.insert(Solo.Keypressed[#Solo.Keypressed], i+1)
+ end
+ end
+ end
+ elseif 0.25 < x and x < 0.375 and #Solo.Keypressed <= 7 and #Solo.Keypressed[#Solo.Keypressed] == 4 then
+ if Solo.checkKeys() then
+ table.insert(GUI.InGame.Polygons, { Type = "print", Text = "FOUND !", Position = {x = 0.5, y = 0.5}, Colors = {255, 255, 255, 255}})
+ else
+ Solo.Keypressed[#Solo.Keypressed+1] = {}
+ end
+ elseif #Solo.Keypressed <= 7 and 0.075 < x and x < 0.200 and #Solo.Keypressed[#Solo.Keypressed] > 0 then
+ Solo.Keypressed[#Solo.Keypressed] = {}
+ end
+ end
+end
+
+return Solo