示例#1
0
        private void Sample()
        {
            CuckComp = new ComputerPlayer();
            CuckHumn = new HumanPlayer();
            CuckBK   = new Book(false);
            CuckGM   = new Game(CuckHumn, CuckComp);
            Position pos = CuckGM.getPos();

            // e4(102) d4(31) ...
            string CurrentBookMoves = CuckBK.getAllBookMoves(pos);

            // Nb1-a3;Nb1-c3;...;a2-a3;a2-a4;...
            string CurrentValidMoves = TextIO.AllMovesTostring(pos, true);

            // RNB...w KQkq...
            string CurrentPositionFEN = TextIO.toFEN(pos);

            // Display board to console
            TextIO.DispBoard(pos);

            // Swap & move
            CuckGM.whitePlayer = CuckComp;
            CuckGM.blackPlayer = CuckHumn;
            ////CuckComp.bookEnabled = false;
            CuckComp.maxTimeMillis = 1 * 100;
            CuckComp.maxTimeMillis = 6 * 100;
            ////CuckComp.maxDepth = 6;

            // Ng1-f3
            string CommandFromComp = CuckComp.getCommand(new Position(pos), CuckGM.haveDrawOffer(), CuckGM.getHistory());
        }
示例#2
0
        public List <string> getPosHistory()
        {
            List <string> ret = new List <string>();

            Position pos2 = new Position(pos /*this.pos*/);

            for (int i = currentMove; i > 0; i--)
            {
                pos2.unMakeMove(moveList[i - 1], uiInfoList[i - 1]);
            }
            ret.Add(TextIO.toFEN(pos2)); // Store initial FEN

            string moves = "";

            for (int i = 0; i < moveList.Count; i++)
            {
                Move   move    = moveList[i];
                string strMove = TextIO.moveTostring(pos2, move, false);
                moves += " " + strMove;
                UndoInfo ui = new UndoInfo();
                pos2.makeMove(move, ui);
            }
            ret.Add(moves); // Store move list string
            int numUndo = moveList.Count - currentMove;

            ret.Add(((int)numUndo).ToString());
            return(ret);
        }
示例#3
0
        public static string simplyCalculateMove(string sFEN, int depth)
        {
            bool w2m = sFEN.Contains(" w ");

            CuckComp = new ComputerPlayer();
            CuckHumn = new HumanPlayer();
            if (w2m)
            {
                CuckGM = new Game(CuckComp, CuckHumn);
            }
            else
            {
                CuckGM = new Game(CuckHumn, CuckComp);
            }
            CuckGM.handleCommand("setpos " + sFEN);
            Position pos = CuckGM.getPos();

            CuckComp.maxTimeMillis = 1 * 100;
            CuckComp.maxTimeMillis = 6 * 100;
            CuckComp.maxDepth      = depth;

            string CurrentPositionFEN = TextIO.toFEN(pos);

            string cmd = CuckComp.getCommand(new Position(pos),
                                             CuckGM.haveDrawOffer(), CuckGM.getHistory());

            string a = cmd;

            if ((a.Length > 1) && (("NBRQK").IndexOf(a[0]) >= 0))
            {
                a = a.Substring(1);
            }
            if (a[0] == 'O')
            {
                a = (a.Length == 3 ? (w2m ? "e1g1" : "e8g8") : (w2m ? "e1c1" : "e8c8"));
            }
            else
            {
                a = ((a.Length > 4) ? a.Substring(0, 2) + a.Substring(3) : "");
            }
            return(a);
        }
示例#4
0
 /**
  * Handle a special command.
  * @param moveStr  The command to handle
  * @return  True if command handled, false otherwise.
  */
 public bool handleCommand(string moveStr)
 {
     if (moveStr == "new")
     {
         moveList         = new List <Move>();
         uiInfoList       = new List <UndoInfo>();
         drawOfferList    = new List <bool>();
         currentMove      = 0;
         pendingDrawOffer = false;
         drawState        = GameState.ALIVE;
         resignState      = GameState.ALIVE;
         try
         {
             pos = TextIO.readFEN(TextIO.startPosFEN);
         }
         catch
         {
             throw new RuntimeException();
         }
         whitePlayer.clearTT();
         blackPlayer.clearTT();
         activateHumanPlayer();
         return(true);
     }
     else if (moveStr == "undo")
     {
         if (currentMove > 0)
         {
             pos.unMakeMove(moveList[currentMove - 1], uiInfoList[currentMove - 1]);
             currentMove--;
             pendingDrawOffer = false;
             drawState        = GameState.ALIVE;
             resignState      = GameState.ALIVE;
             return(handleCommand("swap"));
         }
         else
         {
             SystemHelper.println("Nothing to undo");
         }
         return(true);
     }
     else if (moveStr == "redo")
     {
         if (currentMove < moveList.Count)
         {
             pos.makeMove(moveList[currentMove], uiInfoList[currentMove]);
             currentMove++;
             pendingDrawOffer = false;
             return(handleCommand("swap"));
         }
         else
         {
             SystemHelper.println("Nothing to redo");
         }
         return(true);
     }
     else if ((moveStr == "swap") || (moveStr == "go"))
     {
         Player tmp = whitePlayer;
         whitePlayer = blackPlayer;
         blackPlayer = tmp;
         return(true);
     }
     else if (moveStr == "list")
     {
         listMoves();
         return(true);
     }
     else if (moveStr.StartsWith("setpos "))
     {
         string   fen    = moveStr.Substring(moveStr.IndexOf(" ") + 1);
         Position newPos = null;
         try
         {
             newPos = TextIO.readFEN(fen);
         }
         catch
         {
             SystemHelper.println("Invalid FEN: " + fen);
         }
         if (newPos != null)
         {
             handleCommand("new");
             pos = newPos;
             activateHumanPlayer();
         }
         return(true);
     }
     else if (moveStr == "getpos")
     {
         string fen = TextIO.toFEN(pos);
         SystemHelper.println(fen);
         return(true);
     }
     else if (moveStr.StartsWith("draw "))
     {
         if (getGameState() == GameState.ALIVE)
         {
             string drawCmd = moveStr.Substring(moveStr.IndexOf(" ") + 1);
             return(handleDrawCmd(drawCmd));
         }
         else
         {
             return(true);
         }
     }
     else if (moveStr == "resign")
     {
         if (getGameState() == GameState.ALIVE)
         {
             resignState = pos.whiteMove ? GameState.RESIGN_WHITE : GameState.RESIGN_BLACK;
             return(true);
         }
         else
         {
             return(true);
         }
     }
     else if (moveStr.StartsWith("book"))
     {
         string bookCmd = moveStr.Substring(moveStr.IndexOf(" ") + 1);
         return(handleBookCmd(bookCmd));
     }
     else if (moveStr.StartsWith("time"))
     {
         try
         {
             string timeStr   = moveStr.Substring(moveStr.IndexOf(" ") + 1);
             int    timeLimit = int.Parse(timeStr);
             whitePlayer.timeLimit(timeLimit, timeLimit, false);
             blackPlayer.timeLimit(timeLimit, timeLimit, false);
             return(true);
         }
         catch
         {
             SystemHelper.println("Number format exception: " + moveStr);
             return(false);
         }
     }
     else if (moveStr.StartsWith("perft "))
     {
         try
         {
             string  depthStr = moveStr.Substring(moveStr.IndexOf(" ") + 1);
             int     depth    = int.Parse(depthStr);
             MoveGen moveGen  = new MoveGen();
             long    t0       = SystemHelper.currentTimeMillis();
             ulong   nodes    = perfT(moveGen, pos, depth);
             long    t1       = SystemHelper.currentTimeMillis();
             SystemHelper.println("perft(" + depth.ToString() + ") = " +
                                  nodes.ToString() + " t=" + ((t1 - t0) / 1000).ToString() + "s");
         }
         catch
         {
             SystemHelper.println("Number format exception: " + moveStr);
             return(false);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }