diff --git a/README.md b/README.md index 8939356..ff4cf60 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index edbde8b..9f7d534 100644 --- a/main.go +++ b/main.go @@ -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, }