Console Life Form Game

This is a small console game that simulates life forms as they reproduce on a grid. The code works well enough, but I'm interested in suggestions to make it more optimal/efficient. It just seems a bit too "messy" at the moment.

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
/*  ---------LIFE---------
    Programmer : Me
    Version : 1.0
    Date : 14 February 2014
    ----------------------
*/

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

const int LIFESPAN = 12; // turns until death
const int REPRODUCE_CHANCE = 80; // % chance to successfully reproduce

struct GameBoard
{
	int age;
	bool active;
};
GameBoard gboard[10][10]; 
//'age' != 0 then life is in this grid; otherwise 0 == no life in this grid.  
//'active' is to prevent single turn from overpopulating - explained more below

inline void Randomize() { srand(time(0)); }
int Random(int minVal, int maxVal)
{
	int r;
	r= rand();
	r = (r % (maxVal-minVal+1)) + minVal;
	return r;
}

void DisplayScreen()
{
	/*
		Draw the Game Board on the console screen
		# = lifeform in grid
		blank [space] = empty grid
		* = grid border
	*/
	system("cls");
	string thisLine;
	cout << "* ********** *\n";
	for (int i=0; i<10; ++i)
	{	
		thisLine = "* ";
		
		for (int j=0; j<10; ++j) {
			thisLine += (gboard[i][j].age != 0) ? "#" : " "; }
		
		thisLine += " *\n";
		cout << thisLine;
	}
	cout << "* ********** *\n";
}

void ExecuteTurn()
{
	//Loop through each game board grid
	for (int i=0; i<10; ++i) {
		for (int j=0; j<10; ++j)
		{

			//LIFE FORM CHECK * *
			//If a game board grid contains a lifeform and is active...
			if (gboard[i][j].age != 0 && gboard[i][j].active == true)
			{
				//Increase age by 1
				gboard[i][j].age += 1;

				//REPRODUCTION CHECK * * *
				//Determine if this lifeform will reproduce this turn
				int chanceForLife = Random(1,100);
				if (chanceForLife <= REPRODUCE_CHANCE)
				{
					//Life will reproduce - choose random direction to make new lifeform
					bool goodDirection = false;
					while (goodDirection == false)
					{
						int nDir = Random(0,3);
						int nColumn = i;
						int nRow = j;
						switch (nDir)
						{
						case 0:
							//above this grid
							nColumn -= 1;
							if (nColumn >= 0) goodDirection = true;
							break;
						case 1:
							//right of this grid
							nRow += 1;
							if (nRow <= 9) goodDirection = true;
							break;
						case 2:
							//below this grid
							nColumn += 1;
							if (nColumn <= 9) goodDirection = true;
							break;
						default:
							//left of this grid
							nRow -= 1;
							if (nRow >= 0) goodDirection = true;
							break;
						}
						//Good direction has been choosen
						//make this a new lifeform - age 1
						gboard[nColumn][nRow].age = 1;
						/* ACTIVE =
						Necessary to keep loop from immediately actioning on NEW lifeforms
						Otherwise you will end up with 1/2 the grid full on a single 'turn'
						*/
						gboard[nColumn][nRow].active = false;
					}
				} //END OF REPRODUCTION CHECK * * *

				//Kill old lifeforms
				if (gboard[i][j].age >= LIFESPAN) gboard[i][j].age = 0;

			} //END OF LIFE FORM CHECK * *
		
		}} //Game board grid for loops (x2)

//This turn is complete - make all lifeforms active
	for (int i=0; i<10; ++i)
		for (int j=0; j<10; ++j)
			gboard[i][j].active = true;
}

void CreateRandomLife(int Count)
{
	for (int i=0; i<Count; ++i)
	{
		int iCol = Random(0,9);
		int iRow = Random(0,9);
		gboard[iCol][iRow].age = 1;
		gboard[iCol][iRow].active = true;
	}
}



void ResetGame()
{
	//Kill all life
	for (int i=0; i<10; ++i)
		for (int j=0; j<10; ++j)
		{ gboard[i][j].age = 0; gboard[i][j].active = false; }

	//Create 3 random lifeforms to begin
	CreateRandomLife(3);
}
int main()
{
	Randomize();
	ResetGame();
	DisplayScreen();

        bool gameIsRunning = true;
	while (gameIsRunning)
	{
		cout << "\n\nHow many times would you like to run the simulation? ";
		int simCount;
		cin >> simCount;
		if (simCount < 0) simCount = 0;
		if (simCount > 50) simCount = 50;

		//Run sim
		for (int i=0; i<simCount; ++i)
		{
			ExecuteTurn();
		}
		DisplayScreen();
		if (simCount == 0) 
		{
			cout << "\nWould you like to play again (y/n)? ";
			char answer;
			cin >> answer;
			gameIsRunning = (answer == 'y') ? true : false;
			if (gameIsRunning == true) { ResetGame(); DisplayScreen(); }
		}
	}
	
	return 0;
}
Last edited on
FWIW, you're calling 'Randomize' too frequently and are making your random numbers less random by doing so.

You should call Randomize (srand) exactly once when your program first starts. You should not call it before every call to rand.

Other than that... it really isn't messy. It's pretty straightforward. I think you did a great job.
Thanks for the feedback. I was never really sure how often to call it. That clears up quite a bit - for this and a few other programs I've created. Thanks again.

Corrections made.
Last edited on
You have a minor problem in your Random function. To describe it, I'll use the call in line 81.

You are looking for a number between and including 0 and 3. In line 30,:
maxVal - minVal = 3 - 0 = 3.

(r % 3) + minVal will be either 0, 1 or 2. You will never return the value 3.

You want line 30 to be
r = (r % (maxVal-minVal + 1)) + minVal;

Also, to be safe, you might want to check that maxVal is actually greater than minVal. If not, you can swap the values and continue on.
Ouch... good catch, thanks.
Topic archived. No new replies allowed.