//Läser data från xml-fil, skapar en representation av schackbräädet och returnerar det. public ChessPiece[,] LoadData() { if (!IsFileLocked()) { ChessPiece[,] board = new ChessPiece[8, 8]; XDocument doc = XDocument.Load(@".\chessdata\chessdata.xml"); var data = from item in doc.Descendants("ChessPiece") select new { posx = item.Element("posX").Value, posy = item.Element("posY").Value, color = item.Element("color").Value, type = item.Element("type").Value }; foreach (var c in data) { switch (c.type) { case "Chess2._0.King": ChessPiece king = new King(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = king; break; case "Chess2._0.Queen": ChessPiece queen = new Queen(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = queen; break; case "Chess2._0.Runner": ChessPiece runner = new Runner(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = runner; break; case "Chess2._0.Horse": ChessPiece horse = new Horse(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = horse; break; case "Chess2._0.Tower": ChessPiece tower = new Tower(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = tower; break; case "Chess2._0.Farmer": ChessPiece farmer = new Farmer(c.color, int.Parse(c.posx), int.Parse(c.posy)); board[int.Parse(c.posx), int.Parse(c.posy)] = farmer; break; } } return board; } else { throw new Exception(); } }
//Skriver schackbrädet i sitt nuvarande tillstånd till en xml-fil public void SaveData(ChessPiece[,] board) { watcher.EnableRaisingEvents = false; if (!IsFileLocked()) { ArrayList list = new ArrayList(); for (int x = 0; x < board.GetLength(0); x++) { for (int y = 0; y < board.GetLength(1); y++) { if (board[x, y] != null) { list.Add(board[x, y]); } } } //gör om arraylist till array ChessPiece[] listan = list.ToArray(typeof(ChessPiece)) as ChessPiece[]; XDocument xmlDoc = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XComment("Nils och Fridens coola xml doc!"), new XElement("ChessPieces", from ChessPiece in listan select new XElement("ChessPiece", new XElement("posX", ChessPiece.posX), new XElement("posY", ChessPiece.posY), new XElement("color", ChessPiece.Color), new XElement("type", ChessPiece.GetType()) ))); xmlDoc.Save(@".\chessdata\chessdata.xml"); watcher.EnableRaisingEvents = true; } else { throw new Exception(); } }
//Kollar om ett diagonalt drag är godkänt public bool checkDiagonal(Move move, ChessPiece[,] currentBoard) { if (move.getfromX() < move.gettoX() && move.getfromY() < move.gettoY()) //x och y ökar { int y = move.getfromY() + 1; for (int x = move.getfromX() + 1; x <= move.gettoX(); x++) { if (y < 8) { if (currentBoard[x, y] != null) //om någon pjäs står i vägen { if (x == move.gettoX() && y == move.gettoY()) //pjäsen har nått sitt mål { if (board.squareStatus(move, currentBoard) == 2)//Pjäsen har eliminerat en motståndare { return true; } } return false; } } y++; } return true; } if (move.getfromX() > move.gettoX() && move.getfromY() > move.gettoY()) //x och y minskar { int y = move.getfromY() - 1; for (int x = move.getfromX() - 1; x >= move.gettoX(); x--) { if (y < 8) { if (currentBoard[x, y] != null)//om någon pjäs står i vägen { if (x == move.gettoX() && y == move.gettoY()) //pjäsen har nått sitt mål { if (board.squareStatus(move, currentBoard) == 2)//Pjäsen har eliminerat en motståndare { return true; } } return false; } } y--; } return true; } if (move.getfromX() < move.gettoX() && move.getfromY() > move.gettoY()) //x ökar, y minskar { int y = move.getfromY() - 1; for (int x = move.getfromX() + 1; x <= move.gettoX(); x++) { if (y < 8) { if (currentBoard[x, y] != null)//om någon pjäs står i vägen { if (x == move.gettoX() && y == move.gettoY()) //pjäsen har nått sitt mål { if (board.squareStatus(move, currentBoard) == 2)//Pjäsen har eliminerat en motståndare { return true; } } return false; } } y--; } return true; } if (move.getfromX() > move.gettoX() && move.getfromY() < move.gettoY()) //x minskar, y ökar, alltid här { int y = move.getfromY() + 1; for (int x = move.getfromX() - 1; x >= move.gettoX(); x--) { if (y < 8) { if (currentBoard[x, y] != null)//om någon pjäs står i vägen { if (x == move.gettoX() && y == move.gettoY()) //pjäsen har nått sitt mål { if (board.squareStatus(move, currentBoard) == 2)//Pjäsen har eliminerat en motståndare { return true; } } return false; } } y++; } return true; } return false; }
//Kollar om det är ett tillåtet drag public bool isValid(Move move, String gameStatus, ChessPiece[,] currentBoard) { if (board.colourOfPiece(move, currentBoard) == gameStatus) { if (currentBoard[move.getfromX(), move.getfromY()].isValidMove(move) && isLeagalMove(move, currentBoard)) { if (isCheck(move, gameStatus)) { return false; } return true; } else { return false; } } return false; }
//Kollar om ett vertikalt drag är tillåtet public bool checkVertical(Move move, ChessPiece[,] currentBoard) { //kolla om det går att hoppa över skaer if (move.getfromY() < move.gettoY()) { for (int y = move.getfromY() + 1; y <= move.gettoY(); y++) { if (currentBoard[move.getfromX(), y] != null) { if (y == move.gettoY() && board.squareStatus(move, currentBoard) != 1) { return true; } return false; } } return true; } else if (move.getfromY() > move.gettoY()) { for (int y = move.getfromY() - 1; y >= move.gettoY(); y--) { if (currentBoard[move.getfromX(), y] != null) { if (y == move.gettoY() && (board.squareStatus(move, currentBoard) != 1)) { return true; } return false; } } return true; } return false; }
//Kollar om draget är tillåtet enligt reglerna public bool isLeagalMove(Move move, ChessPiece[,] currentBoard) { ChessPiece current = currentBoard[move.getfromX(), move.getfromY()]; if (current is Horse) { if (board.squareStatus(move, currentBoard) == 1)//En av spelarens egna pjäser står i vägen { return false; } return true; } else if (current is Farmer) { if ((move.getfromX() != move.gettoX() && board.squareStatus(move, currentBoard) == 2) || (move.gettoX() == move.getfromX() && board.squareStatus(move, currentBoard) == 3)) //bonden har tagit en enemy på sidan eller gått rak fram och inte träffat på ngn pjäs { return true; } return false; } else { if (move.isHorizontal() == true) { return checkHorizontal(move, currentBoard); } if (move.isVertical() == true) { return checkVertical(move, currentBoard); } if (move.isDiagonal() == true) { return checkDiagonal(move, currentBoard); } return false; } }
public void setBoard(ChessPiece[,] board) { this.board = board; }
//Kollar om ett horisontellt drag är tillåtet public bool checkHorizontal(Move move, ChessPiece[,] currentBoard) { if (move.getfromX() < move.gettoX()) { for (int x = move.getfromX() + 1; x <= move.gettoX(); x++) { if (currentBoard[x, move.getfromY()] != null) { if (x == move.gettoX() && board.squareStatus(move, currentBoard) != 1) { return true; } return false; } } return true; } if (move.getfromX() > move.gettoX()) { for (int x = move.getfromX() - 1; x >= move.gettoX(); x--) { if (currentBoard[x, move.getfromY()] != null) { if (x == move.gettoX() && board.squareStatus(move, currentBoard) != 1) { return true; } return false; } } return true; } return false; }
//1. Egen pjäs //2. Motståndares pjäs //3. Tom ruta public int squareStatus(Move move, ChessPiece[,] currentBoard) { if (currentBoard[move.gettoX(), move.gettoY()] != null) { ChessPiece p1 = currentBoard[move.getfromX(), move.getfromY()]; ChessPiece p2 = currentBoard[move.gettoX(), move.gettoY()]; if (p1.Color == p2.Color) { return 1; } else { return 2; } } return 3; }
public void setBoard(ChessPiece[,] brd) { board = brd; }
//returnerar kungen från brädet public ChessPiece getKingFromBoard(ChessPiece[,] temp, String gamestatus) { for (int x = 0; x <= temp.GetLength(0); x++) { for (int y = 0; y <= temp.GetLength(1); y++) { if (y < 8 && x < 8) { if (temp[x, y] != null) { ChessPiece piece = temp[x, y]; if (piece.Color == gamestatus && piece is King) { piece.posX = x; piece.posY = y; return piece; } } } } } return null; }
public ChessPiece[,] getCopy() { ChessPiece[,] boardToReturn = new ChessPiece[8,8]; for(int x = 0; x <= 7; x++) { for(int y = 0; y <= 7; y++) { if (board[x, y] != null) boardToReturn[x, y] = board[x,y]; } } return boardToReturn; }
//Returnerar färgen på pjäsen som flyttas public string colourOfPiece(Move move, ChessPiece[,] currentBoard) { if (currentBoard[move.getfromX(), move.getfromY()] != null) { return currentBoard[move.getfromX(), move.getfromY()].Color; } else return ""; }