static void Main() { CardSetPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\JoePitt\\Cards\\"; AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Running baseUI = new Running(); baseUI.Show(); waiting = new Waiting(); try { if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null) { string arg = Uri.UnescapeDataString(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0].Substring(8)); if (File.Exists(arg)) { if (arg.EndsWith(".cardset")) { Dealer.InstallCardSet(arg); } } } } catch { } NewGame: if (Setup()) { while (CurrentGame.Playable) { LeaderBoard.UpdateScores(); CurrentPlayer = CurrentGame.LocalPlayers[0]; CurrentPlayer.NextCommand = "GAMEUPDATE"; CurrentPlayer.NewCommand = true; while (!CurrentPlayer.NewResponse) { Application.DoEvents(); } string[] response = CurrentPlayer.LastResponse.Split(' '); CurrentPlayer.NewResponse = false; switch (response[0]) { case "WAITING": CurrentGame.Stage = 'W'; Wait(); break; case "PLAYING": CurrentGame.Stage = 'P'; CurrentGame.Round = Convert.ToInt32(response[2]); Play(); break; case "VOTING": CurrentGame.Stage = 'V'; Vote(); break; case "END": CurrentGame.Stage = 'E'; if (Replay()) { goto NewGame; } else { Exit(); } break; default: MessageBox.Show("Unexpected Error! Unknown Game State, Application will exit!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); Exit(); break; } } CurrentGame.Stop(); } Exit(); }
/// <summary> /// Loads a Card Set from its XML File. /// </summary> /// <param name="cardSetGuid">The GUID of the Card Set to load.</param> /// <exception cref="ArgumentNullException">Thrown if no Guid is provided.</exception> /// <exception cref="FileNotFoundException">Thrown if the card set cannot be found.</exception> /// <exception cref="XmlException">Thrown if the card set XML is corrupted.</exception> /// <exception cref="ApplicationException">Thrown if the card set is corrupted.</exception> public CardSet(string cardSetGuid) { if (string.IsNullOrEmpty(cardSetGuid)) { throw new ArgumentNullException("cardSetGuid"); } if (!Dealer.TestCardSetPath()) { throw new IOException("Card Set Path does not exist, and could not be created."); } string[] files = Directory.GetFiles(Program.CardSetPath, "*" + cardSetGuid + ".cardset"); if (files == null) { throw new FileNotFoundException("Cannot find card set", Program.CardSetPath + "\\*" + cardSetGuid + ".cardset"); } XmlDocument cardSetDoc = new XmlDocument(); cardSetDoc.Load(files[0]); XmlElement cardSetInfo = (XmlElement)cardSetDoc.GetElementsByTagName("CardSet")[0]; string[] setInfo = new string[5]; CardSetGuid = cardSetInfo.GetAttribute("GUID"); Name = setInfo[1] = cardSetInfo.GetAttribute("Name"); Version = setInfo[2] = cardSetInfo.GetAttribute("Version"); XmlElement xmlBlackCards = (XmlElement)cardSetDoc.GetElementsByTagName("BlackCards")[0]; XmlElement xmlWhiteCards = (XmlElement)cardSetDoc.GetElementsByTagName("WhiteCards")[0]; SHA256CryptoServiceProvider hasher = new SHA256CryptoServiceProvider(); byte[] allCards = Encoding.Default.GetBytes(xmlBlackCards.InnerXml + xmlWhiteCards.InnerXml); byte[] hash = hasher.ComputeHash(allCards); Hash = Convert.ToBase64String(hash); if (cardSetInfo.GetAttribute("Hash") == Hash) { } else { throw new FormatException("Card Set " + Name + " Corrupt."); } BlackCardCount = 0; BlackCards = new Dictionary <string, Card>(); BlackCardIndex = new Dictionary <int, string>(); XmlNodeList CardBlock = cardSetDoc.GetElementsByTagName("CardPack"); CardBlock = cardSetDoc.GetElementsByTagName("BlackCards"); XmlNodeList Cards = CardBlock[0].ChildNodes; foreach (XmlElement Card in Cards) { string cardID = CardSetGuid.ToString() + "/" + Card.Attributes["ID"].Value; BlackCards.Add(cardID, new Card(cardID, Card.InnerText, Convert.ToInt32(Card.Attributes["Needs"].Value))); BlackCardCount++; BlackCardIndex.Add(BlackCardCount, cardID); } WhiteCardCount = 0; WhiteCards = new Dictionary <string, Card>(); WhiteCardIndex = new Dictionary <int, string>(); CardBlock = cardSetDoc.GetElementsByTagName("WhiteCards"); Cards = CardBlock[0].ChildNodes; foreach (XmlElement Card in Cards) { string cardID = cardSetGuid + "/" + Card.Attributes["ID"].Value; WhiteCards.Add(cardID, new Card(cardID, Card.InnerText)); WhiteCardCount++; WhiteCardIndex.Add(WhiteCardCount, cardID); } Dealer.ShuffleCards(BlackCardIndex); Dealer.ShuffleCards(WhiteCardIndex); }
/// <summary> /// Initalise a game including the global settings. /// </summary> /// <param name="type">The Type of game to setup.</param> /// <param name="playerNames">List of player names.</param> public Game(char type, List <string> playerNames) { if (playerNames == null) { throw new ArgumentNullException("playerNames"); } GameType = type; // Settings Round = 0; if (type != 'J') { Rounds = Properties.Settings.Default.Rounds; CardsPerUser = Properties.Settings.Default.Cards; NeverHaveI = Properties.Settings.Default.NeverHaveI; RebootingTheUniverse = Properties.Settings.Default.Rebooting; Cheats = Properties.Settings.Default.Cheats; // Deal First Hand PrepareDeck(); List <Tuple <int, Card> > hands = new List <Tuple <int, Card> >(); try { hands = Dealer.Deal(GameSet, playerNames.Count, CardsPerUser); } catch (ApplicationException ex) { MessageBox.Show("Error, game not started. " + ex.Message, "Game Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); Playable = false; return; } // Setup Players Players = new List <Player>(); int player = 0; foreach (string playerName in playerNames) { List <Card> hand = new List <Card>(); foreach (Tuple <int, Card> card in hands.GetRange(player * CardsPerUser, CardsPerUser)) { hand.Add(card.Item2); } Players.Add(new Player(playerName, hand)); if (Players[player].Name.ToLower().StartsWith("[bot]")) { BotCount++; } else { PlayerCount++; } player++; } //get game ready CurrentBlackCard = 0; HostNetwork = new ServerNetworking(this); LocalPlayers = new List <ClientNetworking>(); Stage = 'W'; Playable = true; NextRound(); } else { Players = new List <Cards.Player>(); Players.Add(new Player(playerNames[0], new List <Card>())); } }