Skip to content
Permalink
Browse files

Added options

  • Loading branch information
EParisot committed Jul 6, 2019
1 parent eaa053b commit 36b89de536d8cf3b4ed17ee76d372c2e5fc15471
14 main.go
@@ -9,9 +9,13 @@ import (

// Env main game struct
type Env struct {
grid []*cell
size int
mode string
mapFile string
imgFile string
difficulty string
grid []*cell
size int
autoMode bool
heuristic string
}

type cell struct {
@@ -22,7 +26,7 @@ type cell struct {
}

func main() {
env := Env{}
env := Env{autoMode: false}
err := env.parseFile()
if err != nil {
log.Fatal(err)
@@ -32,4 +36,6 @@ func main() {
for i := range env.grid {
fmt.Println(env.grid[i])
}
//TODO start GUI + manual controls
//TODO start Algo
}
@@ -0,0 +1,7 @@
package main

import ()

func (env *Env) buildMap() {

}
File renamed without changes.
File renamed without changes.
@@ -0,0 +1 @@
3
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -15,49 +15,69 @@ func (env *Env) parseFile() error {
if err != nil {
return err
}
// Read file
file, err := os.Open(os.Args[1])
if err != nil {
return (err)
}
defer file.Close()
reader := bufio.NewReader(file)
// Read size
err = env.readSize(reader)
if err != nil {
return err
}
// Read map
err = env.readMap(reader)
if err != nil {
return err
if env.mapFile != "" {
// Read file
file, err := os.Open(env.mapFile)
if err != nil {
return (err)
}
defer file.Close()
reader := bufio.NewReader(file)
// Read size
err = env.readSize(reader)
if err != nil {
return err
}
// Read map
err = env.readMap(reader)
if err != nil {
return err
}
}
return nil
}

func printUsage() {
fmt.Println(`Usage : N-Puzzle map_file [-m map] [-i image] [-a] [-d difficulty] [-h heuristic]
with -m map = 'map_file.map'
-h heuristic = 'md' for Manhattan Distance, WIP...
-i image = 'image_file.png'
-d difficulty = 'E'asy, 'M'edium, 'H'ard
-a auto`)
}

func (env *Env) parseArgs() error {
if len(os.Args[1:]) < 1 {
fmt.Println(`Usage : N-Puzzle map_file [-m heuristic]
with heuristic = 'md' for Manhattan Distance, WIP...`)
return errors.New("missing argument")
printUsage()
}
for i, arg := range os.Args {
if arg == "-m" && i+1 < len(os.Args) {
env.mode = os.Args[i+1]
if arg == "-i" && i+1 < len(os.Args) {
env.imgFile = os.Args[i+1]
} else if arg == "-a" {
env.autoMode = true
} else if arg == "-d" && i+1 < len(os.Args) {
env.difficulty = os.Args[i+1]
} else if arg == "-m" && i+1 < len(os.Args) {
env.mapFile = os.Args[i+1]
} else if arg == "-h" && i+1 < len(os.Args) {
env.heuristic = os.Args[i+1]
}
}
if env.mapFile == "" {
env.buildMap()
}
return nil
}

func (env *Env) readSize(reader *bufio.Reader) error {
firstLine, err := parseLine(reader)
if len(strings.Fields(firstLine)) == 0 ||
len(strings.Fields(firstLine)) > 1 {
return errors.New("missing size value")
return errors.New("error missing size value")
}
size, err := strconv.Atoi(firstLine)
if err != nil || size < 3 {
return errors.New("invalid size value")
return errors.New("error invalid size value")
}
env.size = size
return nil
@@ -71,15 +91,15 @@ func (env *Env) readMap(reader *bufio.Reader) error {
}
ids := strings.Fields(line)
if len(ids) != env.size {
return errors.New("invalid map size")
return errors.New("error invalid map size")
}
for i, val := range ids {
valInt, err := strconv.Atoi(val)
if err != nil || valInt > env.size*env.size || valInt < 0 {
return errors.New("invalid cell id")
return errors.New("error invalid cell id")
}
if env.isPresent(valInt) {
return errors.New("duplicated cell id")
return errors.New("error duplicated cell id")
}
env.grid = append(env.grid, &cell{
id: valInt,
@@ -94,7 +114,7 @@ func (env *Env) readMap(reader *bufio.Reader) error {
func parseLine(reader *bufio.Reader) (string, error) {
line, _, err := reader.ReadLine()
if err != nil {
return "", errors.New("Error Reading map file")
return "", errors.New("error reading map file")
}
lineStr := string(line)
lineTab := strings.Split(lineStr, "#")
@@ -4,5 +4,5 @@ for f in maps/*
do
echo "Processing $f file..."
# take action on each file. $f store current file name
./N-Puzzle $f
./N-Puzzle -m $f
done

0 comments on commit 36b89de

Please sign in to comment.
You can’t perform that action at this time.