private AllMoves initMoves(PieceColor colorFlags) { MoveList[] moveLists = new MoveList[64]; for (Square startPosition = Square.A1; startPosition <= Square.H8; startPosition++) { moveLists[(byte)startPosition] = new MoveList(); foreach (MoveDirections dir in KingDirections) { Square position = startPosition; MoveFlags moveFlags = MoveFlags.EitherType; while (movePosition(ref position, dir)) { moveLists[(byte)startPosition].Add(new Move(moveFlags, dir, startPosition, position, position)); if ((dir == MoveDirections.Move_E || dir == MoveDirections.Move_W) && !moveFlags.HasFlag(MoveFlags.Castle)) { moveFlags |= MoveFlags.Castle; moveFlags &= ~MoveFlags.Capture; if ((startPosition == whiteCastlePosition && colorFlags == PieceColor.White || startPosition == blackCastlePosition && colorFlags == PieceColor.Black)) { continue; } } break; } } } return(new AllMoves(moveLists)); }
private AllMoves initMoves(MoveDirections[] moveDirections) { MoveList[] moveLists = new MoveList[64]; for (Square startPosition = Square.A1; startPosition <= Square.H8; startPosition++) { moveLists[(byte)startPosition] = new MoveList(); foreach (MoveDirections dir in moveDirections) { MoveFlags setEnPassant = 0; Square position = startPosition; while (movePosition(ref position, dir)) { MoveFlags moveFlags = ((((int)dir & 7) != 0) ? MoveFlags.Capture : MoveFlags.Move) | setEnPassant; if (isConversionPosition(position)) { addConversionMoves(moveLists, startPosition, dir, position, moveFlags); } else { moveLists[(byte)startPosition].Add(new Move(moveFlags, dir, startPosition, position, position)); } addEnPassantMoves(moveLists, startPosition, dir, position); if ((dir == MoveDirections.Move_N && ((byte)position >> 3) == 2) || dir == MoveDirections.Move_S && ((byte)position >> 3) == 5) { setEnPassant = MoveFlags.SetsEnPassant; continue; } break; } } } return(new AllMoves(moveLists)); }
static public MoveList getMovesToPosition(IEnumerable <Move> moves, Square pos) { MoveList moveList = new MoveList(); foreach (Move move in moves) { if (move.MovePosition == pos) { moveList.Add(move); } } return(moveList); }
private IEnumerable <Move> getAllMoves() { MoveList moves = new MoveList(); List <Square> positions = new List <Square>(gamestate.getPiecePositions()); foreach (Square pos in positions) { Piece piece = gamestate.GetPiece(pos); PossibleMoves possibleMoves = new PossibleMoves(gamestate, pos); foreach (Move move in possibleMoves) { moves.Add(move); } } return(moves); }
protected AllMoves initMoves(MoveDirections[] moveDirections, bool moveMultiple) { MoveList[] moveLists = new MoveList[64]; for (Square startPosition = Square.A1; startPosition <= Square.H8; startPosition++) { moveLists[(byte)startPosition] = new MoveList(); foreach (MoveDirections dir in moveDirections) { Square position = startPosition; while (movePosition(ref position, dir)) { moveLists[(byte)startPosition].Add(new Move(MoveFlags.EitherType, dir, startPosition, position, position)); if (!moveMultiple) { break; } } } } return(new AllMoves(moveLists)); }
private Move calculateBestMove(IEnumerable <Move> possibleMoves, uint depth, ref UInt64 candidateCount) { Console.WriteLine("Please wait. Computer is thinking... "); Stack <Move> moveStack = new Stack <Move>(); MoveList bestMoves = null; Optimize optimize = ((depth & 1) == 1) ? Optimize.Lowest : Optimize.Highest; int bestScore = (optimize == Optimize.Highest) ? int.MinValue : int.MaxValue; foreach (Move move in possibleMoves) { Console.Write("."); Gamestate testState = new Gamestate(gamestate); GameControl testControl = new GameControl(testState); testControl.makeMove(move); Piece piece = gamestate.GetPiece(move.StartPosition); //for (int d = 0; d <= 4 - depth; d++) Console.Write(" "); //Console.WriteLine(" considering move " + gamestate.turnColor + " " + piece.Type + " from " + move.StartPosition + " to " + move.MovePosition); int score = testControl.calculateScore(depth, ref candidateCount, (optimize == Optimize.Highest) ? Optimize.Lowest : Optimize.Highest); //for (int d = 0; d <= 4 - depth; d++) Console.Write(" "); //Console.WriteLine(" -> score " + score); if (optimize == Optimize.Highest && score > bestScore || optimize == Optimize.Lowest && score < bestScore) { bestScore = score; bestMoves = new MoveList(); bestMoves.Add(move); } else if (score == bestScore) { bestMoves.Add(move); } } Random rand = new Random(); int i = rand.Next(bestMoves.Count); Move bestMove = bestMoves[i]; Piece bestPiece = gamestate.GetPiece(bestMove.StartPosition); Console.WriteLine(); Console.WriteLine("Best of " + candidateCount + " moves is " + bestPiece.Color + " " + bestPiece.Type + " from " + bestMove.StartPosition + " to " + bestMove.MovePosition + " with score " + bestScore); return(bestMove); }
public ControlledPositions(Gamestate board, PieceColor color) { foreach (Square StartPosition in board.getPiecePositions(color)) { Piece StartPiece = board.GetPiece(StartPosition); MoveList moves = StartPiece.getPositionMoves(StartPosition); MoveDirections skipDirection = MoveDirections.Move_0; foreach (Move move in moves) { if (move.Direction == skipDirection) { continue; } else { skipDirection = MoveDirections.Move_0; } if (move.Flags.HasFlag(MoveFlags.Capture)) { Piece EndPiece = board.GetPiece(move.CapturePosition); if (!move.Flags.HasFlag(MoveFlags.UseEnPassant) || EndPiece != null && move.CapturePosition == board.getEnPassantPosition(EndPiece.Color)) { ControlledPositionsBitMask |= ((UInt64)1 << (byte)move.CapturePosition); } if (EndPiece != null) { if (EndPiece is King && EndPiece.Color != StartPiece.Color) { Check = move.CapturePosition; } skipDirection = move.Direction; } } } } }
public PossibleMoves(Gamestate board, Square StartPosition) { MoveCount = 0; MoveBitMask = 0; Piece StartPiece = board.GetPiece(StartPosition); if (StartPiece == null) { return; } Moves = StartPiece.getPositionMoves(StartPosition); MoveDirections skipDirection = MoveDirections.Move_0; UInt32 mask = 1; foreach (Move move in Moves) { mask <<= 1; if (move.Direction == skipDirection) { continue; } else { skipDirection = MoveDirections.Move_0; } if (continueIfCastle(board, StartPiece, move)) { continue; } Piece EndPiece = board.GetPiece(move.MovePosition); Piece CapturePiece = board.GetPiece(move.CapturePosition); if (continueIfEnPassant(board, move, CapturePiece)) { continue; } if (endPiecesMatchMoveFlags(StartPiece, move, EndPiece, CapturePiece)) { // check if move is legal Gamestate testBoard = new Gamestate(board); if (testBoard.GetPiece(move.CapturePosition) != null) { testBoard.RemovePiece(move.CapturePosition); } Piece piece = testBoard.GetPiece(StartPosition); testBoard.RemovePiece(StartPosition); testBoard.PutPiece(piece, move.MovePosition); testBoard.turnColor = (testBoard.turnColor == PieceColor.White) ? PieceColor.Black : PieceColor.White; ControlledPositions controlledMoves = new ControlledPositions(testBoard); if (controlledMoves.Check == null) { if (castleSquaresInCheck(move, piece, controlledMoves)) { continue; } EndPositionBitMask |= ((UInt64)1 << (byte)move.MovePosition); MoveBitMask |= mask; MoveCount++; } } if (EndPiece != null || CapturePiece != null) { skipDirection = move.Direction; } } MoveBitMask >>= 1; }