aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/game_of_life/modules/automaton/automaton.v
blob: 5c0bf060b75aa6083d21426bfd7f92cecad56984 (plain)
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
module automaton

// ///////////////////////////////////////////////////////////
pub struct A2D {
pub mut:
	maxx int
	maxy int
	data &int
}

fn new_a2d(maxx int, maxy int) &A2D {
	size := int(sizeof(int)) * (maxx * maxy)
	return &A2D{
		maxx: maxx
		maxy: maxy
		data: unsafe { &int(vcalloc(size)) }
	}
}

[inline]
pub fn (a &A2D) set(x int, y int, newval int) {
	unsafe {
		mut e := &int(0)
		e = a.data + y * a.maxx + x
		*e = newval
		_ = e // TODO compiler bug, this is not necessary
	}
}

[inline]
pub fn (a &A2D) get(x int, y int) int {
	unsafe {
		mut e := &int(0)
		e = a.data + y * a.maxx + x
		_ = e
		return *e
	}
}

[inline]
pub fn (a &A2D) clear() {
	for y := 0; y < a.maxy; y++ {
		for x := 0; x < a.maxx; x++ {
			a.set(x, y, 0)
		}
	}
}

// ///////////////////////////////////////////////////////////
pub struct Automaton {
pub mut:
	field     &A2D
	new_field &A2D
}

fn new_automaton(ftext string) Automaton {
	f := ftext.split('\n').map(it.trim_space()).filter(it.len > 0)
	maxy := f.len
	mut maxx := 0
	for y := 0; y < f.len; y++ {
		if maxx < f[y].len {
			maxx = f[y].len
		}
	}
	field := new_a2d(maxx, maxy)
	new_field := new_a2d(maxx, maxy)
	for y in 0 .. field.maxy {
		for x in 0 .. field.maxx {
			val := if x < f[y].len && f[y][x] == `#` { 1 } else { 0 }
			field.set(x, y, val)
		}
	}
	return Automaton{
		field: field
		new_field: new_field
	}
}

pub fn (mut aa Automaton) update() {
	aa.new_field.clear()
	for y := 1; y < aa.field.maxy; y++ {
		for x := 1; x < aa.field.maxx; x++ {
			moore_sum := (0 + aa.field.get(x - 1, y - 1) + aa.field.get(x, y - 1) + aa.field.get(x +
				1, y - 1) + aa.field.get(x - 1, y) + 0 + aa.field.get(x + 1, y) +
				aa.field.get(x - 1, y + 1) + aa.field.get(x, y + 1) + aa.field.get(x + 1, y + 1))
			cell := aa.field.get(x, y)
			v := if cell == 1 { moore_sum in [2, 3] } else { moore_sum == 3 }
			aa.new_field.set(x, y, if v { 1 } else { 0 })
		}
	}
	tmp := aa.field
	aa.field = aa.new_field
	aa.new_field = tmp
}

pub fn gun() Automaton {
	field := '
*******************************************
*                                         *
*  A shooting gun:                        *
*                          #              *
*                        # #              *
*              ##      ##            ##   *
*             #   #    ##            ##   *
*  ##        #     #   ##                 *
*  ##        #   # ##    # #              *
*            #     #       #              *
*             #   #                       *
*              ##                         *
*                                         *
*  Tetris Life:                           *
*                                         *
*  ##       ####                          *
*  ##                                     *
*                                         *
*                                         *
*                                         *
*  #         ##                           *
*  ###      ##                            *
*                                         *
*                                         *
*                                         *
*        #                                *
*       ###                               *
*                                         *
*                                         *
*                                         *
*                                         *
*******************************************
'
	return new_automaton(field)
}