Make rows, cols and mines customizable with flags
This commit is contained in:
parent
8fe6b9752e
commit
8b8c20fb74
10
README.md
10
README.md
@ -37,6 +37,16 @@ Simply run:
|
|||||||
go run main.go
|
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
|
## How to Play
|
||||||
|
|
||||||
- Left-click: Open a cell
|
- Left-click: Open a cell
|
||||||
|
32
main.go
32
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
@ -382,6 +383,31 @@ func (gs *gameState) showMessage(g *gocui.Gui, title, message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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)
|
g, err := gocui.NewGui(gocui.OutputNormal, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
@ -392,9 +418,9 @@ func main() {
|
|||||||
g.Mouse = true
|
g.Mouse = true
|
||||||
|
|
||||||
gs := &gameState{
|
gs := &gameState{
|
||||||
rows: 10,
|
rows: *rowsFlag,
|
||||||
cols: 10,
|
cols: *colsFlag,
|
||||||
numMines: 10,
|
numMines: *minesFlag,
|
||||||
gameStartedAt: time.Now(),
|
gameStartedAt: time.Now(),
|
||||||
firstMove: true,
|
firstMove: true,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user