From 58731e8b6656a249fec5c7b3d40609b62a4b68a1 Mon Sep 17 00:00:00 2001 From: "MrsHerobrine (Naomi)" Date: Thu, 10 Feb 2022 21:51:18 +0100 Subject: [PATCH] v1.2.0, with dictionary option and better time formatting --- README.md | 3 +++ game/dictionary.v | 27 +++++++++++++++++++++++++++ game/game.v | 39 +++++++++++++++++++++------------------ main.v | 11 +++++++++++ 4 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 game/dictionary.v diff --git a/README.md b/README.md index fe6498c..92d5519 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ vordle [about|help] - shows the name and developer help - explains how to play the game + dict(ionary) + - shows the definition of a word after game + - requires internet connection and is not able to show every word ``` Not passing any argument or passing one that is unknown starts the game. diff --git a/game/dictionary.v b/game/dictionary.v new file mode 100644 index 0000000..14eb1e5 --- /dev/null +++ b/game/dictionary.v @@ -0,0 +1,27 @@ +module game + +import net.http +import x.json2 + +pub fn get_definition(word string) { + resp := http.get('https://api.dictionaryapi.dev/api/v2/entries/en/$word') or { http.Response{} } + if resp.text != '' { + json_resp := json2.raw_decode(resp.text) or { '' } + resp_map := json_resp.as_map() + // gotta love when they use arrays + i := resp_map['0'] or { map[string]json2.Any{} }.as_map() // array index + j := i['meanings'] or { map[string]json2.Any{} }.as_map() + k := j['0'] or { map[string]json2.Any{} }.as_map() // array index + l := k['definitions'] or { map[string]json2.Any{} }.as_map() + m := l['0'] or { map[string]json2.Any{} }.as_map() // array index + + d := m['definition'] or {'!'}.str() + e := m['example'] or {'!'}.str() + if d != '!' { + println('$d\nExample: $e') + } + } else { + println('unable to get the definition of the word.') + println('are you connected to the internet?') + } +} \ No newline at end of file diff --git a/game/game.v b/game/game.v index a04df64..b34f956 100644 --- a/game/game.v +++ b/game/game.v @@ -20,11 +20,20 @@ fn (l Letter) str() string { return util.letter_with_color(l.color, l.char) } -pub fn random_word() string { - return words[rand.int_in_range(0, words.len)] +fn (arr []Letter) to_string() string { + mut str := '' + for i in arr { + str += i.str() + } + return str } -pub fn game() { +pub struct Game { +pub mut: + dictionary bool +} + +pub fn (g Game) game() { println("welcome to vordle.") println("run `vordle help` if you don't know how to play") @@ -131,9 +140,11 @@ pub fn game() { if win { println('${count_nulls(game)}/6 | you took ${format_times(stopwatch)}!') println('you win! word was ${word}') + if g.dictionary{ get_definition(word) } } else { println('X/6') println('you lost! word was ${word}') + if g.dictionary { get_definition(word) } } } @@ -153,23 +164,15 @@ fn count_nulls(array []string) int { return idx } -fn (arr []Letter) to_string() string { - mut str := '' - for i in arr { - str += i.str() - } - return str +fn format_times(stopwatch time.StopWatch) string { + elapsed := stopwatch.elapsed() + mins := int(elapsed.minutes()) + secs := int(elapsed.seconds()) % 60 + return "you took $mins:$secs" } -fn format_times(stopwatch time.StopWatch) string { - time := int(stopwatch.elapsed().minutes()) - if time == 0 { - return "<1 minute" - } else if time == 1 { - return "~1 minute" - } else { - return "~$time minutes" - } +pub fn random_word() string { + return words[rand.intn(words.len)] } fn count_greens(arr []Letter) int { diff --git a/main.v b/main.v index ac244f3..cc6df5c 100644 --- a/main.v +++ b/main.v @@ -7,6 +7,9 @@ import game import os fn main() { + + mut game := game.Game{} + if os.args.len < 2 { game.game() } else { @@ -22,6 +25,14 @@ fn main() { println('if it\'s not the right letter, it\'ll be gray') println('the goal is to guess the word within 6 turns') } + 'dict' { + game.dictionary = true + game.game() + } + 'dictionary' { + game.dictionary = true + game.game() + } else { game.game() } } }