IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » lijamez's snipts » game The latest game snipts from lijamez.

showing 1-1 of 1 snipts for game
  • Conway's Game of Life in C++
    #include <iostream>
    using namespace std;
    
    //The Grid class of the game of life. The grid is a 2D array.
    class Grid {
    	int width, height;
    	bool grid [1000][1000];
    public:
    	Grid(int, int);
    	void nextFrame();
    	void printFrame();
    };
    
    Grid::Grid(int a, int b) {
    	height = a;
    	width = b;
    	
    	//Presets of alive cells. Change these to whatever you wish.
    	grid[0][1] = true;
    	grid[1][2] = true;
    	grid[2][0] = true;
    	grid[2][1] = true;
    	grid[2][2] = true;
    	
    }
    
    /**
     Checks how many surrounding cells are alive for a cell and changes it depending on the game of life rules.
     The changes are stored in a temporary 2D array. Then, it is copied to the main grid array. 
    **/
    void Grid::nextFrame() {
    	
    	int numSurrounding = 0;
    	bool tempGrid [height][width];
    	
    	for (int i = 0; i < height ; i++)
    	{
    		for (int j = 0; j < width ; j++)
    		{
    			if ( (i+1) < height && grid[i + 1][j] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (i-1) >= 0 && grid[i - 1][j] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (j+1) < width && grid[i][j+1] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (j-1) >= 0 && grid[i][j-1] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (i+1) < height && (j+1) < width && grid[i+1][j+1] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (i+1) < height && (j-1) >= 0 && grid[i+1][j-1] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (i-1) >= 0 && (j+1) < width && grid[i-1][j+1] == true )
    			{
    				numSurrounding++;
    			}
    			if ( (i-1) >= 0 && (j-1) >= 0 && grid[i-1][j-1] == true )
    			{
    				numSurrounding++;
    			}
    			
    			if (numSurrounding < 2 || numSurrounding > 3)
    			{
    				tempGrid[i][j] = false;
    			}
    			else if (numSurrounding == 2)
    			{
    				tempGrid[i][j] = grid[i][j];
    			}
    			else if (numSurrounding == 3)
    			{
    				tempGrid[i][j] = true;
    			}
    			
    			numSurrounding = 0;
    			
    		}
    	}
    	
    	for (int i = 0 ; i < height ; i++ )
    	{
    		for (int j = 0 ; j < width ; j++ )
    		{
    			grid[i][j] = tempGrid[i][j];
    		}
    	}
    }
    
    //Prints the grid into the console.
    void Grid::printFrame() {
    	for (int i = 0; i < height ; i++ )
    	{
    		for (int j = 0 ; j < width ; j++ )
    		{
    			if ( grid[i][j] == true )
    			{
    				cout << "x";
    			}
    			else
    			{
    				cout << " ";
    			}
    			
    			if ( (j + 1) >= width )
    			{
    				cout << endl;
    			}
    		}
    	}
    }
    
    
    int main () {	
    	int inputHeight, inputWidth;
    	string kbd;
    
    	cout << "The Game of Life in C++ by James Li\n\n" ;
    	cout << "How many rows? (up to 1000)" << endl;
    	cin >> inputHeight;
    	cout << "How many columns? (up to 1000)" << endl;
    	cin >> inputWidth;
    	
    	Grid life(inputHeight, inputWidth);
    	life.printFrame();
    	cout << endl;
    	
    	do {
    		kbd = "";
    		cout << "Enter \"y\" for next cycle, anything else to quit... ";
    		cin >> kbd;
    		cout << endl;
    		
    		life.nextFrame();
    		life.printFrame();
    		cout << endl;
    	} while (kbd == "y");
    	
    	cout << "Goodbye." << endl;
    	
    	return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by lijamez on Apr 25, 2009 at 7:45 p.m. EDT
Sign up to create your own snipts, or login.