Skip to content

Instantly share code, notes, and snippets.

@pshomov
Created June 24, 2013 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pshomov/5849143 to your computer and use it in GitHub Desktop.
Save pshomov/5849143 to your computer and use it in GitHub Desktop.
Harri Darri Game of Life implementation
// HarrisGameOfLife.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
class HarWay
{
public:
// use: HarWay h(size)
// pre: dimension > 0
// post: h is a new game of life, h.Size() == 'size'
HarWay(int size): m_Size(size), m_State(size * size, false)
{ }
// use: size = h.Size()
// post: 'size' is the number of rows and columns in 'h'
int Size() const
{ return m_Size; }
// use: h.SetLife(x, y, l)
// pre: 0 <= 'x', 'y' < h.Size()
// post: h.IsLife(x, y) == 'l'
void SetLife(int x, int y, bool l)
{ m_State[(x * m_Size) + y] = l; }
// use: l = IsLife(x, y)
// pre: 0 <= 'x', 'y' < h.Size()
// post: 'l' is true iff the cell at in column 'x' and row 'y' of 'h' is alive
bool IsLife(int x, int y) const
{ return m_State[(x * m_Size) + y]; }
// use: h.Evolve()
// post: h' is the state of 'h' after one round of evolution
void Evolve();
private:
std::vector<bool> m_State; // mState[x * m_Size + y] iff cell at row 'x' and col 'y' is life
int m_Size; // m_State.size() == m_Size * m_Size
// return 1 if within bounds and life, 0 otherwise
int LC(int x, int y) const
{
return (x < 0 || x >= m_Size || y < 0 || y >= m_Size) ?
0 : (int)m_State[(x * m_Size) + y];
}
// h.CountLiveNeighbors(counts)
// pre: counts.size() == m_Size * m_Size
// post: counts[_x_ * m_Size + _y_] is the number of life neighbors of cell at row _x_ and col _y_,
// for 0 < 'x', 'y' <= m_Size
void CountLiveNeighbors(std::vector<int>& counts) const
{
for (int x = 0; x < m_Size; x++)
for (int y = 0; y < m_Size; y++)
counts[(x * m_Size) + y] =
LC(x-1, y-1) + LC(x, y-1) + LC(x+1, y-1) +
LC(x-1, y) + LC(x, y) + LC(x+1, y) +
LC(x-1, y+1) + LC(x, y+1) + LC(x+1, y+1);
}
void UpdateGame(const std::vector<int> counts)
{
// for each cell
for (int x = 0; x < m_Size; x++)
for (int y = 0; y < m_Size; y++)
{
int nl = counts[(x * m_Size) + y];
bool life = m_State[(x * m_Size) + y];
if (life)
if (nl < 2 || nl > 3)
m_State[(x * m_Size) + y] = false; // fewer than two or more than three neighbors, die
else if (nl == 3)
m_State[(x * m_Size) + y] = true; // three neighbors, spring to life
}
}
};
void HarWay::Evolve()
{
std::vector<int> counts(m_Size * m_Size);
CountLiveNeighbors(counts);
UpdateGame(counts);
}
void PrintGame(const HarWay& h)
{
for (int x = 0; x < h.Size(); x++)
{
for (int y = 0; y < h.Size(); y++)
std::cout << h.IsLife(x, y) ? "*" : "-";
std::cout << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
HarWay h(4);
h.SetLife(1, 1, true);
h.SetLife(1, 2, true);
h.SetLife(1, 3, true);
h.SetLife(2, 2, true);
h.SetLife(3, 0, true);
h.Evolve();
PrintGame(h);
int x;
std::cin >> x;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment