-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlife.cpp
More file actions
87 lines (78 loc) · 1.72 KB
/
life.cpp
File metadata and controls
87 lines (78 loc) · 1.72 KB
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
// Life.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int XSIZE = 22;
const int YSIZE = 75;
void printWorld(char[][YSIZE]);
void populate(char[][YSIZE]);
void generation(char[][YSIZE]);
int _tmain(int argc, _TCHAR* argv[])
{
//startup - declare the array then clear it of junk
char world[XSIZE][YSIZE] = { };// 2d array 22 tall by 75 wide
//populate the array
populate(world);
//print out array
printWorld(world);
//calculate live and dead cell moves
generation(world);
//print out new array
system("pause > NULL");
return 0;
}
void populate(char world[XSIZE][YSIZE]) //inital seed for the world array
{
char live = 178;
for (int y = 0; y < 75; y++)
{
for (int x = 0; x < 22; x++)
{
if (x % 2 == 0 && y % 4 != 0)
{
world[x][y] = live;
}
}
}
}
void printWorld(char world[XSIZE][YSIZE]) //prints out the world array
{
for (int y = 0; y < 75; y++)
{
for (int x = 0; x < 22; x++)
{
cout << world[x][y];
}
}
}
void generation(char world[XSIZE][YSIZE])
{
int next = 0;
for (int y = 0; y < 75; y++)
{
for (int x = 0; x < 22; x++)
{
//if (y > 1 && x > 1 )
{
if ((world[x - 1][y]) == 178) // search directly to the left for life
next++;
if (world[x - 1][y + 1] == 178)// up left
next++;
if (world[x][y + 1] == 178) // directly above
next++;
if (world[x + 1][y + 1] == 178) // up right
next++;
if (world[x + 1][y] == 178) // directly right
next++;
if (world[x + 1][y - 1] == 178) // down right
next++;
if (world[x][y - 1] == 178) // directly below
next++;
if (world[x - 1][y - 1] == 178) // down left
next++;
}
}
}
cout << next;
}