Advertisement
Guest User

Untitled

a guest
May 4th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.IO.Pipes;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Security.Cryptography.X509Certificates;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using NUnit.Framework;
  12. using System.Drawing;
  13. namespace CodeRetreatConwaysGameLife
  14. {
  15.     class Game
  16.     {
  17.         private readonly ImmutableHashSet<Point> aliveCells;
  18.        
  19.         public Game()
  20.             : this(ImmutableHashSet<Point>.Empty)
  21.         {
  22.         }
  23.  
  24.         private Game(ImmutableHashSet<Point> aliveCells)
  25.         {
  26.             this.aliveCells = aliveCells;
  27.         }
  28.  
  29.         public static Game Stick()
  30.         {
  31.             return new Game()
  32.                 .SetCellAlive(2, 2)
  33.                 .SetCellAlive(2, 3)
  34.                 .SetCellAlive(2, 4)
  35.                 ;
  36.         }
  37.  
  38.  
  39.         private Game SetCellAlive(int x, int y)
  40.         {
  41.             return SetCellAlive(new Point(x, y));
  42.         }
  43.  
  44.         public static IEnumerable<Point> GetNeighbours(Point cell)
  45.         {
  46.             return
  47.                 from dx in new int[] { -1, 0, 1 }
  48.                 from dy in new int[] { -1, 0, 1 }
  49.                 where Math.Abs(dx) + Math.Abs(dy) != 0
  50.                 select new Point(cell.X + dx, cell.Y + dy);
  51.         }
  52.  
  53.         public IEnumerable<Point> GetAliveNeighbours(Point cell)
  54.         {
  55.             return GetNeighbours(cell).Where(IsAlive);
  56.         }
  57.  
  58.         public bool IsAlive(Point cell)
  59.         {
  60.             return aliveCells.Contains(cell);
  61.         }
  62.  
  63.         public Game SetCellAlive(Point cell)
  64.         {
  65.             return new Game(aliveCells.Add(cell));
  66.         }
  67.  
  68.         public Game NextStep()
  69.         {
  70.             return new Game(GetNextGeneration());
  71.         }
  72.  
  73.         private ImmutableHashSet<Point> GetNextGeneration()
  74.         {
  75.             return aliveCells.SelectMany(GetNeighbours)
  76.                 .Where(WillBeAlive)
  77.                 .ToImmutableHashSet();
  78.         }
  79.  
  80.         private bool WillBeAlive(Point cell)
  81.         {
  82.             var count = GetAliveNeighbours(cell).Count();
  83.             return (IsAlive(cell) && count == 2) || count == 3;
  84.         }
  85.  
  86.         public override string ToString()
  87.         {
  88.             return String.Join("\n",
  89.                
  90.                 Enumerable.Range(0, 10)
  91.                 .Select(y =>
  92.                     String.Join("", Enumerable.Range(0, 10)
  93.                         .Select(x => IsAlive(new Point(x, y)) ? "*" : " ")
  94.                         )));
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement