public Saver(Game game, String filename) { StreamWriter file = new StreamWriter(filename); file.WriteLine("<board numberofpawns=\"" + game.Board.NumberOfPawns + "\" height=\"" + game.Board.Height + "\" width=\"" + game.Board.Width + "\">"); for (int i = 0; i < game.Board.Squares.Count; i++) { Square s = game.Board.Squares[i]; file.Write("\t<" + s.GetType().Name + " id=\"" + s.Id + "\""); if (s.up >= 1) { file.Write(" up=\"" + s.up + "\""); } if (s.left >= 1) { file.Write(" left=\"" + s.left + "\""); } if (s.right >= 1) { file.Write(" right=\"" + s.right + "\""); } if (s.down > 1) { file.Write(" down=\"" + s.down + "\""); } if (s is FinishSquare) { file.Write(" x=\"" + s.X + "\" y=\"" + s.Y + "\""); } file.WriteLine("/>"); } for (int i = 0; i < game.Players.Count; i++) { Player player = game.Players.pop(); file.WriteLine(); file.WriteLine("\t<" + player.GetType().Name + " playersquareid=\"" + player.PlayerSquare.Id + "\" color=\"" + player.Color + "\" player=\"" + player.PlayerId + "\" />"); for (int j = 0; j < player.PlayerPawns.Count; j++) { Pawn p = player.PlayerPawns[j]; if(!(p.Square is PlayerSquare)) { file.WriteLine("\t<" + p.GetType().Name + " playerid=\"" + p.PlayerId + "\" squareid=\"" + p.Square.Id + "\" />"); } } } for (int i = 0; i < game.Board.Baricades.Count; i++) { BaricadePiece b = game.Board.Baricades[i]; if (b.Square == null) { file.WriteLine("<" + b.GetType().Name + " playerid=\"" + b.Player.PlayerId + "\"/>"); } else { file.WriteLine("<" + b.GetType().Name + " squareid=\"" + b.Square.Id + "\"/>"); } } file.WriteLine("</board>"); file.Close(); }
public Game Load(String uri) { XmlReader r = XmlReader.Create(uri); board = null; FinishSquare f = null; ForestSquare forest = null; Square previous = null; playerSquares = new List<PlayerSquare>(); linkList = new List<Square>(); playerList = new Circuit<Player>(); baricades = new List<BaricadePiece>(); pawns = new List<Pawn>(); baricadeSquares = new List<Square>(); connectors = new List<Connector>(); while (r.Read()) { if (r.Name.ToLower() == "square") { Square s = new Square(board); if (s.readElement(r)) { s.View = new VSquare(s); insertInto(s.Id, s); previous = s; } } else if (r.Name.ToLower() == "villagesquare") { VillageSquare s = new VillageSquare(board); if (s.readElement(r)) { insertInto(s.Id, s); previous = s; } } else if (r.Name.ToLower() == "baricadesquare") { BaricadeSquare s = new BaricadeSquare(board); if (s.readElement(r)) { insertInto(s.Id, s); baricadeSquares.Add(s); previous = s; } } else if (r.Name.ToLower() == "baricadevillagesquare") { BaricadeVillageSquare s = new BaricadeVillageSquare(board); if(s.readElement(r)) { insertInto(s.Id, s); baricadeSquares.Add(s); previous = s; } } else if (r.Name.ToLower() == "restsquare") { RestSquare s = new RestSquare(board); if(s.readElement(r)) { insertInto(s.Id,s); } } else if (r.Name.ToLower() == "playersquare") { PlayerSquare s = new PlayerSquare(board); if (s.readElement(r)) { playerSquares.Add(s); insertInto(s.Id, s); previous = s; } } else if (r.Name.ToLower() == "pawn") { Pawn p = new Pawn(); if (p.readElement(r)) { pawns.Add(p); } } else if (r.Name.ToLower() == "baricadepiece") { BaricadePiece b = new BaricadePiece(); if (b.readElement(r)) { baricades.Add(b); } } else if (r.Name.ToLower() == "lowrowsquare") { LowRowSquare s = new LowRowSquare(board); if (s.readElement(r)) { insertInto(s.Id, s); } } else if (r.Name.ToLower() == "connector") { Connector s = new Connector(board); if (s.readElement(r)) { insertInto(s.Id, s); connectors.Add(s); } } else if (r.Name.ToLower() == "player") { Player p = new Player(playerList); if (p.readElement(r)) { _numberofPlayers++; _numberOfHumanPlayers++; p.PlayerSquare = (PlayerSquare)linkList[find(p.PlayerSquareId)]; p.PlayerId = playerList.Count + 1; playerList.Add(p); } } else if (r.Name.ToLower() == "aiplayer") { AIPlayer p = new AIPlayer(playerList); if (p.readElement(r)) { _numberOfAIPlayers++; _numberOfHumanPlayers++; p.PlayerSquare = (PlayerSquare)linkList[find(p.PlayerSquareId)]; p.PlayerId = playerList.Count + 1; playerList.Add(p); } } else if (r.Name.ToLower() == "finishsquare") { FinishSquare s = new FinishSquare(board); if (s.readElement(r)) { f = s; insertInto(s.Id, s); previous = s; } } else if (r.Name.ToLower() == "forestsquare") { ForestSquare s = new ForestSquare(board); if (s.readElement(r)) { forest = s; insertInto(s.Id, s); previous = s; } } else if (r.Name.ToLower() == "board") { Board b = new Board(); if (b.readElement(r)) { board = b; _numberOfPawns = board.NumberOfPawns; } } } r.Close(); link(); setPiecesToSquares(); setPawnsToPlayer(); checkPlayers(); checkPieces(); playerList.setCurrent(_currentPlayer); setPosition(f, f.X, f.Y); conectors(); board.Squares = linkList; board.Baricades = baricades; if (forest != null) { board.ForestSquare = forest; } Game game = new Game(board, playerList, f); board.Game = game; return game; }