抽空折腾了一下golang,只调用fmt的话果然还是适合拿来搞生命游戏吧。
package main
import (
"fmt"
)
var (
count int
num_zero [12][12]int
num_add int
num_reduce int
)
func init() {
count = 1
}
func main() {[]
life_map()
test()
fmt.Printf("第%d次迭代结果:", count)
fmt.Println()
life_game()
fmt.Printf("第%d次迭代结果:", count)
fmt.Println()
life_game()
}
func life_map() {
num_zero[3][5] = 1
num_zero[4][5] = 1
num_zero[3][6] = 1
num_zero[4][6] = 1
}
func test() {
//矩阵测试
i := 0
fmt.Println("生成生命地图:")
for i != len(num_zero) {
fmt.Println(num_zero[i])
i += 1
}
//for循环测试
run_1 := 0
for run_1 != 6 {
fmt.Print(run_1)
run_1 += 1
}
fmt.Println(run_1)
for run_2 := 0; run_2 <= 5; run_2++ {
fmt.Print(run_2)
}
fmt.Println(run_1)
//多循环测试
for j := 0; j < 5; j++ {
fmt.Printf("第%d行:", j+1)
for i := 0; i < 10; i++ {
if i > 5 {
fmt.Println(i)
break
}
fmt.Print(i)
}
}
}
func life_game() {
//初次搭建生命游戏模型
count += 1
for j := 1; j <= len(num_zero)-2; j++ {
for i := 1; i <= len(num_zero)-2; i++ {
num_zero[i][j] = near_count(i, j)
}
}
i := 0
for i != len(num_zero) {
fmt.Println(num_zero[i])
i += 1
}
}
func near_count(i, j int) int {
num_add = 0
num_reduce = 0
for y := -1; y <= 1; y++ {
for x := -1; x <= 1; x++ {
if (num_zero[i+x][j+y] == 1) && (x != 0 || y != 0) {
num_add += 1
} else if (num_zero[i+x][j+y] == -1) && (x != 0 || y != 0) {
num_reduce -= 1
}
}
}
if num_add == 3 {
num_add = 1
} else if num_add == 2 && num_zero[i][j] == 1 {
num_add = 1
} else {
num_add = 0
}
if num_reduce == -3 {
num_reduce = -1
} else if num_reduce == -2 && num_zero[i][j] == -1 {
num_reduce = -1
} else {
num_reduce = 0
}
return (num_add + num_reduce)
}