public GameModel(int playerNumber) { Players = new Player[playerNumber]; List <CharacterRole> roleList = GameRules.GetCharacterRoles(playerNumber); roleList = (Utilities.Shuffle <CharacterRole>(roleList)); for (int i = 0; i < Players.Length; i++) { Players[i] = new Player(); Players[i].Type = (i == 0 ? PlayerType.Human : PlayerType.Computer); Players[i].Id = i; Players[i].Role = roleList[i]; Players[i].Name = GameRules.PlayerNames[i]; if (i != 0) { Players[i].Name += " (AI)"; } } Players = (Utilities.Shuffle <Player>(Players.ToList())).ToArray(); MissionTeamLengths = GameRules.GetMissionTeamLength(playerNumber); Missions = new Mission[MissionTeamLengths.Length]; for (int i = 0; i < Missions.Length; i++) { Missions[i] = new Mission(); } MissionNumber = MissionNumber.First; CurrentLeader = Players[0]; GamePhase = GamePhase.TeamPicking; }
private static HashSet <Player> PickATeam(GameModel model, Player player) { LogicalModel <Player> logicalModel = new LogicalModel <Player>( new HashSet <Player>(model.Players), GameRules.GetGoodCount(model.Players.Length)); logicalModel.FilterContainsElement(player); foreach (Mission mission in model.Missions) { if (mission != null && mission.MissionResult == MissionResult.Fail) { logicalModel.AddDirtySet(mission.Team); } } //Mission[] reverseMissions = model.Missions.Reverse() foreach (Mission mission in model.Missions) { if (mission != null && mission.MissionResult == MissionResult.Succeed && !model.Missions.Any(ms => ms != null && ms.MissionResult == MissionResult.Fail && mission.Team.IsProperSubsetOf(ms.Team)) && logicalModel.PossibleTeams.Any(pt => mission.Team.IsSubsetOf(pt))) { logicalModel.AddCleanSet(mission.Team); } } HashSet <Player> pickedTeam = Utilities.PickRandom(logicalModel.PossibleTeams); return(pickedTeam); }
public static GameModel ModelFromString(String lines) { GameModel model = new GameModel(); String[] split = lines.Split(new string[] { newLine }, StringSplitOptions.RemoveEmptyEntries); Utilities.LogToFile("MapFromString started."); //Utilities.LogToFile("Array length: " + split.Length); Queue <String> queue = new Queue <String>(split); //Utilities.LogToFile("Queue length: " + queue.Count); String line = ""; String stringToRead; while (line != longEnd) { line = queue.Dequeue(); } //Utilities.LogToFile("New Line: '" + newLine + "'"); #region Players line = queue.Dequeue(); List <Player> playerList = new List <Player>(); if (line == playerLabel) { line = queue.Dequeue(); while (line != longEnd) { Player player = new Player(); line = queue.Dequeue(); stringToRead = "Id: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); player.Id = Int32.Parse(stringValue); line = queue.Dequeue(); } stringToRead = "Name: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); player.Name = stringValue; line = queue.Dequeue(); } stringToRead = "Role: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); player.Role = (CharacterRole)Enum.Parse(typeof(CharacterRole), stringValue); line = queue.Dequeue(); } stringToRead = "Type: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); player.Type = (PlayerType)Enum.Parse(typeof(PlayerType), stringValue); line = queue.Dequeue(); } playerList.Add(player); } model.Players = playerList.ToArray(); model.MissionTeamLengths = GameRules.GetMissionTeamLength(model.Players.Length); } else { throw new ArgumentException("ModelFromString: No player info found!"); } #endregion #region Game State if (queue.Dequeue() == gameStateLabel) { line = queue.Dequeue(); line = queue.Dequeue(); stringToRead = "Current Phase: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); model.GamePhase = (GamePhase)Enum.Parse(typeof(GamePhase), stringValue); line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: No current phase found."); } stringToRead = "Game Result: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); model.GameResult = (MissionResult)Enum.Parse(typeof(MissionResult), stringValue); line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: No game result found."); } stringToRead = "Defeat Reason: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); model.DefeatReason = (DefeatType)Enum.Parse(typeof(DefeatType), stringValue); line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: No defeat reason found."); } stringToRead = "Current Leader: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); model.CurrentLeader = model.Players.FirstOrDefault(plr => plr.Name == stringValue); //int playerId = Int32.Parse(stringValue); //model.CurrentLeader = model.Players.FirstOrDefault(plr => plr.Id == playerId); if (model.CurrentLeader == null) { throw new ArgumentException("MapFromString: Leader not found."); } line = queue.Dequeue(); } } else { throw new ArgumentException("MapFromString: no info on GameState"); } #endregion #region Missions line = queue.Dequeue(); // Missions if (line == missionLabel) { int missionCounter = 0; model.Missions = new Mission[(int)MissionNumber.Last + 1]; for (int i = 0; i < model.Missions.Length; i++) { model.Missions[i] = new Mission(); } Mission currentMission = null; line = queue.Dequeue(); // shortEnd while (line != longEnd && missionCounter <= (int)MissionNumber.Last) { line = queue.Dequeue(); currentMission = new Mission(); stringToRead = "Team: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); HashSet <Player> playerTeam = PlayerTeamFromStr(stringValue, model.Players); if (playerTeam.Count > 0) { currentMission.SetTeam(playerTeam); } line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: no mission team found"); } stringToRead = "MissionResult: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); MissionResult missionResult = (MissionResult)Enum.Parse(typeof(MissionResult), stringValue); currentMission.MissionResult = missionResult; line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: no mission result found"); } stringToRead = "Missionresult votes: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); currentMission.MissionVoteOf = currentMission.IsInitialized ? MResultVotesFromStr(stringValue, model.Players, currentMission.Team) : null; line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: no mission result votes found"); } #region Votes int voteCounter = 0; Utilities.LogToFile("Current line: '" + line + "', Rows left: " + queue.Count); currentMission.Votes = new Vote[(int)VoteNumber.Last + 1]; for (int i = 0; i < currentMission.Votes.Length; i++) { currentMission.Votes[i] = new Vote(); } if (line == voteLabel) // \tVotes { line = queue.Dequeue(); // shortEnd while (line != longEnd && line != shortEnd && voteCounter <= (int)VoteNumber.Last) { Vote vote = currentMission.Votes[voteCounter]; Player leader = null; HashSet <Player> team = null; //Utilities.LogToFile("1. Current line: '" + line + "', Rows left: " + queue.Count); line = queue.Dequeue(); // shortEnd stringToRead = "\tLeader: "; //Utilities.LogToFile("2. Current line: '" + line + "', Rows left: " + queue.Count); if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); //int leaderId = Int32.Parse(stringValue); //if (!model.Players.Any(plr => plr.Id == leaderId)) //{ // throw new Exception("Persistence: Leader not found"); //} //leader = model.Players[leaderId]; leader = model.Players.FirstOrDefault(plr => plr.Name == stringValue); line = queue.Dequeue(); } else { throw new ArgumentException("MapFromString: Vote leader not found."); } stringToRead = "\tTeam: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); team = PlayerTeamFromStr(stringValue, model.Players); line = queue.Dequeue(); } else { throw new Exception("Persistence: Team undefined."); } vote.InitializeVote(leader, team, model.Players); stringToRead = "\tVoteResult: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); vote.VoteResult = (VoteType)Enum.Parse(typeof(VoteType), stringValue); line = queue.Dequeue(); } stringToRead = "\tPlayerVotes: "; if (line.StartsWith(stringToRead)) { String stringValue = line.Substring(stringToRead.Length); vote.VoteOfPlayer = PlayerVotesFromStr(stringValue, model.Players); line = queue.Dequeue(); } ++voteCounter; } } else { throw new ArgumentException("MapFromString: No mission votes found."); } #endregion currentMission.VoteNumber = (VoteNumber)(voteCounter != 0 ? voteCounter - 1 : 0); model.Missions[missionCounter] = currentMission; ++missionCounter; Utilities.LogToFile("Current line: " + line); } model.MissionNumber = (MissionNumber)(missionCounter != 0 ? missionCounter - 1 : 0); Utilities.LogToFile("MissionNumber wrote: " + (int)model.MissionNumber); } else { throw new ArgumentException("MapFromString: no mission info"); } #endregion #region Commands line = queue.Dequeue(); // Commands if (line == commandLabel) { Queue <BaseCommand> commandQueue = new Queue <BaseCommand>(); queue.Dequeue(); // endLine while ((line = queue.Dequeue()) != longEnd) { BaseCommand command = ConvertedCommand.CommandFromString(line, model); commandQueue.Enqueue(command); } model.CommandQueue = commandQueue; } #endregion return(model); }