Make rows, cols and mines customizable with flags

This commit is contained in:
bdnugget 2025-03-16 23:01:14 +01:00
parent 8fe6b9752e
commit 8b8c20fb74
2 changed files with 39 additions and 3 deletions

View File

@ -37,6 +37,16 @@ Simply run:
go run main.go
```
You can customize the game with command line flags:
```
go run main.go -rows=15 -cols=20 -mines=30
```
Available options:
- `-rows`: Number of rows (default: 10, minimum: 5)
- `-cols`: Number of columns (default: 10, minimum: 5)
- `-mines`: Number of mines (default: 10, maximum: 1/3 of total cells)
## How to Play
- Left-click: Open a cell

32
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"errors"
"flag"
"fmt"
"log"
"math/rand/v2"
@ -382,6 +383,31 @@ func (gs *gameState) showMessage(g *gocui.Gui, title, message string) error {
}
func main() {
// Define command line flags
rowsFlag := flag.Int("rows", 10, "Number of rows in the grid")
colsFlag := flag.Int("cols", 10, "Number of columns in the grid")
minesFlag := flag.Int("mines", 10, "Number of mines in the grid")
// Parse command line flags
flag.Parse()
// Validate input
if *rowsFlag < 5 {
log.Println("Warning: Minimum 5 rows required, setting to 5")
*rowsFlag = 5
}
if *colsFlag < 5 {
log.Println("Warning: Minimum 5 columns required, setting to 5")
*colsFlag = 5
}
// Make sure mines don't exceed 1/3 of the grid
maxMines := (*rowsFlag * *colsFlag) / 3
if *minesFlag > maxMines {
log.Printf("Warning: Too many mines for this grid size, setting to %d\n", maxMines)
*minesFlag = maxMines
}
g, err := gocui.NewGui(gocui.OutputNormal, true)
if err != nil {
log.Panicln(err)
@ -392,9 +418,9 @@ func main() {
g.Mouse = true
gs := &gameState{
rows: 10,
cols: 10,
numMines: 10,
rows: *rowsFlag,
cols: *colsFlag,
numMines: *minesFlag,
gameStartedAt: time.Now(),
firstMove: true,
}