-
Notifications
You must be signed in to change notification settings - Fork 0
/
life0.go
300 lines (286 loc) · 6.2 KB
/
life0.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Package MAIN for proves the functions to include in a next package
// for implementation Cellular Automatas or Conway's Life
// This version is sequential without the characteristics of concurrency
package main
import (
"bufio"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
// Constants in CAPITALS
const (
M = 100 // Rows
N = 100 // Columns
H = 3 // History
T_SIMUL = 100
)
// printw print world -> array of [][]cells in time t
func printw(w [M][N][H]int, t int) {
for _, m := range w {
for j := 0; j < len(m); j++ {
//fmt.Print("[", i, ",", j, ":", m[j][t], "]")
fmt.Print(m[j][t])
}
fmt.Println()
}
}
// figure put patterns in the world
// x,y is the corner on top left of the figure
// the figure is defined:
// 0 - block
// 1 - blinker
// 2 - slider
// TODO: Test the dimension of the figure in the world and the border cases
// beware only put 'true' in the cells and not test the other
func figure(figure int, x int, y int, w *[M][N][H]int) {
switch figure {
case 0: // Block
w[x][y][0] = 1
w[x+1][y][0] = 1
w[x][y+1][0] = 1
w[x+1][y+1][0] = 1
case 1: // Blinker
w[x][y][0] = 1
w[x][y+1][0] = 1
w[x][y+2][0] = 1
case 2: // Slider
w[x][y+1][0] = 1
w[x+1][y+2][0] = 1
w[x+2][y][0] = 1
w[x+2][y+1][0] = 1
w[x+2][y+2][0] = 1
}
}
// randomw generate a random initial state
func randomw(w *[M][N][H]int) {
// A 25% of cells are alive
for i := 0; i < (M * N / 4); i++ {
x := rand.Intn(M)
y := rand.Intn(N)
w[x][y][0] = 1
}
}
// initw read a file .LIF and configure the world with it
func initw(f *os.File, w *[M][N][H]int) bool {
var r *strings.Reader
var b byte
var x, y, oldy int
input := bufio.NewScanner(f)
input.Scan()
if input.Text() != "#Life 1.05" {
fmt.Fprintf(os.Stderr, "ERROR: The file for initialization the world is not a valid .LIF format\n")
return false
}
header:
// Read header of .LIF
for input.Scan() {
r = strings.NewReader(input.Text())
b, _ = r.ReadByte()
if b != '#' {
fmt.Println(input.Text())
} else {
b, _ = r.ReadByte()
switch b {
case 'D':
{
fmt.Println("Description")
}
case 'N':
{
fmt.Println("Rules Conway R 23/3")
}
case 'R':
{
fmt.Fprintf(os.Stderr, "ERROR: 'R' option not implemented\n")
return false
}
case 'P':
{
s := strings.Split(input.Text(), " ")
x, _ = strconv.Atoi(s[1])
y, _ = strconv.Atoi(s[2])
x += (M / 2)
y += (N / 2)
oldy = y
break header // Exit loop, now only blocks of position and cells
}
default:
{
fmt.Fprintf(os.Stderr, "ERROR: Option in header not implemented\n")
return false
}
}
}
}
// Read patterns and positions
for input.Scan() {
r = strings.NewReader(input.Text())
b, _ = r.ReadByte()
if b == '#' {
b, _ = r.ReadByte()
if b == 'P' {
s := strings.Split(input.Text(), " ")
x, _ = strconv.Atoi(s[1])
y, _ = strconv.Atoi(s[2])
x += (M / 2)
y += (N / 2)
oldy = y
} else {
fmt.Fprintf(os.Stderr, "ERROR: Expected Position or blocks not config parameters\n")
return false
}
} else {
for cells := int(r.Size()); cells > 0; cells-- {
switch b {
case '.':
{
w[x][y][0] = 0
}
case '*':
{
w[x][y][0] = 1
}
default:
{
fmt.Fprintf(os.Stderr, "ERROR: Character not valid, only '.' or '*'\n")
return false
}
}
b, _ = r.ReadByte()
y++
}
}
x++
y = oldy
}
return true
// NOTE: ignoring potential errors from input.Err()
}
// oscilt2 compare Actual (t) = Past (t - 2) for know if the system is oscillator
func oscilt2(w *[M][N][H]int, t int) bool {
oscil := true
if t < 2 {
return false
}
at := t % H // Actual time
pt := (t - 2) % H // Past time
loop:
for i := 0; i < M; i++ {
for j := 0; j < N; j++ {
if w[i][j][at] != w[i][j][pt] {
oscil = false
break loop
}
}
}
return oscil
}
// nextw compute for all the world the next state of the cells
func nextw(w *[M][N][H]int, t int) bool {
static := true
at := t % H // Actual time
nt := (t + 1) % H // Next time
for i := 0; i < M; i++ {
for j := 0; j < N; j++ {
w[i][j][nt] = neighbours(w, i, j, at)
if static && (w[i][j][nt] != w[i][j][at]) {
static = false
}
}
}
return static
}
// neighbours calculate the next state of the cells
func neighbours(w *[M][N][H]int, i int, j int, t int) int {
var nb int // number of neifhbours life
top := i - 1
bottom := i + 1
left := j - 1
right := j + 1
if top == -1 {
top = M - 1
}
if bottom == M {
bottom = 0
}
if left == -1 {
left = N - 1
}
if right == N {
right = 0
}
nb = w[top][left][t] + w[top][j][t] + w[top][right][t]
nb += w[i][left][t] + w[i][right][t]
nb += w[bottom][left][t] + w[bottom][j][t] + w[bottom][right][t]
//fmt.Println(top, bottom, left, right)
//fmt.Println(i, j, t, "nb:", nb, "w:", w[i][j][t])
if (nb == 2) && (w[i][j][t] == 1) {
return 1
}
if nb == 3 {
return 1
}
return 0
}
// main function for run and test the implementation of the functions
func main() {
var world [M][N][H]int
start := time.Now()
run := true
randomPtr := flag.Bool("random", false, "Initialize the world with a random state")
testPtr := flag.Bool("test", false, "World initialization for test")
filePtr := flag.String("file", "name.lif", "File name .lif")
flag.Parse()
switch {
case *randomPtr:
{
randomw(&world)
}
case *testPtr:
{
// World initialization
//figure(0, 6, 2, &world)
figure(1, 0, 0, &world)
//figure(2, 0, 0, &world)
}
case *filePtr != "name.lif":
{
f, err := os.Open(*filePtr)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
run = false
} else {
run = initw(f, &world)
f.Close()
}
}
default:
{
fmt.Println("Use $life -h for help")
run = false
}
}
if run {
for t := 0; t < T_SIMUL; t++ {
fmt.Println("Time:", t)
printw(world, t%H)
// Check the world before calculate the next
if oscilt2(&world, t) {
fmt.Println("End simulation, the system is oscillator with period=2")
t = T_SIMUL
}
if nextw(&world, t) {
fmt.Println("End simulation, the system is static.")
t = T_SIMUL
}
}
}
elapsed := time.Since(start)
log.Printf("Total time: %s", elapsed)
}