【golang】py里py气的生命游戏示例,你的第一行代码何必是hello,world

抽空折腾了一下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)
}
golang
214 views
Comments
登录后评论
Sign In
·

封校中,电脑被干掉了,从efi分区根源性报销,在这里更新一波代码,自己留着用termux写着玩,之前的代码莫名多出了一个[],删掉正常使用:

package main

import (
    "fmt"
)

var (
    num_zero [12][12]int
    day        int = 6
    num_add    int
    num_reduce int
)

func main() {
    life_map()
    //test()
    for i := 1; i <= day; i++ {
        fmt.Printf("第%d次迭代结果:", i)
        fmt.Println()
        life_game()
    }
}

func life_map() {
    num_zero_x := [12]int {1,2,3,4,5}
    num_zero_y := [12]int {6,7,8,9}
    num_zero_neg_x := [12]int {1,2,3,4,5}
    num_zero_neg_y := [12]int {6,7,8,9}
    for i := 0; i < life; i++ {
        if (num_zero_x[i] > 1 && num_zero_x[i] < len()
    }
    num_zero[3][5] = 1
    num_zero[4][6] = 1
    num_zero[3][6] = 1
    num_zero[3][9] = 1

    num_zero[4][7] = -1
    num_zero[5][9] = -1
    num_zero[4][8] = -1
    num_zero[5][8] = -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() {
    //初次搭建生命游戏模型
    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
    count := len(num_zero[i])
    for i != len(num_zero) {
        for j := 0; j < count; j++ {
            if num_zero[i][j] == -1 {
                fmt.Print("■")
            } else if num_zero[i][j] == 0 {
                fmt.Print("  ")
            } else if num_zero[i][j] == 1 {
                fmt.Print("□")
            }
        }
        fmt.Println()
        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)
}