private bool IsKingCanBeProtectWithCheckByPawn(ChessSquare[,] board, ChessSquare kingCheckedSquare, ChessSquare chessCheckSquare) { // Pawn can be cancel attack just by kill him // so don't need to check checking path Common.SquaresCheckingPath.Add(board[chessCheckSquare.Row, chessCheckSquare.Col]); return(IsSquareInPathCanBeProtect(board)); }
public ChessSquare(ChessSquare a) { Chess = a.Chess; Image = a.Image; Row = a.Row; Col = a.Col; }
/// <summary> /// return true if checked team has a chess can be protect king /// else return false /// </summary> /// <param name="board"></param> /// <param name="kingCheckedSquare"></param> /// <param name="chessCheckSquare"></param> /// <returns></returns> public bool IsKingCanBeProtect(ChessSquare[,] board, ChessSquare kingCheckedSquare, ChessSquare chessCheckSquare) { if (kingCheckedSquare.Chess == null) { return(true); } if (chessCheckSquare.Chess == null) { return(true); } Chess chessCheck = chessCheckSquare.Chess; if (chessCheck.IsKnight) { return(IsKingCanBeProtectWithCheckByKnight(board, kingCheckedSquare, chessCheckSquare)); } if (chessCheck.IsBishop) { return(IsKingCanBeProtectWithCheckByBishop(board, kingCheckedSquare, chessCheckSquare)); } if (chessCheck.IsQueen) { return(IsKingCanBeProtectWithCheckByQueen(board, kingCheckedSquare, chessCheckSquare)); } if (chessCheck.IsCastle) { return(IsKingCanBeProtectWithCheckByCastle(board, kingCheckedSquare, chessCheckSquare)); } if (chessCheck.IsPawn) { return(IsKingCanBeProtectWithCheckByPawn(board, kingCheckedSquare, chessCheckSquare)); } return(false); }
private void PhongHau(ref ChessSquare temp) { Chess newQueen = new Queen(); if (temp.Chess.Team == 1) //white { newQueen.Team = (int)ColorTeam.White; temp.Chess = newQueen; temp.Image = Image.FromFile(Constants.linkWhiteQueen); temp.Chess.Evaluation = 90; } else //black { newQueen.Team = (int)ColorTeam.Black; temp.Chess = newQueen; temp.Image = Image.FromFile(Constants.linkBlackQueen); temp.Chess.Evaluation = -90; } }
private void KiemTraChieuVua() { bool isCheck = false; ChessSquare Kingtemp = new ChessSquare(); this.Check(ref isCheck, ref Kingtemp); if (isCheck) { Checkmate(Kingtemp); } Common.CanBeEat.Clear(); Common.CanBeMove.Clear(); Common.BackChessBoard(); if (Kingtemp.Chess != null && Kingtemp.Chess.IsKing == true) { Common.Board[Kingtemp.Row, Kingtemp.Col].BackColor = Color.BlueViolet; } }
private void frmChessKing_Load(object sender, EventArgs e) { for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { ChessSquare temp = new ChessSquare(); if (row % 2 == 0) { if (col % 2 == 0) { temp.BackColor = Color.NavajoWhite; } else { temp.BackColor = Color.SaddleBrown; } } else { if (col % 2 == 0) { temp.BackColor = Color.SaddleBrown; } else { temp.BackColor = Color.NavajoWhite; } } temp.Location = new Point((col + 1) * 60, (row + 1) * 60); temp.Row = row; temp.Col = col; Board[row, col] = temp; Board[row, col].findWayAction += new FindWayAction(OnAction); this.Controls.Add(Board[row, col]); } } }
private void Check(ref bool isCheck, ref ChessSquare KingTemp) { // mặc định chưa bị chiếu tướng isCheck=false isCheck = false; Common.CanBeEatTemp.Clear(); //check if King is danger for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (Common.Board[i, j].Chess != null) { Common.Board[i, j].Chess.FindSquaresCanEat(Common.Board, i, j); for (int k = 0; k < Common.CanBeEatTemp.Count; k++) { if (Common.CanBeEatTemp[k].Chess.IsKing && Common.Board[i, j].Chess.Team != Common.CanBeEatTemp[k].Chess.Team) { isCheck = true; KingTemp = new ChessSquare(Common.CanBeEatTemp[k]); if (isCheck) { // Set trạng thái đang bị check khiến không thể nhập thành if (KingTemp.Chess.Team == (int)ColorTeam.White) { Common.isWhiteKingChecked = true; } if (KingTemp.Chess.Team == (int)ColorTeam.Black) { Common.isBlackKingChecked = true; } } } } } Common.CanBeEatTemp.Clear(); Common.CanBeMoveTemp.Clear(); } } }
/// <summary> /// - Check if king is available to move => check mate is false /// - Check if king is available to eat the attacker=> check mate is false /// - If king not avalable to move or eat-> check if teamate can eat the checker /// - Finally check if teamate can protect the checking path /// </summary> /// <param name="temp"></param> private void Checkmate(ChessSquare temp) { Common.CanBeEat.Clear(); Common.CanBeMove.Clear(); temp.Chess.FindWayAndAutoChangeSquareIfNeeded(Common.Board, temp.Row, temp.Col); bool isCheckmate = true; isCheckmate = !(temp.Chess as King). IsKingCanBeProtect(Common.Board, temp, Common.Board[this.Row, this.Col]); if (Common.CanBeMove.Count > 0) { isCheckmate = false; } if (Common.CanBeEat.Count > 0) { isCheckmate = false; } if (isCheckmate) { if (temp.Chess.Team == (int)ColorTeam.White) { MessageBox.Show("The Black Wins"); } else { MessageBox.Show("The White Wins"); } Common.ClearMoveSuggestion(); Common.IsPlaying = false; Common.Close = true; } else { soundManager.PlayCheckSound(); } }
protected double minimax(int depth, ref ChessSquare[,] root, double alpha, double beta, bool isMax) { if (depth == 0) { return(-this.BestValue(root)); } ChessSquare[,] a = new ChessSquare[8, 8]; double bestValue; int team = 0; if (isMax == true) { team = Common.Player2ColorTeam; bestValue = -9999; } //black else { team = Common.Player1ColorTeam; bestValue = 9999; } //white //ke list can move from root //List<ChessSquare[,]> tempList = new List<ChessSquare[,]>(); //createList(root, team, tempList); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { int befRow = i; int befCol = j; if (!Common.IsEmptyChessSquare(root, befRow, befCol) && root[befRow, befCol].Chess.Team == team) { List <ChessSquare> RootTemp = new List <ChessSquare>(); root[befRow, befCol].Chess.FindWayAndAutoChangeSquareIfNeeded(root, befRow, befCol); for (int k = 0; k < Common.CanBeMove.Count; k++) { RootTemp.Add(Common.CanBeMove[k]); } for (int k = 0; k < Common.CanBeEat.Count; k++) { RootTemp.Add(Common.CanBeEat[k]); } Common.CanBeMove.Clear(); Common.CanBeEat.Clear(); Chess tempChess = new Chess(); Image tempImage = null; for (int k = 0; k < RootTemp.Count; k++) { tempChess = root[RootTemp[k].Row, RootTemp[k].Col].Chess; tempImage = root[RootTemp[k].Row, RootTemp[k].Col].Image; root[RootTemp[k].Row, RootTemp[k].Col].Chess = root[befRow, befCol].Chess; root[RootTemp[k].Row, RootTemp[k].Col].Image = root[befRow, befCol].Image; root[befRow, befCol].Chess = null; root[befRow, befCol].Image = null; if (team == Common.Player2ColorTeam) { bestValue = Math.Max(bestValue, minimax(depth - 1, ref root, alpha, beta, !isMax)); alpha = Math.Max(alpha, bestValue); if (beta <= alpha) { root[RootTemp[k].Row, RootTemp[k].Col].Undo(ref root, befRow, befCol, tempChess, tempImage); return(bestValue); } } else { bestValue = Math.Min(bestValue, minimax(depth - 1, ref root, alpha, beta, !isMax)); beta = Math.Min(beta, bestValue); if (beta <= alpha) { root[RootTemp[k].Row, RootTemp[k].Col].Undo(ref root, befRow, befCol, tempChess, tempImage); return(bestValue); } } root[RootTemp[k].Row, RootTemp[k].Col].Undo(ref root, befRow, befCol, tempChess, tempImage); } RootTemp.Clear(); } } } return(bestValue); }
protected void minimaxRoot() { int depth = Common.Depth; double bestValue = -9999; double value = 0; double alpha = -10000, beta = 10000; bool isMax = true; ChessSquare[,] board = new ChessSquare[8, 8]; ChessSquare[,] bestMove = new ChessSquare[8, 8]; board = Common.Board; bool isComputerKingInDefault = false; int defaultRowComputerKing = 0; // Kiểm tra xem ô vua may mặc định lúc thực hiện thuật toán có vua đen không? if (Common.Player2ColorTeam == (int)ColorTeam.Black) { defaultRowComputerKing = 0; } else { defaultRowComputerKing = 7; } if (board[defaultRowComputerKing, 4].Chess != null) { isComputerKingInDefault = board[defaultRowComputerKing, 4].Chess.IsKing && board[defaultRowComputerKing, 4].Chess.Team == Common.Player2ColorTeam; } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board[i, j].Chess != null && board[i, j].Chess.Team == Common.Player2ColorTeam) { List <ChessSquare> RootTemp = new List <ChessSquare>(); int befRow = i; int befCol = j; board[befRow, befCol].Chess.FindWayAndAutoChangeSquareIfNeeded(board, befRow, befCol); for (int k = 0; k < Common.CanBeMove.Count; k++) { RootTemp.Add(Common.CanBeMove[k]); } for (int k = 0; k < Common.CanBeEat.Count; k++) { RootTemp.Add(Common.CanBeEat[k]); } Common.CanBeMove.Clear(); Common.CanBeEat.Clear(); Chess tempChess = new Chess(); Image tempImage = null; // Giả định lần lượt các nước đi quân cờ nếu nó có thể di chuyển //được để tìm được giá trị tốt nhất cho nước đi for (int k = 0; k < RootTemp.Count; k++) { tempChess = board[RootTemp[k].Row, RootTemp[k].Col].Chess; tempImage = board[RootTemp[k].Row, RootTemp[k].Col].Image; board[RootTemp[k].Row, RootTemp[k].Col].Chess = board[befRow, befCol].Chess; board[RootTemp[k].Row, RootTemp[k].Col].Image = board[befRow, befCol].Image; board[befRow, befCol].Chess = null; board[befRow, befCol].Image = null; value = minimax(depth - 1, ref board, alpha, beta, !isMax); if (value >= bestValue) { for (int m = 0; m < 8; m++) { for (int n = 0; n < 8; n++) { bestMove[m, n] = new ChessSquare(board[m, n]); } } bestValue = value; } //phục hồi lại trạng thái ban đầu quân cờ sau khi giả định board[RootTemp[k].Row, RootTemp[k].Col].Undo(ref board, befRow, befCol, tempChess, tempImage); } RootTemp.Clear(); } } } for (int k = 0; k < 8; k++) { for (int l = 0; l < 8; l++) { Common.Board[k, l].Row = bestMove[k, l].Row; Common.Board[k, l].Col = bestMove[k, l].Col; Common.Board[k, l].Chess = bestMove[k, l].Chess; Common.Board[k, l].Image = bestMove[k, l].Image; } } // Nếu ban đầu vị trí mặc định có vua đen, Kiểm tra xem // Vua đen có đi vào 2 vị trí nhập thành ngắn và dài không if (isComputerKingInDefault) { // Vua đã đi khỏi vị trí default => vị trí default không có cờ if (board[defaultRowComputerKing, 4].Chess == null) { //colRightBishop =5 //colRightKnight =6 //colRightCastle = 7 // Kiểm tra king side castle if (board[defaultRowComputerKing, 6].Chess != null) { if (board[defaultRowComputerKing, 6].Chess.IsKing) { Common.Board[defaultRowComputerKing, 5].Image = Common.Board[defaultRowComputerKing, 7].Image; Common.Board[defaultRowComputerKing, 7].Image = null; Common.Board[defaultRowComputerKing, 7].BackColor = Common.OldBackGround; Common.Board[defaultRowComputerKing, 5].Chess = Common.Board[defaultRowComputerKing, 7].Chess; Common.Board[defaultRowComputerKing, 7].Chess = null; //Thêm trạng thái đã castle để không thể castle lần 2 if (Common.Player2ColorTeam == (int)ColorTeam.Black) { Common.isBlackKingMoved = true; } else { Common.isWhiteKingMoved = true; } } } // Kiểm tra queen side castle //colQueen = 3; //colLeftBishop = 2; //colLeftCastle = 0; if (board[defaultRowComputerKing, 2].Chess != null) { if (board[defaultRowComputerKing, 2].Chess.IsKing) { Common.Board[defaultRowComputerKing, 3].Image = Common.Board[defaultRowComputerKing, 0].Image; Common.Board[defaultRowComputerKing, 0].Image = null; Common.Board[defaultRowComputerKing, 0].BackColor = Common.OldBackGround; Common.Board[defaultRowComputerKing, 3].Chess = Common.Board[defaultRowComputerKing, 0].Chess; Common.Board[defaultRowComputerKing, 0].Chess = null; //Thêm trạng thái đã castle để không thể castle lần 2 if (Common.Player2ColorTeam == (int)ColorTeam.Black) { Common.isBlackKingMoved = true; } else { Common.isWhiteKingMoved = true; } } } } } //Kiểm tra xem máy có di chuyển vua chưa, phục vụ cho mục đích xét nhập thành KiemTraVuaDaDiChuyen(); KiemTraCastleDaDiChuyen(); Common.IsTurn++; Common.ClearMoveSuggestion(); }
private void frmChessKing_Load(object sender, EventArgs e) { for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { ChessSquare temp = new ChessSquare(); if (row % 2 == 0) { if (col % 2 == 0) { temp.BackColor = Color.LavenderBlush; } else { temp.BackColor = Color.DarkSlateGray; } } else { if (col % 2 == 0) { temp.BackColor = Color.DarkSlateGray; } else { temp.BackColor = Color.LavenderBlush; } } temp.Location = new Point((col + 1) * 60, (row + 1) * 60); temp.Row = row; temp.Col = col; Board[row, col] = temp; Board[row, col].findWayDisplayAction += new FindWayDisplayAction(OnAction); this.Controls.Add(Board[row, col]); } } for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { ChessSquare temp = new ChessSquare(); if (row % 2 == 0) { if (col % 2 == 0) { temp.BackColor = Color.LavenderBlush; } else { temp.BackColor = Color.DarkSlateGray; } } else { if (col % 2 == 0) { temp.BackColor = Color.DarkSlateGray; } else { temp.BackColor = Color.LavenderBlush; } } temp.Location = new Point((col + 1) * 60, (row + 1) * 60); temp.Row = row; temp.Col = col; Common.VirtualBoard[row, col] = temp; Board[row, col].findWayDisplayAction += new FindWayDisplayAction(OnAction); } } }
private bool IsKingCanBeProtectWithCheckByBishop(ChessSquare[,] board, ChessSquare kingCheckedSquare, ChessSquare chessCheckSquare) { Common.SquaresCheckingPath.Add(board[chessCheckSquare.Row, chessCheckSquare.Col]); // Bishop in south east king if (kingCheckedSquare.Row < chessCheckSquare.Row && kingCheckedSquare.Col < chessCheckSquare.Col) { // Get squares in check path to king int j = chessCheckSquare.Col - 1; for (int i = chessCheckSquare.Row - 1; i >= Constants.firstRowOfTable; i--) { // Kiểm tra điều kiện, nếu ngoài bàn cờ ( col <0) thì xong việc xét chéo trái lên if (j < Constants.firstColOfTable) { break; } // Ô trong checking path từ bishop tới king if (kingCheckedSquare.Row < i && kingCheckedSquare.Col < j) { if (Common.IsEmptyChessSquare(board, i, j)) { Common.SquaresCheckingPath.Add(board[i, j]); } } // nếu có ô ngay sau king trên đường checking path, // loại bỏ ô đó khỏi list can move hoac eat của king else if (kingCheckedSquare.Row - 1 == i && kingCheckedSquare.Col - 1 == j) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, j, i); } j--; } return(IsSquareInPathCanBeProtect(board)); } //Bishop in south west king if (kingCheckedSquare.Row < chessCheckSquare.Row && kingCheckedSquare.Col > chessCheckSquare.Col) { int j = chessCheckSquare.Col + 1; for (int i = chessCheckSquare.Row - 1; i >= Constants.firstRowOfTable; i--) { if (j > Constants.lastColOfTable) { break; } if (kingCheckedSquare.Row < i && kingCheckedSquare.Col > j) { if (Common.IsEmptyChessSquare(board, i, j)) { Common.SquaresCheckingPath.Add(board[i, j]); } } else if (kingCheckedSquare.Row - 1 == i && kingCheckedSquare.Col + 1 == j) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, i, j); } j++; } return(IsSquareInPathCanBeProtect(board)); } //Bishop in north east king if (kingCheckedSquare.Row > chessCheckSquare.Row && kingCheckedSquare.Col < chessCheckSquare.Col) { int j = chessCheckSquare.Col - 1; for (int i = chessCheckSquare.Row + 1; i <= Constants.lastRowOfTable; i++) { if (j < Constants.firstColOfTable) { break; } if (kingCheckedSquare.Row > i && kingCheckedSquare.Col < j) { if (Common.IsEmptyChessSquare(board, i, j)) { Common.SquaresCheckingPath.Add(board[i, j]); } } else if (i == kingCheckedSquare.Row + 1 && j == kingCheckedSquare.Col - 1) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, i, j); } j--; } return(IsSquareInPathCanBeProtect(board)); } //Bishop in north west king if (kingCheckedSquare.Row > chessCheckSquare.Row && kingCheckedSquare.Col > chessCheckSquare.Col) { int j; j = chessCheckSquare.Col + 1; for (int i = chessCheckSquare.Row + 1; i <= Constants.lastRowOfTable; i++) { if (j > Constants.lastColOfTable) { break; } if (kingCheckedSquare.Row > i && kingCheckedSquare.Col > j) { if (Common.IsEmptyChessSquare(board, i, j)) { Common.SquaresCheckingPath.Add(board[i, j]); } } else if (i == kingCheckedSquare.Row + 1 && j == kingCheckedSquare.Col + 1) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, i, j); } j++; } return(IsSquareInPathCanBeProtect(board)); } return(false); }
private bool IsKingCanBeProtectWithCheckByCastle(ChessSquare[,] board, ChessSquare kingCheckedSquare, ChessSquare chessCheckSquare) { Common.SquaresCheckingPath.Add(board[chessCheckSquare.Row, chessCheckSquare.Col]); //Castle is in bottom side ofking if (kingCheckedSquare.Row < chessCheckSquare.Row) { for (int i = chessCheckSquare.Row - 1; i >= 0; i--) { if (kingCheckedSquare.Row < i) { if (Common.IsEmptyChessSquare(board, i, chessCheckSquare.Col)) { Common.SquaresCheckingPath.Add(board[i, chessCheckSquare.Col]); } } else if (kingCheckedSquare.Row - 1 == i) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, chessCheckSquare.Col, i); } } return(IsSquareInPathCanBeProtect(board)); } //Castle is in the top side of the king if (kingCheckedSquare.Row > chessCheckSquare.Row) { for (int i = chessCheckSquare.Row + 1; i <= 7; i++) { if (kingCheckedSquare.Row > i) { if (Common.IsEmptyChessSquare(board, i, chessCheckSquare.Col)) { Common.SquaresCheckingPath.Add(board[i, chessCheckSquare.Col]); } } else if (kingCheckedSquare.Row + 1 == i) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, chessCheckSquare.Col, i); } } return(IsSquareInPathCanBeProtect(board)); } //Castle is in the right side of theking if (kingCheckedSquare.Col < chessCheckSquare.Col) { for (int i = chessCheckSquare.Col - 1; i >= 0; i--) { if (kingCheckedSquare.Col < i) { if (Common.IsEmptyChessSquare(board, chessCheckSquare.Row, i)) { Common.SquaresCheckingPath.Add(board[chessCheckSquare.Row, i]); } } else if (kingCheckedSquare.Col - 1 == i) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, i, chessCheckSquare.Col); } } return(IsSquareInPathCanBeProtect(board)); } //Castle is in left side of the king if (kingCheckedSquare.Col > chessCheckSquare.Col) { for (int i = chessCheckSquare.Col + 1; i <= 7; i++) { if (kingCheckedSquare.Col > i) { if (Common.IsEmptyChessSquare(board, chessCheckSquare.Row, i)) { Common.SquaresCheckingPath.Add(board[chessCheckSquare.Row, i]); } } else if (kingCheckedSquare.Col + 1 == i) { RemoveElementCanBeMoveOrEatAfterKingInCheckPath(board, i, chessCheckSquare.Row); } } } return(false); }