예제 #1
0
        /// <summary>
        /// Sets the very first iteration of the game, randomly.
        /// Adds a new game to list.
        /// </summary>
        /// <param name="boardSize"> Sets the bool size according to chosen board size. Columns and Rows are derived from this. </param>
        public void SetSeed(int boardSize)
        {
            Random random = new Random();

            gameProgress.ID         = 0;
            gameProgress.Columns    = 20 * boardSize - 3;
            gameProgress.Rows       = 7 + boardSize * boardSize;
            gameProgress.Iteration  = 1;
            gameProgress.LiveCells  = 0;
            gameProgress.Generation = new bool[gameProgress.Rows, gameProgress.Columns];

            // Populates the first generation with 2 characters, chosen randomly.
            bool[] randomFiller = new bool[] { true, false };

            for (int rowIndex = 0; rowIndex < gameProgress.Rows; rowIndex++)
            {
                for (int colIndex = 0; colIndex < gameProgress.Columns; colIndex++)
                {
                    int boolIndex = random.Next(randomFiller.Length);
                    gameProgress.Generation[rowIndex, colIndex] = randomFiller[boolIndex];
                    if (boolIndex == 0)
                    {
                        gameProgress.LiveCells++;
                    }
                }
            }

            if (gameList == null)
            {
                gameList = new GameList();
            }

            gameList.Progress.Add(new GameProgress()
            {
                Generation  = gameProgress.Generation,
                LiveCells   = gameProgress.LiveCells,
                BoardSize   = gameProgress.BoardSize,
                Iteration   = gameProgress.Iteration,
                Columns     = gameProgress.Columns,
                Rows        = gameProgress.Rows,
                ID          = gameList.Progress.Count + 1,
                IsGameAlive = true,
            });
        }
예제 #2
0
        /// <summary>
        /// Shows menu where user can choose up to 8 games to load from file.
        /// </summary>
        /// <param name="gameList"> List with game ID's </param>
        public void ChooseGame(GameList gameList, List <int> gamesLoaded)
        {
            Console.Clear();
            Console.SetCursorPosition(0, 3);

            Console.WriteLine("                            GAME LOADER \n\n" +
                              "                 Enter R to return to main menu \n\n" +
                              "               Currently there are {0} games saved\n" +
                              "               You can load and view up to 8 games\n" +
                              "\n\n         Enter the ID numer of game You want to load!", gameList.Progress.Count
                              );
            Console.Write("              Enter L to load games You've chosen: ");

            foreach (int gameId in gamesLoaded)
            {
                Console.Write("{0}, ", gameId);
            }

            Console.SetCursorPosition(32, 14);
        }
예제 #3
0
        /// <summary>
        /// Prints the Generation array of whole list and boarders using StringBuilder.
        /// </summary>
        /// <returns> Appended string of current game iteration. </returns>
        public void PrintList(GameList gameList, List <int> gamesLoaded)
        {
            #region character symbols used
            var boarderTop    = "\u2584";
            var boarderLeft   = " \u2588";
            var dot           = "\u25CF";
            var boarderRight  = "\u2588";
            var boarderBottom = "\u2580";
            #endregion

            Console.SetCursorPosition(0, 0);
            Console.CursorVisible = false;
            var sb = new StringBuilder(string.Empty);

            #region stringBuilder
            sb.AppendLine("  P - pause and resume game     S - save game     R - return to main menu");
            sb.AppendLine();
            sb.AppendFormat("  Games alive: {0}     ", gameList.GamesAlive);
            sb.AppendFormat("  Cells alive: {0}     ", gameList.CellsAlive);
            sb.AppendLine();

            for (int index = 0; index < gamesLoaded.Count; index++)
            {
                for (int progress = 0; progress < gameList.Progress.Count; progress++)
                {
                    if (gamesLoaded[index] == progress + 1)
                    {
                        sb.AppendLine();
                        sb.AppendFormat("  Live cells: {0}    ", gameList.Progress[progress].LiveCells);
                        sb.AppendLine();
                        sb.AppendFormat("  Iteration NR: {0}    ", gameList.Progress[progress].Iteration);
                        sb.AppendLine();
                        sb.Append(" ");

                        for (int width = 1; width < 20 * gameList.Progress[progress].BoardSize; width++)
                        {
                            sb.Append(boarderTop);
                        }

                        sb.AppendLine();

                        for (var rowIndex = 0; rowIndex < gameList.Progress[progress].Rows; rowIndex++)
                        {
                            sb.Append(boarderLeft);
                            for (var colIndex = 0; colIndex < gameList.Progress[progress].Columns; colIndex++)
                            {
                                if (gameList.Progress[progress].Generation[rowIndex, colIndex])
                                {
                                    sb.Append(dot);
                                }
                                else
                                {
                                    sb.Append(" ");
                                }
                            }

                            sb.Append(boarderRight);
                            sb.AppendLine();
                        }

                        sb.Append(" ");

                        for (int width = 1; width < 20 * gameList.Progress[progress].BoardSize; width++)
                        {
                            sb.Append(boarderBottom);
                        }
                        #endregion
                    }
                }
            }

            var result = sb.ToString();
            Console.WriteLine(result);
            Console.SetWindowPosition(0, 0);
        }
예제 #4
0
        /// <summary>
        /// Serializes the current game progress to a file.
        /// </summary>
        public void Serialize(GameList gameList)
        {
            string result = JsonConvert.SerializeObject(gameList);

            File.WriteAllText(FilePath, result);
        }