From 8e379a34f7b816c3bbabcffba5aed00d50dfd6ef Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Fri, 26 May 2017 13:14:18 +0200 Subject: [PATCH] Creazione matrice e inizializzazione --- element.go | 13 +++++++++++++ main.go | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 element.go diff --git a/element.go b/element.go new file mode 100644 index 0000000..667e648 --- /dev/null +++ b/element.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +type Element struct { + IsFood bool + Health int + Age int +} + +func (e Element) String() string { + return fmt.Sprintf("E'Cibo=%t Salute=%d Eta=%d", e.IsFood, e.Health, e.Age) +} diff --git a/main.go b/main.go index 87dd31b..248da36 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,16 @@ package main import ( "fmt" + "math/rand" "os" "strconv" ) -var Matrix [][]int +var Matrix [][]*Element +var SaluteIniziale int func main() { + SaluteIniziale = 50 height, err := strconv.Atoi(os.Args[1]) if err != nil { panic("height not valid") @@ -17,9 +20,34 @@ func main() { if err2 != nil { panic("width not valid") } - Matrix = make([][]int, height) - for i := range Matrix { - Matrix[i] = make([]int, width) + Matrix = make([][]*Element, height) + for i := range Matrix { // inizializzazione matrice + Matrix[i] = make([]*Element, width) + for j := range Matrix[i] { + chose := rand.Intn(2) //scelta rando cibo bug o vuoto (null) + switch chose { + case 0: + Matrix[i][j] = new(Element) // insetto + Matrix[i][j].IsFood = false + Matrix[i][j].Age = 0 + Matrix[i][j].Health = SaluteIniziale + case 1: + Matrix[i][j] = nil //vuota + case 2: + Matrix[i][j] = new(Element) // cibo + Matrix[i][j].IsFood = true + Matrix[i][j].Health = 5 + } + } } + fmt.Println(Matrix) } + +func muovi(h int, w int) { // h verticale, w orizzontale + elemento := Matrix[h][w] + if elemento == nil && elemento.IsFood { + return + } + +}