-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-of-life.sh
executable file
·142 lines (122 loc) · 2.72 KB
/
game-of-life.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
stty -echo
# Init output
OLD_IFS="$IFS"
IFS=
tput clear
tput civis
# Calculate size
SCREEN_WIDTH=$(tput cols)
SCREEN_HEIGHT=$(($(tput lines) - 1))
WIDTH=$((60 % (SCREEN_WIDTH - 2)))
HEIGHT=$((20 % (SCREEN_HEIGHT - 2)))
LEFT=$(((SCREEN_WIDTH - WIDTH) / 2))
TOP=$(((SCREEN_HEIGHT - HEIGHT) / 2))
# Declare world
declare -a prevWorld
declare -a world
function draw {
for (( row = 0; row < HEIGHT; row++ ))
do
buf=""
tput cup $(($TOP + $row)) $LEFT
for (( column = 0; column < WIDTH; column++ ))
do
key=1$row\0$column
if [ ${world[$key]} == 1 ]
then
buf+="+"
else
buf+=" "
fi
done
echo $buf
done
}
function step {
for (( row = 0; row < HEIGHT; row++ ))
do
for (( column = 0; column < WIDTH; column++ ))
do
key=1$row\0$column
x1=$(($row - 1))
if [ $x1 -lt 0 ]; then x1=$(($HEIGHT-1)); fi
x2=$row
x3=$((($row + 1) % $HEIGHT))
y1=$(($column - 1))
if [ $y1 -lt 0 ]; then y1=$(($WIDTH-1)); fi
y2=$column
y3=$((($column + 1) % $WIDTH))
alive=$((
${prevWorld[1$x1\0$y1]} +
${prevWorld[1$x2\0$y1]} +
${prevWorld[1$x3\0$y1]} +
${prevWorld[1$x1\0$y2]} +
${prevWorld[1$x3\0$y2]} +
${prevWorld[1$x1\0$y3]} +
${prevWorld[1$x2\0$y3]} +
${prevWorld[1$x3\0$y3]}
))
if [ ${prevWorld[$key]} == 0 ]
then
if [ $alive -eq 3 ]
then
world[$key]=1
fi
else
if [ \( $alive -lt 2 \) -o \( $alive -gt 3 \) ]
then
world[$key]=0
fi
fi
done
done
}
tput cup $(($TOP - 5)) $(($SCREEN_WIDTH / 2 - 16))
echo " ______ _______ _______ _______ "
tput cup $(($TOP - 4)) $(($SCREEN_WIDTH / 2 - 16))
echo "| |_|_ _| ___| ___|"
tput cup $(($TOP - 3)) $(($SCREEN_WIDTH / 2 - 16))
echo "| |_| |_| ___| ___|"
tput cup $(($TOP - 2)) $(($SCREEN_WIDTH / 2 - 16))
echo "|_______|_______|___| |_______|"
line=""
for (( column = 0; column <= WIDTH; column++ ))
do
line+="_"
done
tput cup $(($TOP - 1)) $(($LEFT))
echo $line
tput cup $(($TOP + $HEIGHT)) $(($LEFT))
echo $line
for (( row = 0; row <= HEIGHT; row++ ))
do
tput cup $(($TOP + $row)) $(($LEFT - 1))
echo "|"
tput cup $(($TOP + $row)) $(($LEFT + $WIDTH + 1))
echo "|"
done
# Init by random
for (( row = 0; row < HEIGHT; row++ ))
do
for (( column = 0; column < WIDTH; column++ ))
do
key=1$row\0$column
world[$key]=$(($RANDOM % 2))
done
done
while [ 1==1 ]
do
for (( row = 0; row < HEIGHT; row++ ))
do
for (( column = 0; column < WIDTH; column++ ))
do
key=1$row\0$column
prevWorld[$key]=${world[$key]}
done
done
step
draw
done
tput cnorm
IFS="$OLD_IFS"