AIsOfCatan.HumanAgent.PerformTurn C# (CSharp) Method

PerformTurn() public method

public PerformTurn ( IGameState state, IGameActions actions ) : void
state IGameState
actions IGameActions
return void
        public void PerformTurn(IGameState state, IGameActions actions)
        {
            Console.WriteLine("It is now your turn (#" + assignedId + ")");
            while (true)
            {
                try
                {
                    Console.Write("Resources: [");
                    foreach (Resource resource in Enum.GetValues(typeof (Resource)))
                    {
                        var count = state.GetOwnResources().Count(r => r == resource);
                        if (count == 0) continue;
                        Console.Write(resource + " x " + count + ", ");
                    }
                    Console.WriteLine("]");
                    Console.Write("Dev. cards: [");
                    foreach (DevelopmentCard devcard in Enum.GetValues(typeof(DevelopmentCard)))
                    {
                        var count = state.GetOwnDevelopmentCards().Count(r => r == devcard);
                        if (count == 0) continue;
                        Console.Write(devcard + " x " + count + ", ");
                    }
                    Console.WriteLine("]");

                    bool canBuildRoad = hasRes(state, Resource.Lumber) && hasRes(state, Resource.Brick);
                    //TODO: has any more pieces
                    bool canBuildSettlement = hasRes(state, Resource.Brick) && hasRes(state, Resource.Lumber) &&
                                              hasRes(state, Resource.Grain) && hasRes(state, Resource.Wool);
                    //TODO: has any more pieces
                    bool canBuildCity = hasRes(state, Resource.Ore, 3) && hasRes(state, Resource.Grain, 2);
                    //TODO: has any more pieces
                    bool canBuyDevCard = hasRes(state, Resource.Grain) && hasRes(state, Resource.Ore) &&
                                         hasRes(state, Resource.Wool); //TODO: any more dev cards

                    bool canTradeBank = true, canTradePlayers = true; //TODO: Implement these

                    Console.WriteLine("Choose an action:");
                                            Console.WriteLine("0) End turn");
                    if (canBuildRoad)       Console.WriteLine("1) Build road");
                    if (canBuildSettlement) Console.WriteLine("2) Build settlement");
                    if (canBuildCity)       Console.WriteLine("3) Build city");
                    if (canBuyDevCard)      Console.WriteLine("4) Buy development card");
                    if (!hasPlayedDevCard &&
                        state.GetOwnDevelopmentCards().Count(d => d != DevelopmentCard.VictoryPoint) > 0)
                                            Console.WriteLine("5) Play development card");
                    if (canTradeBank)       Console.WriteLine("6) Trade resources with the bank");
                    if (canTradePlayers)    Console.WriteLine("7) Trade resources with the other players");

                    int answer = int.Parse(Console.ReadLine() ?? "0");

                    switch (answer)
                    {
                        case 1: //road
                            var roadPos = getRoadPosition();
                            state = actions.BuildRoad(roadPos);
                            break;
                        case 2: //settlement
                            var settlementPos = getSettlementPosition();
                            state = actions.BuildSettlement(settlementPos);
                            break;
                        case 3: //city
                            var cityPos = getCityPosition();
                            state = actions.BuildCity(cityPos);
                            break;
                        case 4: //buy dev
                            state = actions.DrawDevelopmentCard();
                            break;
                        case 5: //play dev
                            state = PlayDevelopmentCard(state, actions) ?? state;
                            break;
                        case 6: //trade bank
                            Console.WriteLine("Choose which resource to give");
                            var tbGive = selectResourceTradeBank(state.Board);
                            Console.WriteLine("Choose which resource to receive");
                            var tbTake = selectResource();
                            state = actions.TradeBank(tbGive, tbTake);
                            break;
                        case 7: //trade players
                            Console.WriteLine("Which resource type do you want to give away:");
                            var tpGiveType = selectResource();
                            Console.WriteLine("How many " + tpGiveType + " do you want to give:");
                            var tpGiveAmount = int.Parse(Console.ReadLine() ?? "2");
                            Console.WriteLine("Which resource type do you want to get in return for " + tpGiveAmount + " " + tpGiveType + "? :");
                            var tpTakeType = selectResource();
                            Console.WriteLine("How many " + tpTakeType + " do you want to get:");
                            var tpTakeAmount = int.Parse(Console.ReadLine() ?? "1");

                            var give = new List<List<Resource>>(){new List<Resource>()};
                            for (int i = 0; i < tpGiveAmount; i++)
                                give[0].Add(tpGiveType);
                            var take = new List<List<Resource>>(){new List<Resource>()};
                            for (int i = 0; i < tpTakeAmount; i++)
                                take[0].Add(tpTakeType);

                            var feedback = actions.ProposeTrade(give, take);
                            Console.WriteLine("The other players responded:");
                            foreach (var f in feedback)
                            {
                                Console.Write(f.Key + ") ");
                                Console.Write(f.Value.Status + " ");
                                if (f.Value.Status == TradeStatus.Countered)
                                {
                                    Console.Write("(They give: ");

                                    Console.Write(f.Value.Give[0].Select(r => r.ToString()).Aggregate((a,b) => a + ", " + b));

                                    Console.Write(" for ");

                                    Console.Write(f.Value.Take[0].Select(r => r.ToString()).Aggregate((a, b) => a + ", " + b));

                                    Console.WriteLine(")");
                                }
                                else
                                {
                                    Console.WriteLine();
                                }
                            }
                            Console.WriteLine("Select a player to trade with by entering the id or -1 to cancel");
                            int reply = int.Parse(Console.ReadLine() ?? "-1");
                            if (reply != -1)
                            {
                                state = actions.Trade(reply);
                            }
                            break;

                        default:
                            return;
                    }
                }
                catch (AgentActionException ex)
                {
                    Console.WriteLine("Illegal action! Message: " + ex.Message);
                }
                catch(FormatException ex)
                {
                    Console.WriteLine("Illegal input! Message: " + ex.Message);
                }
            }
        }