//Check if move is putting ourselves in check private bool causeCheck(Board board, Coord fromPos, Coord toPos) { Board tempBoard = new Board(board); Piece temp = tempBoard.board [fromPos.xpos, fromPos.ypos]; temp.coord = toPos; if (tempBoard.board [toPos.xpos, toPos.ypos].type != Piece.PieceType.NONE) { tempBoard.board [toPos.xpos, toPos.ypos] = new Piece(Piece.PieceType.NONE, toPos.xpos, toPos.ypos, Piece.PieceColor.NONE, 99); } tempBoard.board [fromPos.xpos, fromPos.ypos] = tempBoard.board [toPos.xpos, toPos.ypos]; tempBoard.board [toPos.xpos, toPos.ypos] = temp; Piece.PieceColor colorToCheck; if (color == Piece.PieceColor.WHITE) { colorToCheck = Piece.PieceColor.BLACK; } else { colorToCheck = Piece.PieceColor.WHITE; } Coord kingCoord = new Coord(); foreach (Piece p in tempBoard.board) { if (p.type == Piece.PieceType.KING && p.color == color) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } if (Rules.isCheck(tempBoard.board, colorToCheck, kingCoord)) { return(true); } return(false); }
// Ask the rules if a move between two positions is legal, calls swap() if ok public void tryMove(Coord fromPos, Coord toPos) { Coord kingCoord = new Coord(); if (Rules.isLegalMove(board, fromPos, toPos)) { board[fromPos.xpos, fromPos.ypos].moved = true; Board tempBoard = new Board(this); Piece temp = tempBoard.board [fromPos.xpos, fromPos.ypos]; temp.coord = toPos; if (tempBoard.board [toPos.xpos, toPos.ypos].type != Piece.PieceType.NONE) { tempBoard.board [toPos.xpos, toPos.ypos] = new Piece(Piece.PieceType.NONE, toPos.xpos, toPos.ypos, Piece.PieceColor.NONE, 99); } tempBoard.board [fromPos.xpos, fromPos.ypos] = tempBoard.board [toPos.xpos, toPos.ypos]; tempBoard.board [toPos.xpos, toPos.ypos] = temp; foreach (Piece p in tempBoard.board) { if (p.type == Piece.PieceType.KING && p.color == turn) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } Piece.PieceColor colorToCheck; if (turn == Piece.PieceColor.WHITE) { colorToCheck = Piece.PieceColor.BLACK; } else { colorToCheck = Piece.PieceColor.WHITE; } if (!(Rules.isCheck(tempBoard.board, colorToCheck, kingCoord))) { swap(fromPos, toPos); //Investigate if move caused a Check. foreach (Piece p in board) { if (p.type == Piece.PieceType.KING && p.color != turn) { kingCoord = new Coord(p.coord.xpos, p.coord.ypos); } } if (Rules.isCheck(board, turn, kingCoord)) { if (turn == Piece.PieceColor.BLACK) { Console.WriteLine("White is in check"); whiteIsCheck = true; } else { Console.WriteLine("Black is in check"); blackIsCheck = true; } } else { whiteIsCheck = false; blackIsCheck = false; } if (turn == Piece.PieceColor.WHITE) { turn = Piece.PieceColor.BLACK; player2.nextMove(); } else { turn = Piece.PieceColor.WHITE; player1.nextMove(); } } else { Console.WriteLine("Cant move because king will be checked"); } if (whiteIsCheck) { Console.WriteLine("check if white is checkmate"); if (Rules.isCheckMate(this, Piece.PieceColor.WHITE)) { Console.WriteLine("GAME IS OVER, BLACK WON"); } } if (blackIsCheck) { Console.WriteLine("check if black is checkmate"); if (Rules.isCheckMate(this, Piece.PieceColor.BLACK)) { Console.WriteLine("GAME IS OVER, WHITE WON"); } } saveBoardToFile(); } }