summaryrefslogtreecommitdiff
path: root/apps/csharp/life/src/life_board.cs
blob: 26dbf62cbc836461b608cce9b68028cf32244549 (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
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
using System;

public class LifeBoard
{
    public const int Width = 47;
    public const int Height = 31;

    public static int IndexForPosition(int x, int y)
    {
        return y * Width + x;
    }

    public bool[] Cells
    {
        get { return board; }
    }

    private int gen = 0;

    private bool[] board;
    private bool[] board1, board2;
    private Efl.LoopTimer lifeTimer = null;
    private LifeRender lifeRender = null;

    private void CellOn(int x, int y)
    {
        board1[IndexForPosition(x, y)] = true;
    }

    private void BoardSetup()
    {
        // glide
        CellOn(16, 1);
        CellOn(17, 2);
        CellOn(18, 2);
        CellOn(16, 3);
        CellOn(17, 3);

        // oscilate
        CellOn(22, 15);
        CellOn(23, 15);
        CellOn(24, 15);

        // block
        CellOn(32, 15);
        CellOn(33, 15);
        CellOn(32, 16);
        CellOn(33, 16);
    }

    public LifeBoard()
    {
        board1 = new bool[Height * Width];
        board2 = new bool[Height * Width];

        BoardSetup();

        board = board1;
    }

    public void Run(Efl.Ui.Win win)
    {
        lifeTimer = new Efl.LoopTimer(win, 0.1);

        lifeTimer.TickEvt += (object sender, EventArgs ev) => {
            Nextgen();
            if (this.lifeRender != null)
                this.lifeRender.Refresh(win);
        };
    }

    public int SumAround(int x, int y)
    {
        int i, sum = 0;
        int max = Width * Height;

        i = IndexForPosition(x - 1, (y - 1));
        if (i >= 0 && board[i])
            ++sum;
        ++i;
        if (i >= 0 && board[i])
            ++sum;
        ++i;
        if (i >= 0 && board[i])
            ++sum;

        i = IndexForPosition(x - 1, y);
        if (i >= 0 && board[i])
            ++sum;
        i += 2;
        if (i < max && board[i])
            ++sum;

        i = IndexForPosition(x - 1, (y + 1));
        if (i < max && board[i])
            ++sum;
        ++i;
        if (i < max && board[i])
            ++sum;
        ++i;
        if (i < max && board[i])
            ++sum;

        return sum;
    }

    public void Nextgen()
    {
        bool[] work = null;
        ++gen;

        if (board == board1)
            work = board2;
        else
            work = board1;

        for (int y = 0; y < Height; y++)
            for (int x = 0; x < Width; x++)
            {
                int i = IndexForPosition(x, y);

                int n = SumAround(x, y);
                if (board[i])
                {
                    if (n > 3 || n < 2)
                        work[i] = false;
                    else
                        work[i] = true;
                }
                else
                {
                    if (n == 3)
                        work[i] = true;
                    else
                        work[i] = false;
                }
            }

        board = work;
    }

    public void TogglePause(Efl.Ui.Win win)
    {
        if (lifeTimer != null)
        {
            lifeTimer.SetParent(null);
            lifeTimer.Dispose();
            lifeTimer = null;
        }
        else
        {
            Run(win);
        }
    }

    public void SetRender(LifeRender render)
    {
        lifeRender = render;
    }
}