Major updates -- Better styling, fix game logic, new game, game feedbacks, etc

This commit is contained in:
Syed Daanish
2024-12-27 23:13:23 +03:00
parent 4d5cb45c75
commit 0ef7e28e39

98
main.go
View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math"
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
@@ -12,8 +13,9 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
var start int var start, pline int
var pline int
var offx, offy int
var screen tcell.Screen var screen tcell.Screen
@@ -38,7 +40,7 @@ func main() {
symbols = []rune("󰸞󰷚󰉀") symbols = []rune("󰸞󰷚󰉀")
numbers = []rune("🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹") numbers = []rune("🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹")
start = 0 start = 0
pline = 30 pline = 20
screen_tmp, err := tcell.NewScreen() screen_tmp, err := tcell.NewScreen()
if err != nil { if err != nil {
@@ -53,16 +55,24 @@ func main() {
screen.Clear() screen.Clear()
screen.Show() screen.Show()
var fx, fy int var fx, fy, sx, sy, mines int
board, _ := gen_board(10, 10, 15, fx, fy) fx = -1
print_it(fmt.Sprintf("Welcome to go-mines! %c %c", symbols[4], symbols[5])) fy = -1
mines = 10
sx = 9
sy = 9
width, height := screen.Size()
offx = width/2 - sx*5/2
offy = height/2 - sy*3/2
board, _ := gen_board(sx, sy, mines, fx, fy)
refresh(board) refresh(board)
screen.Show()
for { for {
ev := screen.PollEvent() ev := screen.PollEvent()
switch ev := ev.(type) { switch ev := ev.(type) {
case *tcell.EventKey: case *tcell.EventKey:
if ev.Rune() == 'q' { if ev.Rune() == 'l' || ev.Rune() == 'q' {
return return
} }
if ev.Key() == tcell.KeyCtrlC { if ev.Key() == tcell.KeyCtrlC {
@@ -71,12 +81,23 @@ func main() {
case *tcell.EventMouse: case *tcell.EventMouse:
x, y := ev.Position() x, y := ev.Position()
button := buttonify(ev.Buttons()) button := buttonify(ev.Buttons())
boardX := x / 5 if x >= offx+(len(board[0])*5/2)-3 && x <= offx+(len(board[0])*5/2)+3 && y >= offy-4 && y <= offy-3 && button == "L" {
boardY := y / 3 fx = -1
if fx == 0 && fy == 0 { fy = -1
gameOver = false
board, _ := gen_board(sx, sy, mines, fx, fy)
refresh(board)
screen.Show()
continue
}
x = x - offx
y = y - offy
boardX := int(math.Floor(float64(x) / 5))
boardY := int(math.Floor(float64(y) / 3))
if fx == -1 && fy == -1 && (button == "L" || button == "R") {
fx = boardX fx = boardX
fx = boardY fy = boardY
board, _ = gen_board(10, 10, 15, fx, fy) board, _ = gen_board(sx, sy, mines, fx, fy)
} }
if !gameOver { if !gameOver {
if (button == "L" || button == "R") && !mousey { if (button == "L" || button == "R") && !mousey {
@@ -94,20 +115,26 @@ func main() {
} }
screen.Show() screen.Show()
case *tcell.EventResize: case *tcell.EventResize:
// screen.Sync() screen.Sync()
// screen.Clear() screen.Clear()
// refresh(board) width, height := screen.Size()
// screen.Show() offx = width/2 - sx*5/2
offy = height/2 - sy*3/2
print_it(fmt.Sprintf("Welcome to go-mines! %c %c", symbols[4], symbols[5]))
refresh(board)
screen.Show()
} }
} }
} }
func refresh(board [][]cell) { func refresh(board [][]cell) {
solved := true solved := true
state := 0
for i := 0; i < len(board); i++ { for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ { for j := 0; j < len(board[0]); j++ {
if board[i][j].isMine && board[i][j].reveal { if board[i][j].isMine && board[i][j].reveal {
solved = false solved = false
state = -1
break break
} else if !board[i][j].isMine && !board[i][j].reveal { } else if !board[i][j].isMine && !board[i][j].reveal {
solved = false solved = false
@@ -116,10 +143,18 @@ func refresh(board [][]cell) {
} }
} }
if solved { if solved {
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ {
if board[i][j].isMine {
board[i][j].flag = true
}
}
}
print_it("You won!") print_it("You won!")
gameOver = true gameOver = true
state = 1
} }
print_board(board) print_board(board, state)
} }
func reveal(board [][]cell, x, y, width, height int) error { func reveal(board [][]cell, x, y, width, height int) error {
@@ -195,11 +230,9 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
if (x == fx && y == fy) || board[y][x].isMine { if (x == fx && y == fy) || board[y][x].isMine {
continue continue
} }
if !board[y][x].isMine {
board[y][x].isMine = true board[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 board[y][x].isMine {
@@ -239,9 +272,26 @@ func count_mines(board [][]cell, x, y, width, height int) int {
return count return count
} }
func print_board(board [][]cell) { func print_board(board [][]cell, solved int) {
style := tcell.StyleDefault.Foreground(tcell.ColorWhite) style := tcell.StyleDefault.Foreground(tcell.GetColor("#464e56"))
style2 := tcell.StyleDefault.Foreground(tcell.ColorWhite) style2 := tcell.StyleDefault.Background(tcell.GetColor("#464e56")).Foreground(tcell.GetColor("#1e262e"))
draw_box(offx-2, offy-1, len(board[0])*5+offx+1, len(board)*3+offy, style, "██████")
draw_box(offx-3, offy-2, len(board[0])*5+offx+2, len(board)*3+offy+1, style, "██████")
draw_box(offx-3, offy-3, len(board[0])*5+offx+2, len(board)*3+offy+1, style, "██████")
draw_box(offx-3, offy-4, len(board[0])*5+offx+2, len(board)*3+offy+1, style, "██████")
draw_box(offx-4, offy-5, len(board[0])*5+offx+3, len(board)*3+offy+1, style, "██████")
draw_box(offx-5, offy-6, len(board[0])*5+offx+4, len(board)*3+offy+2, style2.Background(tcell.GetColor("#00000000")).Foreground(tcell.GetColor("#788088")), "🬭▌🬞🬁🬏🬀▐🬂")
draw_box(offx-1, offy-1, len(board[0])*5+offx, len(board)*3+offy, style2, "🬭▌🬞🬁🬏🬀▐🬂")
if solved == -1 {
print_at(offx+(len(board[0])*5/2)-3, offy-3, " X _ X ", style2.Foreground(tcell.GetColor("#da9160")))
draw_box(offx+(len(board[0])*5/2)-3, offy-4, offx+(len(board[0])*5/2)+3, offy-2, style2.Foreground(tcell.GetColor("#da9160")), "")
} else if solved == 0 {
print_at(offx+(len(board[0])*5/2)-3, offy-3, " • ‿ • ", style2.Foreground(tcell.GetColor("#f5f646")))
draw_box(offx+(len(board[0])*5/2)-3, offy-4, offx+(len(board[0])*5/2)+3, offy-2, style2.Foreground(tcell.GetColor("#f5f646")), "")
} else {
print_at(offx+(len(board[0])*5/2)-3, offy-3, " • ‿ • ", style2.Foreground(tcell.GetColor("#66c266")))
draw_box(offx+(len(board[0])*5/2)-3, offy-4, offx+(len(board[0])*5/2)+3, offy-2, style2.Foreground(tcell.GetColor("#66c266")), "")
}
chars_i := "▔▕🭽🭼🭾🭿▏▁" chars_i := "▔▕🭽🭼🭾🭿▏▁"
symbol := "H" symbol := "H"
for i := 0; i < len(board); i++ { for i := 0; i < len(board); i++ {
@@ -279,8 +329,8 @@ func print_board(board [][]cell) {
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)
} }
print_at(j*5, i*3+1, fmt.Sprint(" ", symbol, " "), style2) print_at(j*5+offx, i*3+1+offy, fmt.Sprint(" ", symbol, " "), style2)
draw_box(j*5, i*3, j*5+4, i*3+2, style, chars_i) draw_box(j*5+offx, i*3+offy, j*5+4+offx, i*3+2+offy, style, chars_i)
} }
} }
} }