//Loads the boardstate from the file written in the GUI textbox public static bool loadState(ref src.Board board, string fileName) { // Clear file, might be a better way string fullName = savePath + fileName + "_" + STATE + "." + fileEnding; // Check if file exists if (!File.Exists(fullName)) { return(false); } // Clear the board board.clearBoard(); // Read the savefile var loadFile = XElement.Load(fullName); IEnumerable <XElement> elements = from el in loadFile.Elements() select el; // Insert in board foreach (XElement el in elements) { if (el.Name == "Turn") { board.setTurn(Convert.ToUInt32(el.Value)); } else if (el.Name == "Piece") { // The pieces values uint x = Convert.ToUInt32(el.Element("X-coordinate").Value); uint y = Convert.ToUInt32(el.Element("Y-coordinate").Value); string type = el.Element("Type").Value; string col = el.Element("Colour").Value; bool moved = Convert.ToBoolean(el.Element("Moved").Value); src.Piece piece; switch (type) { case "Pawn": piece = new src.Pawn(x, y, col); ((src.Pawn)piece).setDoubleStepTurn(Convert.ToUInt32(el.Element("DoubleStepTurn").Value)); break; case "Rook": piece = new src.Rook(x, y, col); break; case "Knight": piece = new src.Knight(x, y, col); break; case "Bishop": piece = new src.Bishop(x, y, col); break; case "Queen": piece = new src.Queen(x, y, col); break; case "King": piece = new src.King(x, y, col); break; default: //Should not happen return(false); } // Update pieces piece.setHasMoved(moved); board.setPiece(x, y, piece); } } return(true); }
//Loads the boardstate from the file written in the GUI textbox public static bool loadState(ref src.Board board, string fileName) { // Clear file, might be a better way string fullName = savePath + fileName + "_" + STATE + "." + fileEnding; // Check if file exists if (!File.Exists(fullName)) { return false; } // Clear the board board.clearBoard(); // Read the savefile var loadFile = XElement.Load(fullName); IEnumerable<XElement> elements = from el in loadFile.Elements() select el; // Insert in board foreach (XElement el in elements) { if (el.Name == "Turn") { board.setTurn(Convert.ToUInt32(el.Value)); } else if (el.Name == "Piece") { // The pieces values uint x = Convert.ToUInt32(el.Element("X-coordinate").Value); uint y = Convert.ToUInt32(el.Element("Y-coordinate").Value); string type = el.Element("Type").Value; string col = el.Element("Colour").Value; bool moved = Convert.ToBoolean(el.Element("Moved").Value); src.Piece piece; switch (type) { case "Pawn": piece = new src.Pawn(x, y, col); ((src.Pawn)piece).setDoubleStepTurn(Convert.ToUInt32(el.Element("DoubleStepTurn").Value)); break; case "Rook": piece = new src.Rook(x, y, col); break; case "Knight": piece = new src.Knight(x, y, col); break; case "Bishop": piece = new src.Bishop(x, y, col); break; case "Queen": piece = new src.Queen(x, y, col); break; case "King": piece = new src.King(x, y, col); break; default: //Should not happen return false; } // Update pieces piece.setHasMoved(moved); board.setPiece(x, y, piece); } } return true; }