/// <summary> /// Find all possible Moves of a Chesspiece in position of a State but not check the Chess Rule /// </summary> /// <param name="arrState"></param> /// <param name="pos"></param> /// <returns></returns> public static ArrayList FindAllPosibleMovesNotRule(ChessState[,] arrState, Point pos, eChessSide ownside) { ArrayList arrMove = new ArrayList(); switch (arrState[pos.X, pos.Y].Type) { case eChessPieceType.Bishop: arrMove = ChessPiece_Bishop.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.King: arrMove = ChessPiece_King.FindAllPosibleMoves(arrState, pos, ownside); break; case eChessPieceType.Knight: arrMove = ChessPiece_Knight.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.Pawn: arrMove = ChessPiece_Pawn.FindAllPosibleMoves(arrState, pos, ownside); break; case eChessPieceType.Queen: arrMove = ChessPiece_Queen.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.Rook: arrMove = ChessPiece_Rook.FindAllPosibleMoves(arrState, pos); break; } return(arrMove); }
/// <summary> /// Find all possible Moves of a Chesspiece in position of a State /// </summary> /// <param name="arrState"></param> /// <param name="pos"></param> /// <returns></returns> public static ArrayList FindAllPosibleMoves(ChessState[,] arrState, Point pos, eChessSide ownside) { ArrayList arrMove = new ArrayList(); switch (arrState[pos.X, pos.Y].Type) { case eChessPieceType.Bishop: arrMove = ChessPiece_Bishop.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.King: arrMove = ChessPiece_King.FindAllPosibleMoves(arrState, pos, ownside); break; case eChessPieceType.Knight: arrMove = ChessPiece_Knight.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.Pawn: arrMove = ChessPiece_Pawn.FindAllPosibleMoves(arrState, pos, ownside); break; case eChessPieceType.Queen: arrMove = ChessPiece_Queen.FindAllPosibleMoves(arrState, pos); break; case eChessPieceType.Rook: arrMove = ChessPiece_Rook.FindAllPosibleMoves(arrState, pos); break; } if (arrMove.Count == 0) { return(arrMove); } ArrayList arr_CanMoves = new ArrayList(); ChessState[,] NewState = new ChessState[10, 10]; for (int i = 0; i <= 9; i++) { for (int j = 0; j <= 9; j++) { NewState[i, j] = new ChessState(arrState[i, j].Type, arrState[i, j].Side, arrState[i, j].Moves); } } foreach (ChessMove m in arrMove) { //If one move that makes king will be checked -> remove PredictMove(NewState, m); if (Checked(NewState, arrState[pos.X, pos.Y].Side, ownside) == false) { arr_CanMoves.Add(m); } UnPredictMove(NewState, m); } return(arr_CanMoves); }
public static ArrayList FindWhoCheckAndKingPosition(ChessState[,] arrState, eChessSide side, eChessSide ownside) { ArrayList WhoCheck = new ArrayList(); ArrayList cantMoves = new ArrayList(); int i, j; for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { if (arrState[i, j].Side != side) { if (arrState[i, j].Type == eChessPieceType.Bishop) { cantMoves = ChessPiece_Bishop.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.King) { cantMoves = ChessPiece_King.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Knight) { cantMoves = ChessPiece_Knight.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Pawn) { cantMoves = ChessPiece_Pawn.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Queen) { cantMoves = ChessPiece_Queen.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Rook) { cantMoves = ChessPiece_Rook.FindAllPosibleMoves(arrState, new Point(i, j)); } foreach (ChessMove p in cantMoves) { if (arrState[p.MoveTo.X, p.MoveTo.Y].Side == side && arrState[p.MoveTo.X, p.MoveTo.Y].Type == eChessPieceType.King) { WhoCheck.Add(p); break; } } } } } return(WhoCheck); }
/// <summary> /// Check Is that one side is checked? /// </summary> /// <param name="arrState"></param> /// <param name="side"></param> /// <returns></returns> public static bool Checked(ChessState[,] arrState, eChessSide side, eChessSide ownside) { int i, j; ArrayList cantMoves = new ArrayList(); for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { if (arrState[i, j].Side != side) { if (arrState[i, j].Type == eChessPieceType.Bishop) { cantMoves = ChessPiece_Bishop.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.King) { cantMoves = ChessPiece_King.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Knight) { cantMoves = ChessPiece_Knight.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Pawn) { cantMoves = ChessPiece_Pawn.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Queen) { cantMoves = ChessPiece_Queen.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Rook) { cantMoves = ChessPiece_Rook.FindAllPosibleMoves(arrState, new Point(i, j)); } foreach (ChessMove p in cantMoves) { if (arrState[p.MoveTo.X, p.MoveTo.Y].Side == side && arrState[p.MoveTo.X, p.MoveTo.Y].Type == eChessPieceType.King) { return(true); } } } } } return(false); }
/// <summary> /// Evaluate value of ChessBoard for a side /// </summary> /// <param name="arrState"></param> /// <param name="myside"></param> /// <returns></returns> private static int EvaluateChessBoard(ChessState[,] arrState, eChessSide myside) { int value = 0; eChessSide opSide = eChessSide.Black; if (myside == eChessSide.Black) { opSide = eChessSide.White; } bool isGonnaDraw = true; bool isEndGame = IsEndGame(arrState, myside); int countMyKnight = 0; int countOpKnight = 0; bool myWhiteBishopAvailable = false; bool myBlackBishopAvailable = false; bool opWhiteBishopAvailable = false; bool opBlackBishopAvailable = false; int i, j; for (i = 1; i <= 8; i++) { for (j = 1; j <= 8; j++) { if (arrState[i, j].Type > 0) { if (arrState[i, j].Side == myside) { switch (arrState[i, j].Type) { case eChessPieceType.Bishop: value += (int)PieceValue.Bishop; value += ChessPiece_Bishop.GetPositionValue(new Point(i, j), myside, _ownSide); if ((i + j) % 2 == 0) { myBlackBishopAvailable = true; } else { myWhiteBishopAvailable = true; } break; case eChessPieceType.Knight: value += (int)PieceValue.Knight; value += ChessPiece_Knight.GetPositionValue(new Point(i, j), myside, _ownSide); countMyKnight += 1; break; case eChessPieceType.Pawn: value += (int)PieceValue.Pawn; value += ChessPiece_Pawn.GetPositionValue(new Point(i, j), myside, _ownSide); isGonnaDraw = false; break; case eChessPieceType.Queen: value += (int)PieceValue.Queen; value += ChessPiece_Queen.GetPositionValue(new Point(i, j), myside, _ownSide); isGonnaDraw = false; break; case eChessPieceType.Rook: value += (int)PieceValue.Rook; value += ChessPiece_Rook.GetPositionValue(new Point(i, j), myside, _ownSide); isGonnaDraw = false; break; case eChessPieceType.King: value += (int)PieceValue.King; value += ChessPiece_King.GetPositionValue(new Point(i, j), myside, isEndGame, _ownSide); break; } } else { switch (arrState[i, j].Type) { case eChessPieceType.Bishop: value -= (int)PieceValue.Bishop; value -= ChessPiece_Bishop.GetPositionValue(new Point(i, j), opSide, _ownSide); if ((i + j) % 2 == 0) { opBlackBishopAvailable = true; } else { opWhiteBishopAvailable = true; } break; case eChessPieceType.Knight: value -= (int)PieceValue.Knight; value -= ChessPiece_Knight.GetPositionValue(new Point(i, j), opSide, _ownSide); countOpKnight += 1; break; case eChessPieceType.Pawn: value -= (int)PieceValue.Pawn; value -= ChessPiece_Pawn.GetPositionValue(new Point(i, j), opSide, _ownSide); isGonnaDraw = false; break; case eChessPieceType.Queen: value -= (int)PieceValue.Queen; value -= ChessPiece_Queen.GetPositionValue(new Point(i, j), opSide, _ownSide); isGonnaDraw = false; break; case eChessPieceType.Rook: value -= (int)PieceValue.Rook; value -= ChessPiece_Rook.GetPositionValue(new Point(i, j), opSide, _ownSide); isGonnaDraw = false; break; case eChessPieceType.King: value -= (int)PieceValue.King; value -= ChessPiece_King.GetPositionValue(new Point(i, j), opSide, isEndGame, _ownSide); break; } } } } } /* 2 Bishop is useful for us * Bishop is more useful than knight in the end * * The game is not draw when * - 1 of each player has Pawn or Rook or Queen * - 1 of each player has 2 Knight or 2 Bishop in different color * - 1 of each player has 1 Knight and 1 Bishop */ if (countMyKnight > 1 || countOpKnight > 1) { isGonnaDraw = false; } if ((countMyKnight >= 1 && (myBlackBishopAvailable || myWhiteBishopAvailable)) || (countOpKnight >= 1 && (opBlackBishopAvailable || opWhiteBishopAvailable))) { isGonnaDraw = false; } if (myBlackBishopAvailable && myWhiteBishopAvailable) { value += 10; isGonnaDraw = false; } if (opWhiteBishopAvailable && opBlackBishopAvailable) { value -= 10; isGonnaDraw = false; } if ((opBlackBishopAvailable && myWhiteBishopAvailable) || (opWhiteBishopAvailable && myBlackBishopAvailable)) { isGonnaDraw = false; } if (isGonnaDraw) { return(0); } if (isEndGame) { if (countMyKnight > 1) { value -= 10; } if (countOpKnight > 1) { value += 10; } if (myBlackBishopAvailable || myWhiteBishopAvailable) { value += 10; } if (opBlackBishopAvailable || opWhiteBishopAvailable) { value -= 10; } } return(value); }
/// <summary> /// Find all Possible Moves of a King of a side /// </summary> /// <param name="side"></param> /// <returns></returns> public static bool[,] FindAllKingCanMove(ChessState[,] arrState, eChessSide side, eChessSide ownside) { bool[,] Result = new bool[10, 10]; ArrayList cantMoves = new ArrayList(); int i, j; //Init Result for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { Result[i, j] = true; } } for (i = 0; i <= 9; i++) { Result[0, i] = false; Result[i, 0] = false; Result[9, i] = false; Result[i, 9] = false; } for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { if (arrState[i, j].Side != side) { if (arrState[i, j].Type == eChessPieceType.Bishop) { cantMoves = ChessPiece_Bishop.FindAllImposibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.King) { cantMoves = ChessPiece_King.FindAllImposibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Knight) { cantMoves = ChessPiece_Knight.FindAllImposibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Pawn) { cantMoves = ChessPiece_Pawn.FindAllImposibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Queen) { cantMoves = ChessPiece_Queen.FindAllImposibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Rook) { cantMoves = ChessPiece_Rook.FindAllImposibleMoves(arrState, new Point(i, j)); } foreach (ChessMove p in cantMoves) { Result[p.MoveTo.X, p.MoveTo.Y] = false; } } } } return(Result); }