예제 #1
0
파일: Engine.cs 프로젝트: IzC87/Ludo-Revamp
        public List <Token> PlayGame()
        {
            List <Token> tokens = new List <Token>();
            var          player = Game.WhoseTurnIsIt();

            // Player is Computer
            if (player.Computer)
            {
                // Computer needs to roll the die
                if (player.DieRoll == 0)
                {
                    player.DieRoll = RollDie();
                }

                // Make move for computer
                var moveableTokens = player.GetMovableTokens();
                if (moveableTokens.Count == 1)
                {
                    tokens = MoveToken(moveableTokens[0]);
                }

                // computer can move more than one token so we select a random movable token to move
                else if (moveableTokens.Count > 1)
                {
                    var token = moveableTokens[randomSeed.Next(0, moveableTokens.Count)];
                    tokens = MoveToken(token);
                }
                else if (player.IsStartLocked())
                {
                    AddMessageToHistoryList($"P{player.PlayerNumber + 1} needs a 6 to start! ({player.DieRoll})");
                    player.HasMoved      = true;
                    player.NumberOfRolls = 3;
                }
                else
                {
                    player.HasMoved = true;
                }
            }
            // Player is a real person
            else
            {
                // Player needs to roll the die
                if (player.DieRoll == 0)
                {
                    // Tell the user that it's their turn
                    AddMessageToHistoryList($"It's your turn P{player.PlayerNumber + 1}!");
                }
                // Make move for player
                else
                {
                    var moveableTokens = player.GetMovableTokens();
                    if (moveableTokens.Count == 1)
                    {
                        tokens = MoveToken(moveableTokens[0]);
                    }
                    // Player can move more than one token
                    else if (moveableTokens.Count > 1)
                    {
                        var token = player.GetSelectedToken();
                        // No Token selected
                        if (token == null)
                        {
                            AddMessageToHistoryList("Select a token to move");
                        }
                        // If token can be moved then we move it
                        else if (player.CanTokenMove(token))
                        {
                            tokens = MoveToken(token);
                        }
                        else
                        {
                            AddMessageToHistoryList("Unable to move that token");
                        }
                    }
                    else if (player.IsStartLocked())
                    {
                        AddMessageToHistoryList($"P{player.PlayerNumber + 1} needs a 6 to start! ({player.DieRoll})");
                        player.HasMoved      = true;
                        player.NumberOfRolls = 3;
                    }
                }
            }

            return(tokens);
        }