Skip to content

Commit

Permalink
update GameOfLife
Browse files Browse the repository at this point in the history
  • Loading branch information
dowell2020 committed Nov 18, 2021
1 parent 7f34c8b commit a7b7ccc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
60 changes: 60 additions & 0 deletions algs15/3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @Author: dowell87
* @Date: 2021-11-18 14:27:18
* @Descripttion:
* @LastEditTime: 2021-11-18 15:28:23
*/
package algs15

func GameOfLife(board [][]int) {
if board == nil || len(board) == 0 {
return
}
m, n := len(board), len(board[0])

for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
// 循环每一个数字
// 统计细胞周边的细胞
count := liveNeighbors(board, m, n, i, j)
if board[i][j] == 1 && count >= 2 && count <= 3 {
board[i][j] = 3
}
if board[i][j] == 0 && count == 3 {
board[i][j] = 2
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
board[i][j] >>= 1
}
}
}

// 获取八个角度的方法可以记录下来
func liveNeighbors(board [][]int, m, n, i, j int) int {
lives := 0
for x := max(i-1, 0); x <= min(i+1, m-1); x++ {
for y := max(j-1, 0); y <= min(j+1, n-1); y++ {
lives += board[x][y] & 1
}
}
lives -= board[i][j] & 1
return lives
}

func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
18 changes: 18 additions & 0 deletions algs15/3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @Author: dowell87
* @Date: 2021-11-18 14:27:52
* @Descripttion:
* @LastEditTime: 2021-11-18 14:30:34
*/
package algs15

import (
"fmt"
"testing"
)

func TestGameOfLife(t *testing.T) {
board := [][]int{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}}
GameOfLife(board)
fmt.Println(board)
}

0 comments on commit a7b7ccc

Please sign in to comment.