// Searches for a new best move public override Move getInput(Board B) { MinMax minMaxSearch = new MinMax(); Move bestMove; put(0); bestMove = minMaxSearch.runMinMax(B, colour, getSearchDepth(B), true, MinMax.MINIMUM, MinMax.MAXIMUM, put).Item1; if (bestMove != null) { if (bestMove.Equals(secondToLastMove)) { repetitionMod += 1; } else { repetitionMod = 0; } secondToLastMove = lastMove; lastMove = bestMove; return bestMove; } else { repetitionMod += 1; return new Move(); } }
// --- Graphics interface --- // Functions for printing the text and image versions of the board. // Update the board and render the GUI. Also reset the selected move. public void updateBoard(src.Board board) { this.board = board; drawControl.setBoard(board); selectedMove = new src.Move(); renderGUI(); }
// Returns and resets the last move selected via click input. public src.Move readSelectedMove() { src.Move tmp = selectedMove.Copy(); // If correct move, reset selection if (!selectedMove.Illegal) { selectedMove = new src.Move(); } return(tmp); }
//Checks if move is possible public static bool movePossible(Board board, Piece piece, Move move) { List<Tuple<uint, uint>> tmp; tmp = getPossibleMoves(board, piece); foreach (Tuple<uint, uint> item in tmp) { if ((item.Item1 == move.ToX) && (item.Item2 == move.ToY)) { return true; } } return false; }
// Check if a move is possible on a specific board. public static bool movePossible(Board board, Move move, string activePlayer) { // Check if piece at move.from Piece p = board.getPieceAt(move.FromX, move.FromY); if (p == null) { return false; } // Check if correct colour if (p.getColour() != activePlayer) { return false; } // Check if move is possible if (p is Pawn) { return PawnRules.movePossible(board, p, move); } else if (p is Rook) { return RookRules.movePossible(board, p, move); } else if (p is Knight) { return KnightRules.movePossible(board, p, move); } else if (p is Bishop) { return BishopRules.movePossible(board, p, move); } else if (p is Queen) { return QueenRules.movePossible(board, p, move); } else if (p is King) { return KingRules.movePossible(board, p, move); } // Should not get here ever... return false; }
// Called when the custom Drawing Control is clicked. // Will calculate the square clicked and save this move for Graphics Agents to use in the future. private void onDrawingClick(object sender, MouseEventArgs e) { if (!selectedMove.Illegal) { // A move has already been selected. // Wait for a Graphics Agent to read the selected move before accepting new clicks in the window. return; } // Calculate the square coordinates of the click. // TODO: use Board size. int newClickX = e.X / (drawControl.Width / 9) - 1; int newClickY = 7 - (e.Y / (drawControl.Height / 9) - 1); // Clicked outside of the board if (newClickX == -1 || newClickY == 8) { return; } if (newClickX == selectedMove.FromX && newClickY == selectedMove.FromY) { selectedMove = new src.Move(); } else { // New square clicked List <Tuple <uint, uint> > moves; if (selectedMove.hasFrom()) { moves = ChessForms.rules.Rules.getPossibleMoves(board, board.getPieceAt(selectedMove.FromX, selectedMove.FromY)); //moves = board.getSquareAt((uint)selectedSquareX, (uint)selectedSquareY).getPiece().getPossibleMoves(board.getSquareAt, turn); } else { moves = new List <Tuple <uint, uint> >(); } // Check if part of possible moves. if (moves.Contains(new Tuple <uint, uint>((uint)newClickX, (uint)newClickY))) { // Move selected selectedMove.ToX = (uint)newClickX; selectedMove.ToY = (uint)newClickY; selectedMove.Illegal = false; } else { // Move not selected, check if new piece clicked src.Piece p = board.getPieceAt((uint)newClickX, (uint)newClickY); if (p != null) { selectedMove.FromX = (uint)newClickX; selectedMove.FromY = (uint)newClickY; } else { selectedMove = new src.Move(); } } } renderGraphicsGUI(); }
// Called when the custom Drawing Control is clicked. // Will calculate the square clicked and save this move for Graphics Agents to use in the future. private void onDrawingClick(object sender, MouseEventArgs e) { if (!selectedMove.Illegal) { // A move has already been selected. // Wait for a Graphics Agent to read the selected move before accepting new clicks in the window. return; } // Calculate the square coordinates of the click. // TODO: use Board size. int newClickX = e.X / (drawControl.Width / 9) - 1; int newClickY = 7 - (e.Y / (drawControl.Height / 9) - 1); // Clicked outside of the board if (newClickX == -1 || newClickY == 8) { return; } if (newClickX == selectedMove.FromX && newClickY == selectedMove.FromY) { selectedMove = new src.Move(); } else { // New square clicked List<Tuple<uint, uint>> moves; if (selectedMove.hasFrom()) { moves = ChessForms.rules.Rules.getPossibleMoves(board, board.getPieceAt(selectedMove.FromX, selectedMove.FromY)); //moves = board.getSquareAt((uint)selectedSquareX, (uint)selectedSquareY).getPiece().getPossibleMoves(board.getSquareAt, turn); } else { moves = new List<Tuple<uint, uint>>(); } // Check if part of possible moves. if (moves.Contains(new Tuple<uint, uint>((uint)newClickX, (uint)newClickY))) { // Move selected selectedMove.ToX = (uint) newClickX; selectedMove.ToY = (uint) newClickY; selectedMove.Illegal = false; } else { // Move not selected, check if new piece clicked src.Piece p = board.getPieceAt((uint)newClickX, (uint)newClickY); if (p != null) { selectedMove.FromX = (uint) newClickX; selectedMove.FromY = (uint) newClickY; } else { selectedMove = new src.Move(); } } } renderGraphicsGUI(); }
// Returns and resets the last move selected via click input. public src.Move readSelectedMove() { src.Move tmp = selectedMove.Copy(); // If correct move, reset selection if (!selectedMove.Illegal) { selectedMove = new src.Move(); } return tmp; }
public bool makeMove(string col, Move move) { uint x1 = move.FromX; uint y1 = move.FromY; uint x2 = move.ToX; uint y2 = move.ToY; Piece p = getPieceAt(x1, y1); Square s1 = getSquareAt(x1, y1); Square s2 = getSquareAt(x2, y2); bool castling = false; bool enPassant = false; // Check if squares are ok. if (s1 == null || s2 == null) { return false; } // Check if piece is ok. if (p == null) { return false; } if (p.getColour() != col) { return false; } // Move is legal //Check if this is a castling if ((p is King) && (((x1 - x2) == 2) || ((x2 - x1) == 2))) { castling = true; } // Special handling for pawns if ((p is Pawn)) { // Check double step if (Math.Abs((int)move.FromY - (int)move.ToY) == 2) { ((Pawn)p).setDoubleStepTurn(getTurn()); } //Check if en passant if ((getSquareAt(x2, y2).getPiece() == null) && (x1 != x2)) { enPassant = true; } } // Remove from old position s1.removePiece(); // Add to new position if ((p is Pawn && p.getColour() == "white" && y2 == 7) || (p is Pawn && p.getColour() == "black" && y2 == 0)) { p = new Queen(x1, y1, p.getColour()); } s2.setPiece(p); p.move(x2, y2); // Handles the special castling case if (castling) { uint x2rook; uint x1rook; if (x2 > x1)// Castling right { x1rook = 7; x2rook = x2 - 1; } else { x1rook = 0; x2rook = x2 + 1; } Square s3 = getSquareAt(x1rook, y1); Piece p3 = s3.getPiece(); Square s4 = getSquareAt(x2rook, y1); s3.removePiece(); s4.setPiece(p3); p3.move(x2rook, y2); } // Handles the special en passant case if (enPassant) { int yMod; if (p.getColour() == "white") yMod = -1; else yMod = 1; Square s3 = getSquareAt(x2, (uint)(y2 + yMod)); Piece p3 = s3.getPiece(); s3.removePiece(); } updateCover(); return true; }
// Compare this move to another. public bool Equals(Move m) { if (m == null) { return false; } return m.FromX == fromX && m.FromY == fromY && m.ToX == toX && m.ToY == toY && m.GiveUp == giveUp && m.Illegal == illegal; }
// Return an exact copy of the move. public Move Copy() { Move copy = new Move(fromX, fromY, toX, toY); copy.GiveUp = giveUp; copy.Illegal = illegal; return copy; }