コード例 #1
0
ファイル: GameOfLife.cs プロジェクト: kevPo/GameOfLife
 public GameOfLife(String gameInput, GameCriteria criteria, 
                   ITranslator<GameData> translator, BoardFactory boardFactory)
 {
     this.criteria = criteria;
     var gameData = translator.Translate(gameInput);
     board = boardFactory.GetBoard(gameData);
 }
コード例 #2
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldCellExistWhenWasAdded()
        {
            Board b = new Board();

            b.AddCell(1, 1);

            Assert.IsTrue(b.IsCellExist(1, 1));
        }
コード例 #3
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldCellNoneExistWhenWasRemoved()
        {
            Board b = new Board();

            b.AddCell(1, 1);
            b.RemoveCell(1, 1);

            Assert.IsFalse(b.IsCellExist(1, 1));
        }
コード例 #4
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void CountCellNeighbours()
        {
            Board b = new Board();

            b.AddCell(2, 2);
            b.AddCell(3, 2);

            Assert.AreEqual(2, b.CountNeighbours(3, 3));
        }
コード例 #5
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldCellDieWhenIsLonely()
        {
            Board b = new Board();

            b.AddCell(1,1);
            b.NextState();

            Assert.IsFalse(b.IsCellExist(1,1));
        }
コード例 #6
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void CountCells()
        {
            Board b = new Board();

            b.AddCell(-1, 34);
            b.AddCell(2, 2);
            b.AddCell(3, 2);
            b.AddCell(23, -2);

            Assert.AreEqual(4, b.CountCells());
        }
コード例 #7
0
ファイル: Main.cs プロジェクト: keelerm84/game-of-life-csharp
        public static void Main(string[] args)
        {
            Board board = new Board(int.Parse(args[0]), int.Parse(args[1]));

              for( int i = 0; i < int.Parse(args[2]); i++ )
              {
            board.print();
            board.evolve();
            System.Threading.Thread.Sleep(int.Parse(args[3]));
            Console.Clear();
              }
        }
コード例 #8
0
ファイル: Game.cs プロジェクト: BasiaFusinska/GameOfLife
        public void Step()
        {
            var points = new List<Point>();

            _board.ForeachCell((i, j) =>
                {
                    if(ApplyRule(i, j))
                        points.Add(new Point(i, j));
                });

            _board = new Board(points, _board.Length, _board.Width);
        }
コード例 #9
0
ファイル: Form1.cs プロジェクト: ozelentok/OzGameOfLife
 public Form1()
 {
     InitializeComponent();
     game = new Board();
     br = new SolidBrush(Color.Black);
     p = new Pen(br);
     generation = 1;
     clock = new System.Timers.Timer(1000);
     clock.Elapsed += new ElapsedEventHandler(Advance);
     zero = new Point(margin, margin);
     xLimit = new Point(margin + game.Size * squareSize, margin);
     yLimit = new Point(margin, margin + game.Size * squareSize);
     limit = new Point(margin + game.Size * squareSize, margin + game.Size * squareSize);
 }
コード例 #10
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
 public void ChangeStatusTest()
 {
     uint size = 8; // Initialize to an appropriate size
       Board target = new Board(size);
       Board.BoardEdit[] edits = {
                           new Board.BoardEdit(1, 1, 1), // should change state
                           new Board.BoardEdit(1, 2, 0), // should change state
                           new Board.BoardEdit(2, 3, 3), // should not change state
                           new Board.BoardEdit(3, 1, null), // should not change state
                           new Board.BoardEdit(9, 6, 4), // out of range
                         };
       target.ChangeStatus(edits);
       Assert.AreEqual(1, target[1, 1]);
       Assert.AreEqual(0, target[1, 2]);
       Assert.AreEqual(null, target[2, 3]);
       Assert.AreEqual(null, target[3, 1]);
       // The last "test" should not throw an exception, but simply do nothing
 }
コード例 #11
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
        public void GenerateRandomBoardTest()
        {
            uint size = 20; // Initialize to an appropriate size
              Board target = new Board(size);
              target.GenerateRandomBoard();

              bool n = false;
              bool l = false;
              bool d = false;
              bool fail = false;
              for (uint i = 0; i < size; i++) {
            for (uint j = 0; j < size; j++) {
              switch (target[i, j]) {
            case null:
              n = true;
              break;
            case 0:
              d = true;
              break;
            case 1:
              l = true;
              break;
            default:
              fail = true;
              break;
              }
              if (fail) break;
              if (n && l && d) break;
            }
              }
              Assert.IsTrue(n && l && d); // all posible values have been reached
              Assert.IsFalse(fail); // no imposible values have been reached
        }
コード例 #12
0
ファイル: Game.cs プロジェクト: BasiaFusinska/GameOfLife
 public Game(IEnumerable<Point> points, int length, int width)
 {
     _board = new Board(points, length, width);
 }
コード例 #13
0
        static void Main(string[] args)
        {
            Board<bool, int, int> board = new Board<bool, int, int>();
            Board<bool, int, int> nextGeneration = new Board<bool, int, int>();

            //Board<bool, int, int> noDupesOnBoard = new Board<bool, int, int>();

            //take user input
            string userInput;
            Console.Write("Enter 'x,y' coordinates for a living cell:");
            userInput = Console.ReadLine();

            //convert user input and add to Board

            int xcoord;
            int ycoord;

            string[] xyString = userInput.Split(',');
            int[] convertedxyString = Array.ConvertAll<string, int>(xyString, int.Parse);

            xcoord = convertedxyString[0];
            ycoord = convertedxyString[1];

            Console.WriteLine("x: {0}, y: {1}", xcoord, ycoord);

            //add cell to board
            board.Add(true, xcoord, ycoord);

            //add more cells to board
            do
            {
                Console.Write("Enter 'x,y' coordinates for another living cell or enter 'start':");
                userInput = Console.ReadLine();

                if (userInput == "start")
                {
                    break;
                }

                //convert user input and add to Board
                xyString = userInput.Split(',');
                convertedxyString = Array.ConvertAll<string, int>(xyString, int.Parse);
                xcoord = convertedxyString[0];
                ycoord = convertedxyString[1];

                //Console.WriteLine("x: {0}, y: {1}", xcoord, ycoord);

                board.Add(true, xcoord, ycoord);
                //Console.WriteLine("board in loop: {0}", board);

            } while (userInput != "start");

            foreach (var item in board)
            {
                //Console.WriteLine(item);
            }

            //remove duplicates
            IEnumerable<Tuple<bool, int, int>> noDupesOnBoard = board.Distinct();

            //Console.WriteLine(noDupesOnBoard);

            foreach (var item in noDupesOnBoard)
            {
                //Console.WriteLine(item);
            }

            Board<bool, int, int> transGen = new Board<bool, int, int>();
            //add eight neighboors for each distint cell on initial board set up
            foreach (var item in noDupesOnBoard)
            {
                transGen.Add(false, item.Item2 + 0, item.Item3 + 1);
                transGen.Add(false, item.Item2 + 1, item.Item3 + 1);
                transGen.Add(false, item.Item2 + 1, item.Item3 + 0);
                transGen.Add(false, item.Item2 + 1, item.Item3 - 1);
                transGen.Add(false, item.Item2 - 0, item.Item3 - 1);
                transGen.Add(false, item.Item2 - 1, item.Item3 - 1);
                transGen.Add(false, item.Item2 - 1, item.Item3 + 0);
                transGen.Add(false, item.Item2 - 1, item.Item3 + 1);
            }

            int addedNCount = 0;
            foreach (var item in transGen)
            {
                addedNCount++;
                //Console.WriteLine("{1}: {0}",item, addedNCount);

            }

            //combine initial set up with neighbors
            IEnumerable<Tuple<bool, int, int>> boardOfTheLivingAndDead = board.Concat(transGen);
            int boardOfTheLivingAndDeadNumber = 0;
            foreach (var item in boardOfTheLivingAndDead)
            {
                boardOfTheLivingAndDeadNumber++;
                //Console.WriteLine("{0}: {1}", boardOfTheLivingAndDeadNumber, item);
            }

            //count how many time each cell in in the list, this is the count of adjacent living cells used for birth
            var groups = boardOfTheLivingAndDead.GroupBy(cell => cell);
            foreach (var item in groups)
            {
                //Console.WriteLine("{0} occurs {1} times, if 3 and false: birth!",item.Key, item.Count());
                if (item.Count() == 3 && item.Key.Item1 == false)
                {
                    nextGeneration.Add(item.Key);
                }

            }

            //find living cells that will survive to next generation
            // item1 == true && 2 or 3 neighbors with item1 == true
            // look at initial board set up (board)
            foreach (var item in noDupesOnBoard)
            {
                int numberOfLiveNeighbors = 0;
                // create local neighbor list
                Board<bool, int, int> localNeighbors = new Board<bool, int, int>();
                localNeighbors.Add(true, item.Item2 + 0, item.Item3 + 1);
                localNeighbors.Add(true, item.Item2 + 1, item.Item3 + 1);
                localNeighbors.Add(true, item.Item2 + 1, item.Item3 + 0);
                localNeighbors.Add(true, item.Item2 + 1, item.Item3 - 1);
                localNeighbors.Add(true, item.Item2 - 0, item.Item3 - 1);
                localNeighbors.Add(true, item.Item2 - 1, item.Item3 - 1);
                localNeighbors.Add(true, item.Item2 - 1, item.Item3 + 0);
                localNeighbors.Add(true, item.Item2 - 1, item.Item3 + 1);

                foreach (var liveCell in noDupesOnBoard)
                {
                    if (localNeighbors.Contains(liveCell))
                    {
                        numberOfLiveNeighbors++;
                    }
                }

                if (numberOfLiveNeighbors == 2 || numberOfLiveNeighbors == 3)
                {
                    //Console.WriteLine("the cell {0} survives with {1} neighbors", item, numberOfLiveNeighbors);
                    nextGeneration.Add(item);
                }
            }

            Console.WriteLine("These are the living in the next generation:");
            foreach (var item in nextGeneration)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
コード例 #14
0
ファイル: Game1.cs プロジェクト: jxl31/GameOfLife
        //when the player wants the random board
        //changes gamestate to RANDOM and making other boards null
        public void selectedRandom()
        {
            randomBoard = new RandomBoard();
            gamestate = GAMESTATE.RANDOM;

            menuScreen = null;
            board = null;
            coolBoard = null;
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: partition/Game-of-Life
        static void Main(string[] args)
        {
            // new object of game
            Board b = new Board();

            // set your own size of board
            b.AreaSize = 7;

            // Set cells coordinates here

            // toad vertical
            // http://en.wikipedia.org/wiki/File:Game_of_life_toad.gif
            b.AddCell(1, 1);
            b.AddCell(1, 2);
            b.AddCell(1, 3);
            b.AddCell(2, 2);
            b.AddCell(2, 3);
            b.AddCell(2, 4);

            // tub
            // http://pl.wikipedia.org/wiki/Plik:JdlV_bloc_4.9.gif
            b.AddCell(-5, 0);
            b.AddCell(-5, 2);
            b.AddCell(-6, 1);
            b.AddCell(-4, 1);

            // glider
            // http://en.wikipedia.org/wiki/File:Game_of_life_animated_glider.gif
            b.AddCell(-18, -5);
            b.AddCell(-18, -4);
            b.AddCell(-18, -3);
            b.AddCell(-19, -3);
            b.AddCell(-20, -4);

            // blinker
            // http://en.wikipedia.org/wiki/File:Game_of_life_blinker.gif
            b.AddCell(10, 0);
            b.AddCell(11, 0);
            b.AddCell(12, 0);

            // blinker
            b.AddCell(-10, 5);
            b.AddCell(-10, 6);
            b.AddCell(-10, 7);

            // some random cells
            Random random = new Random();
            int randomX, randomY;

            // 50 random cells
            for (int i = 0; i < 50; i++)
            {
                randomX = random.Next(-10, 20);
                randomY = random.Next(-10, 20);

                b.AddCell(randomX, randomY);
            }

            //// display board
            //b.PrintCurrentBoard();

            //// display coordinates of cells
            //b.PrintCurrentStateCoordinates();

            //// calculate next state of game
            //b.NextState();

            //// and print board :)
            //b.PrintCurrentBoard();

            //// pause befor start game
            //Thread.Sleep(2000);

            // run game in infinity loop
            b.PlayGame(timeSleep: 250, displayCurrentCoordinates: false);

            // shows amount of cells and recursion at step
            //for (int i = 0; i < 100; i++)
            //{
            //    b.NextState();
            //    Console.WriteLine("Step " + i + ", Cells on board: " + b.CountCells() + ", Recursion: " + b.CounterMethod_GiveLifeToNeighboursIfPossible);
            //}

            Console.ReadKey();
        }
コード例 #16
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
 public void BoardConstructorTest200()
 {
     uint size = 200; // Initialize board with an appropriate value
       Board target = new Board(size);
       Assert.AreEqual(size, target.Size);
 }
コード例 #17
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
        public void NextDayTestZombie()
        {
            uint testrun = 10;
              uint size = 2; //  Initialize to an appropriate size
              Board target = new Board(size);
              bool zombie = false;

              for (int i = 0; i < testrun; i++) {
            // Set board
            target.ChangeStatus(smallBoard);

            // Next day
            target.NextDay();

            // Check
            if (target[0, 0] == null || target[1, 0] == null || target[0, 1] == null) {
              zombie = true; break;
            }
              }
              Assert.AreEqual(true, target[0, 0] == null || target[0, 0] == 1);
              Assert.AreEqual(true, target[0, 1] == null || target[0, 1] == 1);
              Assert.AreEqual(true, target[1, 0] == null || target[1, 0] == 1);
              Assert.AreEqual(null, target[1, 1]);
              Assert.IsTrue(zombie);
        }
コード例 #18
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
        public void ItemTest()
        {
            uint size = 2; // Initialize to an appropriate size
              Board target = new Board(size);
              target.ChangeStatus(smallBoard);

              Assert.AreEqual(1, target[0, 0]);
              Assert.AreEqual(1, target[0, 1]);
              Assert.AreEqual(1, target[1, 0]);
              Assert.AreEqual(null, target[1, 1]);
        }
コード例 #19
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldLivingCellStillLiveWhenHasTwoOrThreeNeighbours()
        {
            // test for two neighbours
            Board b = new Board();

            b.AddCell(2, 2);
            b.AddCell(3, 2);
            b.AddCell(2, 3);
            b.NextState();

            Assert.IsTrue(b.IsCellExist(2, 2));

            // test for three neighbours
            b = new Board();

            b.AddCell(2, 2);
            b.AddCell(3, 2);
            b.AddCell(2, 3);
            b.AddCell(3, 3);
            b.NextState();

            Assert.IsTrue(b.IsCellExist(2, 2));
        }
コード例 #20
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldOneCellExistWhenAddedTwoTimesTheSameCell()
        {
            Board b = new Board();

            b.AddCell(1, 1);
            b.AddCell(1, 1);

            Assert.AreEqual(1, b.CountCells());
        }
コード例 #21
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
        public void NextDayTest()
        {
            uint size = 4; //  Initialize to an appropriate size
              Board target = new Board(size);

              // Set board
              target.ChangeStatus(bigBoard);

              // Next day
              target.NextDay();

              // Check
              Assert.AreEqual(1, target[0, 0]);
              Assert.AreEqual(1, target[0, 1]);
              Assert.AreEqual(0, target[0, 2]);
              Assert.AreEqual(null, target[0, 3]);
              Assert.AreEqual(1, target[1, 0]);
              Assert.AreEqual(0, target[1, 1]);
              Assert.AreEqual(0, target[1, 2]);
              Assert.AreEqual(0, target[1, 3]);
              Assert.AreEqual(0, target[2, 0]);
              Assert.AreEqual(0, target[2, 1]);
              Assert.AreEqual(0, target[2, 2]);
              Assert.AreEqual(0, target[2, 3]);
              Assert.AreEqual(0, target[3, 0]);
              Assert.AreEqual(0, target[3, 1]);
              Assert.AreEqual(0, target[3, 2]);
              Assert.AreEqual(null, target[3, 3]);
        }
コード例 #22
0
ファイル: Game1.cs プロジェクト: jxl31/GameOfLife
        //when the player wants the cool board
        //changes gamestate to COOL and making other boards null
        public void selectedCool()
        {
            coolBoard = new CoolBoard();
            gamestate = GAMESTATE.COOL;

            menuScreen = null;
            board = null;
            randomBoard = null;
        }
コード例 #23
0
ファイル: BoardTest.cs プロジェクト: ButterFree/Archive
 public void SizeTest()
 {
     uint size = 5; // Initialize to an appropriate size
       Board target = new Board(size);
       Assert.AreEqual(size, target.Size);
 }
コード例 #24
0
ファイル: Game1.cs プロジェクト: jxl31/GameOfLife
        //when the player wants the clear board
        //changes gamestate to CLEAR and making other boards null
        public void selectedBoard()
        {
            board = new Board();
            gamestate = GAMESTATE.CLEAR;

            menuScreen = null;
            randomBoard = null;
            coolBoard = null;
        }
コード例 #25
0
ファイル: UnitTests.cs プロジェクト: partition/Game-of-Life
        public void ShouldLivingCellDieWhenHasLessThanTwoNeighbours()
        {
            Board b = new Board();

            b.AddCell(2, 2);
            b.AddCell(3, 2);
            b.NextState();

            Assert.IsFalse(b.IsCellExist(2, 2));
        }
コード例 #26
0
ファイル: Game1.cs プロジェクト: jxl31/GameOfLife
 //when player wants to exit the game
 //makes everything null and changes gamestate to QUIT
 public void selectedQuit()
 {
     gamestate = GAMESTATE.QUIT;
     menuScreen = null;
     randomBoard = null;
     board = null;
     coolBoard = null;
 }