Пример #1
0
 public FuegoEngine(int boardSize)
 {
     this.boardSize      = boardSize;
     this._fuegoInstance = new FuegoInstance();
     this._fuegoInstance.StartGame((byte)boardSize);
     this._fuegoInstance.HandleCommand("uct_max_memory 200000000");
 }
Пример #2
0
        /// <summary>
        /// Starts the fuego engine and resets its game state to the given state.
        /// </summary>
        /// <param name="gameid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public async Task <GoGameStateResponse> StartAsync(Guid gameid, GoGameState state)
        {
            GoGameStateResponse rval;

            try
            {
                // Either we're starting a new game, or a game already exist and we're just
                // setting up fuego.
                Debug.Assert(state != null || _state != null, "state != null || _state != null");

                _fuego = new FuegoInstance();

                await Task.Factory.StartNew(() => StartProcess(state));

                rval = new GoGameStateResponse(GoResultCode.Success, state);
            }
            catch (GoEngineException gex)
            {
                rval = new GoGameStateResponse(gex.Code, null);
            }
            catch (Exception ex)
            {
                rval = new GoGameStateResponse(GoResultCode.ServerInternalError, null);
            }

            return(rval);
        }
Пример #3
0
        //private string CalculateGameResult()
        //{
        //    var result = ParseResponse(WriteCommand("final_score"));

        //    return result.Msg;
        //}

        private async Task EnsureFuegoStartedAndMatchingGame(Guid id)
        {
            await Task.Run(() =>
            {
                // We don't want callers to handle GameDoesNotExist errors.
                _states.TryGetValue(id, out var state);
                Debug.Assert(state != null,
                             $"Don't call {nameof(EnsureFuegoStartedAndMatchingGame)} with the ID of a game that does not exist!");

                var needSaveAndRestartEngine = false;
                if (_fuego == null)
                {
                    _fuego = new FuegoInstance();
                    needSaveAndRestartEngine = true;
                }
                else if (_state == null || _state.Id != id)
                {
                    needSaveAndRestartEngine = true;
                }

                if (!needSaveAndRestartEngine)
                {
                    return;
                }

                _state = state;

                Debug.Assert(
                    _states.ContainsKey(_state.Id) &&
                    _states[_state.Id] == _state,
                    "_state or _state.Id is not in _states dct!");

                _fuego.StartGame(_state.Size);

                // Set up parameters and clear board.
                //await WriteCommand("uct_max_memory", (1024 * 1024 * 250).ToString());


                SetDifficulty();

                ParseResponse(WriteCommand("komi", _state.Player2.Komi.ToString(CultureInfo.InvariantCulture)));
                ParseResponse(WriteCommand("clear_board"));
                ParseResponse(WriteCommand("go_param_rules", "capture_dead 1"));

                // Set up board with some pre-existing moves.
                if (_state.GoMoveHistory.Count > 0)
                {
                    // Must actually play every move back because otherwise undo operations
                    // won't work.
                    foreach (var m in _state.GoMoveHistory)
                    {
                        string position;
                        switch (m.Move.MoveType)
                        {
                        case MoveType.Normal:
                            position = m.Move.Position;
                            break;

                        case MoveType.Pass:
                            position = "PASS";
                            break;

                        default:
                            throw new ArgumentException("Unrecognized move type: " + m.Move.MoveType);
                        }

                        ParseResponse(WriteCommand("play",
                                                   (m.Move.Color == GoColor.Black ? "black" : "white") + ' ' + position));
                    }
                }
            });
        }