// Saves current save state. The filename is put in a textbox in the GUI public static void saveState(src.Board b, string fileName) { // Create directory if not exists bool exists = System.IO.Directory.Exists(savePath); if (!exists) { System.IO.Directory.CreateDirectory(savePath); } // Clear file, might be a better way string fullName = savePath + fileName + "_" + STATE + "." + fileEnding; File.WriteAllText(fullName, string.Empty); // Create Xml tree root XElement tree = new XElement("Board"); tree.Add(new XElement("Turn", b.getTurn())); // Get all squares with a piece var saveSquares = from s in b.getAllSquares() where s.getPiece() != null select s.getPiece(); // Create squares subtree tree foreach (src.Piece p in saveSquares) { XElement piece = new XElement("Piece", new XElement("X-coordinate", p.getX()), new XElement("Y-coordinate", p.getY()), new XElement("Type", p.GetType().Name), new XElement("Colour", p.getColour()), new XElement("Moved", p.movedFromInit())); if (p is src.Pawn) { piece.Add(new XElement("DoubleStepTurn", ((src.Pawn)p).getDoubleStepTurn())); } tree.Add(piece); } using (StreamWriter sw = File.AppendText(fullName)) { sw.WriteLine(tree); } }
public static List <Tuple <uint, uint> > getCover(src.Board board, src.Piece piece) { // Get cover List <Tuple <uint, uint> > cover = new List <Tuple <uint, uint> >(); int x = (int)piece.getX(); int y = (int)piece.getY(); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { // Do not cover self continue; } int nx = x + i; int ny = y + j; if (board.withinBoard(nx, ny)) { Square s = board.getSquareAt((uint)nx, (uint)ny); if ((piece.getColour() == "white" && s.getBlackCover() == 0) || (piece.getColour() == "black" && s.getWhiteCover() == 0)) { Tuple <uint, uint> t = new Tuple <uint, uint>((uint)nx, (uint)ny); if (!getEnemyKingReach(board, piece).Contains(t)) { cover.Add(t); } } } } } return(cover); }
//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); }
// Savefile that is saved after every move and loaded at startup public static void saveCurrent(src.Board board) { SaveManager.saveState(board, CURRENT); }
// Loads current game at startup public static bool loadCurrent(ref src.Board board) { return(SaveManager.loadState(ref board, CURRENT)); }
public void setBoard(src.Board board) { this.board = board; }