static void Main(string[] args)
        {
            // Simple Pattern
            Game myLifeGame = new Game(4, 4);
            myLifeGame.FlipGridCell(1, 1);
            myLifeGame.FlipGridCell(1, 2);
            myLifeGame.FlipGridCell(2, 1);
            myLifeGame.FlipGridCell(2, 2);
            myLifeGame.FlipGridCell(2, 3);
            myLifeGame.FlipGridCell(3, 3);
            myLifeGame.MaxGenerations = 4;
            myLifeGame.Init();

            // The Period 3 Oscillator pattern
            //Game myLifeGame = new Game(5, 3);
            //myLifeGame.ToggleGridCell(0, 1);
            //myLifeGame.ToggleGridCell(1, 0);
            //myLifeGame.ToggleGridCell(1, 1);
            //myLifeGame.ToggleGridCell(1, 2);
            //myLifeGame.ToggleGridCell(2, 0);
            //myLifeGame.ToggleGridCell(2, 2);
            //myLifeGame.ToggleGridCell(3, 0);
            //myLifeGame.ToggleGridCell(3, 1);
            //myLifeGame.ToggleGridCell(3, 2);
            //myLifeGame.ToggleGridCell(4, 1);
            //myLifeGame.MaxGenerations = 50;
            //myLifeGame.Init();

            Console.ReadKey();
        }
示例#2
0
        public MainWindow()
        {
            InitializeComponent();

            this.Text = "Game of Life";

            // Setter opp tabell
            Console.WriteLine ("Setter opp tabell..");
            table = new Table ();

            // Teiknar forma på nytt dersom tabellen blir forandra
            table.TableChangedEvent += new Table.TableChangedHandler (this.OnTableChanged);

            // Setter opp grid
            Console.WriteLine ("Setter opp grid..");
            grid = new Grid (this);

            // Teikn enkeltcelle dersom dei blir forandra i tabellen
            table.TableCellChangedEvent += new Table.TableCellChangedHandler (grid.DrawCell);

            // Teikn gridden når forma blir teikna
            this.Paint += new PaintEventHandler (grid.Draw);

            clock = new Timer();
            clock.Interval = 1000;
            clock.Start();

            // Tooltips
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(this.btnRun, "Starter og stopper");

            // Setter opp eit nytt spel av typen 'MaxCells'
            game = new MaxCells (this);
            // game = new Sandbox (this);
        }
示例#3
0
        static void Main(string[] args)
        {
            int noOfTicks; string readLine;

            do
            {
                Console.WriteLine("Please enter the number of ticks:");
                readLine = Console.ReadLine();
                if (!IsValidNumber(readLine, out noOfTicks))
                {
                    Console.WriteLine("'{0}' is not a valid number.", readLine);
                }
            } while (!IsValidNumber(readLine, out noOfTicks));

            var game = new Game(60);
            foreach (Point point in GetSeed())
            {
                game.BringCellToLifeAt(point.X, point.Y);
            }

            for (int i = 0; i < noOfTicks; i++)
            {
                Console.Clear();
                game.Tick();
                Console.WriteLine(game.ToString());
                Thread.Sleep(300);
            }

            Console.ReadKey();
        }
        private static void DisplayGameOfLife(GameOfLife.Game gameOfLife)
        {
            Console.Clear();

            Console.WriteLine("Iteration " + gameOfLife.Iteration);

            DisplayHabitants(gameOfLife.LifeBoard);
        }
示例#5
0
 public void GameConstructorPositiveTest()
 {
     int rows = 2;
     int columns = 2;
     Game target = new Game(rows, columns);
     Assert.AreEqual(target.RowCount, 2);
     Assert.AreEqual(target.ColumnCount, 2);
 }
 public void FlipGridCellTest3()
 {
     int rows = 2;
     int columns = 3;
     Game target = new Game(rows, columns);
     int x = 3;
     int y = 3;
     target.FlipGridCell(x, y);
 }
 public void FlipGridCellTest2()
 {
     int rows = 0;
     int columns = 1;
     Game target = new Game(rows, columns);
     int x = 1;
     int y = 1;
     target.FlipGridCell(x, y);
 }
示例#8
0
        public void TestGetCellinvalidOverflow2()
        {
            game = new Game(2);
            game.Map = new bool[,] {
                { true, true },
                { false,false}
            };

            game.GetCell(2, 0);
        }
 public void FlipGridCellPositiveTest()
 {
     int rows = 2;
     int columns = 3;
     Game target = new Game(rows, columns);
     int x = 1;
     int y = 2;
     target.FlipGridCell(x, y);
     Assert.AreEqual(target.InputGrid[1, 2].IsAlive, true);
 }
示例#10
0
        public void InitBlinkerPatternTest()
        {
            int rows = 3;
            int columns = 3;
            Game target = new Game(rows, columns);
            target.ToggleGridCell(0, 1);
            target.ToggleGridCell(1, 1);
            target.ToggleGridCell(2, 1);

            target.Init();
            Assert.AreEqual(target.InputGrid[1, 0].IsAlive, true);
            Assert.AreEqual(target.InputGrid[1, 1].IsAlive, true);
            Assert.AreEqual(target.InputGrid[1, 2].IsAlive, true);
        }
示例#11
0
        static void Main(string[] args)
        {
            int[,] map = {
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            };

            Game game = new Game(map);

            while (true)
            {
                Console.Clear();

                // set all in map to empty (0)
                for (int x = 0; x < game.GetMapHeight(); ++x)
                {
                    string s = "";
                    for (int y = 0; y < game.GetMapWidth(); ++y)
                    {
                        s += game.GetPoint(x, y);
                        if (y + 1 < game.GetMapWidth())
                        {
                            s += " ";
                        }
                    }

                    Console.WriteLine(s);
                }

                game.Update();

                Console.ReadLine();
            }
        }
示例#12
0
        /*
         * Having received the whole information saved in the file as to what
         * elements the game has, now this method is parsing it from string
         * and populating a new game object which could be returned to the main game and
         * take over the flow of information.
         */
        public static Game parseFromFile(String info)
        {
            //define deliminator of new line
            string [] delim = new string[1];
            delim[0]= (string)(System.Environment.NewLine);
            //split the text by new lines
            string[] splitText = info.Split(delim,System.StringSplitOptions.RemoveEmptyEntries);
            //get size from the string
            int size = Convert.ToInt32(splitText[0]);
            //get rate from the string
            int rate = Convert.ToInt32(splitText[1]);
            //create a new game object with the new size and rate
            Game theGame = new Game(size, rate);

            //set new deliminator
            delim[0] = ",";

            //start splitting each row into seperate values - for each value check if it is a zero or one and populate the cell
            for (int x = 2; x < splitText.Length; x++)
            {
                //split row into characters by the comma
                string[] array = splitText[x].Split(delim, System.StringSplitOptions.RemoveEmptyEntries);

                //for each of the characters check one or zero and set the value at row and column index to proper cell value
                for (int y = 0; y < array.Length; y++)
                {
                    //(x-2) is the row index because the 0th and the 1st element in the array for the rows (whose index is x) where size and rate
                    if (array[y] == "1") theGame.setValueAt(x - 2, y, true);
                    else theGame.setValueAt(x - 2, y, false);
                }
            }

            return theGame;
        }
示例#13
0
 public void ToggleGridCellExceptionTest2()
 {
     int rows = 2;
     int columns = 3;
     Game target = new Game(rows, columns);
     int x = 3;
     int y = 3;
     target.ToggleGridCell(x, y);
 }
示例#14
0
 public void GameConstructorExceptionTest3()
 {
     int rows = 0;
     int columns = 0;
     Game target = new Game(rows, columns);
 }
示例#15
0
 public void ToggleGridCellExcecptinoTest2()
 {
     int rows = 0;
     int columns = 1;
     Game target = new Game(rows, columns);
     int x = 1;
     int y = 1;
     target.ToggleGridCell(x, y);
 }
示例#16
0
 public void ToggleGridCellExceptionTest1()
 {
     int rows = 1;
     int columns = 0;
     Game target = new Game(rows, columns);
     int x = 0;
     int y = 0;
     target.ToggleGridCell(x, y);
 }
示例#17
0
 public void InitToadPattern2Test()
 {
     int rows = 4;
     int columns = 2;
     Game target = new Game(rows, columns);
     target.ToggleGridCell(0, 0);
     target.ToggleGridCell(1, 0);
     target.ToggleGridCell(1, 1);
     target.ToggleGridCell(2, 0);
     target.ToggleGridCell(2, 1);
     target.ToggleGridCell(3, 1);
     target.Init();
     Assert.AreEqual(target.InputGrid[0, 1].IsAlive, true);
     Assert.AreEqual(target.InputGrid[0, 2].IsAlive, true);
     Assert.AreEqual(target.InputGrid[1, 0].IsAlive, true);
     Assert.AreEqual(target.InputGrid[2, 3].IsAlive, true);
     Assert.AreEqual(target.InputGrid[3, 1].IsAlive, true);
     Assert.AreEqual(target.InputGrid[3, 2].IsAlive, true);
 }
示例#18
0
        public void MaxGenerationTest()
        {
            int rows = 2;
            int columns = 2;

            Game target = new Game(rows, columns);
            target.MaxGenerations = 2;
            Assert.AreEqual(target.MaxGenerations, 2);
        }
示例#19
0
        public void InitPeriod15OscillatorTest()
        {
            // The Queen Bee Shuttle pattern
            int rows = 1;
            int columns = 10;
            Game target = new Game(rows, columns);

            target.ToggleGridCell(0, 0);
            target.ToggleGridCell(0, 1);
            target.ToggleGridCell(0, 2);
            target.ToggleGridCell(0, 3);
            target.ToggleGridCell(0, 4);
            target.ToggleGridCell(0, 5);
            target.ToggleGridCell(0, 6);
            target.ToggleGridCell(0, 7);
            target.ToggleGridCell(0, 8);
            target.ToggleGridCell(0, 9);
            target.MaxGenerations = 50;
            target.Init();
            Assert.AreEqual(target.InputGrid[4, 2].IsAlive, true);
            Assert.AreEqual(target.InputGrid[3, 3].IsAlive, true);
            Assert.AreEqual(target.InputGrid[4, 3].IsAlive, true);
            Assert.AreEqual(target.InputGrid[5, 3].IsAlive, true);
            Assert.AreEqual(target.InputGrid[2, 4].IsAlive, true);
            Assert.AreEqual(target.InputGrid[3, 4].IsAlive, true);
            Assert.AreEqual(target.InputGrid[4, 4].IsAlive, true);
            Assert.AreEqual(target.InputGrid[5, 4].IsAlive, true);
            Assert.AreEqual(target.InputGrid[6, 4].IsAlive, true);

            Assert.AreEqual(target.InputGrid[4, 13].IsAlive, true);
            Assert.AreEqual(target.InputGrid[3, 12].IsAlive, true);
            Assert.AreEqual(target.InputGrid[4, 12].IsAlive, true);
            Assert.AreEqual(target.InputGrid[5, 12].IsAlive, true);
            Assert.AreEqual(target.InputGrid[2, 11].IsAlive, true);
            Assert.AreEqual(target.InputGrid[3, 11].IsAlive, true);
            Assert.AreEqual(target.InputGrid[4, 11].IsAlive, true);
            Assert.AreEqual(target.InputGrid[5, 11].IsAlive, true);
            Assert.AreEqual(target.InputGrid[6, 11].IsAlive, true);
        }
示例#20
0
 public void InitQueenBeeShuttleTest()
 {
     // The Queen Bee Shuttle pattern
     int rows = 7;
     int columns = 4;
     Game target = new Game(rows, columns);
     target.ToggleGridCell(0, 0);
     target.ToggleGridCell(0, 1);
     target.ToggleGridCell(1, 2);
     target.ToggleGridCell(2, 3);
     target.ToggleGridCell(3, 3);
     target.ToggleGridCell(4, 3);
     target.ToggleGridCell(5, 2);
     target.ToggleGridCell(6, 0);
     target.ToggleGridCell(6, 1);
     target.MaxGenerations = 100;
     target.Init();
     Assert.AreEqual(target.InputGrid[4, 1].IsAlive, true);
     Assert.AreEqual(target.InputGrid[4, 2].IsAlive, true);
     Assert.AreEqual(target.InputGrid[5, 0].IsAlive, true);
     Assert.AreEqual(target.InputGrid[5, 3].IsAlive, true);
     Assert.AreEqual(target.InputGrid[6, 1].IsAlive, true);
     Assert.AreEqual(target.InputGrid[6, 2].IsAlive, true);
     Assert.AreEqual(target.InputGrid[3, 8].IsAlive, true);
     Assert.AreEqual(target.InputGrid[4, 7].IsAlive, true);
     Assert.AreEqual(target.InputGrid[4, 9].IsAlive, true);
     Assert.AreEqual(target.InputGrid[5, 6].IsAlive, true);
     Assert.AreEqual(target.InputGrid[5, 10].IsAlive, true);
     Assert.AreEqual(target.InputGrid[6, 6].IsAlive, true);
     Assert.AreEqual(target.InputGrid[6, 10].IsAlive, true);
     Assert.AreEqual(target.InputGrid[7, 7].IsAlive, true);
     Assert.AreEqual(target.InputGrid[7, 8].IsAlive, true);
     Assert.AreEqual(target.InputGrid[7, 9].IsAlive, true);
     Assert.AreEqual(target.InputGrid[9, 7].IsAlive, true);
     Assert.AreEqual(target.InputGrid[9, 8].IsAlive, true);
     Assert.AreEqual(target.InputGrid[9, 9].IsAlive, true);
     Assert.AreEqual(target.InputGrid[10, 6].IsAlive, true);
     Assert.AreEqual(target.InputGrid[10, 10].IsAlive, true);
     Assert.AreEqual(target.InputGrid[11, 6].IsAlive, true);
     Assert.AreEqual(target.InputGrid[11, 10].IsAlive, true);
     Assert.AreEqual(target.InputGrid[12, 7].IsAlive, true);
     Assert.AreEqual(target.InputGrid[12, 9].IsAlive, true);
     Assert.AreEqual(target.InputGrid[13, 8].IsAlive, true);
 }
示例#21
0
        //constructor for the gameform taking the new size and rate from the properties form
        //and setting up all tha values of the needed variables
        public GameForm(int newSize, int newRate)
        {
            //initialize component
            InitializeComponent();
            //set ticker to uset the timer_Ticke event handler method ( user defined )
            myTimer.Tick += new EventHandler(this.timer_Tick);

            //set up new game and new grid
            game = new Game(newSize, newRate);
            //grid is the appearance of the game board on the game form
            grid = new Button[Game.SIZE, Game.SIZE];

            //set up the start button
            start = new Button();
            // give it green back color
            start.BackColor = Color.Green;
            //add it a user defined event handler method startEvent_Click
            start.Click += new EventHandler(this.startEvent_Click);

            //create new label for the generation number to appear
            generations = new Label();
            //add it new text
            generations.Text = "Generations:";
            //generations.Enabled = false;
            //set up its appearance
            generations.BorderStyle = BorderStyle.None;
            generations.Font = new Font(FontFamily.GenericMonospace, 12.5f, FontStyle.Bold);

            //set up the stop button
            stop = new Button();
            // give it red back color
            stop.BackColor = Color.Red;
            //add it a user defined event handler method stopEvent_Click
            stop.Click += new EventHandler(this.stopEvent_Click);

            //Set up the save button and its appearance
            save = new Button();
            save.Text = "SAVE";
            save.Font = new Font(FontFamily.GenericMonospace, 12.5f, FontStyle.Bold);
            //add it a user defined event handler method saveEvent_Click
            save.Click += new EventHandler(this.saveEvent_Click);

            /*
             * Set up different boundaries for the game dependent on the size of the board:
             * if the board is 16 x 16 or bigger than the size of the game form would be generated
             * relevent to the size of the board so that the game would keep one and the same ratio proportions
             * in look and feel no matter what size the board is.
             * If on the other hand the board is smaller than 16 x 16 again to preserve the look and fell of the game,
             * the size of the game board and the game form would be generated using the size at 16 x 16 to
             * obtain a more pleasant look.
             */
            if (Game.SIZE >= 16)
            {
                this.Size = new Size((Game.SIZE) * 20 + 17, Game.SIZE * 20 + 180);
                start.SetBounds((Game.SIZE + 1) * 20 / 2 - 150, (Game.SIZE + 1) * 20, 40, 40);
                generations.SetBounds((Game.SIZE + 1) * 20 / 2 - 90, (Game.SIZE + 1) * 20 + 10, 160, 40);
                stop.SetBounds((Game.SIZE + 1) * 20 / 2 + 100, (Game.SIZE + 1) * 20, 40, 40);
                save.SetBounds((Game.SIZE + 1) * 20 / 2 - 90 , (Game.SIZE + 1) * 20 + 50, 160, 40);
            }
            else
            {
                this.Size = new Size(16 * 20 + 17, 16* 20 + 180);
                start.SetBounds(17 * 20 / 2 - 150, 17 * 20, 40, 40);
                generations.SetBounds(17 * 20 / 2 - 90, 17 * 20 + 10, 160, 40);
                stop.SetBounds(17 * 20 / 2 + 100, 17 * 20, 40, 40);
                save.SetBounds(17 * 20 / 2 - 90, 17 * 20 + 50, 160, 40);
            }

            //variable to center the board it is is smaller than 16 x 16 so that it is in the middle of the form (used as an indent variable)
            int center = (this.Size.Width - ((Game.SIZE + 1) * 20)) / 2;

            //Creates the grid as a board of buttons of a particular size, on a particular place and particular color.
            for (int x = 0; x < Game.SIZE; x++)
            {
                for (int y = 0; y < Game.SIZE; y++)
                {
                    grid[x, y] = new Button();

                    /*
                     * the tag of the button would be the mechanism to sense what the place of the cell changed by the user is.
                     * it uses a formula to calculate a single number to withhold the row and the column index all together
                     * tag number = (row * grid size) + column
                     * thus then row = tag / grid size
                     * then column = tag % grid size
                     */
                    grid[x, y].Tag = x * Game.SIZE + y;

                    grid[x, y].SetBounds(y * 20 + center, x * 20 + center, 20, 20);
                    grid[x, y].BackColor = dead;
                    grid[x, y].UseVisualStyleBackColor = false;
                    //adds an event handle for the button click
                    grid[x, y].Click += new EventHandler(this.btnEvent_Click);
                    //adds the control to the board
                    Controls.Add(grid[x, y]);
                }
            }
            //adds the rest of the controls to the board
            Controls.Add(start);
            Controls.Add(generations);
            Controls.Add(stop);
            Controls.Add(save);
        }
示例#22
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                PrintUsage();
                return;
            }

            string configFileName = args[0];
            Game game = new Game(new GameConfig(configFileName), new GameDisplay());
            game.Run();
        }
示例#23
0
        public void TestGetCellValid()
        {
            game = new Game(2);
            game.Map = new bool[,] {
                { true, true },
                { false,false}
            };

            Assert.IsTrue(game.GetCell(0, 0));
            Assert.IsTrue(game.GetCell(0, 1));
            Assert.IsFalse(game.GetCell(1, 0));
            Assert.IsFalse(game.GetCell(1, 1));
        }
示例#24
0
        public void TestGetCellinvalidUnderflow1()
        {
            game = new Game(2);
            game.Map = new bool[,] {
                { true, true },
                { false,false}
            };

            game.GetCell(0, -1);
        }
示例#25
0
        public void TestInBound()
        {
            game = new Game(2);
            game.Map = new bool[,] {
                { true, true },
                { false,false}
            };

            Assert.IsTrue(game.InBound(0, 0));
            Assert.IsTrue(game.InBound(1, 1));
            Assert.IsFalse(game.InBound(2, 2));
            Assert.IsFalse(game.InBound(-1, -1));
        }
示例#26
0
 public void init()
 {
     game = new Game(GAME_SIZE);
     game.Map = InitTestMap(GAME_SIZE);
 }
示例#27
0
 public void TestInvalidGameSize()
 {
     game = new Game(-1);
 }
示例#28
0
 public void InitBlockPatternTest()
 {
     int rows = 2;
     int columns = 2;
     Game target = new Game(rows, columns);
     target.ToggleGridCell(0, 0);
     target.ToggleGridCell(0, 1);
     target.ToggleGridCell(1, 0);
     target.ToggleGridCell(1, 1);
     target.MaxGenerations = 100;// This pattern remains unchanged for infinite generation, testing it for 100 generations
     target.Init();
     Assert.AreEqual(target.InputGrid[0, 0].IsAlive, true);
     Assert.AreEqual(target.InputGrid[0, 1].IsAlive, true);
     Assert.AreEqual(target.InputGrid[1, 0].IsAlive, true);
     Assert.AreEqual(target.InputGrid[1, 1].IsAlive, true);
 }
示例#29
0
        public void InitDefaultValueTest()
        {
            int rows = 2;
            int columns = 2;

            Game target = new Game(rows, columns);
            target.Init();
            Assert.AreEqual(target.InputGrid[0, 0].IsAlive, false);
            Assert.AreEqual(target.InputGrid[0, 1].IsAlive, false);
            Assert.AreEqual(target.InputGrid[1, 0].IsAlive, false);
            Assert.AreEqual(target.InputGrid[1, 1].IsAlive, false);
        }
示例#30
0
 static void Main(string[] args)
 {
     Game game = new Game(1);
     game.Play();
 }
示例#31
0
        public void InitDiamondPatternTest()
        {
            int rows = 3;
            int columns = 4;

            Game target = new Game(rows, columns);
            target.ToggleGridCell(0, 1);
            target.ToggleGridCell(0, 2);
            target.ToggleGridCell(1, 0);
            target.ToggleGridCell(1, 3);
            target.ToggleGridCell(2, 1);
            target.ToggleGridCell(2, 2);
            target.MaxGenerations = 50; // This pattern remains unchanged for infinite generation, testing it for 50 generations
            target.Init();
            Assert.AreEqual(target.InputGrid[0, 1].IsAlive, true);
            Assert.AreEqual(target.InputGrid[0, 2].IsAlive, true);
            Assert.AreEqual(target.InputGrid[1, 0].IsAlive, true);
            Assert.AreEqual(target.InputGrid[1, 3].IsAlive, true);
            Assert.AreEqual(target.InputGrid[2, 1].IsAlive, true);
            Assert.AreEqual(target.InputGrid[2, 2].IsAlive, true);
        }