Skip to content

Commit

Permalink
v1.2.0, with dictionary option and better time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
boonsboos committed Feb 10, 2022
1 parent 3d53af8 commit 58731e8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 27 additions & 0 deletions game/dictionary.v
Original file line number Diff line number Diff line change
@@ -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?')
}
}
39 changes: 21 additions & 18 deletions game/game.v
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) }
}

}
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import game
import os

fn main() {

mut game := game.Game{}

if os.args.len < 2 {
game.game()
} else {
Expand All @@ -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() }
}
}
Expand Down

0 comments on commit 58731e8

Please sign in to comment.