Today I want to show you a small project for AI: The Game Of Life.
Basically we have a grid with "cells" in it, a cell is alive or not depending on the cells next to this one (8 to take care in total).
And that´s basically it,I will give you the source code so you can take a look and try it by urself!
/* main.cc Ignacio Cortizo Pol (nachocpol@gmail.com) Game Of Life Implemented in ASCII Use a 8x8 bitmap font in the cmd. */ #include <Windows.h> #include <iostream> #include <stdio.h> #define GRID_SIZE 50 struct Grid { int grid[GRID_SIZE][GRID_SIZE]; }; Grid gridA, gridB; void Init() { //Initialize grids to 0 (dead) //(1 is alive) for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { gridA.grid[i][j] = 0; gridB.grid[i][j] = 0; } } //Now give a few values to start off //grid[row][col] gridB.grid[20][15] = 1; gridB.grid[20][16] = 1; gridB.grid[21][16] = 1; gridB.grid[19][16] = 1; gridB.grid[19][17] = 1; } //Set the grid to '0' (dead) void ResetGrid(Grid &g) { for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { g.grid[i][j] = 0; } } } //Copy 'in' into 'out' void CopyGrid(Grid &in, Grid &out) { for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { out.grid[i][j] = in.grid[i][j]; } } } //Update looking at gridA and //save the info in the gridB void Update() { int aliveCnt = 0; CopyGrid(gridB, gridA); ResetGrid(gridB); for (int row = 0; row < GRID_SIZE; row++) { for (int col = 0; col < GRID_SIZE; col++) { //////////////////////////////////////////// aliveCnt = 0; //////////////////////////////////////////// //top left if (row != 0 && col != 0) { if (gridA.grid[row-1][col-1] == 1) { aliveCnt++; } } //top if (row != 0) { if (gridA.grid[row-1][col] == 1) { aliveCnt++; } } //Top-right if (row != 0 && col != GRID_SIZE) { if (gridA.grid[row - 1][col +1] == 1) { aliveCnt++; } } //Left if (col != 0) { if (gridA.grid[row][col - 1] == 1) { aliveCnt++; } } //Right if (col != GRID_SIZE) { if (gridA.grid[row][col + 1] == 1) { aliveCnt++; } } //Bottom left if (row != GRID_SIZE && col != 0) { if (gridA.grid[row+1][col-1] == 1) { aliveCnt++; } } //Bottom if (row != GRID_SIZE) { if (gridA.grid[row + 1][col] == 1) { aliveCnt++; } } //Bottom left if (row != GRID_SIZE && col != GRID_SIZE) { if (gridA.grid[row + 1][col + 1] == 1) { aliveCnt++; } } /////////////////////////////////////////// //Decide if it is alive or no if (gridA.grid[row][col]==1 && aliveCnt < 2) { gridB.grid[row][col] = 0; } if (gridA.grid[row][col] == 1 && (aliveCnt == 2 || aliveCnt == 3)) { gridB.grid[row][col] = 1; } if (gridA.grid[row][col] == 1 && aliveCnt > 3) { gridB.grid[row][col] = 0; } if (gridA.grid[row][col] == 0 && aliveCnt == 3) { gridB.grid[row][col] = 1; } /////////////////////////////////////////// } } } //Draw the grid using ASCII void Draw() { for (int i = 0; i < GRID_SIZE; i++) { for (int j = 0; j < GRID_SIZE; j++) { if (gridB.grid[i][j] == 1){ printf("%c", 254); } else{ printf("%c", 176); } } std::cout<<"\n"; } } int main() { int turn = 0; Init(); while (true) { turn++; /////////// Update(); Draw(); /////////// std::cout << "\nTurn:" << turn << "\n"; Sleep(400); system("cls"); } return 0; }