コード例 #1
0
ファイル: GameArea.cs プロジェクト: andre197/SnakeGame
        public void Draw(IWriterService writerService)
        {
            writerService.Clear();
            var border = new string('#', BottomRightCoordinates.X - UpperLeftCoordinates.X);

            writerService.SetWritePosition(UpperLeftCoordinates.Y, UpperLeftCoordinates.X);
            writerService.WriteLine(border);
            for (int i = UpperLeftCoordinates.Y + 1; i < BottomRightCoordinates.Y - 1; i++)
            {
                writerService.SetWritePosition(i, UpperLeftCoordinates.X);
                writerService.Write("#");
                writerService.SetWritePosition(i, BottomRightCoordinates.X - 1);
                writerService.WriteLine("#");
            }

            writerService.WriteLine(border);
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: andre197/SnakeGame
        public void Start()
        {
            _writerService.WriteLine("Welcome to snake game!");

            while (true)
            {
                if (_game != null)
                {
                    break;
                }

                _writerService.WriteLine("Press enter to start. Type help to get help for the game!");

                var input = _readerService.Read().ToLower();

                var result = _loadSettingsStrategyFactory
                             .GetStrategy(input)
                             .LoadSettings();
                if (!result.Loaded || result.GameSettings == null)
                {
                    continue;
                }

                _cancellationToken = new CancellationTokenSource();
                _gameSettings      = result.GameSettings;
                _game = BeginGame(_gameSettings, PauseTokenSource.Token, _cancellationToken.Token);
                Task.WaitAll(_game);

                _writerService.Clear();
                _highLevelEventsService.Stop();
                _writerService.WriteLine("You just died. If you want to restart press r");
                var readInfo = _readerService.Read();
                if (readInfo.ToLower() != "r")
                {
                    break;
                }

                _game  = null;
                _score = 0;
            }
        }
コード例 #3
0
        public LoadSettingsStrategyResult LoadSettings()
        {
            while (true)
            {
                _writerService.WriteLine("To change the default speed please enter number between 1 and 16 or press enter. The default speed is 1:");
                var inp = _readerService.Read();
                if (!int.TryParse(inp, out int speed) || speed < 1 || speed > 16)
                {
                    _writerService.WriteLine("Invalid input! Please try again!");
                    continue;
                }

                return(new LoadSettingsStrategyResult
                {
                    GameSettings = new GameSettings
                    {
                        Speed = speed
                    },
                    Loaded = true
                });
            }
        }
コード例 #4
0
        public LoadSettingsStrategyResult LoadSettings()
        {
            var savedSettings      = _settingsLoader.FindSettings();
            var savedSettingsCount = savedSettings.Count();

            if (savedSettings == null || savedSettingsCount == 0)
            {
                _writerService.WriteLine("Saved settings cannot be found! The game will start with the default settings!");
                return(new LoadSettingsStrategyResult());
            }

            int index = 1;

            _writerService.WriteLine("Index | Settings name");
            foreach (var item in savedSettings)
            {
                _writerService.WriteLine($"{index:D5} | {item}");
                index++;
            }

            bool isFirstRun = true;

            while (true)
            {
                _writerService.WriteLine("Type the index of file with saved settings from the files written above and press enter!");
                if (!isFirstRun)
                {
                    _writerService.WriteLine("If you want to start with default settings press enter!");
                }

                var input = _readerService.Read();
                if (string.IsNullOrWhiteSpace(input))
                {
                    return(new LoadSettingsStrategyResult());
                }


                if (int.TryParse(input, out int idx) && 0 < idx && idx <= savedSettingsCount)
                {
                    return(new LoadSettingsStrategyResult {
                        Loaded = true, GameSettings = _settingsLoader.Load(idx - 1)
                    });
                }

                _writerService.WriteLine("Invalid input please try again!");

                isFirstRun = false;
            }
        }
コード例 #5
0
        public void Draw(IWriterService writerService)
        {
            if (_lastRemovedPoint.HasValue)
            {
                writerService.SetWritePosition(_lastRemovedPoint.Value.Y, _lastRemovedPoint.Value.X);
                writerService.WriteLine(" ");
            }

            if (_isFirstDraw)
            {
                foreach (var p in _activePoints)
                {
                    writerService.SetWritePosition(p.Y, p.X);
                    writerService.Write("S");
                }

                _isFirstDraw = false;
            }

            writerService.SetWritePosition(HeadCoordinates.Y, HeadCoordinates.X);
            writerService.Write("S");
            writerService.SetWritePosition(32, 0);
        }
コード例 #6
0
        public LoadSettingsStrategyResult LoadSettings()
        {
            _writerService.WriteLine("Welcome to Snake game help center.");
            _writerService.WriteLine("\t The rules of the game are quite simple:");
            _writerService.WriteLine("\t\t You play with the arrows which move the snake in the direction you want and you try not to hit the walls or yourself!");
            _writerService.WriteLine("\t\t e.g. If you press the left arrow the snake will go to the left side of the game area");
            _writerService.WriteLine("\t\t If you press the up arrow the snake will go to the top side of the game area etc.");
            _writerService.WriteLine("\t If you want to stop for a while just space button (the long one on the keyboard)");
            _writerService.WriteLine("\t If you want to resume the game just hit space button again");
            _writerService.WriteLine("\t If you want to save your game press ctrl + s (I recommend doing so when the game is paused)");
            _writerService.WriteLine("\t If you want to load the saved game type load in the console after this message and follow the instructions you will see!");
            _writerService.WriteLine("\t If you want to set custom speed settings type custom in the console after this message and follow the instructions you will see!");
            if (!_isFisrtTimeCallingHelp)
            {
                _writerService.WriteLine("\t If you want to exit the game just press ctrl + x and you are done but doing so will not save anything!");
            }

            _writerService.WriteLine("That is all the help I can give you. From now on it is up to you how to play the game!");
            _writerService.WriteLine("");

            if (_isFisrtTimeCallingHelp)
            {
                Task.Delay(1_000).GetAwaiter().GetResult();
                _writerService.WriteLine("Oops almost forgot. If you want to exit the game just press ctrl + x and you are done but doing so will not save anything!");
                _isFisrtTimeCallingHelp = false;
            }

            return(new LoadSettingsStrategyResult());
        }
コード例 #7
0
 public LoadSettingsStrategyResult LoadSettings()
 {
     _writerService.WriteLine("Invalid input! To start the game please press enter or type help to get help with the game!");
     return(new LoadSettingsStrategyResult());
 }