Exemplo n.º 1
0
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 static void Main(string[] args)
 {
     using (LifeGame game = new LifeGame())
     {
         game.Run();
     }
 }
Exemplo n.º 2
0
 public LifeGameView()
 {
     m_lifeGameCore = new LifeGame();
     m_rootLives    = new Dictionary <RoomPoint, ICollection <Unit> >();
     Size           = new Size(DEFAULT_PICTURE_WIDTH, DEFAULT_PICTURE_HEIGHT);
     m_lifeGameCore.WorldView.Update += (sender, e) => ShowNowLifeGameStatus();
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var input    = @"#.#..
.#.#.
#...#
.#..#
##.#.";
            var lifeGame = new LifeGame(input);
            var dico     = new Dictionary <int, int>();

            while (true)
            {
                lifeGame.Iterate();
                var rating = lifeGame.BioDiversityRating;
                if (dico.TryGetValue(rating, out var counter))
                {
                    Console.WriteLine($"Result = {rating}");
                    break;
                }
                else
                {
                    dico[rating] = 1;
                }
            }

            var lifeGame1 = new LifeGame(input);

            for (int i = 0; i < 200; i++)
            {
                lifeGame1.Iterate2();
            }
            lifeGame1.Dump();
            Console.WriteLine($"Bug Count={lifeGame1.BugCount}");
        }
Exemplo n.º 4
0
 public void Initialize(int x, int y, LifeGame lifeGame)
 {
     this.x            = x;
     this.y            = y;
     this.lifeGame     = lifeGame;
     this.currentState = States.Dead;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Resets the grid.
        /// </summary>
        private void ResetGame()
        {
            // stop the game from performing any more calculations
            _isGameRunning = false;

            // reference's the master game grid
            var gameGrid = this.GameGrid;

            // reset all contents of the grid
            gameGrid.Children.Clear();

            // get the row and column count for the grid
            var rowCount    = (int)gameGrid.ActualHeight / 20;
            var columnCount = (int)gameGrid.ActualWidth / 20;

            // initialise a new game instance
            _game = new LifeGame(columnCount, rowCount);

            // reset current generation
            _currentGeneration = 0;

            // initialise grid
            InitialiseGrid(rowCount, columnCount);

            // reset generation tag
            this.GenerationTag.Text = "Generation 0";

            // enable start button again
            // disable restart button
            this.StartBtn.IsEnabled = true;
            this.ResetBtn.IsEnabled = false;
        }
Exemplo n.º 6
0
        public void TestBioDiversityRate()
        {
            var input    = @".....
.....
.....
#....
.#...";
            var lifeGame = new LifeGame(input);

            Assert.Equal(2129920, lifeGame.BioDiversityRating);
        }
Exemplo n.º 7
0
        private void ExecuteTest(bool[,] inputArray, bool[,] expectedArray, int rounds = 1)
        {
            var   board    = new BoardArrayImp(inputArray);
            var   lifeGame = new LifeGame(board);
            Board newBoard = null;

            for (int i = 0; i < rounds; i++)
            {
                newBoard = lifeGame.NextRound();
            }
            new ArrayableBoardDecorator(newBoard).GetArray().ShouldBeEquivalentTo(expectedArray);
        }
Exemplo n.º 8
0
        public void SurroundTest()
        {
            LifeGame test = new LifeGame(25);

            int[] cells = test.SurrondingCells(22); //Bottom square test
            Assert.Equal(23, cells[1]);             // Right
            Assert.Equal(21, cells[0]);             // left
            Assert.Equal(17, cells[2]);             // top
            Assert.Equal(2, cells[5]);              // bottom
            Assert.Equal(16, cells[3]);             // top left
            Assert.Equal(18, cells[4]);             // top right
            Assert.Equal(1, cells[6]);              // bottom left
            Assert.Equal(3, cells[7]);              // bottom right
        }
Exemplo n.º 9
0
        public void SurroundTestTopRightCorner()
        {
            LifeGame test = new LifeGame(25);

            int[] cells = test.SurrondingCells(4); //Top left corner test
            Assert.Equal(0, cells[1]);             // Right
            Assert.Equal(3, cells[0]);             // left
            Assert.Equal(24, cells[2]);            // top
            Assert.Equal(9, cells[5]);             // bottom
            Assert.Equal(23, cells[3]);            // top left
            Assert.Equal(20, cells[4]);            // top right
            Assert.Equal(8, cells[6]);             // bottom left
            Assert.Equal(5, cells[7]);             // bottom right
        }
Exemplo n.º 10
0
        public void SurroundTestBottomLeftCorner()
        {
            LifeGame test = new LifeGame(36);

            int[] cells = test.SurrondingCells(30); //bottom left corner test
            Assert.Equal(31, cells[1]);             // Right
            Assert.Equal(35, cells[0]);             // left
            Assert.Equal(24, cells[2]);             // top
            Assert.Equal(0, cells[5]);              // bottom
            Assert.Equal(29, cells[3]);             // top left
            Assert.Equal(25, cells[4]);             // top right
            Assert.Equal(5, cells[6]);              // bottom left
            Assert.Equal(1, cells[7]);              // bottom right
        }
Exemplo n.º 11
0
        public void TestIterate()
        {
            var input    = @"....#
#..#.
#.?##
..#..
#....";
            var lifeGame = new LifeGame(input);

            for (int i = 0; i < 10; i++)
            {
                lifeGame.Iterate2();
            }
            Assert.Equal(99, lifeGame.BugCount);
        }
Exemplo n.º 12
0
    public void Instantiate(int x, int y)
    {
        state = 0;
        coordinate.Set(x, y);
        lifeGame  = GameObject.Find("GameBoad").GetComponent <LifeGame>();
        GRID_SIZE = lifeGame.GRID_SIZE;
        CELL_SIZE = lifeGame.CELL_SIZE;

        animator     = GetComponent <Animator>();
        anim         = GetComponent <Animation>();
        audio_source = GetComponent <AudioSource>();

        default_position = new Vector2(x, y);
        default_scale    = this.transform.localScale;
        damaged          = 0;
    }
Exemplo n.º 13
0
    private int ReturnNumNeighbor()
    {
        LifeGame game = lifeGame.GetComponent <LifeGame> ();

        Life[,,] lifeBoard = game.lifeBoard;
        int gameX           = game.X;
        int gameY           = game.Y;
        int gameZ           = game.Z;
        int numLiveNeighbor = 0;

        // Check currentState of lives at position (this.x-1, this.y-1), (this.x-1, this.y), etc
        for (int i = -1; i <= 1; i++)
        {
            // Check for index out of bounds
            if (x + i < 0 || x + i >= gameX)
            {
                continue;
            }
            for (int j = -1; j <= 1; j++)
            {
                // Check for index out of bounds
                if (y + j < 0 || y + j >= gameY)
                {
                    continue;
                }
                for (int k = -1; k <= 1; k++)
                {
                    // Skip i=0 and j=0 and k=0 (itself)
                    if (i == 0 && j == 0 && k == 0)
                    {
                        continue;
                    }
                    // Check for index out of bounds
                    if (z + k < 0 || z + k >= gameZ)
                    {
                        continue;
                    }
                    Life neighbor = lifeBoard [x + i, y + j, z + k];
                    numLiveNeighbor += (neighbor.currentState == States.Alive) ? 1 : 0;
                }
            }
        }

        return(numLiveNeighbor);
    }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            var input = new bool[20, 20];

            input[1, 0] = true;

            input[0, 2] = true;
            input[1, 2] = true;
            input[2, 2] = true;
            input[2, 1] = true;

            input[8, 0] = true;
            input[8, 1] = true;
            input[8, 2] = true;
            input[9, 1] = true;
            input[9, 2] = true;
            input[9, 3] = true;



            input[6, 11] = true;
            input[7, 11] = true;
            input[8, 11] = true;
            //input[9, 11] = true;
            //input[10, 11] = true;
            //input[11, 11] = true;
            input[12, 11] = true;
            input[13, 11] = true;
            input[14, 11] = true;
            //input[15, 11] = true;


            Board board = new BoardArrayImp(input);
            var   game  = new LifeGame(board);


            while (true)
            {
                Painter.Paint(board);
                board = game.NextRound();
                //Thread.Sleep(200);
                Console.ReadLine();
            }
            Console.ReadLine();
        }
Exemplo n.º 15
0
            public void ReadXml(XmlReader reader)
            {
                reader.ReadToFollowing("GameType");
                string _typeString = reader.ReadElementContentAsString();

                this.GameType = (GameType)Enum.Parse(typeof(GameType), _typeString);

                switch (this.GameType)
                {
                case ViewModel.GameType.Hexagon:
                {
                    XmlSerializer _xs = new XmlSerializer(typeof(HexagonLifeGameFacrory));
                    this.GameFactory = (HexagonLifeGameFacrory)_xs.Deserialize(reader);
                    break;
                }

                case ViewModel.GameType.Square:
                {
                    XmlSerializer _xs = new XmlSerializer(typeof(SquareLifeGameFactory));
                    this.GameFactory = (SquareLifeGameFactory)_xs.Deserialize(reader);
                    break;
                }

                default: break;
                }

                XmlSerializer _gxs  = new XmlSerializer(typeof(LifeGame));
                LifeGame      _game = (LifeGame)_gxs.Deserialize(reader);

                _gxs = new XmlSerializer(typeof(ItemData[]));
                ItemData[] _position = (ItemData[])_gxs.Deserialize(reader);

                this.Game                  = this.GameFactory.GetLifeGame(0);
                this.Game.MaxAge           = _game.MaxAge;
                this.Game.MaxNeighbours    = _game.MaxNeighbours;
                this.Game.MinNeighbours    = _game.MinNeighbours;
                this.Game.NeighboursForNew = _game.NeighboursForNew;
                this.Game.SetPosition(_position);
            }
Exemplo n.º 16
0
        static void Main()
        {
            var game = new LifeGame();

            game.Run();
        }
Exemplo n.º 17
0
 private void Start()
 {
     _lifeGame = GetComponent <LifeGame>();
 }
Exemplo n.º 18
0
        public void Test1(int square, bool expected)
        {
            LifeGame test = new LifeGame(25);

            Assert.Equal(expected, test.World[square]);
        }
Exemplo n.º 19
0
 public void Given_a_grid(int width, int height)
 {
     lifeGame = new LifeGame(width, height);
 }
Exemplo n.º 20
0
 public void Initialise()
 {
     this._game = new LifeGame(Width, Height);
 }
Exemplo n.º 21
0
 private void init()
 {
     this.lg = new LifeGame(this.Bounds.Width / this.pixelSize, this.Bounds.Height / this.pixelSize);
     this.lifegameTimer.Start();
 }
Exemplo n.º 22
0
 public void Initialise()
 {
     this._game = new LifeGame(Width, Height);
 }
Exemplo n.º 23
0
        static void Main(string[] args)
        {
            int w, h, c;

            if (args.Any() && args.Count() == 2)
            {
                int.TryParse(args[0], out w);
                int.TryParse(args[1], out h);
            }
            else
            {
                w = 60;
                h = 60;
            }

            int.TryParse(args.ElementAtOrDefault(2) ?? "45", out c);

            var game = new LifeGame(w, h);

            Console.SetWindowSize(w, h);
            Console.SetWindowPosition(0, 0);
            Console.SetBufferSize(w, h);
            Console.WriteLine("Created {0} x {1} grid", w, h);

            Console.WriteLine("Enter location of a live cell in the format [x,y] and press Enter.");
            Console.WriteLine("Leave blank to finish entering initial  Ex: 2,5");
            Console.WriteLine("No entries will cause initial state to be randomly determined");

            bool done = false;

            while (!done)
            {
                var val = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(val))
                {
                    done = true;
                    continue;
                }
                var tupe = val.Split(',');
                int x, y;
                int.TryParse(tupe.ElementAtOrDefault(0), out x);
                int.TryParse(tupe.ElementAtOrDefault(1), out y);


                var newCell = new Cell(x, y, Cell.CellState.Alive);
                if (game.Cells.Any(p => p.Position == newCell.Position))
                {
                    continue;
                }

                game.SpawnCell(newCell);
            }

            if (!game.Cells.Any())
            {
                var rng           = new Random(DateTime.Now.Millisecond);
                var randCountGoal = rng.Next(2, w * h - 1);
                while (game.Cells.Count() < randCountGoal)
                {
                    var randCell = new Cell(rng.Next(0, w), rng.Next(0, h), rng.NextDouble() > 0.15 ? Cell.CellState.Alive : Cell.CellState.Dead);

                    if (!game.Cells.Select(x => x.Position).Contains(randCell.Position))
                    {
                        game.SpawnCell(randCell);
                    }
                }
            }

            Console.Clear();
            var oldColor = Console.ForegroundColor;

            Console.CursorVisible = false;

            for (int i = 0; i < c; i++)
            {
                game.Step(1);
                var cells = game.Cells;
                DrawGrid(cells);
                Thread.Sleep(33);
            }
            Console.WriteLine();
            Console.ForegroundColor = oldColor;
            Console.ReadKey();
        }