Snippets
Created by
Willy Pillow
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #include <iostream>
#include <limits>
// Defines the number of rows and columns
const int ROWLEN = 10;
const int COLLEN = 10;
const int DEAD = 0;
const int ALIVE = 1;
int cell[ROWLEN][COLLEN];
int celltemp[ROWLEN][COLLEN]; // Global arrays that store the cell status
void initCell(); // Sets the starting cells with input from the user (using stdin)
int sum(int row, int col); // Calculates the number of alive neighbors for a cell
void printCell(); // Prints the cells to the console(stdout)
void run(); // Where the real calculation happens
// Pauses the program until a key is pressed
inline void pause()
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return;
}
int main()
{
std::cout << "--------Game Of Life---------" << std::endl;
// Init...
initCell();
printCell();
char mode = '\0';
std::cout << "1 or 2?:"; // 1 for interactive and 2 for going certin number of turns
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get(mode);
std::cout << "Press any key to start..." << std::endl;
pause();
switch(mode)
{
// Interactive mode
case '1':
{
char again = '\0';
while(true)
{
run();
ReEnter:
std::cout << "\nContinue?(y/n)";
std::cin >> again;
switch(again)
{
case 'y':
{
// Go back to the beginning of the loop
continue;
}
case 'n':
{
// Drop to the break below...
break;
}
default:
{
// Wrong input, go back
// Used "goto" here since IMO it's easier & shorter for this purpose
std::cout << "Input incorrect!" << std::endl;
goto ReEnter;
}
}
// If 'n' is entered above, it'll be dropped here
break;
}
break;
}
// Certin number of turns
case '2':
{
int times;
std::cout << "Enter the times you want to run :";
std::cin >> times;
for(int i=0; i<times; i++)
{
run();
}
break;
}
default:
{
break;
}
}
std::cout << "END!!" << std::endl;
pause();
return 0;
}
// Sets the starting cells with input from the user (using stdin)
void initCell()
{
// Set all cells as dead
for(int row = 0; row < ROWLEN; row++)
{
for(int col = 0; col < COLLEN; col++)
{
cell[row][col] = DEAD;
}
}
// Start to set cell positions
std::cout << "Please enter the position of cells. Type(-1,-1) to end.\n";
int row = 0, col = 0;
while(true)
{
std::cout << ">";
std::cin >> row >> col;
if(row >= 0 && row < ROWLEN && col >= 0 && col < COLLEN) // If row & col are in range...
{
cell[row][col] = ALIVE;
row = 0;
col = 0;
}
else if(row == -1 && col == -1) // Or is it an ending command...
{
row = 0;
col = 0;
break;
}
else // Wrong input
{
std::cout << "Input incorrect!" << std::endl;
}
}
}
// Calculates the number of alive neighbors for a cell
int sum(int row, int col)
{
int count = 0;
for(int r = row-1; r <= row+1; r++)
{
for(int c = col-1; c <= col+1; c++)
{
if(r < 0 || r >= ROWLEN || c < 0|| c >= COLLEN) // If the position is not in range, skip it
{
continue;
}
if(cell[r][c] == ALIVE)
{
count++;
}
}
}
if(cell[row][col] == ALIVE)
{
// Because the cell itself is calculated in the loop above,
// so we have to subtract count by 1 if the cell is alive
count--;
}
return count;
}
// Prints the cells to the console(stdout)
void printCell()
{
std::cout << "\nCell status:\n";
// Display the top line of the grid
std::cout << "┌";
for(int col = 0; col < COLLEN-1; col++)
{
std::cout << "─┬";
}
std::cout << "─┐\n";
for(int row = 0; row < ROWLEN; row++)
{
std::cout << "│";
for(int col = 0; col < COLLEN; col++)
{
switch(cell[row][col])
{
case ALIVE:
{
std::cout << "●";
break;
}
case DEAD:
{
std::cout << "○";
break;
}
default:
{
// Shouldn't happen!
std::cerr << "Error!!";
}
}
std::cout << "│";
}
std::cout << "\n";
if(row < ROWLEN-1)
{
std::cout << "├";
for(int col = 0; col < COLLEN-1; col++)
{
std::cout << "─┼";
}
std::cout << "─┤\n";
}
}
// Display the bottom line of the grid
std::cout << "└";
for(int col = 0; col < COLLEN-1; col++)
{
std::cout << "─┴";
}
std::cout << "─┘" << std::endl;
}
// Where the real calculation happens
void run()
{
int count = 0;
for (int row = 0; row < ROWLEN; row++)
{
for(int col = 0; col < COLLEN; col++)
{
switch(sum(row, col))
{
// All changes are made in "celltemp" which we'll copy back to "cell" later
case 2:
{
// If neighbors count is 2, then set the cell to what it is now
// (The S2 in B3/S23)
celltemp[row][col] = cell[row][col];
break;
}
case 3:
{
// If neighbors count is 3, then set it as alive
// since if the cell is alive, it stays that way
// (The S3 in B3/S23)
// otherwise if it is dead, it reborns
// (The B3 in B3/S23)
// thus, the cell would be alive regardless of its current status
celltemp[row][col] = ALIVE;
break;
}
default:
{
// Otherwise, set the cell as dead
celltemp[row][col] = DEAD;
break;
}
}
}
}
// Copies from celltemp to cell and calculates the total population
for(int row = 0; row < ROWLEN; row++)
{
for(int col = 0; col < COLLEN; col++)
{
cell[row][col] = celltemp[row][col];
if(cell[row][col] == ALIVE)
{
// If this cell is alive
count++;
}
}
}
printCell();
std::cout << "Currently " << count << " cells alive." << std::endl;
}
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
I was looking for a way to spend time more interesting than just watching TV. A friend of mine recommended finding an online casino. After a few searches, I came across zar casino login. The site looks very user-friendly, and registration takes only a few minutes. I was pleasantly surprised by the number of games and various bonuses that are offered. It is especially nice that zar casino provides special offers for players from South Africa. Thanks to this site, I now have a great way to have fun after work and even sometimes win good amounts.