public override MoveEventResponse DescribeMove(MoveInfo move, MoveNotation format, ref string description) { if (format == MoveNotation.MoveSelectionText && move.MoveType == MoveType.CustomMove) { // describe the square on which extra captures are occuring List <string> captureSquares = new List <string>(); int direction = Board.DirectionLookup(move.FromSquare, move.ToSquare); int nSteps = 1; int activeBits = move.Tag; int nextSquare = Board.NextSquare(direction, move.FromSquare); while (nextSquare != move.ToSquare && activeBits != 0) { if ((activeBits & (1 << (nSteps - 1))) != 0) { captureSquares.Add(Game.GetSquareNotation(nextSquare)); } nextSquare = Board.NextSquare(direction, nextSquare); nSteps++; } description = "Capture pieces on " + captureSquares[0]; for (int x = 1; x < captureSquares.Count; x++) { if (x < captureSquares.Count - 1) { description += ", " + captureSquares[x]; } else { description += " and " + captureSquares[x]; } } return(MoveEventResponse.Handled); } return(MoveEventResponse.NotHandled); }
bool ValidateNotation() { ValidatedMove = null; ValidatedNotation = null; MoveNotation notated; if (!MoveNotation.TryParse(txtNotation.Text, out notated)) { lblError.Text = "Unrecognized notation"; lblError.Visible = true; return(false); } var ai = new TakAI(_game.Size); var moves = new List <IMove>(); TakAI.EnumerateMoves(moves, _game, ai.NormalPositions); ValidatedMove = notated.MatchLegalMove(moves); if (ValidatedMove == null) { lblError.Text = "Illegal move"; lblError.Visible = true; return(false); } ValidatedNotation = notated; lblError.Visible = false; return(true); }
/// <summary> /// Makes a move based on move notation. Calls <c>MakeMove(Move)</c> once move is translated. /// </summary> /// <param name="move"></param> /// <returns></returns> public bool PerformMove(string move, MoveNotation notationType, bool startNextTurn = true) { Move pieceMove = this.GetMoveByNotation(move, this.CurrentTeamTurn, notationType); if (pieceMove is null) { return(false); } return(this.PerformMove(pieceMove, startNextTurn)); }
public string ToString(MoveNotation notationType) { switch (notationType) { case MoveNotation.UCI: return(this.Moves[0].ToString(MoveNotation.UCI)); case MoveNotation.StandardAlgebraic: return(this.ToString()); default: break; } return(""); }
/// <summary> /// Returns the move expressed in either Algebraic Notation or Universal Chess Interface-notation. /// </summary> /// <param name="notation"></param> /// <returns></returns> public string ToString(MoveNotation notation) { switch (notation) { case MoveNotation.UCI: if (this.PromotePiece is null) { return(this.Source.ToString() + this.Destination.ToString()); } else { return(this.Source.ToString() + this.Destination.ToString() + this.PromotePiece.Notation); } case MoveNotation.StandardAlgebraic: return(this.ToString()); default: break; } return(string.Empty); }
/// <summary> /// 把一盘PGN对局里的前N个着法放入对局库中 /// </summary> /// <param name="pgnFilename">PGN文件名</param> /// <returns>加入到开局库时,返回true /// 如果在解析PGN时遇到不规范的棋谱时,返回false,此时控制台会打印出错误信息</returns> public static bool AddPgnFileToOpeningBook(string bookFilename, string pgnFilename, int maxStep) { using (OpeningBook book = new OpeningBook(bookFilename)) { // 从PGN文件里读出所有着法来,这里用的是纵列格式 string[] allmoves = PgnUtil.GetAllMovesFromPgnFile(pgnFilename); Board board = new Board(); int numMove = 0; // 记录已走了第几步了 foreach (string strMove in allmoves) { if (numMove >= maxStep) { break; } try { // 走一步后,把盘面生成zobrist值,保存到开局库里 // TODO: board.CreateMoveFromString(NotationConverter.Convert(board, strMove)); Move move = MoveNotation.CreateMoveFromChineseNotation(board, strMove); board.MakeMove(move); // Console.WriteLine("==== " + strMove + "\n" + board); ++numMove; ulong zobrist = Zobrist.ZoristHash(board); book.InsertBoardZobrist(zobrist); } catch (Exception e) { Console.WriteLine("--- " + strMove + " ---"); Console.WriteLine(e.Message); Console.WriteLine(board); return(false); } } // end 对每一着法循环结束 } return(true); }
public virtual MoveEventResponse DescribeMove( MoveInfo move, MoveNotation format, ref string description ) { return MoveEventResponse.NotHandled; }
/// <summary> /// Translates move notation into a move instance. /// </summary> /// <param name="notation"></param> /// <param name="player"></param> /// <returns></returns> public Move GetMoveByNotation(string notation, TeamColor player, MoveNotation notationType) => this.GetMovesByNotation(notation, player, notationType).FirstOrDefault();
/// <summary> /// This method gets a collection of moves by the notation you put in as parameters. /// </summary> /// <param name="notation"></param> /// <param name="player"></param> /// <param name="notationType"></param> /// <returns></returns> public IEnumerable <Move> GetMovesByNotation(string notation, TeamColor player, MoveNotation notationType) { Coordinate?source = null; Coordinate?destination = null; bool? captures = null; char? pieceNotation = null; char? promotionTarget = null; bool customNotation = false; try { switch (notationType) { case MoveNotation.UCI: if (notation.Length >= 4) { source = new Coordinate(notation.Substring(0, 2)); destination = new Coordinate(notation.Substring(2, 2)); } if (notation.Length == 5) { promotionTarget = notation[4]; } break; case MoveNotation.StandardAlgebraic: // read destination square int lastNumberIndex = 0; for (int i = 0; i < notation.Length; i++) { if (char.IsDigit(notation[i])) { lastNumberIndex = i; } if (notation[i] == 'x') { captures = true; } } if (lastNumberIndex == 0) { customNotation = true; break; } destination = new Coordinate(notation.Substring(lastNumberIndex - 1, 2)); // more at the end if (notation.Length - 1 > lastNumberIndex) { // promotion if (notation[lastNumberIndex + 1] == '=') { promotionTarget = notation[lastNumberIndex + 2]; pieceNotation = '\0'; } } if (lastNumberIndex - 1 == 0) { pieceNotation = '\0'; } else { pieceNotation = notation[0]; } break; } } catch (Exception) { // if notation parsing threw exception, then it's probably custom notation. customNotation = true; } // Look for matching moves. foreach (var move in this.GetMoves(player)) { // If looking for move by custom notation, return it here. if (customNotation && move.CustomNotation == notation) { if (this.gamemode.ValidateMove(move, this)) { yield return(move); } continue; } foreach (var pieceMove in move.Moves) { if (!(source is null) && source != pieceMove.Source) { continue; } if (!(destination is null) && destination != pieceMove.Destination) { continue; } if (!(captures is null) && captures != pieceMove.Captures) { continue; } if (!(pieceNotation is null) && pieceNotation != pieceMove.Piece.Notation) { continue; } if (!(promotionTarget is null) && promotionTarget != pieceMove.PromotesTo) { continue; } if (!this.gamemode.ValidateMove(move, this)) { continue; } yield return(move); } } }