From e6a7b1342a4265269f0e8d0ad26ed8e5ecc0fce0 Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Sat, 28 Dec 2024 21:10:47 +0300 Subject: [PATCH] Allow sizing board custom and add logo on top. Signed-off-by: Syed Daanish --- main.go | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index a3efb06..280068c 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "errors" + "flag" "fmt" "log" "math" @@ -96,6 +97,10 @@ func main() { " ", }, } + sx_o := flag.Int("w", 9, "Width of board") + sy_o := flag.Int("h", 9, "Height of board") + m_o := flag.Int("m", 10, "No. of mines on board") + flag.Parse() screen_tmp, err := tcell.NewScreen() if err != nil { log.Fatalf("Failed to create screen: %v", err) @@ -108,14 +113,23 @@ func main() { screen.EnableMouse() screen.Clear() start = 0 - pline = 20 + pline = 7 fx = -1 fy = -1 - mines = 20 - sx = 15 - sy = 12 + mines = *m_o + sx = *sx_o + sy = *sy_o flags = mines width, height := screen.Size() + if width <= sx*5+5 || height <= sy*3+5 { + screen.Suspend() + fmt.Println("Sorry screen size not enough for playing this size of board!") + return + } else if sx <= 7 || sy <= 2 { + screen.Suspend() + fmt.Println("Sorry size provided is too small - minimum width: 6 & height: 2!") + return + } offx = width/2 - (sx*5)/2 offy = height/2 - (sy*3-3)/2 board, _ := gen_board(sx, sy, mines, fx, fy) @@ -151,6 +165,7 @@ func main() { refresh(board) screen.Show() doit = false + continue } x = x - offx y = y - offy @@ -165,10 +180,10 @@ func main() { if !gameOver { if (button == "L" || button == "R") && !mousey { mouseThing = button - if mouseThing == "L" { + if mouseThing == "L" && !(boardX < 0 || boardX >= sx || boardY < 0 || boardY >= sy) && !board[boardY][boardX].flag { _ = reveal(board, boardX, boardY, sx, sy) } else if mouseThing == "R" { - _ = flag(board, boardX, boardY, sx, sy) + _ = flag_it(board, boardX, boardY, sx, sy) } mousey = true mouseThing = "" @@ -183,6 +198,16 @@ func main() { width, height := screen.Size() offx = width/2 - sx*5/2 offy = height/2 - (sy*3-3)/2 + if height-sy*3+5 > 5 { + str := ` +┏┓ ┳┳┓ • +┃┓ ┏┓ ━━ ┃┃┃ ┓ ┏┓ ┏┓ ┏ +┗┛ ┗┛ ┛ ┗ ┗ ┛┗ ┗ ┛ + ` + print_at(width/2-12, 1, strings.Split(str, "\n")[1], tcell.StyleDefault) + print_at(width/2-12, 2, strings.Split(str, "\n")[2], tcell.StyleDefault) + print_at(width/2-12, 3, strings.Split(str, "\n")[3], tcell.StyleDefault) + } print_it(fmt.Sprintf("Welcome to go-mines! %c %c", symbols[4], symbols[5])) refresh(board) screen.Show() @@ -302,7 +327,7 @@ func reveal(board [][]cell, x, y, width, height int) error { return nil } -func flag(board [][]cell, x, y, width, height int) error { +func flag_it(board [][]cell, x, y, width, height int) error { if x < 0 || x >= width || y < 0 || y >= height { return nil } @@ -396,7 +421,7 @@ func print_board(board [][]cell, solved int) { print_at(offx+(sx*5/2)-3, offy-3, " • ‿ • ", 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 { - print_at(offx+(sx*5/2)-3, offy-3, " • ‿ • ", style2.Foreground(tcell.GetColor("#66c266"))) + print_at(offx+(sx*5/2)-3, offy-3, " ^ o ^ ", 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 := "▔▕🭽🭼🭾🭿▏▁"