Add timer and buff up ui to look amazing!
Signed-off-by: Syed Daanish <syed@sixzix.com>
This commit is contained in:
190
main.go
190
main.go
@@ -14,17 +14,16 @@ 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 screen tcell.Screen
|
var screen tcell.Screen
|
||||||
|
var gameTime time.Time
|
||||||
|
var timr int
|
||||||
var symbols []rune
|
var symbols []rune
|
||||||
var numbers []rune
|
var numbers []rune
|
||||||
|
|
||||||
var mousey bool
|
var mousey bool
|
||||||
var mouseThing string
|
var mouseThing string
|
||||||
|
var asciiDigits map[rune][]string
|
||||||
var gameOver bool
|
var gameOver bool
|
||||||
|
|
||||||
type cell struct {
|
type cell struct {
|
||||||
@@ -39,9 +38,60 @@ type cell struct {
|
|||||||
func main() {
|
func main() {
|
||||||
symbols = []rune("")
|
symbols = []rune("")
|
||||||
numbers = []rune("🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹")
|
numbers = []rune("🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹")
|
||||||
|
asciiDigits = map[rune][]string{
|
||||||
|
'0': {
|
||||||
|
"┏━┓",
|
||||||
|
"┃ ┃",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'1': {
|
||||||
|
"┏┓ ",
|
||||||
|
" ┃ ",
|
||||||
|
"━┻━",
|
||||||
|
},
|
||||||
|
'2': {
|
||||||
|
"┏━┓",
|
||||||
|
"┏━┛",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'3': {
|
||||||
|
"┏━┓",
|
||||||
|
" ━┫",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'4': {
|
||||||
|
"╻ ",
|
||||||
|
"┗━╋",
|
||||||
|
" ╹",
|
||||||
|
},
|
||||||
|
'5': {
|
||||||
|
"┏━┓",
|
||||||
|
"┗━┓",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'6': {
|
||||||
|
"┏━┓",
|
||||||
|
"┣━┓",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'7': {
|
||||||
|
"━━┓",
|
||||||
|
" ┃",
|
||||||
|
" ╹",
|
||||||
|
},
|
||||||
|
'8': {
|
||||||
|
"┏━┓",
|
||||||
|
"┣━┫",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
'9': {
|
||||||
|
"┏━┓",
|
||||||
|
"┗━┫",
|
||||||
|
"┗━┛",
|
||||||
|
},
|
||||||
|
}
|
||||||
start = 0
|
start = 0
|
||||||
pline = 20
|
pline = 20
|
||||||
|
|
||||||
screen_tmp, err := tcell.NewScreen()
|
screen_tmp, err := tcell.NewScreen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create screen: %v", err)
|
log.Fatalf("Failed to create screen: %v", err)
|
||||||
@@ -55,7 +105,6 @@ func main() {
|
|||||||
screen.Clear()
|
screen.Clear()
|
||||||
screen.Show()
|
screen.Show()
|
||||||
|
|
||||||
var fx, fy, sx, sy, mines int
|
|
||||||
fx = -1
|
fx = -1
|
||||||
fy = -1
|
fy = -1
|
||||||
mines = 10
|
mines = 10
|
||||||
@@ -68,6 +117,13 @@ func main() {
|
|||||||
refresh(board)
|
refresh(board)
|
||||||
screen.Show()
|
screen.Show()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
updateTime()
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ev := screen.PollEvent()
|
ev := screen.PollEvent()
|
||||||
switch ev := ev.(type) {
|
switch ev := ev.(type) {
|
||||||
@@ -80,32 +136,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
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+(len(board[0])*5/2)-3 && x <= offx+(len(board[0])*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
|
||||||
fy = -1
|
fy = -1
|
||||||
gameOver = false
|
gameOver = false
|
||||||
|
gameTime = time.Time{}
|
||||||
board, _ := gen_board(sx, sy, mines, fx, fy)
|
board, _ := gen_board(sx, sy, mines, fx, fy)
|
||||||
refresh(board)
|
refresh(board)
|
||||||
screen.Show()
|
screen.Show()
|
||||||
continue
|
doit = false
|
||||||
}
|
}
|
||||||
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") {
|
if fx == -1 && fy == -1 && (button == "L" || button == "R") && doit {
|
||||||
fx = boardX
|
fx = boardX
|
||||||
fy = boardY
|
fy = boardY
|
||||||
board, _ = gen_board(sx, sy, mines, fx, fy)
|
board, _ = gen_board(sx, sy, mines, fx, fy)
|
||||||
|
gameTime = time.Now()
|
||||||
}
|
}
|
||||||
if !gameOver {
|
if !gameOver {
|
||||||
if (button == "L" || button == "R") && !mousey {
|
if (button == "L" || button == "R") && !mousey {
|
||||||
mouseThing = button
|
mouseThing = button
|
||||||
if mouseThing == "L" {
|
if mouseThing == "L" {
|
||||||
_ = reveal(board, boardX, boardY, len(board[0]), len(board))
|
_ = reveal(board, boardX, boardY, sx, sy)
|
||||||
} else if mouseThing == "R" {
|
} else if mouseThing == "R" {
|
||||||
_ = flag(board, boardX, boardY, len(board[0]), len(board))
|
_ = flag(board, boardX, boardY, sx, sy)
|
||||||
}
|
}
|
||||||
mousey = true
|
mousey = true
|
||||||
mouseThing = ""
|
mouseThing = ""
|
||||||
@@ -127,13 +186,50 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateTime() {
|
||||||
|
if !gameTime.IsZero() {
|
||||||
|
elapsedTime := time.Since(gameTime)
|
||||||
|
seconds := int(elapsedTime.Seconds())
|
||||||
|
if seconds > 999 {
|
||||||
|
seconds = 999
|
||||||
|
}
|
||||||
|
secondsStr := fmt.Sprintf("%03d", seconds)
|
||||||
|
timr = seconds
|
||||||
|
timeLines := []string{"", "", ""}
|
||||||
|
for _, digit := range secondsStr {
|
||||||
|
digitArt := asciiDigits[digit]
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
timeLines[i] += " " + digitArt[i] + " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, line := range timeLines {
|
||||||
|
print_at(offx, offy-4+i, line, tcell.StyleDefault.Foreground(tcell.GetColor("#cc0000")).Background(tcell.GetColor("#000000")))
|
||||||
|
}
|
||||||
|
screen.Show()
|
||||||
|
} else {
|
||||||
|
secondsStr := fmt.Sprintf("%03d", timr)
|
||||||
|
timeLines := []string{"", "", ""}
|
||||||
|
for _, digit := range secondsStr {
|
||||||
|
digitArt := asciiDigits[digit]
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
timeLines[i] += " " + digitArt[i] + " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, line := range timeLines {
|
||||||
|
print_at(offx, offy-4+i, line, tcell.StyleDefault.Foreground(tcell.GetColor("#cc0000")).Background(tcell.GetColor("#000000")))
|
||||||
|
}
|
||||||
|
screen.Show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func refresh(board [][]cell) {
|
func refresh(board [][]cell) {
|
||||||
solved := true
|
solved := true
|
||||||
state := 0
|
state := 0
|
||||||
for i := 0; i < len(board); i++ {
|
for i := 0; i < sy; i++ {
|
||||||
for j := 0; j < len(board[0]); j++ {
|
for j := 0; j < sx; j++ {
|
||||||
if board[i][j].isMine && board[i][j].reveal {
|
if board[i][j].isMine && board[i][j].reveal {
|
||||||
solved = false
|
solved = false
|
||||||
|
gameTime = time.Time{}
|
||||||
state = -1
|
state = -1
|
||||||
break
|
break
|
||||||
} else if !board[i][j].isMine && !board[i][j].reveal {
|
} else if !board[i][j].isMine && !board[i][j].reveal {
|
||||||
@@ -143,8 +239,8 @@ func refresh(board [][]cell) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if solved {
|
if solved {
|
||||||
for i := 0; i < len(board); i++ {
|
for i := 0; i < sy; i++ {
|
||||||
for j := 0; j < len(board[0]); j++ {
|
for j := 0; j < sx; j++ {
|
||||||
if board[i][j].isMine {
|
if board[i][j].isMine {
|
||||||
board[i][j].flag = true
|
board[i][j].flag = true
|
||||||
}
|
}
|
||||||
@@ -152,6 +248,7 @@ func refresh(board [][]cell) {
|
|||||||
}
|
}
|
||||||
print_it("You won!")
|
print_it("You won!")
|
||||||
gameOver = true
|
gameOver = true
|
||||||
|
gameTime = time.Time{}
|
||||||
state = 1
|
state = 1
|
||||||
}
|
}
|
||||||
print_board(board, state)
|
print_board(board, state)
|
||||||
@@ -238,7 +335,19 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
|
|||||||
if board[y][x].isMine {
|
if board[y][x].isMine {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
board[y][x].mines = count_mines(board, x, y, width, height)
|
directions := []struct{ dx, dy int }{
|
||||||
|
{-1, -1}, {0, -1}, {1, -1},
|
||||||
|
{-1, 0}, {1, 0},
|
||||||
|
{-1, 1}, {0, 1}, {1, 1},
|
||||||
|
}
|
||||||
|
count := 0
|
||||||
|
for _, dir := range directions {
|
||||||
|
nx, ny := x+dir.dx, y+dir.dy
|
||||||
|
if nx >= 0 && nx < width && ny >= 0 && ny < height && board[ny][nx].isMine {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
board[y][x].mines = count
|
||||||
colors := map[int]string{
|
colors := map[int]string{
|
||||||
0: "#41e8b0",
|
0: "#41e8b0",
|
||||||
1: "#7cc7ff",
|
1: "#7cc7ff",
|
||||||
@@ -256,46 +365,30 @@ func gen_board(width, height, mines, fx, fy int) ([][]cell, error) {
|
|||||||
return board, nil
|
return board, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func count_mines(board [][]cell, x, y, width, height int) int {
|
|
||||||
directions := []struct{ dx, dy int }{
|
|
||||||
{-1, -1}, {0, -1}, {1, -1},
|
|
||||||
{-1, 0}, {1, 0},
|
|
||||||
{-1, 1}, {0, 1}, {1, 1},
|
|
||||||
}
|
|
||||||
count := 0
|
|
||||||
for _, dir := range directions {
|
|
||||||
nx, ny := x+dir.dx, y+dir.dy
|
|
||||||
if nx >= 0 && nx < width && ny >= 0 && ny < height && board[ny][nx].isMine {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
func print_board(board [][]cell, solved int) {
|
func print_board(board [][]cell, solved int) {
|
||||||
style := tcell.StyleDefault.Foreground(tcell.GetColor("#464e56"))
|
style := tcell.StyleDefault.Foreground(tcell.GetColor("#464e56"))
|
||||||
style2 := tcell.StyleDefault.Background(tcell.GetColor("#464e56")).Foreground(tcell.GetColor("#1e262e"))
|
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-2, offy-1, sx*5+offx+1, sy*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-2, sx*5+offx+2, sy*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-3, sx*5+offx+2, sy*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-3, offy-4, sx*5+offx+2, sy*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-4, offy-5, sx*5+offx+3, sy*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-5, offy-6, sx*5+offx+4, sy*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, "🬭▌🬞🬁🬏🬀▐🬂")
|
draw_box(offx-1, offy-1, sx*5+offx, sy*3+offy, style2, "🬭▌🬞🬁🬏🬀▐🬂")
|
||||||
if solved == -1 {
|
if solved == -1 {
|
||||||
print_at(offx+(len(board[0])*5/2)-3, offy-3, " X _ X ", style2.Foreground(tcell.GetColor("#da9160")))
|
print_at(offx+(sx*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")), "")
|
draw_box(offx+(sx*5/2)-3, offy-4, offx+(sx*5/2)+3, offy-2, style2.Foreground(tcell.GetColor("#da9160")), "")
|
||||||
} else if solved == 0 {
|
} else if solved == 0 {
|
||||||
print_at(offx+(len(board[0])*5/2)-3, offy-3, " • ‿ • ", style2.Foreground(tcell.GetColor("#f5f646")))
|
print_at(offx+(sx*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")), "")
|
draw_box(offx+(sx*5/2)-3, offy-4, offx+(sx*5/2)+3, offy-2, style2.Foreground(tcell.GetColor("#f5f646")), "")
|
||||||
} else {
|
} else {
|
||||||
print_at(offx+(len(board[0])*5/2)-3, offy-3, " • ‿ • ", style2.Foreground(tcell.GetColor("#66c266")))
|
print_at(offx+(sx*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")), "")
|
draw_box(offx+(sx*5/2)-3, offy-4, offx+(sx*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 < sy; i++ {
|
||||||
for j := 0; j < len(board[0]); j++ {
|
for j := 0; j < sx; j++ {
|
||||||
chars_i = "▔▕🭽🭼🭾🭿▏▁"
|
chars_i = "▔▕🭽🭼🭾🭿▏▁"
|
||||||
if board[i][j].mines == 0 && board[i][j].reveal && !board[i][j].isMine {
|
if board[i][j].mines == 0 && board[i][j].reveal && !board[i][j].isMine {
|
||||||
symbol = ""
|
symbol = ""
|
||||||
@@ -333,6 +426,7 @@ func print_board(board [][]cell, solved int) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
func buttonify(button tcell.ButtonMask) string {
|
func buttonify(button tcell.ButtonMask) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user