コード例 #1
0
        /// <summary>
        /// Create Players from input provider
        /// </summary>
        private void createPlayersForTable()
        {
            outputProvider.Write("How many players: ");
            var numsPlayer = 1;

            int.TryParse(inputProvider.Read(), out numsPlayer);

            if (numsPlayer <= 0)
            {
                numsPlayer = 1;
                outputProvider.WriteLine();
                outputProvider.WriteLine($"Defaulting to {numsPlayer} players");
            }


            this.players = new List <IPlayer>();
            for (int i = 0; i < numsPlayer; i++)
            {
                var name = GetName();
                this.players.Add(new HumanPlayer(name));
            }

            this.table.Players = this.players as IEnumerable <IPlayer>;
        }
コード例 #2
0
        public void StartGame(Snake snake, Board board)
        {
            // Customize Snake
            snake = CustomizeSnake();

            // set the snake in the middle of the board
            snake.XPosition = board.Boardwidth / 2;
            snake.YPosition = board.Boardheight / 2;

            // set defaults
            //game is in play, no history of moves, snake length is 1 and game speed is 75
            IsGameOver = false;
            DidHitWall = false;
            Eaten      = new Dictionary <string, bool>();
            int GameSpeed = 75;

            snake.Length = 1;

            //do not show the cursor and initialize a variable for key input
            Console.CursorVisible = false;
            ConsoleKeyInfo command;

            while (!IsGameOver)
            {
                // clear the console, set the title bar, and draw the board
                outputProvider.Clear();
                outputProvider.CreateTitle(Message.Instructions);
                board.DrawBoard();

                //clear move history, and add current position, then draw the snake
                Eaten.Clear();
                Eaten.Add(snake.XPosition.ToString() + snake.YPosition.ToString(), true);
                snake.DrawSnake();

                //wait for the player to move
                WaitForMove();

                //set the speed by checking for keystrokes at the gamespeed in miliseconds
                DateTime nextCheck = DateTime.Now.AddMilliseconds(GameSpeed);

                while (!IsGameOver && !DidHitWall)
                {
                    //Display the length at the top of the screen
                    outputProvider.CreateTitle(Message.Score + snake.Length.ToString());

                    //wait for the next time you can check for keys
                    while (nextCheck > DateTime.Now)
                    {
                        // see if the player has changed direction
                        if (Console.KeyAvailable)
                        {
                            //read the key and map it to a direction
                            command = Console.ReadKey(true);
                            MapCommandToDirection(command);
                        }
                    }

                    if (!IsGameOver)
                    {
                        ChangeDirection(snake, board);

                        if (!DidHitWall)
                        {
                            //format the current positions to two rounded digits
                            string positions = snake.XPosition.ToString("00") + snake.YPosition.ToString("00");
                            //if the snake hasn't been to the current positions, add the length and keep going
                            if (!Eaten.ContainsKey(positions))
                            {
                                snake.Length++;
                                Eaten.Add(positions, true);
                                snake.DrawSnake();
                            }
                            //otherwise say they hit the wall
                            else
                            {
                                DidHitWall = true;
                            }
                        }

                        nextCheck = DateTime.Now.AddMilliseconds(GameSpeed);
                    }
                }

                if (DidHitWall)
                {
                    outputProvider.CreateTitle(Message.You_Died + snake.Length.ToString());
                    SetConsoleToDefault();
                    outputProvider.WriteLine(Message.SkullArt);
                    JustWait();
                    IsGameOver = true;
                }
            }
            if (IsGameOver)
            {
                SetConsoleToDefault();
                outputProvider.CreateTitle(Message.Youre_Done + snake.Length.ToString());
                outputProvider.Write(Message.SnakeArt);
                JustWait();
            }
        }
コード例 #3
0
ファイル: Interpreter.cs プロジェクト: marco-fiset/migraine
        private void InitializeActionList(IInputProvider input, IOutputProvider output)
        {
            actions = new Dictionary<char, Action>();

            //Pointer manipulation
            actions.Add('>', () =>
            {
                PointerPosition++;

                //Allocate new memory cells as we go
                if (MemoryCells.Count <= PointerPosition)
                    MemoryCells.Add(0);
            });

            actions.Add('<', () =>
            {
                PointerPosition--;

                if (PointerPosition < 0)
                    PointerPosition = 0;
            });

            //Current cell manipulation
            actions.Add('+', () => MemoryCells[PointerPosition]++);
            actions.Add('-', () => MemoryCells[PointerPosition]--);

            //Input and output
            actions.Add(',', () => MemoryCells[PointerPosition] = Convert.ToInt32(input.Get()));
            actions.Add('.', () => output.Write(MemoryCells[PointerPosition].ToString()));

            actions.Add('[', () =>
            {
                loopIndexes.Push(currentProgramStringIndex);

                //Enter the loop if the current memory cell is different than zero
                if (MemoryCells[PointerPosition] != 0)
                    return;

                //Else we skip until the end of that loop
                do
                {
                    currentProgramStringIndex++;

                    //Stack-based logic in case we encounter any inner loops
                    if (ProgramString[currentProgramStringIndex] == '[')
                    {
                        loopIndexes.Push(currentProgramStringIndex);
                    }
                    else if (ProgramString[currentProgramStringIndex] == ']')
                    {
                        loopIndexes.Pop();
                    }
                //FIXME: Potential bug here if we are already in a loop
                } while (loopIndexes.Count > 0);
            });

            actions.Add(']', () =>
            {
                //Go back to the start of the loop
                if (MemoryCells[PointerPosition] != 0)
                    currentProgramStringIndex = loopIndexes.Peek();
                else
                    loopIndexes.Pop();
            });
        }