public override ChessBoard MovePiece(ChessBoard board, int[] coord, int[] desiredCoord) { Coord desCoord = new Coord { CoordX = desiredCoord[0], CoordY = desiredCoord[1] }; List <Coord> pos = new List <Coord>(); if (Color == Color.White) { if (coord[0] != desiredCoord[0]) // If move on row { if (coord[0] > desiredCoord[0]) // If rook below target { int y = 1; for (int i = coord[0]; i > desiredCoord[0]; i--) { if (board.IsCaseEmpty(coord[0] - y, coord[1])) { pos.Add(new Coord { CoordX = coord[0] - y, CoordY = coord[1] }); y++; } else if (board.ContainsEnemyPiece(coord[0] - y, coord[1], Color)) { pos.Add(new Coord { CoordX = coord[0] - y, CoordY = coord[1] }); break; } else { break; } } } else // Rook upper target { int y = 1; for (int i = coord[0]; i < desiredCoord[0]; i++) { if (board.IsCaseEmpty(coord[0] + y, coord[1])) { pos.Add(new Coord { CoordX = coord[0] + y, CoordY = coord[1] }); y++; } else if (board.ContainsEnemyPiece(coord[0] + y, coord[1], Color)) { pos.Add(new Coord { CoordX = coord[0] + y, CoordY = coord[1] }); break; } else { break; } } } } else if (coord[1] != desiredCoord[1]) { if (coord[1] < desiredCoord[1]) // move on X right { int y = 1; for (int i = coord[1]; i < desiredCoord[1]; i++) { if (board.IsCaseEmpty(coord[0], coord[1] + y)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] + y }); y++; } else if (board.ContainsEnemyPiece(coord[0], coord[1] + y, Color)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] + y }); break; } else { break; } } } else // move on X left { int y = 1; for (int i = coord[1]; i > desiredCoord[1]; i--) { if (board.IsCaseEmpty(coord[0], coord[1] - y)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] - y }); y++; } else if (board.ContainsEnemyPiece(coord[0], coord[1] - y, Color)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] - y }); break; } else { break; } } } } } else // Color.Black { if (coord[0] != desiredCoord[0]) // If move on row { if (coord[0] < desiredCoord[0]) { int y = 1; for (int i = coord[0]; i < desiredCoord[0]; i++) { if (board.IsCaseEmpty(coord[0] + y, coord[1])) { pos.Add(new Coord { CoordX = coord[0] + y, CoordY = coord[1] }); y++; } else if (board.ContainsEnemyPiece(coord[0] + y, coord[1], Color)) { pos.Add(new Coord { CoordX = coord[0] + y, CoordY = coord[1] }); break; } else { break; } } } else { int y = 1; for (int i = coord[0]; i > desiredCoord[0]; i--) { if (board.IsCaseEmpty(coord[0] - y, coord[1])) { pos.Add(new Coord { CoordX = coord[0] - y, CoordY = coord[1] }); y++; } else if (board.ContainsEnemyPiece(coord[0] - y, coord[1], Color)) { pos.Add(new Coord { CoordX = coord[0] - y, CoordY = coord[1] }); break; } else { break; } } } } else if (coord[1] != desiredCoord[1]) { if (coord[1] < desiredCoord[1]) // move on X right { int y = 1; for (int i = coord[1]; i < desiredCoord[1]; i++) { if (board.IsCaseEmpty(coord[0], coord[1] + y)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] + y }); y++; } else if (board.ContainsEnemyPiece(coord[0], coord[1] + y, Color)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] + y }); break; } else { break; } } } else // move on X left { int y = 1; for (int i = coord[1]; i > desiredCoord[1]; i--) { if (board.IsCaseEmpty(coord[0], coord[1] - y)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] - y }); y++; } else if (board.ContainsEnemyPiece(coord[0], coord[1] - y, Color)) { pos.Add(new Coord { CoordX = coord[0], CoordY = coord[1] - y }); break; } else { break; } } } } } if (ObjectContainsValue(pos, desCoord)) { Piece tmpPiece = board.GetCaseAtPos(coord[0], coord[1]).PieceBoard; board.Board[coord[0], coord[1]].PieceBoard = new EmptyPiece(); board.Board[desiredCoord[0], desiredCoord[1]].PieceBoard = tmpPiece; return(board); } else { return(null); } }
public override ChessBoard MovePiece(ChessBoard board, int[] coord, int[] desiredCoord) { Coord desCoord = new Coord { CoordY = desiredCoord[0], CoordX = desiredCoord[1] }; List <Coord> pos = new List <Coord>(); // SWW if (coord[0] + 1 == desiredCoord[0] && coord[1] - 2 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] + 1, coord[1] - 2, Color)) { pos.Add(new Coord { CoordY = coord[0] + 1, CoordX = coord[1] - 2 }); } } // SW if (coord[0] + 2 == desiredCoord[0] && coord[1] - 1 == desiredCoord[1]) // & Check if move is legal { if (!board.ContainsEnemyPiece(coord[0] + 2, coord[1] - 1, Color)) { pos.Add(new Coord { CoordY = coord[0] + 2, CoordX = coord[1] - 1 }); } } // NWW if (coord[0] - 1 == desiredCoord[0] && coord[1] - 2 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] - 1, coord[1] - 2, Color)) { pos.Add(new Coord { CoordY = coord[0] - 1, CoordX = coord[1] - 2 }); } } // NW if (coord[0] - 2 == desiredCoord[0] && coord[1] - 1 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] - 2, coord[1] - 1, Color)) { pos.Add(new Coord { CoordY = coord[0] - 2, CoordX = coord[1] - 1 }); } } // SEE if (coord[0] + 1 == desiredCoord[0] && coord[1] + 2 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] + 1, coord[1] + 2, Color)) { pos.Add(new Coord { CoordY = coord[0] + 1, CoordX = coord[1] + 2 }); } } // SE if (coord[0] + 2 == desiredCoord[0] && coord[1] + 1 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] + 2, coord[1] + 1, Color)) { pos.Add(new Coord { CoordY = coord[0] + 2, CoordX = coord[1] + 1 }); } } // NEE if (coord[0] - 1 == desiredCoord[0] && coord[1] + 2 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] - 1, coord[1] + 2, Color)) { pos.Add(new Coord { CoordY = coord[0] - 1, CoordX = coord[1] + 2 }); } } // NE if (coord[0] - 2 == desiredCoord[0] && coord[1] + 1 == desiredCoord[1]) // & Check if move is legal { if (board.ContainsEnemyPiece(coord[0] - 2, coord[1] + 1, Color)) { pos.Add(new Coord { CoordY = coord[0] - 2, CoordX = coord[1] + 1 }); } } if (ObjectContainsValue(pos, desCoord)) { Piece tmpPiece = board.GetCaseAtPos(coord[0], coord[1]).PieceBoard; board.Board[coord[0], coord[1]].PieceBoard = new EmptyPiece(); board.Board[desiredCoord[0], desiredCoord[1]].PieceBoard = tmpPiece; return(board); } else { return(null); } }