Esempio n. 1
0
        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);
                }
            }
        }
Esempio n. 2
0
        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);
                }
            }
        }
Esempio n. 3
0
        public void PerformTurn(IGameState state, IGameActions actions)
        {
            if (!silent)
                Console.WriteLine(id + ": Performing main turn");

            for (bool changed = true; changed; )
            {
                changed = false;
                var resources = state.GetOwnResources();

                //Build city
                if (state.GetCitiesLeft(id) > 0 && resources.Count(r => r == Resource.Grain) >= 2 && resources.Count(r => r == Resource.Ore) >= 3)
                {
                    var pos = state.Board.GetPossibleCities(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state = actions.BuildCity(FindBestIntersection(pos,state.Board));
                    }
                }
                //Build settlement
                if (!changed && state.GetSettlementsLeft(id) > 0 && resources.Contains(Resource.Grain) && resources.Contains(Resource.Wool) && resources.Contains(Resource.Lumber) && resources.Contains(Resource.Brick))
                {
                    var pos = state.Board.GetPossibleSettlements(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state = actions.BuildSettlement(FindBestIntersection(pos,state.Board));
                    }
                }
                //Build road
                if (!changed && state.GetRoadsLeft(id) > 0 && resources.Contains(Resource.Lumber) && resources.Contains(Resource.Brick))
                {
                    var pos = state.Board.GetPossibleRoads(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state = actions.BuildRoad(FindBestRoad(pos, state.Board));
                    }
                }

                //Trade players
                if (!changed && Enum.GetValues(typeof(Resource)).Cast<Resource>().Any(r => resources.Count(res => res == r) > 2))
                {
                    // trade 1 of most for 1 missing
                    List<List<Resource>> give = resources.OrderByDescending(r => resources.Count(res => res == r))
                                                    .GroupBy(r => resources.Count(res => res == r)).First().Distinct()
                                                    .Select(r => { var list = new List<Resource>(); list.Add(r); return list; }).ToList();
                    List<List<Resource>> take = Enum.GetValues(typeof(Resource)).Cast<Resource>()
                                                    .OrderBy(r => resources.Count(res => res == r))
                                                    .GroupBy(r => resources.Count(res => res == r)).First()
                                                    .Select(r => { var list = new List<Resource>(); list.Add(r); return list; }).ToList();
                    if (give.Count > 0 && take.Count > 0)
                    {
                        Dictionary<int,ITrade> answers = actions.ProposeTrade(give, take);

                        if (answers.Count > 0)
                        {
                            // trade with lowest score
                            int otherPlayer = answers.OrderBy(kv => state.GetPlayerScore(kv.Key)).First().Key;
                            state = actions.Trade(otherPlayer);
                            changed = true;
                        }
                    }
                }

                //Trade bank
                foreach (Resource give in Enum.GetValues(typeof(Resource)))
                {
                    if (changed) break;
                    if (resources.Count(r => r == give) > 4)
                    {
                        foreach (Resource take in Enum.GetValues(typeof(Resource)))
                        {
                            if (changed) break;
                            if (resources.Count(r => r == take) == 0 && state.ResourceBank[(int)take] > 0)
                            {
                                state = actions.TradeBank(give, take);
                                changed = true;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public void PerformTurn(IGameState state, IGameActions actions)
        {
            if (!silent)
            {
                Console.WriteLine(id + ": Performing main turn");
            }

            for (bool changed = true; changed;)
            {
                changed = false;
                var resources = state.GetOwnResources();

                //Build city
                if (state.GetCitiesLeft(id) > 0 && resources.Count(r => r == Resource.Grain) >= 2 && resources.Count(r => r == Resource.Ore) >= 3)
                {
                    var pos = state.Board.GetPossibleCities(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state   = actions.BuildCity(FindBestIntersection(pos, state.Board));
                    }
                }
                //Build settlement
                if (!changed && state.GetSettlementsLeft(id) > 0 && resources.Contains(Resource.Grain) && resources.Contains(Resource.Wool) && resources.Contains(Resource.Lumber) && resources.Contains(Resource.Brick))
                {
                    var pos = state.Board.GetPossibleSettlements(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state   = actions.BuildSettlement(FindBestIntersection(pos, state.Board));
                    }
                }
                //Build road
                if (!changed && state.GetRoadsLeft(id) > 0 && resources.Contains(Resource.Lumber) && resources.Contains(Resource.Brick))
                {
                    var pos = state.Board.GetPossibleRoads(id);
                    if (pos.Length > 0)
                    {
                        changed = true;
                        state   = actions.BuildRoad(FindBestRoad(pos, state.Board));
                    }
                }

                //Trade players
                if (!changed && Enum.GetValues(typeof(Resource)).Cast <Resource>().Any(r => resources.Count(res => res == r) > 2))
                {
                    // trade 1 of most for 1 missing
                    List <List <Resource> > give = resources.OrderByDescending(r => resources.Count(res => res == r))
                                                   .GroupBy(r => resources.Count(res => res == r)).First().Distinct()
                                                   .Select(r => { var list = new List <Resource>(); list.Add(r); return(list); }).ToList();
                    List <List <Resource> > take = Enum.GetValues(typeof(Resource)).Cast <Resource>()
                                                   .OrderBy(r => resources.Count(res => res == r))
                                                   .GroupBy(r => resources.Count(res => res == r)).First()
                                                   .Select(r => { var list = new List <Resource>(); list.Add(r); return(list); }).ToList();
                    if (give.Count > 0 && take.Count > 0)
                    {
                        Dictionary <int, ITrade> answers = actions.ProposeTrade(give, take);

                        if (answers.Count > 0)
                        {
                            // trade with lowest score
                            int otherPlayer = answers.OrderBy(kv => state.GetPlayerScore(kv.Key)).First().Key;
                            state   = actions.Trade(otherPlayer);
                            changed = true;
                        }
                    }
                }


                //Trade bank
                foreach (Resource give in Enum.GetValues(typeof(Resource)))
                {
                    if (changed)
                    {
                        break;
                    }
                    if (resources.Count(r => r == give) > 4)
                    {
                        foreach (Resource take in Enum.GetValues(typeof(Resource)))
                        {
                            if (changed)
                            {
                                break;
                            }
                            if (resources.Count(r => r == take) == 0 && state.ResourceBank[(int)take] > 0)
                            {
                                state   = actions.TradeBank(give, take);
                                changed = true;
                            }
                        }
                    }
                }
            }
        }