Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace GameOfLife.CellClasses
  7. {
  8.     class CellGrid
  9.     {
  10.         public readonly int Height;
  11.         public readonly int Width;
  12.         Cell[,] cells;
  13.  
  14.         public CellGrid(int height, int width)
  15.         {
  16.             this.Height = height;
  17.             this.Width = width;
  18.             cells = new Cell[Width, Height];
  19.         }
  20.  
  21.         public Cell this[int x, int y]
  22.         {
  23.             get { return cells[x, y]; }
  24.             set { cells[x, y] = value; }
  25.         }
  26.  
  27.         public void PassGeneration(RuleSet Rule)
  28.         {
  29.             RefreshFriends();
  30.             switch (Rule)
  31.             {
  32.                 case RuleSet.Conway:
  33.                     {
  34.                         Conway();
  35.                         break;
  36.                     }
  37.             }
  38.         }
  39.        
  40.         public void RefreshFriends()
  41.         {
  42.             for (int x = 0; x < this.Width; x++)
  43.                 for (int y = 0; y < this.Width; y++)
  44.                 {
  45.                     cells[x, y].Neighbors = 0;
  46.                     for (int i = -1; i < 2; i++)
  47.                         for (int j = -1; j < 2; j++)
  48.                             if (!(i == 0 && j == 0) && IsValidCoordinates(x + i, y + j) && cells[x + i, y + j].Alive)
  49.                                 cells[x, y].Neighbors++;
  50.                 }
  51.         }
  52.  
  53.         public bool IsValidCoordinates(int x, int y)
  54.         {
  55.             return (x >= 0 &&
  56.                     x < Width &&
  57.                     y >= 0 &&
  58.                     y < Height);
  59.         }
  60.         #region Generation Functions
  61.         private void Conway()
  62.         {
  63.             for (int x = 0; x < Width; x++)
  64.             {
  65.                 for (int y = 0; y < Height; y++)
  66.                 {
  67.                     if (cells[x, y].Alive)
  68.                     {
  69.                         cells[x, y].Alive = (cells[x, y].Neighbors > 1 && cells[x, y].Neighbors < 4);
  70.                         continue;
  71.                     }
  72.                     cells[x, y].Alive = (cells[x, y].Neighbors == 3);
  73.                 }
  74.             }
  75.         }
  76.         #endregion
  77.         public void GenerateNegetive()
  78.         {
  79.             for (int x = 0; x < this.Width; x++)
  80.                 for (int y = 0; y < this.Height; y++)
  81.                     cells[x, y].Alive = !cells[x, y].Alive;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement