Skip to content

Commit 58731e8

Browse files
committed
v1.2.0, with dictionary option and better time formatting
1 parent 3d53af8 commit 58731e8

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ vordle [about|help]
1111
- shows the name and developer
1212
help
1313
- explains how to play the game
14+
dict(ionary)
15+
- shows the definition of a word after game
16+
- requires internet connection and is not able to show every word
1417
```
1518
Not passing any argument or passing one that is unknown starts the game.
1619

game/dictionary.v

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module game
2+
3+
import net.http
4+
import x.json2
5+
6+
pub fn get_definition(word string) {
7+
resp := http.get('https://api.dictionaryapi.dev/api/v2/entries/en/$word') or { http.Response{} }
8+
if resp.text != '' {
9+
json_resp := json2.raw_decode(resp.text) or { '' }
10+
resp_map := json_resp.as_map()
11+
// gotta love when they use arrays
12+
i := resp_map['0'] or { map[string]json2.Any{} }.as_map() // array index
13+
j := i['meanings'] or { map[string]json2.Any{} }.as_map()
14+
k := j['0'] or { map[string]json2.Any{} }.as_map() // array index
15+
l := k['definitions'] or { map[string]json2.Any{} }.as_map()
16+
m := l['0'] or { map[string]json2.Any{} }.as_map() // array index
17+
18+
d := m['definition'] or {'!'}.str()
19+
e := m['example'] or {'!'}.str()
20+
if d != '!' {
21+
println('$d\nExample: $e')
22+
}
23+
} else {
24+
println('unable to get the definition of the word.')
25+
println('are you connected to the internet?')
26+
}
27+
}

game/game.v

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ fn (l Letter) str() string {
2020
return util.letter_with_color(l.color, l.char)
2121
}
2222

23-
pub fn random_word() string {
24-
return words[rand.int_in_range(0, words.len)]
23+
fn (arr []Letter) to_string() string {
24+
mut str := ''
25+
for i in arr {
26+
str += i.str()
27+
}
28+
return str
2529
}
2630

27-
pub fn game() {
31+
pub struct Game {
32+
pub mut:
33+
dictionary bool
34+
}
35+
36+
pub fn (g Game) game() {
2837

2938
println("welcome to vordle.")
3039
println("run `vordle help` if you don't know how to play")
@@ -131,9 +140,11 @@ pub fn game() {
131140
if win {
132141
println('${count_nulls(game)}/6 | you took ${format_times(stopwatch)}!')
133142
println('you win! word was ${word}')
143+
if g.dictionary{ get_definition(word) }
134144
} else {
135145
println('X/6')
136146
println('you lost! word was ${word}')
147+
if g.dictionary { get_definition(word) }
137148
}
138149

139150
}
@@ -153,23 +164,15 @@ fn count_nulls(array []string) int {
153164
return idx
154165
}
155166

156-
fn (arr []Letter) to_string() string {
157-
mut str := ''
158-
for i in arr {
159-
str += i.str()
160-
}
161-
return str
167+
fn format_times(stopwatch time.StopWatch) string {
168+
elapsed := stopwatch.elapsed()
169+
mins := int(elapsed.minutes())
170+
secs := int(elapsed.seconds()) % 60
171+
return "you took $mins:$secs"
162172
}
163173

164-
fn format_times(stopwatch time.StopWatch) string {
165-
time := int(stopwatch.elapsed().minutes())
166-
if time == 0 {
167-
return "<1 minute"
168-
} else if time == 1 {
169-
return "~1 minute"
170-
} else {
171-
return "~$time minutes"
172-
}
174+
pub fn random_word() string {
175+
return words[rand.intn(words.len)]
173176
}
174177

175178
fn count_greens(arr []Letter) int {

main.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import game
77
import os
88

99
fn main() {
10+
11+
mut game := game.Game{}
12+
1013
if os.args.len < 2 {
1114
game.game()
1215
} else {
@@ -22,6 +25,14 @@ fn main() {
2225
println('if it\'s not the right letter, it\'ll be gray')
2326
println('the goal is to guess the word within 6 turns')
2427
}
28+
'dict' {
29+
game.dictionary = true
30+
game.game()
31+
}
32+
'dictionary' {
33+
game.dictionary = true
34+
game.game()
35+
}
2536
else { game.game() }
2637
}
2738
}

0 commit comments

Comments
 (0)