/// <summary> /// Constructor that creates a new ChessMove object based on two ChessLocations and a ChessFlag /// </summary> /// <param name="from">From ChessLocation</param> /// <param name="to">To ChessLocation</param> /// <param name="flag">The ChessFlag to create the move with.</param> public ChessMove(ChessLocation from, ChessLocation to, ChessFlag flag) { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_ctor_ChessLocation_ChessLocation_ChessFlag); From = from; To = to; Flag = flag; ToStringPrefix = string.Empty; }
/// <summary> /// Gets or sets the piece on the board in a specified location; 0,0 is the upper left hand corner of the board. /// </summary> /// <param name="location">Location of the desired piece</param> /// <returns>ChessPiece</returns> public ChessPiece this[ChessLocation location] { get { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_get_ChessLocationIndexer); return(this[location.X, location.Y]); } set { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_set_ChessLocationIndexer); this[location.X, location.Y] = value; } }
public override bool Equals(object obj) { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessLocation_Equals); if (obj == null) { return(false); } if (!(obj is ChessLocation)) { return(false); } ChessLocation tmpLoc = (ChessLocation)obj; if ((this.X == tmpLoc.X) && (this.Y == tmpLoc.Y)) { return(true); } return(false); }
/// <summary> /// gets move for a specfic piece /// </summary> public static List<ChessMove> getmovesofpiece(StudentAI ai,ChessColor color, ChessBoard board,ChessLocation location) { List<ChessMove> piecemoves = new List<ChessMove>(); ChessMove move = new ChessMove(location,location); switch (board[location]) { case ChessPiece.BlackPawn: ChessMove bdiag1 = new ChessMove(location,new ChessLocation((location.X)-1,(location.Y)+1)); ChessMove bdown = new ChessMove(location,new ChessLocation((location.X),(location.Y)+1)); ChessMove bdown2 = new ChessMove(location, new ChessLocation((location.X), (location.Y) + 2)); ChessMove bdiag2 = new ChessMove(location,new ChessLocation((location.X)+1,(location.Y)+1)); if (ai.IsValidMove(board, bdiag1, color)) piecemoves.Add(bdiag1); if (ai.IsValidMove(board, bdown, color)) piecemoves.Add(bdown); if (ai.IsValidMove(board, bdiag2, color)) piecemoves.Add(bdiag2); if (ai.IsValidMove(board, bdown2, color)) piecemoves.Add(bdown2); break; case ChessPiece.WhitePawn: ChessMove wdiag1 = new ChessMove(location,new ChessLocation((location.X)-1,(location.Y)-1)); ChessMove wup = new ChessMove(location,new ChessLocation((location.X),(location.Y)-1)); ChessMove wup2 = new ChessMove(location, new ChessLocation((location.X), (location.Y) - 2)); ChessMove wdiag2 = new ChessMove(location,new ChessLocation((location.X)+1,(location.Y)-1)); if (ai.IsValidMove(board, wdiag1, color)) piecemoves.Add(wdiag1); if (ai.IsValidMove(board, wup, color)) piecemoves.Add(wup); if (ai.IsValidMove(board, wdiag2, color)) piecemoves.Add(wdiag2); if (ai.IsValidMove(board, wup2, color)) piecemoves.Add(wup2); break; case ChessPiece.BlackKing: for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { move = new ChessMove(location, new ChessLocation(location.X + i, location.Y + j)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); } } break; case ChessPiece.WhiteKing: for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { move = new ChessMove(location, new ChessLocation(location.X + i, location.Y + j)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); } } break; case ChessPiece.BlackKnight: move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + 1)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + -1)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + 2)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + -2)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + 1)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + -1)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + 2)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + -2)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); break; case ChessPiece.WhiteKnight: move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + 1)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 2, location.Y + -1)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + 2)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + 1, location.Y + -2)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + 1)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -2, location.Y + -1)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + 2)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); move = new ChessMove(location, new ChessLocation(location.X + -1, location.Y + -2)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); break; case ChessPiece.BlackBishop: case ChessPiece.WhiteBishop: bool flag = true; int x = 1; while(flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y + x)); if(ai.IsValidMove(board,move,color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y - x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y - x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y + x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } break; case ChessPiece.BlackRook: case ChessPiece.WhiteRook: flag = true; x = 1; while(flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while(flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while(flag) { move = new ChessMove(location, new ChessLocation(location.X, location.Y+x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while(flag) { move = new ChessMove(location, new ChessLocation(location.X, location.Y-x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } break; case ChessPiece.BlackQueen: case ChessPiece.WhiteQueen: flag = true; x = 1; while (flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y + x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y - x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y - x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y + x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X + x, location.Y)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X - x, location.Y)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X, location.Y + x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } x = 1; flag = true; while (flag) { move = new ChessMove(location, new ChessLocation(location.X, location.Y - x)); if (ai.IsValidMove(board, move, color)) piecemoves.Add(move); else flag = false; x++; } break; default: return piecemoves; } return piecemoves; }
private bool shouldAttemptToQueenPawn(int[,] state, ChessLocation pawnLocation) { if (state[pawnLocation.X, pawnLocation.Y] == SimpleState.Pawn) return false; for (int row = pawnLocation.Y + 1; row < state.GetLength(1); row++) { if (state[pawnLocation.X, row] != SimpleState.Empty) return false; } return true; }
/// <summary> /// Gets or sets the piece on the board in a specified location; 0,0 is the upper left hand corner of the board. /// </summary> /// <param name="location">Location of the desired piece</param> /// <returns>ChessPiece</returns> public ChessPiece this[ChessLocation location] { get { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_get_ChessLocationIndexer); return this[location.X, location.Y]; } set { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessBoard_set_ChessLocationIndexer); this[location.X, location.Y] = value; } }
/// <summary> /// Determines if the specified piece is in danger of being killed by any enemy pieces. /// </summary> /// <param name="stateToCheck">The current state of the board</param> /// <param name="pieceLocation">The location of the piece to check check if it is in danger</param> /// <returns>True if the piece can be killed by any enemy pieces, otherwise false</returns> public bool InDanger(int[,] stateToCheck, ChessLocation pieceLocation) { int columnCount = stateToCheck.GetLength(0); int rowCount = stateToCheck.GetLength(1); bool enemy = stateToCheck[pieceLocation.X, pieceLocation.Y] < SimpleState.Empty; int multiplier = enemy ? -1 : 1; int attackingPawn = multiplier * -SimpleState.Pawn; int attackingKnight = multiplier * -SimpleState.Knight; int attackingBishop = multiplier * -SimpleState.Bishop; int attackingRook = multiplier * -SimpleState.Rook; int attackingQueen = multiplier * -SimpleState.Queen; int attackingKing = multiplier * -SimpleState.King; int col, row; bool checkUp = true; bool checkDown = true; bool checkUpLeft = true; bool checkUpRight = true; bool checkLeft = true; bool checkRight = true; bool checkDownLeft = true; bool checkDownRight = true; bool done = false; int distance = 1; while (!done) { col = pieceLocation.X; row = pieceLocation.Y; int up = row - distance; int down = row + distance; int left = col - distance; int right = col + distance; if (checkUp && up >= 0 && stateToCheck[col, up] != SimpleState.Empty) { checkUp = false; if (stateToCheck[col, up] == attackingQueen || stateToCheck[col, up] == attackingRook) return true; if (distance == 1 && stateToCheck[col, up] == attackingKing) return true; } if (checkDown && down < rowCount && stateToCheck[col, down] != SimpleState.Empty) { checkDown = false; if (stateToCheck[col, down] == attackingQueen || stateToCheck[col, down] == attackingRook) return true; if (distance == 1 && stateToCheck[col, down] == attackingKing) return true; } if (checkLeft && left >= 0 && stateToCheck[left, row] != SimpleState.Empty) { checkLeft = false; if (stateToCheck[left, row] == attackingQueen || stateToCheck[left, row] == attackingRook) return true; if (distance == 1 && stateToCheck[left, row] == attackingKing) return true; } if (checkRight && right < columnCount && stateToCheck[right, row] != SimpleState.Empty) { checkRight = false; if (stateToCheck[right, row] == attackingQueen || stateToCheck[right, row] == attackingRook) return true; if (distance == 1 && stateToCheck[right, row] == attackingKing) return true; } if (checkUpLeft && up >= 0 && left >= 0 && stateToCheck[left, up] != SimpleState.Empty) { checkUpLeft = false; if (stateToCheck[left, up] == attackingQueen || stateToCheck[left, up] == attackingBishop) return true; if (distance == 1 && (stateToCheck[left, up] == attackingKing || (enemy && stateToCheck[left, up] == attackingPawn))) return true; } if (checkUpRight && up >= 0 && right < columnCount && stateToCheck[right, up] != SimpleState.Empty) { checkUpRight = false; if (stateToCheck[right, up] == attackingQueen || stateToCheck[right, up] == attackingBishop) return true; if (distance == 1 && (stateToCheck[right, up] == attackingKing || (enemy && stateToCheck[right, up] == attackingPawn))) return true; } if (checkDownLeft && down < rowCount && left >= 0 && stateToCheck[left, down] != SimpleState.Empty) { checkDownLeft = false; if (stateToCheck[left, down] == attackingQueen || stateToCheck[left, down] == attackingBishop) return true; if (distance == 1 && (stateToCheck[left, down] == attackingKing || (!enemy && stateToCheck[left, down] == attackingPawn))) return true; } if (checkDownRight && down < rowCount && right < columnCount && stateToCheck[right, down] != SimpleState.Empty) { checkDownRight = false; if (stateToCheck[right, down] == attackingQueen || stateToCheck[right, down] == attackingBishop) return true; if (distance == 1 && (stateToCheck[right, down] == attackingKing || (!enemy && stateToCheck[right, down] == attackingPawn))) return true; } done = !(checkUp || checkDown || checkLeft || checkRight || checkUpLeft || checkUpRight || checkDownLeft || checkDownRight); done = done || (up < 0 && left < 0 && down >= rowCount && right >= columnCount); distance++; } // Check knight left up col = pieceLocation.X - 2; row = pieceLocation.Y - 1; if (col >= 0 && row >= 0 && stateToCheck[col, row] == attackingKnight) return true; // Check knight left down col = pieceLocation.X - 2; row = pieceLocation.Y + 1; if (col >= 0 && row < rowCount && stateToCheck[col, row] == attackingKnight) return true; // Check knight up left col = pieceLocation.X - 1; row = pieceLocation.Y - 2; if (col >= 0 && row >= 0 && stateToCheck[col, row] == attackingKnight) return true; // Check knight up right col = pieceLocation.X + 1; row = pieceLocation.Y - 2; if (col < columnCount && row >= 0 && stateToCheck[col, row] == attackingKnight) return true; // Check knight right up col = pieceLocation.X + 2; row = pieceLocation.Y - 1; if (col < columnCount && row >= 0 && stateToCheck[col, row] == attackingKnight) return true; // Check knight right down col = pieceLocation.X + 2; row = pieceLocation.Y + 1; if (col < columnCount && row < rowCount && stateToCheck[col, row] == attackingKnight) return true; // Check knight down left col = pieceLocation.X - 1; row = pieceLocation.Y + 2; if (col >= 0 && row < rowCount && stateToCheck[col, row] == attackingKnight) return true; // Check knight down right col = pieceLocation.X + 1; row = pieceLocation.Y + 2; if (col < columnCount && row < rowCount && stateToCheck[col, row] == attackingKnight) return true; return false; }
/// <summary> /// Constructor that creates a new ChessMove object based on two ChessLocations /// </summary> /// <param name="from">From ChessLocation</param> /// <param name="to">To ChessLocation</param> public ChessMove(ChessLocation from, ChessLocation to) : this(from, to, ChessFlag.NoFlag) { Profiler.AddToMainProfile((int)ProfilerMethodKey.ChessMove_ctor_ChessLocation_ChessLocation); }