This commit is contained in:
Syed Daanish
2025-02-09 15:00:17 +03:00
parent e6a7b1342a
commit 0e211e15be
4 changed files with 70 additions and 66 deletions

2
.gitignore vendored
View File

@@ -14,3 +14,5 @@
# Dependency directories # Dependency directories
/Godeps/ /Godeps/
go.sum

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.23.4
require ( require (
github.com/gdamore/encoding v1.0.0 // indirect github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.7.4 // indirect github.com/gdamore/tcell/v2 v2.7.4 // direct
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.3 // indirect github.com/rivo/uniseg v0.4.3 // indirect

47
go.sum
View File

@@ -1,47 +0,0 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

85
main.go
View File

@@ -16,7 +16,7 @@ import (
var start, pline int var start, pline int
var offx, offy int var offx, offy int
var fx, fy, sx, sy, mines int var fx, fy, sx, sy, ax, ay, mines int
var screen tcell.Screen var screen tcell.Screen
var gameTime time.Time var gameTime time.Time
var timr int var timr int
@@ -27,6 +27,7 @@ var mouseThing string
var asciiDigits map[rune][]string var asciiDigits map[rune][]string
var gameOver bool var gameOver bool
var flags int var flags int
var board [][]cell
type cell struct { type cell struct {
err bool err bool
@@ -35,6 +36,7 @@ type cell struct {
reveal bool reveal bool
isMine bool isMine bool
color string color string
sel bool
} }
func main() { func main() {
@@ -116,6 +118,8 @@ func main() {
pline = 7 pline = 7
fx = -1 fx = -1
fy = -1 fy = -1
ax = 0
ay = 0
mines = *m_o mines = *m_o
sx = *sx_o sx = *sx_o
sy = *sy_o sy = *sy_o
@@ -132,7 +136,7 @@ func main() {
} }
offx = width/2 - (sx*5)/2 offx = width/2 - (sx*5)/2
offy = height/2 - (sy*3-3)/2 offy = height/2 - (sy*3-3)/2
board, _ := gen_board(sx, sy, mines, fx, fy) board, _ = gen_board(sx, sy, mines, fx, fy)
refresh(board) refresh(board)
screen.Show() screen.Show()
go func() { go func() {
@@ -145,15 +149,54 @@ func main() {
ev := screen.PollEvent() ev := screen.PollEvent()
switch ev := ev.(type) { switch ev := ev.(type) {
case *tcell.EventKey: case *tcell.EventKey:
if ev.Rune() == 'l' || ev.Rune() == 'q' { switch ev.Rune() {
case 'l', 'q':
return return
case 'f':
if !gameOver {
_ = flag_it(board, ax, ay, sx, sy)
}
case 'r', ' ':
if !gameOver {
_ = reveal(board, ax, ay, sx, sy)
}
} }
if ev.Key() == tcell.KeyCtrlC { switch ev.Key() {
case tcell.KeyCtrlC:
return return
case tcell.KeyLeft:
board[ay][ax].sel = false
ax--
board[ay][ax].sel = true
refresh(board)
case tcell.KeyRight:
board[ay][ax].sel = false
ax++
board[ay][ax].sel = true
refresh(board)
case tcell.KeyUp:
board[ay][ax].sel = false
ay--
board[ay][ax].sel = true
refresh(board)
case tcell.KeyDown:
board[ay][ax].sel = false
ay++
board[ay][ax].sel = true
refresh(board)
case tcell.KeyEnter:
if fx == -1 && fy == -1 {
fx = ax
fy = ay
board, _ = gen_board(sx, sy, mines, fx, fy)
gameTime = time.Now()
}
if !gameOver {
_ = reveal(board, ax, ay, sx, sy)
}
} }
case *tcell.EventMouse: case *tcell.EventMouse:
x, y := ev.Position() x, y := ev.Position()
doit := true
button := buttonify(ev.Buttons()) button := buttonify(ev.Buttons())
if x >= offx+(sx*5/2)-3 && x <= offx+(sx*5/2)+3 && y >= offy-4 && y <= offy-3 && button == "L" { if x >= offx+(sx*5/2)-3 && x <= offx+(sx*5/2)+3 && y >= offy-4 && y <= offy-3 && button == "L" {
fx = -1 fx = -1
@@ -164,14 +207,13 @@ func main() {
board, _ := gen_board(sx, sy, mines, fx, fy) board, _ := gen_board(sx, sy, mines, fx, fy)
refresh(board) refresh(board)
screen.Show() screen.Show()
doit = false
continue continue
} }
x = x - offx x = x - offx
y = y - offy y = y - offy
boardX := int(math.Floor(float64(x) / 5)) boardX := int(math.Floor(float64(x) / 5))
boardY := int(math.Floor(float64(y) / 3)) boardY := int(math.Floor(float64(y) / 3))
if fx == -1 && fy == -1 && (button == "L" || button == "R") && doit { if fx == -1 && fy == -1 && (button == "L" || button == "R") {
fx = boardX fx = boardX
fy = boardY fy = boardY
board, _ = gen_board(sx, sy, mines, fx, fy) board, _ = gen_board(sx, sy, mines, fx, fy)
@@ -346,12 +388,12 @@ func flag_it(board [][]cell, x, y, width, height int) error {
} }
func gen_board(width, height, mines, fx, fy int) ([][]cell, error) { func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
board := make([][]cell, height) board2 := make([][]cell, height)
for i := range board { for i := range board2 {
board[i] = make([]cell, width) board2[i] = make([]cell, width)
} }
if mines >= width*height { if mines >= width*height {
return board, errors.New("too many mines") return board2, errors.New("too many mines")
} }
source := rand.NewSource(time.Now().UnixNano()) source := rand.NewSource(time.Now().UnixNano())
rand := rand.New(source) rand := rand.New(source)
@@ -359,17 +401,20 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
for mineCount < mines { for mineCount < mines {
x := rand.Intn(width) x := rand.Intn(width)
y := rand.Intn(height) y := rand.Intn(height)
if (x == fx && y == fy) || board[y][x].isMine { if (x == fx && y == fy) || board2[y][x].isMine {
continue continue
} }
board[y][x].isMine = true board2[y][x].isMine = true
mineCount++ mineCount++
} }
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
if board[y][x].isMine { if board2[y][x].isMine {
continue continue
} }
if ax == x && ay == y {
board2[y][x].sel = true
}
directions := []struct{ dx, dy int }{ directions := []struct{ dx, dy int }{
{-1, -1}, {0, -1}, {1, -1}, {-1, -1}, {0, -1}, {1, -1},
{-1, 0}, {1, 0}, {-1, 0}, {1, 0},
@@ -378,11 +423,11 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
count := 0 count := 0
for _, dir := range directions { for _, dir := range directions {
nx, ny := x+dir.dx, y+dir.dy nx, ny := x+dir.dx, y+dir.dy
if nx >= 0 && nx < width && ny >= 0 && ny < height && board[ny][nx].isMine { if nx >= 0 && nx < width && ny >= 0 && ny < height && board2[ny][nx].isMine {
count++ count++
} }
} }
board[y][x].mines = count board2[y][x].mines = count
colors := map[int]string{ colors := map[int]string{
0: "#41e8b0", 0: "#41e8b0",
1: "#7cc7ff", 1: "#7cc7ff",
@@ -394,10 +439,10 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
7: "#999999", 7: "#999999",
8: "#d0d8e0", 8: "#d0d8e0",
} }
board[y][x].color = colors[board[y][x].mines] board2[y][x].color = colors[board2[y][x].mines]
} }
} }
return board, nil return board2, nil
} }
func print_board(board [][]cell, solved int) { func print_board(board [][]cell, solved int) {
@@ -461,6 +506,10 @@ func print_board(board [][]cell, solved int) {
style = tcell.StyleDefault.Background(tcell.GetColor("#384048")).Foreground(tcell.GetColor("#1f272f")).Bold(true) style = tcell.StyleDefault.Background(tcell.GetColor("#384048")).Foreground(tcell.GetColor("#1f272f")).Bold(true)
style2 = tcell.StyleDefault.Background(tcell.GetColor("#384048")).Foreground(tcell.GetColor(board[i][j].color)).Bold(true) style2 = tcell.StyleDefault.Background(tcell.GetColor("#384048")).Foreground(tcell.GetColor(board[i][j].color)).Bold(true)
} }
// Chaange color of border if selected
if board[i][j].sel {
style = style.Foreground(tcell.GetColor("#ff7d7d"))
}
print_at(j*5+offx, i*3+1+offy, fmt.Sprint(" ", symbol, " "), style2) print_at(j*5+offx, i*3+1+offy, fmt.Sprint(" ", symbol, " "), style2)
draw_box(j*5+offx, i*3+offy, j*5+4+offx, i*3+2+offy, style, chars_i) draw_box(j*5+offx, i*3+offy, j*5+4+offx, i*3+2+offy, style, chars_i)
} }