local Numbers={"0","1","2","3","4","5","6","7","8","9"}
local Guess={}
local count=1
function numberRandom()
math.random()
math.random()
return math.random(0,9)
end
function mixer( t ) -- mixed a table
if t == nil or type( t ) ~= "table" then
return
end
math.random()
math.random()
math.random()
local TempTable = {}
for i = 1, #t do
local r = math.random( 1, #t )
TempTable[i] = t[r]
table.remove( t, r )
end
return TempTable
end
function repetitive(t)
local temp={}
for x,y in t:gmatch("%d") do
table.insert(temp, x)
end
if temp[1]== temp[2]or temp[1]== temp[3]
or temp[1]== temp[4]
or temp[2]== temp[3]
or temp[2]== temp[4]
or temp[3]== temp[4] then
return false
end
return true
end
function stringToTable(s)
local temp={}
s:gsub(".",function(c) table.insert(temp,c) end)
return temp
end
function tableToString(t)
local temp=""
for i,j in pairs(t) do
temp=temp..t[i]
end
return temp
end
function compare(s)
local t= stringToTable(s)
local result={}
for i=1, #t do
for j=1, #Guess do
if (t[i]== Guess[j]) then
if (i==j) then
result[#result+1]= "+"
else
result[#result+1]= "-"
end
end
end
end
return tableToString(result)
end
Numbers= mixer(Numbers)
while (Numbers[1]==0) do
Numbers= mixer(Numbers)
end
for i=1, 4 do
Guess[i]=Numbers[i]
end
local ui = require "ui"
local win = ui.Window("Guessing a 4 digit number", 640, 640)
local label_1 = ui.Label(win, "Enter a 4-digit number that is different from each other. : ", 40, 30)
-- local label_2 = ui.Label(win, "Bilgisayarın sayısı= "..(Guess[1]..Guess[2]..Guess[3]..Guess[4]), 40, 330)
local entry = ui.Entry(win, "", 300, 23, 90, 30)
local list_1 = ui.List(win, {}, 400, 23, 65,500)
local list_2 = ui.List(win, {}, 465, 23, 70,500)
entry.fontsize = 20
list_1.fontsize = 14
list_2.fontsize = 14
list_1.items = {}
list_2.items = {}
list_1.enabled = false
list_2.enabled = false
-- Event fired when user press RETURN in the Entry field
function entry:onSelect()
if tonumber(entry.text)~= nil then
if tonumber(entry.text) < 1000 then
ui.msg("You entered a number with less than 4 digits !!")
elseif tonumber(entry.text) > 9999 then
ui.msg("You entered a number with more than 4 digits !!")
elseif not (repetitive(entry.text)) then
ui.msg("your number contains similar numbers !!")
else
table.insert(list_1.items,entry.text)
table.insert(list_2.items, compare(entry.text))
count= count+1
if (count > 20) then
ui.msg("You couldn't find the number in 20 guesses. I am sad. You should learn more Mathematics.")
win.visible=false
end
if compare(entry.text)== "++++" then
ui.msg("** "..entry.text.."** You hit the point with "..count.." guesses. Congratulations!!")
win.visible=false
end
end
end
entry.text=""
end
entry:onSelect()
win:show()
-- update user interface
repeat
ui.update()
until not win.visible