Пример #1
0
        public PgnMove(string pgnString)
        {
            Match m = r.Match(pgnString);

            if (m.Groups["Move"].Value == "")
            {
                ColorToPlay = PieceColor.Black;
            }
            else
            {
                ColorToPlay = PieceColor.White;
            }

            if (m.Groups["Castling"].Value == "")
            {
                PieceToPlay       = StringToPiece(m.Groups["Piece"].Value, ColorToPlay);
                FromFile          = StringToFile(m.Groups["File"].Value);
                FromRank          = StringToRank(m.Groups["Rank"].Value);
                DestinationSquare = StringToSquare(m.Groups["Destination"].Value);
                PromotionPiece    = StringToPromotion(m.Groups["Promotion"].Value);
            }
            else if (m.Groups["Castling"].Value == "O-O")
            {
                if (ColorToPlay == PieceColor.White)
                {
                    DestinationSquare = Square.G1;
                    PieceToPlay       = Piece.WhiteKing;
                    FromFile          = 4;
                    FromRank          = 0;
                }

                if (ColorToPlay == PieceColor.Black)
                {
                    DestinationSquare = Square.G8;
                    PieceToPlay       = Piece.BlackKing;
                    FromFile          = 4;
                    FromRank          = 7;
                }
            }
            else if (m.Groups["Castling"].Value == "O-O-O")
            {
                if (ColorToPlay == PieceColor.White)
                {
                    DestinationSquare = Square.C1;
                    PieceToPlay       = Piece.WhiteKing;
                    FromFile          = 4;
                    FromRank          = 0;
                }

                if (ColorToPlay == PieceColor.Black)
                {
                    DestinationSquare = Square.C8;
                    PieceToPlay       = Piece.BlackKing;
                    FromFile          = 4;
                    FromRank          = 7;
                }
            }
        }
Пример #2
0
        public PgnMove(string pgnString)
        {
            Match m = r.Match(pgnString);

              if (m.Groups["Move"].Value == "")
              {
            ColorToPlay = PieceColor.Black;
              }
              else
              {
            ColorToPlay = PieceColor.White;
              }

              if (m.Groups["Castling"].Value == "")
              {
            PieceToPlay = StringToPiece(m.Groups["Piece"].Value, ColorToPlay);
            FromFile = StringToFile(m.Groups["File"].Value);
            FromRank = StringToRank(m.Groups["Rank"].Value);
            DestinationSquare = StringToSquare(m.Groups["Destination"].Value);
            PromotionPiece = StringToPromotion(m.Groups["Promotion"].Value);
              }
              else if (m.Groups["Castling"].Value == "O-O")
              {
            if (ColorToPlay == PieceColor.White)
            {
              DestinationSquare = Square.G1;
              PieceToPlay = Piece.WhiteKing;
              FromFile = 4;
              FromRank = 0;
            }

            if (ColorToPlay == PieceColor.Black)
            {
              DestinationSquare = Square.G8;
              PieceToPlay = Piece.BlackKing;
              FromFile = 4;
              FromRank = 7;
            }
              }
              else if (m.Groups["Castling"].Value == "O-O-O")
              {
            if (ColorToPlay == PieceColor.White)
            {
              DestinationSquare = Square.C1;
              PieceToPlay = Piece.WhiteKing;
              FromFile = 4;
              FromRank = 0;
            }

            if (ColorToPlay == PieceColor.Black)
            {
              DestinationSquare = Square.C8;
              PieceToPlay = Piece.BlackKing;
              FromFile = 4;
              FromRank = 7;
            }
              }
        }
Пример #3
0
        public static PromotionPiece ShowPawnPromotionDialog(IWin32Window owner)
        {
            PawnPromotionDialog promotionDlg = new PawnPromotionDialog();

            promotionDlg.ShowDialog(owner);
            PromotionPiece result = promotionDlg.m_promotionPiece;

            promotionDlg.Dispose();
            return(result);
        }
Пример #4
0
        /// <summary>
        ///     Creates a <see cref="Move" /> object from move details.
        /// </summary>
        /// <remarks>Basically makes a ushort from the 4 details needed to make a move</remarks>
        /// <returns></returns>
        public static Move GenerateMove(ushort fromIdx, ushort toIdx, MoveType moveType = MoveType.Normal,
                                        PromotionPiece promotionPiece = 0)
        {
            var mt     = (ushort)moveType << 14;
            var pp     = (ushort)promotionPiece << 12;
            var origin = fromIdx << 6;
            var dest   = toIdx << 0;

            return(new Move((ushort)(mt | pp | origin | dest)));
        }
Пример #5
0
 public virtual bool Equals(ChessMove other)
 {
     if (MoveType != ChessMoveType.PawnPromote)
     {
         return(other != null && StartPosition.Equals(other.StartPosition) &&
                EndPosition.Equals(other.EndPosition));
     }
     else
     {
         return(other != null && StartPosition.Equals(other.StartPosition) &&
                EndPosition.Equals(other.EndPosition) && PromotionPiece.Equals(other.PromotionPiece));
     }
 }
Пример #6
0
        /// <summary>
        ///     Returns uppercase character representing promotion pieces
        /// </summary>
        /// <exception cref="PieceException">
        ///     Thrown if promotion piece is not found in enumeration. Highly unlikely and indicative
        ///     of another problem.
        /// </exception>
        public static char GetCharFromPromotionPiece(PromotionPiece p)
        {
            switch (p)
            {
            case PromotionPiece.Bishop: return('B');

            case PromotionPiece.Knight: return('N');

            case PromotionPiece.Queen: return('Q');

            case PromotionPiece.Rook: return('R');

            default:
                throw new PieceException(
                          $"Promotion Piece not found in switch cases.{Environment.NewLine}(Exception is not likely and probably indicative of another issue.)");
            }
        }
Пример #7
0
 private void buttonRook_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Rook;
     this.Close();
 }
Пример #8
0
 private void buttonQueen_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Queen;
     this.Close();
 }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of PromotionEventArgs.
 /// </summary>
 /// <param name="defaultPromotion">The default piece to promote to if another piece is not set in the "PromotionPiece" proberty.</param>
 internal PromotionEventArgs(PromotionPiece defaultPromotion)
 {
     m_promotionPiece = defaultPromotion;
 }
Пример #10
0
 private void buttonKnight_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Knight;
       this.Close();
 }
Пример #11
0
        static void Main(string[] args)
        {
            ChessFacade chess = new ChessFacade();

            chess.WhitePawnPromoted += new EventHandler <PromotionEventArgs>(chess_WhitePawnPromoted);
            chess.BlackPawnPromoted += new EventHandler <PromotionEventArgs>(chess_BlackPawnPromoted);
            chess.StatusInfo        += new EventHandler <GameStatusEventArgs>(chess_StatusChanged);

            EngineConfiguration engConfig = chess.EngineConfiguration;

            engConfig.EngineAutoPlay  = false;
            chess.EngineConfiguration = engConfig;

            ClockConfiguration clockConfig = chess.ClockConfiguration;

            clockConfig.ClockType    = ClockType.None;
            chess.ClockConfiguration = clockConfig;

            StreamReader file;

            if (args.Length == 1)
            {
                file = File.OpenText(args[0]);
            }
            else
            {
                file = File.OpenText(@"..\..\input\book_1.00.pgn");
            }

            int           lineCounter = 0;
            string        line;
            StringBuilder gameString = new StringBuilder();

            while ((line = file.ReadLine()) != null)
            {
                ++lineCounter;
                if (line.Length > 0 && line[0] != '[')
                {
                    gameString.Append(line);
                    if (line[line.Length - 1] != '.')
                    {
                        gameString.Append(" ");
                    }
                }
                else if (gameString.Length > 0)
                {
                    bool     breaking   = false;
                    string[] lineBlocks = gameString.ToString().Split(new char[] { ' ' });

                    PieceColor winner = PieceColor.None;
                    if (lineBlocks[lineBlocks.Length - 2] == "1-0")
                    {
                        winner = PieceColor.White;
                    }
                    else if (lineBlocks[lineBlocks.Length - 2] == "0-1")
                    {
                        winner = PieceColor.Black;
                    }
                    else if (lineBlocks[lineBlocks.Length - 2] == "1/2-1/2" || lineBlocks[lineBlocks.Length - 2] == "*")
                    {
                        winner = PieceColor.None;
                    }
                    else
                    {
                        Console.WriteLine("Unknown result");
                    }

                    int itLength = Math.Min(lineBlocks.Length, 20);
                    for (int i = 0; i < itLength; ++i)
                    {
                        string lineBlock = lineBlocks[i];
                        if (lineBlock != "" && lineBlock != "*" && lineBlock != "1-0" && lineBlock != "0-1" && lineBlock != "1/2-1/2")
                        {
                            PgnMove pgnMove = new PgnMove(lineBlock);

                            for (Square square = Square.A1; square <= Square.H8; ++square)
                            {
                                if ((pgnMove.PieceToPlay == chess.PieceAt(square)) &&
                                    (pgnMove.FromFile == -1 || pgnMove.FromFile == ChessFacade.File(square)) &&
                                    (pgnMove.FromRank == -1 || pgnMove.FromRank == ChessFacade.Rank(square)))
                                {
                                    promotion = pgnMove.PromotionPiece;
                                    if (chess.PerformMove(square, pgnMove.DestinationSquare))
                                    {
                                        //Console.Write(square + "-" + pgnMove.DestinationSquare + " | ");
                                        if (winner == PieceColor.None)
                                        {
                                            chess.AddToOpeningBook(1);
                                        }
                                        else if (winner == pgnMove.ColorToPlay)
                                        {
                                            chess.AddToOpeningBook(2);
                                        }
                                        //else
                                        //	chess.AddToOpeningBook(1);
                                        break;
                                    }
                                }

                                if (square == Square.H8)
                                {
                                    Console.WriteLine("No move performed: " + lineCounter);
                                    Console.ReadLine();
                                    breaking = true;
                                    break;
                                }
                            }
                        }

                        if (breaking)
                        {
                            break;
                        }
                    }

                    //Console.WriteLine("new game");
                    gameString = new StringBuilder();
                    while (chess.UndoCount > 0)
                    {
                        chess.UndoMove();
                    }
                }

                if (lineCounter % 1000 == 0)
                {
                    Console.WriteLine(lineCounter.ToString());
                }
            }

            chess.SaveOpeningBook();
            Console.WriteLine("Done");
            Console.ReadLine();
        }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of PromotionEventArgs.
 /// </summary>
 /// <param name="defaultPromotion">The default piece to promote to if another piece is not set in the "PromotionPiece" proberty.</param>
 internal PromotionEventArgs(PromotionPiece defaultPromotion)
 {
     m_promotionPiece = defaultPromotion;
 }
Пример #13
0
        static void Main(string[] args)
        {
            ChessFacade chess = new ChessFacade();

              chess.WhitePawnPromoted += new EventHandler<PromotionEventArgs>(chess_WhitePawnPromoted);
              chess.BlackPawnPromoted += new EventHandler<PromotionEventArgs>(chess_BlackPawnPromoted);
              chess.StatusInfo += new EventHandler<GameStatusEventArgs>(chess_StatusChanged);

              EngineConfiguration engConfig = chess.EngineConfiguration;
              engConfig.EngineAutoPlay = false;
              chess.EngineConfiguration = engConfig;

              ClockConfiguration clockConfig = chess.ClockConfiguration;
              clockConfig.ClockType = ClockType.None;
              chess.ClockConfiguration = clockConfig;

              StreamReader file;
              if (args.Length == 1)
            file = File.OpenText(args[0]);
              else
            file = File.OpenText(@"..\..\input\book_1.00.pgn");

              int lineCounter = 0;
              string line;
              StringBuilder gameString = new StringBuilder();
              while ((line = file.ReadLine()) != null)
              {
            ++lineCounter;
            if (line.Length > 0 && line[0] != '[')
            {
              gameString.Append(line);
              if (line[line.Length - 1] != '.')
            gameString.Append(" ");
            }
            else if (gameString.Length > 0)
            {
              bool breaking = false;
              string[] lineBlocks = gameString.ToString().Split(new char[] { ' ' });

              PieceColor winner = PieceColor.None;
              if (lineBlocks[lineBlocks.Length - 2] == "1-0")
              {
            winner = PieceColor.White;
              }
              else if (lineBlocks[lineBlocks.Length - 2] == "0-1")
              {
            winner = PieceColor.Black;
              }
              else if (lineBlocks[lineBlocks.Length - 2] == "1/2-1/2" || lineBlocks[lineBlocks.Length - 2] == "*")
              {
            winner = PieceColor.None;
              }
              else
              {
            Console.WriteLine("Unknown result");
              }

              int itLength = Math.Min(lineBlocks.Length, 20);
              for (int i = 0; i < itLength; ++i)
              {
            string lineBlock = lineBlocks[i];
            if (lineBlock != "" && lineBlock != "*" && lineBlock != "1-0" && lineBlock != "0-1" && lineBlock != "1/2-1/2")
            {
              PgnMove pgnMove = new PgnMove(lineBlock);

              for (Square square = Square.A1; square <= Square.H8; ++square)
              {
                if ((pgnMove.PieceToPlay == chess.PieceAt(square)) &&
                  (pgnMove.FromFile == -1 || pgnMove.FromFile == ChessFacade.File(square)) &&
                  (pgnMove.FromRank == -1 || pgnMove.FromRank == ChessFacade.Rank(square)))
                {
                  promotion = pgnMove.PromotionPiece;
                  if (chess.PerformMove(square, pgnMove.DestinationSquare))
                  {
                    //Console.Write(square + "-" + pgnMove.DestinationSquare + " | ");
                    if (winner == PieceColor.None)
                      chess.AddToOpeningBook(1);
                    else if (winner == pgnMove.ColorToPlay)
                      chess.AddToOpeningBook(2);
                    //else
                    //	chess.AddToOpeningBook(1);
                    break;
                  }
                }

                if (square == Square.H8)
                {
                  Console.WriteLine("No move performed: " + lineCounter);
                  Console.ReadLine();
                  breaking = true;
                  break;
                }
              }
            }

            if (breaking)
              break;
              }

              //Console.WriteLine("new game");
              gameString = new StringBuilder();
              while (chess.UndoCount > 0)
            chess.UndoMove();
            }

            if (lineCounter % 1000 == 0)
              Console.WriteLine(lineCounter.ToString());
              }

              chess.SaveOpeningBook();
              Console.WriteLine("Done");
              Console.ReadLine();
        }
Пример #14
0
 private void buttonRook_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Rook;
       this.Close();
 }
Пример #15
0
 private void buttonQueen_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Queen;
       this.Close();
 }
Пример #16
0
 private void buttonBishop_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Bishop;
     this.Close();
 }
Пример #17
0
 private void buttonKnight_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Knight;
     this.Close();
 }
Пример #18
0
 private void buttonBishop_Click(object sender, EventArgs e)
 {
     m_promotionPiece = PromotionPiece.Bishop;
       this.Close();
 }