Example #1
0
 public void VaiStato2()
 {
     //Window.Alert("KK");
     state.Go("state2.inner");
 }
Example #2
0
            public Action Start(
                Func<ArraySegment<byte>, Action, bool> next,
                Action<Exception> error,
                Action complete)
            {
                const int bufferSize = 4096;
                var buffer = new byte[bufferSize];
                var retval = new[] {0};
                StreamAwaiter write = null;
                long totalBytes = 0;

                // This machine moves data from the spool to the replay storage
                var state = new State();
                state.Go = mark =>
                {
                    try
                    {
                        switch (mark)
                        {
                            case 1:
                                goto mark1;
                            case 2:
                                goto mark2;
                            case 3:
                                goto mark3;
                        }

                        //// MEMORY LOOP
                        mark4:

                        // grab data from the spool
                        retval[0] = 0;
                        if (_spool.Pull(new ArraySegment<byte>(buffer, 0, bufferSize), retval, () => state.Go(3)))
                            return;
                        mark3:

                        if (retval[0] <= 0)
                            goto markreturn;

                        if (state.Cancelled)
                            goto markreturn;

                        // add to memory until threshold reached
                        _pages.Add(new ArraySegment<byte>(buffer, 0, retval[0]));

                        buffer = new byte[bufferSize]; // todo: get these from a page pool

                        totalBytes += retval[0];

                        if (totalBytes < _tempFileThresholdBytes)
                            goto mark4;
                        //// MEMORY LOOP

                        // create temp file at this point
                        _tempFileName = Path.GetTempFileName();
                        _tempFile = new FileStream(
                            _tempFileName,
                            FileMode.Create,
                            FileSystemRights.WriteData | FileSystemRights.ReadData,
                            FileShare.None,
                            4096,
                            FileOptions.SequentialScan | FileOptions.Asynchronous | FileOptions.WriteThrough | FileOptions.DeleteOnClose);

                        //// TEMPFILE LOOP
                        mark0:

                        // grab data from the spool
                        retval[0] = 0;
                        if (_spool.Pull(new ArraySegment<byte>(buffer, 0, bufferSize), retval, () => state.Go(1)))
                            return;
                        mark1:

                        // no more data - normal completion
                        if (retval[0] == 0)
                            goto markreturn;

                        // cancelled from outside the loop
                        if (state.Cancelled)
                            goto markreturn;

                        // send along to temp file
                        write = StreamAwaiter.Write(_tempFile, buffer, 0, retval[0]);
                        if (write.BeginAwait(() => state.Go(2)))
                            return;
                        mark2:
                        write.EndAwait();

                        // cancelled from outside the loop
                        if (state.Cancelled)
                            goto markreturn;

                        goto mark0;
                        //// TEMPFILE LOOP

                        markreturn:
                        _spoolComplete.Set();
                    }
                    catch (Exception ex)
                    {
                        /* need to comminucate failures outward!!! */
                        _spoolComplete.Set();
                    }
                };
                state.Go(0);

                // This machine moves data from the source to the caller and the spool
                var bodyCancel = _body(
                    (data, continuation) =>
                    {
                        _spool.Push(data, null);
                        return next(data, continuation);
                    },
                    ex =>
                    {
                        _spool.PushComplete();
                        error(ex);
                    },
                    () =>
                    {
                        _spool.PushComplete();
                        complete();
                    });

                // returns a cancellation of both machines
                return () =>
                {
                    state.Cancelled = true;
                    bodyCancel();
                };
            }
Example #3
0
        /*
         * Please see https://ucichessengine.wordpress.com/2011/03/16/description-of-uci-protocol/ for details
         * of the UCI Command Interface as documented by Stefan Meyer-Kahlen
         *
         * Commands
         * --------
         * uci => must respond w "id name" and "id author" then w option support below, ending w "uciok"
         * debug [on | off] => invites "info string" responses.
         * isready => starts Engine, which must respond w "readyok" (even when searching.)
         * setoption name Name [value Value]
         * ucinewgame
         * position [epd | fen | startpos] [moves ...]
         * position random
         * go [searchmoves | ponder | wtime | btime | winc | binc | movestogo |
         *     depth | nodes | mate | movetime | infinite] => respond w "bestmove ... [ponder ...]"
         * stop
         * ponderhit
         * register [later | name | code] => registration [checking | ok | error]
         * quit
         *
         * Info
         * ----
         * info [depth | seldepth | time | nodes | pw | multipv |
         *       score [cp | mate | lowerbound | upperbound] |
         *       currmove | currmovenumber | hashfull | nps | tbhits |
         *       cpuload | string | refutation | currline]
         *
         * Option
         * ------
         * option name [Hash | SyzygyPath | SyzygyCache | Nullmove | OwnBook | Ponder | MultiPV |
         *              UCI_ShowCurrLine | UCI_ShowShowRefutations | UCI_LimitStrength |
         *              UCI_Elo | UCI_AnalyseMode | UCI_Opponent]
         *
         * Type
         * ----
         * type [check | spin | combo | button | string] default | min | max | var*
         */
        public Boolean Dispatch(String sVerb)
        {
            if (Parser is null)
            {
                throw new ChessException("Uninitialized Parser");
            }

            var bContinue = true;

            switch (sVerb.ToLower())
            {
            case "":                    // Empty Command String
                break;

            case "uci":                 //[UCI]
                Info();
                break;

            case "isready":             //[UCI]
                initStaticFields();
                LogLine("readyok");
                break;

            case "register":            //[UCI]
                parseRegister(Parser);
                break;

            case "timertest":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }

                State.OnMoveCommand();
                State.MovePosition.TimerTest();
                break;

            case "test":                //[ToDo]End gracefully when a Search is in progress!
                if (State is null)
                {
                    newState();
                }
                if (State.IsSearchInProgress)
                {
                    throw new ChessException("Search in progress");
                }
                else
                {
                    newGameFEN(DefaultFEN);
                }
                break;

            case "testepd":             //[ToDo]End gracefully when a Search is in progress!
                if (State is null)
                {
                    newState();
                }
                if (State.IsSearchInProgress)
                {
                    throw new ChessException("Search in progress");
                }
                else
                {
                    newGameEPD(DefaultEPD);
                }
                break;

            case "reset":               // Intuitive
            case "ucinewgame":          //[UCI]
                if (State is null)
                {
                    newState();
                }

                if (State.IsSearchInProgress)
                {
                    State.Stop();
                }

                newGameFEN();
                break;

            case "position":            //[UCI]
                if (State is null)
                {
                    newState();
                }
                if (State.IsSearchInProgress)
                {
                    throw new ChessException("Search in progress");
                }
                else
                {
                    var position = NewGame();
                    //
                    //[Note]ParsePosition() will apply a sequence
                    // of moves that follows the initial position.
                    //
                    State.MovePosition = Parser.ParsePosition(position);
                }
                break;

            case "board":               //[Test]In the absence of a GUI
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.MovePosition is null)
                {
                    throw new ChessException("Uninitialized Position");
                }
                else
                {
                    State.MovePosition.Display();
                }
                break;

            case "tabiya":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.MovePosition is null)
                {
                    throw new ChessException("Uninitialized Position");
                }

                Parser.ParseTabiya(State.MovePosition);
                break;

            case "moves":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }

                State.OnMoveCommand();
                State.MovePosition = State.MovePosition.ParsePACNMakeMoves(Parser);
                break;

            case "unmove":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }

                State.OnMoveCommand();
                if (ReferenceEquals(State.MovePosition, State.RootPosition))
                {
                    throw new ChessException("No Move has been made");
                }

                State.Unmove();
                break;

            case "list":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.MovePosition is null)
                {
                    throw new ChessException("Uninitialized Position");
                }

                State.ListMovesFromRoot(State.MovePosition, Parser.ParseList());
                break;

            case "getoption":           //[Debug]
                Parser.ParseGetOption();
                break;

            case "resetoption":         //[Debug]
                if (State is null)
                {
                    newState();         // Event Handler may require GameState
                }
                Parser.ParseResetOption();
                break;

            case "setoption":           //[UCI]
                if (State is null)
                {
                    newState();         // Event Handler may require GameState
                }
                Parser.ParseSetOption();
                break;

            case "debug":
                parseDebug(Parser);
                break;

            case "perft":               //[Test]Look for corresponding Tabiya and run PerftCases
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }

                State.OnMoveCommand();
                Parser.ExpectEOL();
                State.Perft();
                break;

            case "go":                  //[UCI]
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }

                State.OnMoveCommand();
                State.Go(Parser);
                break;

            case "best":                //[Test]
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.MovePosition is null)
                {
                    throw new ChessException("Uninitialized Position");
                }
                else if (State.BestMoves is not null)
                {
                    var sb = new StringBuilder();
                    //[Note]refreshPV() may not have been called
                    sb.BestMove(State.BestMoves, State.Rule);
                    if (sb.Length > 0)
                    {
                        throw new ChessException(sb.ToString());
                    }
                }
                break;

            case "ponderhit":           //[UCI]
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else
                {
                    State.Ponderhit();
                }
                break;

#if UseTask
            case "status":
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.EngineTask is null)
                {
                    throw new ChessException("No search in progress");
                }

                throw new ChessException($"Search is {State.EngineTask.Status}");
            //[Unreachable]break;

            case "stop":                //[UCI]
                if (State is null)
                {
                    throw new ChessException("Uninitialized Game");
                }
                else if (State.EngineTask is null)
                {
                    throw new ChessException("No search in progress");
                }

                if (State.IsSearchInProgress)
                {
                    State.Stop();
                }
                break;
#endif
            case "exit":                // Make it easy to quit
            case "quit":                //[UCI]
                if (State is not null && State.IsSearchInProgress)
                {
                    State.Stop();
                }
                bContinue = false;
                break;

            default:
                throw new ParseException($"Ignored {sVerb} command");
            }

            return(bContinue);
        }
Example #4
0
            public Action Replay(Func<ArraySegment<byte>, Action, bool> next, Action<Exception> error, Action complete)
            {
                const int bufferSize = 4096;
                var buffer = new byte[bufferSize];
                StreamAwaiter read = null;
                var pageEnumerator = _pages.GetEnumerator();

                // This machine draws from the temp file and sends to the caller
                var state = new State();
                state.Go = mark =>
                {
                    try
                    {
                        switch (mark)
                        {
                            case 1:
                                goto mark1;
                            case 2:
                                goto mark2;
                            case 4:
                                goto mark4;
                        }

                        //// MEMORY LOOP
                        mark5:
                        // move to the next memory page
                        if (!pageEnumerator.MoveNext())
                            goto mark3;

                        // send along to output
                        if (next(pageEnumerator.Current, () => state.Go(4)))
                            return;
                        mark4:

                        if (state.Cancelled)
                            goto markreturn;
                        goto mark5;
                        //// MEMORY LOOP

                        mark3:

                        if (_tempFile == null)
                            goto markreturn;

                        _tempFile.Seek(0, SeekOrigin.Begin);

                        //// TEMPFILE LOOP
                        mark0:

                        // read some data from the temp file
                        read = StreamAwaiter.Read(_tempFile, buffer, 0, bufferSize);
                        if (read.BeginAwait(() => state.Go(1)))
                            return;
                        mark1:
                        var count = read.EndAwait();

                        // end of file, normal completion
                        if (count == 0)
                            goto markreturn;

                        // cancelled from outside the loop
                        if (state.Cancelled)
                            return;

                        // send along to output
                        if (next(new ArraySegment<byte>(buffer, 0, count), () => state.Go(2)))
                            return;
                        mark2:

                        // cancelled from outside the loop
                        if (state.Cancelled)
                            return;

                        // loop for more
                        goto mark0;
                        //// TEMPFILE LOOP

                        markreturn:
                        complete();
                    }
                    catch (Exception ex)
                    {
                        error(ex);
                    }
                };

                // starts the machine only when the earlier transfer form spool to file is fully complete
                _spoolComplete.Continue(() => state.Go(0));

                return () => state.Cancelled = true;
            }