-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_of_life.rs
119 lines (101 loc) · 3.83 KB
/
game_of_life.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2020 Claus Jørgensen
// Conway's Game of Life
// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
fn find_neighbors(map: &Vec<Vec<i8>>, x: usize, y: usize) -> Vec<i8> {
let mut neighbors: Vec<i8> = vec![];
let width = map.len();
let height = map[0].len();
if x >= 1 {
neighbors.push(map[x - 1][y]); // left
}
if (x + 1) < width {
neighbors.push(map[x + 1][y]) // right
}
if y >= 1 {
neighbors.push(map[x][y - 1]); // top
}
if (y + 1) < height {
neighbors.push(map[x][y + 1]); // bottom
}
if x >= 1 && y >= 1 {
neighbors.push(map[x - 1][y - 1]); // top-left
}
if x >= 1 && (y + 1) < height {
neighbors.push(map[x - 1][y + 1]); // bottom-left
}
if (x + 1) < width && (y + 1) < height {
neighbors.push(map[x + 1][y + 1]); // top-right
}
if (x + 1) < width && y >= 1 {
neighbors.push(map[x + 1][y - 1]); // bottom-right
}
return neighbors
}
fn transition(map: Vec<Vec<i8>>) -> Vec<Vec<i8>> {
let mut tmp_map: Vec<Vec<i8>> = map.clone();
let width = map.len();
let height = map[0].len();
for x in 0..width {
for y in 0..height {
let neighbors = find_neighbors(&map, x, y);
let mut num_alive = 0;
for n in &neighbors {
if *n == 1 { num_alive += 1 }
}
if num_alive < 2 {
tmp_map[x][y] = 0; // death by under population
} else if num_alive > 3 {
tmp_map[x][y] = 0; // death by over population
} else if map[x][y] == 0 && num_alive == 3 {
tmp_map[x][y] = 1; // alive by reproduction
} else {
tmp_map[x][y] = map[x][y]; // continue unchanged
}
}
}
return tmp_map;
}
fn print_map(map: &Vec<Vec<i8>>) {
let width = map.len();
let height = map[0].len();
for x in 0..width {
for y in 0..height {
if map[x][y] == 1 {
print!("+ ");
} else {
print!(" ");
}
}
println!();
}
}
fn main() {
let pentadecathlon = vec![
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
];
let mut map = pentadecathlon;
loop {
std::process::Command::new("clear").status().unwrap();
print_map(&map);
map = transition(map);
std::thread::sleep(std::time::Duration::from_secs(1));
}
}