예제 #1
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));
                    }
                }
            });
        }
예제 #2
0
        async Task StartProcess(GoGameState state)
        {
            if (state != null)
            {
                _state = state;
            }

            _state.Operation = GoOperation.Starting;
            SaveState();

            _fuego.StartGame(_state.Size);

            var level = _state.Player1.PlayerType == PlayerType.AI
                ? _state.Player1.Level
                : _state.Player2.Level;

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

            if (level < 3)
            {
                ParseResponse(WriteCommand("uct_param_player max_games",
                                           ((level + 1) * 10).ToString(CultureInfo.InvariantCulture)));
            }
            else if (level < 6)
            {
                ParseResponse(WriteCommand("uct_param_player max_games",
                                           (level * 2000).ToString(CultureInfo.InvariantCulture)));
            }
            else if (level < 9)
            {
                ParseResponse(WriteCommand("uct_param_player max_games",
                                           (level * 10000).ToString(CultureInfo.InvariantCulture)));
            }
            else //if (level < 9)
            {
                ParseResponse(WriteCommand("uct_param_player max_games",
                                           int.MaxValue.ToString(CultureInfo.InvariantCulture)));
            }

            //WriteCommand("komi", state.Komi.ToString(CultureInfo.InvariantCulture));
            //ReadResponse();
            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));
                }
            }


            _state.Operation = GoOperation.Idle;
            SaveState();
        }