//create an XML version of the game public bool SaveGame(GameData game, string path) { // Create an XmlWriterSettings object with the correct options. XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = ("\t"), OmitXmlDeclaration = true, NewLineChars = Environment.NewLine }; // Create the XmlWriter object and write some content. XmlWriter writer = XmlWriter.Create(path, settings); //bug cannot open file if file already open //return false if a problem occurs creating the new file if (writer == null) { return(false); } // Opens the document writer.WriteStartDocument(); //Write element 'game' writer.WriteStartElement("game"); // Write game elements writer.WriteElementString("GameTitle", game.GameTitle); writer.WriteElementString("GamePath", game.GamePath); writer.WriteElementString("P1Name", game.P1Name); writer.WriteElementString("P2Name", game.P2Name); writer.WriteElementString("AutomaticBoard", game.AutomaticBoard ? "true" : "false"); writer.WriteElementString("BoardRowNumber", game.BoardRowNumber.ToString()); writer.WriteElementString("BoardColumnNumber", game.BoardColumnNumber.ToString()); writer.WriteElementString("PassTurn", game.PassTurn.ToString()); writer.WriteElementString("BoardSymmetry", game.BoardSymmetry); //Colors byte r = game.Color1.R, g = game.Color1.G, b = game.Color1.B, a = game.Color1.A; writer.WriteStartElement("Color1"); writer.WriteElementString("r", r.ToString()); writer.WriteElementString("g", g.ToString()); writer.WriteElementString("b", b.ToString()); writer.WriteElementString("a", a.ToString()); writer.WriteEndElement(); r = game.Color2.R; g = game.Color2.G; b = game.Color2.B; a = game.Color2.A; writer.WriteStartElement("Color2"); writer.WriteElementString("r", r.ToString()); writer.WriteElementString("g", g.ToString()); writer.WriteElementString("b", b.ToString()); writer.WriteElementString("a", a.ToString()); writer.WriteEndElement(); if (!string.IsNullOrWhiteSpace(game.GameDescription)) { writer.WriteElementString("GameDescription", game.GameDescription); } if (!string.IsNullOrWhiteSpace(game.GameHistory)) { writer.WriteElementString("GameHistory", game.GameHistory); } if (!string.IsNullOrWhiteSpace(game.GameStrategy)) { writer.WriteElementString("GameStrategy", game.GameStrategy); } if (!string.IsNullOrWhiteSpace(game.BoardImagePath)) { writer.WriteElementString("BoardImagePath", game.BoardImagePath); } //write zone nodes foreach (GameData.Zone zone in game.ZoneList) { writer.WriteStartElement("zone"); writer.WriteElementString("zoneName", zone.Name); writer.WriteElementString("zonePlayersAffected", zone.PlayersAffected.ToString()); writer.WriteStartElement("zonePositions"); foreach (string position in zone.Positions) { writer.WriteElementString("zonePosition", position); } writer.WriteEndElement(); writer.WriteEndElement(); } //write move nodes foreach (GameData.Move move in game.MovesList) { writer.WriteStartElement("move"); writer.WriteElementString("moveName", move.Name); writer.WriteElementString("Priority", move.Priority.ToString()); writer.WriteElementString("Capture", move.Capture.ToString()); writer.WriteElementString("moveType", move.Type); writer.WriteElementString("moveZoneType", move.ZoneType.ToString()); if (move.MoveZones != null) { if (move.MoveZones.Count > 0) { writer.WriteStartElement("moveZones"); foreach (GameData.Zone zone in move.MoveZones) { writer.WriteElementString("moveZone", zone.Name); } writer.WriteEndElement(); } } switch (move.Type) { case "drop": GameData.Drop drop = (GameData.Drop)move; writer.WriteElementString("DropType", drop.DropType); break; case "slide": GameData.Slide slide = (GameData.Slide)move; writer.WriteElementString("TravelType", slide.TravelType); writer.WriteElementString("Distance", slide.Distance.ToString()); writer.WriteStartElement("directions"); foreach (string direction in slide.Directions) { writer.WriteElementString("direction", direction); } writer.WriteEndElement(); break; case "jump": GameData.Jump jump = (GameData.Jump)move; writer.WriteElementString("TravelType", jump.TravelType); writer.WriteElementString("Distance", jump.Distance.ToString()); writer.WriteElementString("JumpOver", jump.JumpOver.ToString()); writer.WriteElementString("JumpCapture", jump.JumpCapture.ToString()); writer.WriteStartElement("directions"); foreach (string direction in jump.Directions) { writer.WriteElementString("direction", direction); } writer.WriteEndElement(); break; } writer.WriteEndElement(); } //write Piece nodes foreach (var piece in game.PieceList) { writer.WriteStartElement("piece"); writer.WriteElementString("pieceName", piece.Name); if (!string.IsNullOrWhiteSpace(piece.Help)) { writer.WriteElementString("Help", piece.Help); } if (!string.IsNullOrWhiteSpace(piece.Description)) { writer.WriteElementString("Description", piece.Description); } writer.WriteElementString("ImageP1Path", piece.ImageP1Path); writer.WriteElementString("ImageP2Path", piece.ImageP2Path); writer.WriteElementString("OffBoardP1", piece.OffBoardP1.ToString()); writer.WriteElementString("OffBoardP2", piece.OffBoardP2.ToString()); if (piece.PositionsP1 != null) { if (piece.PositionsP1.Count > 0) { writer.WriteStartElement("positionsP1"); foreach (var position in piece.PositionsP1) { writer.WriteElementString("positionP1", position); } writer.WriteEndElement(); } } if (piece.PositionsP1 != null) { if (piece.PositionsP2.Count > 0) { writer.WriteStartElement("positionsP2"); foreach (var position in piece.PositionsP2) { writer.WriteElementString("positionP2", position); } writer.WriteEndElement(); } } if (piece.MovesList.Count != 0) { if (piece.MovesList.Count > 0) { writer.WriteStartElement("pieceMoves"); foreach (var move in piece.MovesList) { writer.WriteElementString("pieceMove", move.Name); } writer.WriteEndElement(); } } if (piece.MultipleMovesList.Count != 0) { if (piece.MultipleMovesList.Count > 0) { writer.WriteStartElement("pieceMultipleMoves"); foreach (var multiMove in piece.MultipleMovesList) { writer.WriteElementString("pieceMultiMove", multiMove.Name); } writer.WriteEndElement(); } } if (piece.PiecePromotion != null) { writer.WriteStartElement("piecePromotion"); writer.WriteElementString("PromotionZone", piece.PiecePromotion.PromotionZone); writer.WriteStartElement("piecePromotionTypes"); foreach (var pieceType in piece.PiecePromotion.PieceTypes) { writer.WriteElementString("promotionPieceType", pieceType); } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); } //write Goal nodes foreach (var goal in game.GoalList) { writer.WriteStartElement("goal"); writer.WriteElementString("goalName", goal.Name); writer.WriteElementString("goalType", goal.Type); writer.WriteElementString("Value", goal.Value.ToString()); writer.WriteElementString("WinDrawLoss", goal.WinLossDraw.ToString()); writer.WriteElementString("goalPlayersAffected", goal.PlayersAffected.ToString()); if (goal.PieceTypesAffected.Count > 0) { foreach (var pieceType in goal.PieceTypesAffected) { writer.WriteElementString("goalPieceType", pieceType.Name); } } if (goal.GoalZones.Count > 0) { foreach (var zone in goal.GoalZones) { writer.WriteElementString("goalZone", zone.Name); } } if (goal.Directions.Count > 0) { foreach (var direction in goal.Directions) { writer.WriteElementString("goalDirection", direction); } } writer.WriteEndElement(); } //write multiple moves?? //Ends the 'game' element writer.WriteEndElement(); // Ends the document. writer.Flush(); writer.WriteEndDocument(); // close writer writer.Close(); return(true); // Write comments //writer.WriteComment("This file was generated using Abstract Games Creation Kit"); }
private static List <GameData.Move> LoadMoves(XmlNodeList moveNodes, GameData game) { List <GameData.Move> movesList = new List <GameData.Move>(); foreach (XmlNode node in moveNodes) { XmlElement moveNode = (XmlElement)node; string name = moveNode.GetElementsByTagName("moveName")[0].InnerText; int priority = Convert.ToInt32(moveNode.GetElementsByTagName("Priority")[0].InnerText); string type = moveNode.GetElementsByTagName("moveType")[0].InnerText; int capture = Convert.ToInt32(moveNode.GetElementsByTagName("Capture")[0].InnerText); int zoneType = Convert.ToInt32(moveNode.GetElementsByTagName("moveZoneType")[0].InnerText); List <string> zoneNameList = new List <string>(); List <GameData.Zone> zoneList = new List <GameData.Zone>(); XmlNode zonesNode = moveNode.GetElementsByTagName("moveZones")[0]; if (zonesNode != null) { foreach (XmlNode zoneNode in zonesNode) { string zoneName = zoneNode.Value; zoneNameList.Add(zoneName); } } foreach (var zone in zoneNameList) { zoneList.Add(game.FindZone(zoneNameList.IndexOf(zone))); //zone order in the XML file must be preserved! } GameData.Move move = new GameData.Move(name, type, priority, capture, zoneList, zoneType); switch (type) { case "drop": string dropType = moveNode.GetElementsByTagName("DropType")[0].InnerText; GameData.Drop drop = new GameData.Drop(move.Name, move.Type, move.Priority, move.Capture, move.MoveZones, move.ZoneType, dropType); movesList.Add(drop); break; case "slide": string slideTravelType = moveNode.GetElementsByTagName("TravelType")[0].InnerText; int slideDistance = Convert.ToInt32(moveNode.GetElementsByTagName("Distance")[0].InnerText); List <string> slideDirections = new List <string>(); XmlNode slideDirectionsNode = moveNode.GetElementsByTagName("directions")[0]; foreach (XmlNode direction in slideDirectionsNode) { XmlElement dNode = (XmlElement)direction; slideDirections.Add(dNode.InnerText); } GameData.Slide slide = new GameData.Slide(move.Name, move.Type, move.Priority, move.Capture, move.MoveZones, move.ZoneType, slideDirections, slideDistance, slideTravelType); movesList.Add(slide); break; case "jump": string jumpTravelType = moveNode.GetElementsByTagName("TravelType")[0].InnerText; int jumpDistance = Convert.ToInt32(moveNode.GetElementsByTagName("Distance")[0].InnerText); List <string> jumpDirections = new List <string>(); XmlNode jumpDirectionsNode = moveNode.GetElementsByTagName("directions")[0]; foreach (XmlNode direction in jumpDirectionsNode) { XmlElement dNode = (XmlElement)direction; jumpDirections.Add(dNode.InnerText); } int jumpOver = Convert.ToInt32(moveNode.GetElementsByTagName("JumpOver")[0].InnerText); int jumpCapture = Convert.ToInt32(moveNode.GetElementsByTagName("JumpCapture")[0].InnerText); GameData.Jump jump = new GameData.Jump(move.Name, move.Type, move.Priority, move.Capture, move.MoveZones, move.ZoneType, jumpDirections, jumpDistance, jumpTravelType, jumpOver, jumpCapture); movesList.Add(jump); break; } } return(movesList); }