public static StringBuilder GetlineForPrinting(IGameOfLife game, StringBuilder line, int height)//Creates concatenated string of all games
 {
     if (height > game.Height)
     {
         for (int j = 0; j < game.Width; j++)
         {
             line.Append(" ");
         }
     }
     if (height == game.Height)
     {
         for (int j = 0; j < game.Width; j++)
         {
             line.Append("-");
         }
         line.Append("|");
     }
     else
     {
         for (int j = 0; j < game.Width; j++)
         {
             if (game.Matrix[j, height] == true)
             {
                 line.Append("X");
             }
             else if (game.Matrix[j, height] == false)
             {
                 line.Append(" ");
             }
         }
         line.Append("|");
     }
     return(line);
 }
Exemple #2
0
        static void Main(string[] args)
        {
            Cell[] cells = null;
            // Ask the user for initial state
            Console.WriteLine("\t\tWelcome to the game of life.\n");
            PrintWelcomeAndInstructions();

            // Get proper initial state from the user
            string userinput = GetValidUserInput(ref cells);

            if (!string.IsNullOrWhiteSpace(userinput))
            {
                // Create a Grid
                Grid grid = new Grid(4, 4);

                SetInitialGridState(cells, grid);

                IGameOfLife gameoflife = GetGameOfLifeController(grid);

                // Print the current state
                PrintCellState(grid);

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Press to evolve to next generation.\n");
                    Console.ReadLine();

                    gameoflife.Evolve();
                    PrintCellState(grid);
                }

                Console.WriteLine("Game over. Press enter to exit.");
                Console.ReadLine();
            }
        }
Exemple #3
0
 public Animation(PaintGrid dgv, IGameOfLife gameOfLife, System.Windows.Forms.Label iterationCount)
 {
     _grid           = dgv;
     _gameOfLife     = gameOfLife;
     Delay           = 100;
     _iterationCount = iterationCount;
 }
Exemple #4
0
        private static void DisplayGameOfLifeSetup(List <IGameOfLife> games)// Setup or dispaying one Game of Life
        {
            UserInput userinput = new UserInput();
            TimerGOL  timer     = new TimerGOL();

            IGameOfLife game = userinput.CaptureGameNr(games);

            timer.StartTimer(game);
        }
Exemple #5
0
        /// <summary>
        /// Save the game state to database.
        /// </summary>
        /// <param name="game">The current game state.</param>
        /// <returns></returns>
        public async Task SaveAsync(IGameOfLife game)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                await connection.OpenAsync();

                SqlCommand command = new SqlCommand($"DELETE FROM {_tableName}", connection)
                {
                    CommandType = CommandType.Text
                };

                try
                {
                    await command.ExecuteNonQueryAsync();
                }
                catch (SqlException)
                {
                    // Could be thrown by there not being a valid table present or database communication issue.
                    throw new Exception("Error contacting the database");
                }

                String query = $"INSERT INTO {_tableName}(Height, Width, Cells) VALUES (@Height, @Width, @Cells)";
                command = new SqlCommand(query, connection)
                {
                    CommandType = CommandType.Text
                };

                // Adopted for academic-purposes.
                // from Week 4 Tutorial ADO example 6.
                command.Parameters.Add(new SqlParameter
                {
                    ParameterName = "@Height",
                    SqlDbType = SqlDbType.Int,
                    Value = game.Height
                });

                command.Parameters.Add(new SqlParameter
                {
                    ParameterName = "@Width",
                    SqlDbType = SqlDbType.Int,
                    Value = game.Width
                });

                command.Parameters.Add(new SqlParameter
                {
                    ParameterName = "@Cells",
                    SqlDbType = SqlDbType.NVarChar,
                    Value = JsonConvert.SerializeObject(game.Cells)
                });

                if (await command.ExecuteNonQueryAsync() == 0)
                    throw new Exception("Error saving game");

                connection.Close();
            }
        }
Exemple #6
0
 public override void Add(IGameOfLife game)
 {
     if (game.Width >= 13 && game.Height >= 13)
     {
         game.AddCell(11, 10);
         game.AddCell(12, 11);
         game.AddCell(10, 12);
         game.AddCell(11, 12);
         game.AddCell(12, 12);
     }
 }
Exemple #7
0
        private static void CreateGameOfLifeSetup(List <IGameOfLife> games) //Setup for creating Game of Life
        {
            UserInput            userinput = new UserInput();
            TimerGOL             timer     = new TimerGOL();
            LightWeightSpaceship lws       = new LightWeightSpaceship();

            IGameOfLife game = userinput.Capture(); //initialazing game of life

            lws.Add(game);                          //Adding lightweightspaceship
            timer.StartTimer(game);
            games.Add(game);
        }
Exemple #8
0
        public override void Add(IGameOfLife game)
        {
            Random random = new Random();

            for (int y = 0; y < game.Height; y++)
            {
                for (int x = 0; x < game.Width; x++)
                {
                    if (random.Next(2) == 0)
                    {
                        game.AddCell(x, y);
                    }
                }
            }
        }
Exemple #9
0
 public override void Add(IGameOfLife game)
 {
     if (game.Width >= 6 && game.Height >= 5)
     {
         game.AddCell(1, 1);
         game.AddCell(4, 1);
         game.AddCell(5, 2);
         game.AddCell(1, 3);
         game.AddCell(5, 3);
         game.AddCell(2, 4);
         game.AddCell(3, 4);
         game.AddCell(4, 4);
         game.AddCell(5, 4);
     }
 }
        public override void Add(IGameOfLife game)
        {
            if (game.Width >= 36 && game.Height >= 22)
            {
                game.AddCell(3, 1);
                game.AddCell(3, 2);
                game.AddCell(4, 1);
                game.AddCell(4, 2);

                game.AddCell(7, 4);
                game.AddCell(8, 4);
                game.AddCell(7, 5);
                game.AddCell(8, 5);

                game.AddCell(10, 1);
                game.AddCell(10, 2);
                game.AddCell(11, 1);
                game.AddCell(11, 2);

                game.AddCell(23, 18);
                game.AddCell(23, 19);
                game.AddCell(24, 18);
                game.AddCell(24, 20);
                game.AddCell(25, 20);
                game.AddCell(26, 20);
                game.AddCell(26, 21);

                game.AddCell(24, 11);
                game.AddCell(24, 12);
                game.AddCell(24, 13);
                game.AddCell(25, 10);
                game.AddCell(26, 10);
                game.AddCell(25, 13);
                game.AddCell(26, 13);

                game.AddCell(28, 10);
                game.AddCell(29, 10);
                game.AddCell(30, 11);
                game.AddCell(31, 12);
                game.AddCell(30, 13);
                game.AddCell(29, 14);

                game.AddCell(34, 12);
                game.AddCell(35, 12);
                game.AddCell(34, 13);
                game.AddCell(35, 13);
            }
        }
Exemple #11
0
        public void StartTimer(IGameOfLife game)//Timer for displaying one game
        {
            Timer timer = new Timer(1000);
            List <IGameOfLife> games = new List <IGameOfLife>()
            {
                game
            };

            timer.Elapsed += (sender, e) => TimerTick(games, games, games, 1);
            timer.Start();
            Console.Clear();
            game.PrintMatrix();
            Messages.PressKeyToStopMessage();
            Console.ReadLine();
            timer.Stop();
            timer.Dispose();
            Messages.TerminatingApplicationMessage();
        }
Exemple #12
0
        public Boolean LoadGame()
        {
            try
            {
                game = _database.Load();

                if (game == null)
                {
                    return(false);
                }

                return(true);
            }
            catch (SqlException)
            {
                Console.WriteLine("No game table present in the database.\r\n");
                return(false);
            }
        }
        public Form1()
        {
            InitializeComponent();
            paintGrid1.SuspendLayout();
            for (int x = 0; x < gridSize.X; x++)
            {
                paintGrid1.Columns.Add("", "");
                paintGrid1.Columns[x].Width = paintGrid1.Size.Width / gridSize.X;
            }

            paintGrid1.Rows.Add(gridSize.Y);

            for (int x = 0; x < gridSize.Y; x++)
            {
                paintGrid1.Rows[x].Height = paintGrid1.Size.Height / gridSize.Y;
            }
            paintGrid1.ResumeLayout();
            _gameOfLife = new GameOfLife.Core.GameOfLife(gridSize);
            _animation  = new Animation(paintGrid1, _gameOfLife, label2);
            paintGrid1.DefaultCellStyle.SelectionBackColor = Color.Transparent;
            paintGrid1.DefaultCellStyle.SelectionForeColor = Color.Transparent;
            textBox1.Text = _animation.Delay.ToString();
        }
Exemple #14
0
 public GameOfLifeHub(IGameOfLife game)
 {
     _game = game;
 }
Exemple #15
0
 public abstract void Add(IGameOfLife game);
Exemple #16
0
 public void NewGame(Template template, int height, int width, int x, int y)
 {
     game = new GameOfLife(height, width);
     game.InsertTemplate(template, x, y);
 }