예제 #1
0
 //*********************************************************     
 //
 /// <summary>
 /// Accept the content of the form
 /// </summary>
 /// <param name="sender">   Sender object</param>
 /// <param name="e">        Event argument</param>
 //
 //*********************************************************     
 private void butOk_Click(object sender, EventArgs e) {
     string                      strGame;
     PgnParser                   parser;
     List<ChessBoard.MovePosS>   listGame;
     int                         iSkip;
     int                         iTruncated;
     
     strGame = textBox1.Text;
     if (String.IsNullOrEmpty(strGame)) {
         MessageBox.Show("No PGN text has been pasted.");
     } else {
         listGame    = new List<ChessBoard.MovePosS>(256);
         parser      = new PgnParser(false);
         if (!parser.ParseSingle(strGame,
                                 false,
                                 listGame,
                                 out iSkip,
                                 out iTruncated,
                                 out m_chessBoardStarting,
                                 out m_eStartingColor,
                                 out m_strWhitePlayerName,
                                 out m_strBlackPlayerName,
                                 out m_eWhiteType,
                                 out m_eBlackType,
                                 out m_spanWhitePlayer,
                                 out m_spanBlackPlayer)) {
             MessageBox.Show("The specified board is invalid.");
         } else if (iSkip != 0) {
             MessageBox.Show("The game is incomplete. Paste another game.");
         } else if (iTruncated != 0) {
             MessageBox.Show("The selected game includes an unsupported pawn promotion (only pawn promotion to queen is supported).");
         } else if (listGame.Count == 0 && m_chessBoardStarting == null) {
             MessageBox.Show("Game is empty.");
         } else {
             m_arrMoveList = listGame;
             DialogResult  = DialogResult.OK;
             Close();
         }
     }
 }
예제 #2
0
 //*********************************************************     
 //
 /// <summary>
 /// Create a book from files selected by the user
 /// </summary>
 //  
 //*********************************************************
  public void CreateBookFromFiles() {
     OpenFileDialog          openDlg;
     SaveFileDialog          saveDlg;
     PgnParser               parser;
     Stream                  stream;
     TextReader              reader;
     Book                    book;
     string                  strText;
     List<int[]>             arrMoveList;
     int                     iSkip;
     int                     iTruncated;
     int                     iTotalSkip = 0;
     int                     iTotalTruncated = 0;
     int                     iTotalFiles = 0;
     int                     iBookEntries;
     bool                    bAbort = false;
     
     arrMoveList = new List<int[]>(8192);
     book        = new Book();
     using(openDlg = new OpenFileDialog()) {
         openDlg.AddExtension        = true;
         openDlg.CheckFileExists     = true;
         openDlg.CheckPathExists     = true;
         openDlg.DefaultExt          = "pgn";
         openDlg.Filter              = "Chess PGN Files (*.pgn)|*.pgn";
         openDlg.Multiselect         = true;
         if (openDlg.ShowDialog() == DialogResult.OK) {
             foreach (string strFileName in openDlg.FileNames) {
                 try {
                     stream = File.OpenRead(strFileName);
                 } catch(System.Exception) {
                     MessageBox.Show("Unable to open the file - " + strFileName);
                     stream = null;
                 }
                 if (stream != null) {
                     reader  = new StreamReader(stream);
                     strText = reader.ReadToEnd();
                     parser  = new PgnParser(false);
                     try {
                         parser.Parse(strText, arrMoveList, out iSkip, out iTruncated);
                         iTotalSkip      += iSkip;
                         iTotalTruncated += iTruncated;
                         iTotalFiles++;
                     } catch(PgnParserException exc) {
                         MessageBox.Show("Error processing file '" + strFileName + "'\r\n" + exc.Message + "\r\n" + exc.CodeInError);
                         bAbort =  true;
                     }
                     stream.Close();
                 }
                 if (bAbort) {
                     break;
                 }
             }
             if (!bAbort) {
                 iBookEntries = book.CreateBookList(arrMoveList, 30, 10);
                 MessageBox.Show(iTotalFiles.ToString() + " PNG file(s) read. " + arrMoveList.Count.ToString() + " games processed. " + iTotalTruncated.ToString() + " truncated. " + iTotalSkip.ToString() + " skipped. " + iBookEntries.ToString() + " book entries defined.");
                 using(saveDlg = new SaveFileDialog()) {
                     saveDlg.AddExtension        = true;
                     saveDlg.CheckPathExists     = true;
                     saveDlg.DefaultExt          = "bin";
                     saveDlg.Filter              = "Chess Opening Book (*.bin)|*.bin";
                     saveDlg.OverwritePrompt     = true;
                     if (saveDlg.ShowDialog() == DialogResult.OK) {
                         try {
                             book.SaveBookToFile(saveDlg.FileName);
                         } catch (System.Exception ex) {
                             MessageBox.Show(ex.Message);
                         }
                     }
                 }
             }
         }
     }
 }
예제 #3
0
 //*********************************************************     
 //
 /// <summary>
 /// Create a new game using the specified list of moves
 /// </summary>
 /// <param name="chessBoardStarting">   Starting board or null if standard board</param>
 /// <param name="listMove">             List of moves</param>
 /// <param name="eNextMoveColor">       Color starting to play</param>
 /// <param name="strWhitePlayerName">   Name of the player playing white pieces</param>
 /// <param name="strBlackPlayerName">   Name of the player playing black pieces</param>
 /// <param name="eWhitePlayerType">     Type of player playing white pieces</param>
 /// <param name="eBlackPlayerType">     Type of player playing black pieces</param>
 /// <param name="spanPlayerWhite">      Timer for white</param>
 /// <param name="spanPlayerBlack">      Timer for black</param>
 //  
 //*********************************************************     
 public virtual void CreateGameFromMove(ChessBoard                   chessBoardStarting,
                                        List<ChessBoard.MovePosS>    listMove,
                                        ChessBoard.PlayerColorE      eNextMoveColor,
                                        string                       strWhitePlayerName,
                                        string                       strBlackPlayerName,
                                        PgnParser.PlayerTypeE        eWhitePlayerType,
                                        PgnParser.PlayerTypeE        eBlackPlayerType,
                                        TimeSpan                     spanPlayerWhite,
                                        TimeSpan                     spanPlayerBlack) {
     m_board.CreateGameFromMove(chessBoardStarting,
                                listMove,
                                eNextMoveColor);
     if (m_moveListUI != null) {
         m_moveListUI.Reset(m_board);
     }
     WhitePlayerName = strWhitePlayerName;
     BlackPlayerName = strBlackPlayerName;
     WhitePlayerType = eWhitePlayerType;
     BlackPlayerType = eBlackPlayerType;
     OnUpdateCmdState(System.EventArgs.Empty);
     m_gameTimer.ResetTo(m_board.NextMoveColor,
                         spanPlayerWhite.Ticks,
                         spanPlayerBlack.Ticks);
     m_gameTimer.Enabled = true;
     Invalidate();
 }
예제 #4
0
 //*********************************************************     
 //
 /// <summary>
 /// Create a new game using the specified list of moves
 /// </summary>
 /// <param name="chessBoardStarting">   Starting board or null if standard board</param>
 /// <param name="listMove">             List of moves</param>
 /// <param name="eNextMoveColor">       Color starting to play</param>
 /// <param name="strWhitePlayerName">   Name of the player playing white pieces</param>
 /// <param name="strBlackPlayerName">   Name of the player playing black pieces</param>
 /// <param name="eWhitePlayerType">     Type of player playing white pieces</param>
 /// <param name="eBlackPlayerType">     Type of player playing black pieces</param>
 /// <param name="spanPlayerWhite">      Timer for white</param>
 /// <param name="spanPlayerBlack">      Timer for black</param>
 //  
 //*********************************************************     
 public override void CreateGameFromMove(ChessBoard                  chessBoardStarting,
                                         List<ChessBoard.MovePosS>   listMove,
                                         ChessBoard.PlayerColorE     eNextMoveColor,
                                         string                      strWhitePlayerName,
                                         string                      strBlackPlayerName,
                                         PgnParser.PlayerTypeE       eWhitePlayerType,
                                         PgnParser.PlayerTypeE       eBlackPlayerType,
                                         TimeSpan                    spanPlayerWhite,
                                         TimeSpan                    spanPlayerBlack) {
     base.CreateGameFromMove(chessBoardStarting,
                             listMove,
                             eNextMoveColor,
                             strWhitePlayerName,
                             strBlackPlayerName,
                             eWhitePlayerType,
                             eBlackPlayerType,
                             spanPlayerWhite,
                             spanPlayerBlack);
     if (eWhitePlayerType == PgnParser.PlayerTypeE.Program) {
         if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
             Father.PlayingMode              = PlayingModeE.ComputerAgainstComputer;
         } else {
             Father.PlayingMode              = PlayingModeE.PlayerAgainstComputer;
             Father.m_eComputerPlayingColor  = ChessBoard.PlayerColorE.White;
         }
     } else if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
         Father.PlayingMode              = PlayingModeE.PlayerAgainstComputer;
         Father.m_eComputerPlayingColor  = ChessBoard.PlayerColorE.Black;
     } else {
         Father.PlayingMode = PlayingModeE.PlayerAgainstPlayer;
     }
     Father.SetCmdState();
 }