Reworked Colorize() into TextColor(), TextBackground() and TextEffect(). Readjusted tables. Added support for multiple meaning kanjis.

This commit is contained in:
UndeadMaelys 2021-05-29 20:40:58 +02:00
parent 111b4641b4
commit 7a4df1d03d
3 changed files with 65 additions and 59 deletions

View File

@ -16,7 +16,7 @@ function GetTerminalSize()
io.write("\027[u") io.write("\027[u")
end end
COLORMODE = Enum { EFFECT = Enum {
"Normal", "Normal",
"Bold", "Bold",
"Dim", "Dim",
@ -48,21 +48,13 @@ COLOR = Enum {
"White" "White"
} }
function Colorize(Text,Color,Background,Colormode) function TextEffect(Text, Effect)
return "\027["..tostring(Effect-1).."m"..Text.."\027[0;m"
if Colormode then end
lColormode = "\027["..tostring(Colormode-1).."m" function TextBackground(Text, Color)
else return "\027[48;5;"..tostring(Color-1).."m"..Text.."\027[0;m"
lColormode = ""
end end
if Background then function TextColor(Text, Color)
lBackground = "\027[48;5;"..tostring(Background-1).."m" return "\027[38;5;"..tostring(Color-1).."m"..Text.."\027[0;m"
else
lBackground = ""
end
lColoredText = "\027[38;5;"..tostring(Color-1).."m"..Text
return lColormode..lBackground..lColoredText.."\027[0;m"
end end

View File

@ -74,19 +74,19 @@ KanjiData = {
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Gold, Money", Meaning = {"Gold", "Money"},
On = {"キン","コン"}, On = {"キン","コン"},
Kun = {"かね","かな-"} Kun = {"かね","かな-"}
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Sky, Empty", Meaning = {"Sky", "Empty"},
On = {"クウ"}, On = {"クウ"},
Kun = {"そら","から","あ(く)","むな(しい)","す(く)","うつ(ろ)","あだ"} Kun = {"そら","から","あ(く)","むな(しい)","す(く)","うつ(ろ)","あだ"}
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Moon, Month", Meaning = {"Moon", "Month"},
On = {"ゲツ","ガツ"}, On = {"ゲツ","ガツ"},
Kun = {"つき"} Kun = {"つき"}
}, },
@ -278,19 +278,19 @@ KanjiData = {
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Previous, Tip", Meaning = {"Previous", "Tip"},
On = {"セン"}, On = {"セン"},
Kun = {"さき"} Kun = {"さき"}
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Early, Fast", Meaning = {"Early", "Fast"},
On = {"ソウ","サッ"}, On = {"ソウ","サッ"},
Kun = {"はや(い)"} Kun = {"はや(い)"}
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Leg, Sufficient", Meaning = {"Leg", "Sufficient"},
On = {"ソク"}, On = {"ソク"},
Kun = {"あし","た(りる)"} Kun = {"あし","た(りる)"}
}, },
@ -356,7 +356,7 @@ KanjiData = {
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Sun, Day", Meaning = {"Sun", "Day"},
On = {"ニチ","ジツ"}, On = {"ニチ","ジツ"},
Kun = {"","-か"} Kun = {"","-か"}
}, },
@ -410,7 +410,7 @@ KanjiData = {
}, },
{ {
Symbol = "", Symbol = "",
Meaning = "Exit, Emerge", Meaning = {"Exit", "Emerge"},
On = {"シュツ"}, On = {"シュツ"},
Kun = {"で(る)","だ(す)"} Kun = {"で(る)","だ(す)"}
}, },

View File

@ -12,56 +12,70 @@ function GetUnrepeatedKanji(lAvoid)
return lKanji return lKanji
end end
function AskMeaning() function GetOneFrom(Table)
io.write(Colorize("Q: ",COLOR.HighBlue).."What does "..CurrentKanji.Symbol.." mean? \n") if type(Table) == "table" then
io.write("\n") return Table[math.random(#Table)]
elseif type(Table) == "string" then
for i=1, 4 do return Table
if i == Solution then
io.write(Colorize(" ("..i..") ",COLOR.HighYellow)..CurrentKanji.Meaning)
io.write("\n")
else else
lRandomKanji = GetUnrepeatedKanji(AvoidList) return "oopsie"
table.insert(AvoidList,lRandomKanji)
io.write(Colorize(" ("..i..") ",COLOR.HighYellow)..lRandomKanji.Meaning)
io.write("\n")
end
end end
end end
function AskKanji() function AskMeaning4Kanji()
io.write(Colorize("Q: ",COLOR.HighBlue).."Which is the kanji for \""..CurrentKanji.Meaning.."\"? \n") CurrentKanji = KanjiData[math.random(#KanjiData)]
io.write("\n")
for i=1, 4 do
if i == Solution then
io.write(Colorize(" ("..i..") ",COLOR.HighYellow)..CurrentKanji.Symbol)
io.write("\n")
else
lRandomKanji = GetUnrepeatedKanji(AvoidList)
table.insert(AvoidList,lRandomKanji)
io.write(Colorize(" ("..i..") ",COLOR.HighYellow)..lRandomKanji.Symbol)
io.write("\n")
end
end
end
function Ask(Type)
Solution = math.random(4)
AvoidList = {}
table.insert(AvoidList,CurrentKanji) table.insert(AvoidList,CurrentKanji)
io.write(TextColor("Q: ",COLOR.HighBlue).."What does "..CurrentKanji.Symbol.." mean? \n")
io.write("\n")
for i=1, 4 do
if i == Solution then
io.write(TextColor(" ("..i..") ",COLOR.HighYellow)..GetOneFrom(CurrentKanji.Meaning))
io.write("\n")
else
lRandomKanji = GetUnrepeatedKanji(AvoidList)
table.insert(AvoidList,lRandomKanji)
io.write(TextColor(" ("..i..") ",COLOR.HighYellow)..GetOneFrom(lRandomKanji.Meaning))
io.write("\n")
end
end
end
function AskKanji4Meaning()
CurrentKanji = KanjiData[math.random(#KanjiData)]
table.insert(AvoidList,CurrentKanji)
io.write(TextColor("Q: ",COLOR.HighBlue).."Which is the kanji for \""..GetOneFrom(CurrentKanji.Meaning).."\"? \n")
io.write("\n")
for i=1, 4 do
if i == Solution then
io.write(TextColor(" ("..i..") ",COLOR.HighYellow)..CurrentKanji.Symbol)
io.write("\n")
else
lRandomKanji = GetUnrepeatedKanji(AvoidList)
table.insert(AvoidList,lRandomKanji)
io.write(TextColor(" ("..i..") ",COLOR.HighYellow)..lRandomKanji.Symbol)
io.write("\n")
end
end
end
function Ask()
local Type = math.random(#QuestionType)
Solution = math.random(4)
AvoidList = {}
if Type == QuestionType.KANJI then if Type == QuestionType.KANJI then
AskKanji() AskKanji4Meaning()
elseif Type == QuestionType.MEANING then elseif Type == QuestionType.MEANING then
AskMeaning() AskMeaning4Kanji()
end end
io.write("\n") io.write("\n")
io.write(Colorize("A: ",COLOR.HighPurple)) io.write(TextColor("A: ",COLOR.HighPurple))
end end
function Check(answer) function Check(answer)
@ -71,13 +85,13 @@ function Check(answer)
else else
io.write("\n") io.write("\n")
if answer == tostring(Solution) then if answer == tostring(Solution) then
io.write(Colorize("Correct!",COLOR.HighGreen)) io.write(TextColor("Correct!",COLOR.HighGreen))
io.write(Colorize(" [Streak +1]",COLOR.HighCyan,nil,COLORMODE.BlinkFast)) io.write(TextEffect(TextColor(" [Streak +1]",COLOR.HighCyan),EFFECT.BlinkFast))
Streak = Streak + 1 Streak = Streak + 1
Correct = Correct + 1 Correct = Correct + 1
else else
io.write(Colorize("Wrong!",COLOR.HighRed).." Answer is ("..Solution..")") io.write(TextColor("Wrong!",COLOR.HighRed).." Answer is ("..Solution..")")
if Streak >= 5 then io.write(Colorize(" ["..Streak.." Streak Lost]",COLOR.Cyan,nil,COLORMODE.BlinkFast)) end if Streak >= 5 then io.write(TextEffect(TextColor(" ["..Streak.." Streak Lost]",COLOR.Cyan),EFFECT.BlinkFast)) end
Streak = 0 Streak = 0
Wrong = Wrong + 1 Wrong = Wrong + 1
end end
@ -90,21 +104,22 @@ end
QuestionType = Enum { QuestionType = Enum {
"KANJI", "KANJI",
"MEANING", "MEANING"
"ON",
"KUN"
} }
function ShowProgress() function ShowProgress()
io.write("#"..tostring(Total+1).."\t") io.write("\027[2K#"..tostring(Total+1).."\t")
io.write("Right: "..Colorize(tostring(Correct),COLOR.HighGreen).."\t") io.write("Right: "..TextColor(tostring(Correct),COLOR.HighGreen).."\t")
io.write("Wrong: "..Colorize(tostring(Wrong),COLOR.HighRed).."\t") io.write("Wrong: "..TextColor(tostring(Wrong),COLOR.HighRed).."\t")
if Streak >= 5 then if Streak >= 5 then
io.write(Colorize("CURRENT STREAK: ", COLOR.HighCyan,nil,COLORMODE.BlinkFast)..Colorize(Streak,COLOR.White,nil,COLORMODE.BlinkFast)) io.write(
TextEffect(TextColor("CURRENT STREAK: ", COLOR.HighCyan),EFFECT.BlinkFast)..
TextEffect(TextColor(Streak,COLOR.White),EFFECT.BlinkFast)
)
end end
io.write(" \n\n") io.write("\n")
end end
function UpdateProgress() function UpdateProgress()
@ -122,15 +137,14 @@ math.randomseed(os.time())
while 1 do while 1 do
os.execute("clear") os.execute("clear")
CurrentKanji = KanjiData[math.random(#KanjiData)]
ShowProgress() ShowProgress()
--Ask(math.random(#QuestionType)) --Ask()
Ask(math.random(2)) Ask()
--Ask(QuestionType.KANJI) --Ask(QuestionType.KANJI)
response = io.read() response = io.read()
Check(response) Check(response)
end end
os.execute("clear")