forked from WildernessLabs/Meadow.Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeadowApp.cs
147 lines (125 loc) · 3.94 KB
/
MeadowApp.cs
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
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Graphics;
using System;
using System.Threading.Tasks;
namespace GameOfLife;
// Change F7FeatherV2 to F7FeatherV1 for V1.x boards
public class MeadowApp : App<F7CoreComputeV2>
{
IJuegoHardware juego;
MicroGraphics graphics;
readonly Random rand = new Random();
readonly int NUMBER_OF_GENERATIONS = 1000;
static readonly int NUM_COLUMNS = 160;
static readonly int NUM_ROWS = 120;
readonly int SIZE = 2;
readonly byte[,] currentBoard = new byte[NUM_COLUMNS, NUM_ROWS];
readonly byte[,] nextBoard = new byte[NUM_COLUMNS, NUM_ROWS];
public override Task Initialize()
{
Console.WriteLine("Initialize...");
juego = Juego.Create();
graphics = new MicroGraphics(juego.Display);
InitBoard();
Console.WriteLine("Initialize complete");
return Task.CompletedTask;
}
public override Task Run()
{
graphics.Clear();
for (int i = 0; i < NUMBER_OF_GENERATIONS; i++)
{
ComputeCA();
DrawBoard();
for (int x = 1; x < NUM_COLUMNS - 1; x++)
{
for (int y = 1; y < NUM_ROWS - 1; y++)
{
currentBoard[x, y] = nextBoard[x, y];
}
}
graphics.Show();
}
return Task.CompletedTask;
}
void InitBoard()
{
for (int x = 1; x < NUM_COLUMNS - 1; x++)
{
for (int y = 1; y < NUM_ROWS - 1; y++)
{
nextBoard[x, y] = 0;
if (x == 0 || x == NUM_COLUMNS - 1 || y == 0 || y == NUM_ROWS - 1)
{
currentBoard[x, y] = 0;
}
else
{
if (rand.Next(2) == 0)
{
currentBoard[x, y] = 1;
}
else
{
currentBoard[x, y] = 0;
}
}
}
}
}
void DrawBoard()
{
for (int x = 1; x < NUM_COLUMNS - 1; x++)
{
for (int y = 1; y < NUM_ROWS - 1; y++)
{
if (currentBoard[x, y] != nextBoard[x, y])
{
if (nextBoard[x, y] == 1)
{
graphics.DrawRectangle(x * SIZE, y * SIZE, SIZE, SIZE, Color.LawnGreen, true);
}
else
{
graphics.DrawRectangle(x * SIZE, y * SIZE, SIZE, SIZE, Color.Black, true);
}
}
}
}
}
void ComputeCA()
{
for (int x = 1; x < NUM_COLUMNS - 1; x++)
{
for (int y = 1; y < NUM_ROWS - 1; y++)
{
int neighbors = GetNumberOfNeighbors(x, y);
if (currentBoard[x, y] == 1 && (neighbors == 2 || neighbors == 3))
{
nextBoard[x, y] = 1;
}
else if (currentBoard[x, y] == 1)
{
nextBoard[x, y] = 0;
}
if (currentBoard[x, y] == 0 && (neighbors == 3))
{
nextBoard[x, y] = 1;
}
else if (currentBoard[x, y] == 0)
{
nextBoard[x, y] = 0;
}
}
}
}
int GetNumberOfNeighbors(int x, int y)
{
return currentBoard[x - 1, y] +
currentBoard[x - 1, y - 1] +
currentBoard[x, y - 1] + currentBoard[x + 1, y - 1] +
currentBoard[x + 1, y] + currentBoard[x + 1, y + 1] +
currentBoard[x, y + 1] + currentBoard[x - 1, y + 1];
}
}