Creazione matrice e inizializzazione

This commit is contained in:
Claudio Maggioni 2017-05-26 13:14:18 +02:00
parent 2abbdbb61b
commit 8e379a34f7
2 changed files with 45 additions and 4 deletions

13
element.go Normal file
View file

@ -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)
}

36
main.go
View file

@ -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
}
}