Skip to content

Commit b85c9e0

Browse files
committed
examples: add a small minesweeper game in examples/gg/minesweeper.v
1 parent 32ad697 commit b85c9e0

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

examples/gg/minesweeper.v

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
module main
2+
3+
import gg
4+
import gx
5+
import rand
6+
7+
const header_size = 30
8+
9+
enum Cell {
10+
empty
11+
n1
12+
n2
13+
n3
14+
n4
15+
n5
16+
n6
17+
n7
18+
n8
19+
mine
20+
}
21+
22+
@[heap]
23+
struct Game {
24+
mut:
25+
ctx &gg.Context = unsafe { nil }
26+
grid [][]Cell
27+
flags [][]bool
28+
revealed [][]bool
29+
size int = 10 // in cells
30+
csize int = 30 // in pixels
31+
game_over bool
32+
first_click bool = true
33+
mines int = 10
34+
mines_flagged int
35+
}
36+
37+
@[inline]
38+
fn (mut g Game) in_grid(cy int, cx int) bool {
39+
return cx >= 0 && cx < g.size && cy >= 0 && cy < g.size
40+
}
41+
42+
fn (mut g Game) generate_mines(first_y int, first_x int) {
43+
mut mines_placed := 0
44+
for mines_placed < g.mines {
45+
x, y := rand.intn(g.size) or { 0 }, rand.intn(g.size) or { 0 }
46+
// avoid placing mines at the position of the first click
47+
if (x == first_x && y == first_y) || g.grid[y][x] == .mine {
48+
continue
49+
}
50+
g.grid[y][x] = .mine
51+
mines_placed++
52+
}
53+
for y in 0 .. g.size {
54+
for x in 0 .. g.size {
55+
if g.grid[y][x] == .mine {
56+
continue
57+
}
58+
mut count := 0
59+
for dy in -1 .. 2 {
60+
for dx in -1 .. 2 {
61+
cy, cx := y + dy, x + dx
62+
if g.in_grid(cy, cx) {
63+
if g.grid[cy][cx] == .mine {
64+
count++
65+
}
66+
}
67+
}
68+
}
69+
g.grid[y][x] = unsafe { Cell(count) }
70+
}
71+
}
72+
}
73+
74+
fn (mut g Game) reveal(y int, x int) {
75+
if !g.in_grid(y, x) {
76+
return
77+
}
78+
if g.revealed[y][x] {
79+
return
80+
}
81+
g.revealed[y][x] = true
82+
if g.grid[y][x] == .mine {
83+
g.game_over = true
84+
return
85+
}
86+
if g.grid[y][x] == .empty {
87+
for dy in -1 .. 2 {
88+
for dx in -1 .. 2 {
89+
g.reveal(y + dy, x + dx)
90+
}
91+
}
92+
}
93+
}
94+
95+
fn (mut g Game) restart() {
96+
g.grid = [][]Cell{len: g.size, init: []Cell{len: g.size}}
97+
g.flags = [][]bool{len: g.size, init: []bool{len: g.size}}
98+
g.revealed = [][]bool{len: g.size, init: []bool{len: g.size}}
99+
g.game_over = false
100+
g.first_click = true
101+
g.mines_flagged = 0
102+
}
103+
104+
fn (mut g Game) on_event(e &gg.Event, _ voidptr) {
105+
if e.typ == .key_down {
106+
match e.key_code {
107+
.escape { g.ctx.quit() }
108+
.r { g.restart() }
109+
else {}
110+
}
111+
return
112+
}
113+
if g.game_over {
114+
return
115+
}
116+
if e.typ != .mouse_down {
117+
return
118+
}
119+
x := int(e.mouse_x / g.csize)
120+
y := int((e.mouse_y - header_size) / g.csize)
121+
if e.mouse_button == .left {
122+
if g.first_click {
123+
g.generate_mines(y, x)
124+
g.first_click = false
125+
}
126+
g.reveal(y, x)
127+
}
128+
if e.mouse_button == .right {
129+
if !g.revealed[y][x] {
130+
old := g.flags[y][x]
131+
g.flags[y][x] = !old
132+
g.mines_flagged += if old { -1 } else { 1 }
133+
}
134+
}
135+
if e.mouse_button == .middle {
136+
if g.revealed[y][x] {
137+
count := g.act_on_neighbors(y, x, fn (mut g Game, cy int, cx int) int {
138+
if g.in_grid(cy, cx) {
139+
return int(g.flags[cy][cx])
140+
}
141+
return 0
142+
})
143+
if int(g.grid[y][x]) == count {
144+
g.act_on_neighbors(y, x, fn (mut g Game, cy int, cx int) int {
145+
if g.in_grid(cy, cx) && !g.flags[cy][cx] {
146+
g.reveal(cy, cx)
147+
}
148+
return 0
149+
})
150+
}
151+
}
152+
}
153+
}
154+
155+
fn (mut g Game) act_on_neighbors(y int, x int, f fn (mut g Game, cy int, cx int) int) int {
156+
mut count := 0
157+
for dy in -1 .. 2 {
158+
for dx in -1 .. 2 {
159+
if dy == 0 && dx == 0 {
160+
continue
161+
}
162+
cy, cx := y + dy, x + dx
163+
count += f(mut g, cy, cx)
164+
}
165+
}
166+
return count
167+
}
168+
169+
fn (mut g Game) draw_cell(y int, x int) {
170+
o := header_size
171+
rect_x, rect_y := x * g.csize, y * g.csize
172+
if g.revealed[y][x] {
173+
if g.grid[y][x] == .mine {
174+
g.ctx.draw_rect_filled(rect_x, o + rect_y, g.csize, g.csize, gx.red)
175+
g.ctx.draw_text(rect_x + 10, o + rect_y + 5, '*', color: gx.black)
176+
} else if int(g.grid[y][x]) > 0 {
177+
g.ctx.draw_rect_filled(rect_x, o + rect_y, g.csize, g.csize, gx.light_gray)
178+
n := int(g.grid[y][x]).str()
179+
g.ctx.draw_text(rect_x + 10, o + rect_y + 5, n, color: gx.black)
180+
} else {
181+
c := gx.rgb(240, 240, 240)
182+
g.ctx.draw_rect_filled(rect_x, o + rect_y, g.csize, g.csize, c)
183+
}
184+
} else if g.flags[y][x] {
185+
g.ctx.draw_rect_filled(rect_x, o + rect_y, g.csize, g.csize, gx.gray)
186+
g.ctx.draw_text(rect_x + 10, o + rect_y + 5, 'F', color: gx.white)
187+
} else {
188+
g.ctx.draw_rect_filled(rect_x, o + rect_y, g.csize, g.csize, gx.gray)
189+
}
190+
g.ctx.draw_rect_empty(rect_x, o + rect_y, g.csize, g.csize, gx.black)
191+
}
192+
193+
fn (mut g Game) frame(_ voidptr) {
194+
g.ctx.begin()
195+
for y in 0 .. g.size {
196+
for x in 0 .. g.size {
197+
g.draw_cell(y, x)
198+
}
199+
}
200+
message := 'Flagged: ${g.mines_flagged:02}/${g.mines:02} (r)estart (ESC)ape'
201+
g.ctx.draw_text(5, 7, message, color: gx.green)
202+
g.ctx.end()
203+
}
204+
205+
fn main() {
206+
mut g := &Game{}
207+
g.restart()
208+
g.ctx = gg.new_context(
209+
bg_color: gx.black
210+
width: g.size * g.csize
211+
height: header_size + g.size * g.csize
212+
window_title: 'V Minesweeper'
213+
user_data: g
214+
frame_fn: g.frame
215+
event_fn: g.on_event
216+
)
217+
g.ctx.run()
218+
}

0 commit comments

Comments
 (0)