Exemplo n.º 1
0
        public void GenMove(Guid gameId, GoColor color, out GoMove newMove, out GoMoveResult result)
        {
            // Get a Fuego instance and start it up.
            FuegoInstance inst = null;

            try
            {
                inst = GetFuegoInstance(gameId, GoOperation.GenMove);
                inst.EnsureRunning();
                inst.GenMove(color, out newMove, out result);
            }
// ReSharper disable RedundantCatchClause
            catch (Exception)
            {
                throw;
            }
// ReSharper restore RedundantCatchClause
            finally
            {
                if (inst != null)
                {
                    inst.CurrentOperation = GoOperation.Idle;
                }
            }
        }
Exemplo n.º 2
0
        private GoMoveResult AddMoveAndUpdateState(GoMove move)
        {
            GoMoveResult rval;

            try
            {
                var beforeBlack = _state.BlackPositions;
                var beforeWhite = _state.WhitePositions;

                GetStones(); // Gets the new _state.BlackPositions and _state.WhitePositions.

                if (move.Color == GoColor.Black)
                {
                    rval = new GoMoveResult(beforeWhite, _state.WhitePositions);
                }
                else
                {
                    rval = new GoMoveResult(beforeBlack, _state.BlackPositions);
                }

                _state.GoMoveHistory.Add(new GoMoveHistoryItem {
                    Move = move, Result = rval
                });

                // Change turn.
                _state.WhoseTurn = _state.WhoseTurn == GoColor.Black ? GoColor.White : GoColor.Black;

                switch (move.MoveType)
                {
                case MoveType.Resign:
                    _state.Status = move.Color == GoColor.Black
                            ? GoGameStatus.WhiteWonDueToResignation
                            : GoGameStatus.BlackWonDueToResignation;
                    break;

                case MoveType.Pass:
                    // If previous move was a pass also, mark end game, but don't record
                    // the game in the _fuego instance so that the end game
                    // dead stone calculation will still work properly.
                    var moveCount           = _state.GoMoveHistory.Count;
                    var previousMoveWasPass = moveCount >= 2 &&
                                              _state.GoMoveHistory[moveCount - 2].Move.MoveType == MoveType.Pass;
                    if (previousMoveWasPass)
                    {
                        _state.Status = GoGameStatus.Ended;
                        ParseResponse(WriteCommand("gg-undo", "1"));
                    }
                    break;
                }

                rval.Status = _state.Status;
            }
            catch (Exception)
            {
                throw;
            }

            return(rval);
        }
Exemplo n.º 3
0
        private GoMoveResult AddMoveAndUpdateState(GoMove move)
        {
            GoMoveResult rval;

            try
            {
                var beforeBlack = _state.BlackPositions;
                var beforeWhite = _state.WhitePositions;

                GetStones(); // Gets the new _state.BlackPositions and _state.WhitePositions.

                if (move.Color == GoColor.Black)
                {
                    rval = new GoMoveResult(beforeWhite, _state.WhitePositions);
                }
                else
                {
                    rval = new GoMoveResult(beforeBlack, _state.BlackPositions);
                }

                _state.GoMoveHistory.Add(new GoMoveHistoryItem {
                    Move = move, Result = rval
                });

                // Change turn.
                _state.WhoseTurn = _state.WhoseTurn == GoColor.Black ? GoColor.White : GoColor.Black;

                switch (move.MoveType)
                {
                case MoveType.Resign:
                    _state.Status = move.Color == GoColor.Black
                            ? GoGameStatus.WhiteWonDueToResignation
                            : GoGameStatus.BlackWonDueToResignation;
                    break;

                case MoveType.Pass:
                    // If previous move was a pass also, calculate winner.
                    var  moveCount           = _state.GoMoveHistory.Count;
                    bool previousMoveWasPass = moveCount >= 2 &&
                                               _state.GoMoveHistory[moveCount - 2].Move.MoveType == MoveType.Pass;
                    if (previousMoveWasPass)
                    {
                        var gameResult2 = CalculateGameResult();
                        _state.WinMargin = decimal.Parse(gameResult2.Substring(1));
                        _state.Status    = gameResult2.StartsWith("B") ? GoGameStatus.BlackWon : GoGameStatus.WhiteWon;
                    }
                    break;
                }

                rval.Status    = _state.Status;
                rval.WinMargin = _state.WinMargin;
            }
            catch (Exception ex)
            {
                throw;
            }

            return(rval);
        }
Exemplo n.º 4
0
        //public void Place(IEnumerable<string> positions, GoColor color)
        //{
        //    GoMove[] moves = positions.Select(m => new GoMove(MoveType.Normal, color, m)).ToArray();
        //    Place(moves);
        //}

        public void GenMove(GoColor color, out GoMove newMove, out GoMoveResult result)
        {
            // This debug code generates a resign from the AI randomly.
            //int x = r.Next(5);
            //if (x == 0)
            //{
            //    newMove = new GoMove(MoveType.Resign, color, null);
            //    //WriteCommand("play", color == GoColor.Black ? "black resign" : "white resign");
            //    //ReadResponse();
            //    result = AddMoveAndUpdateStateAndSaveToDatabase(newMove);
            //    return;
            //}

            WriteCommand("genmove", color == GoColor.Black ? "black" : "white");
            string code, msg;

            ReadResponse(out code, out msg);

            switch (msg)
            {
            case "PASS":
                newMove = new GoMove(MoveType.Pass, color, null);
                break;

            case "resign":
                newMove = new GoMove(MoveType.Resign, color, null);
                break;

            default:
                newMove = new GoMove(MoveType.Normal, color, msg);
                break;
            }

            // Add to move history and record new game state in database so user can
            // see what happened.
            //Thread.Sleep(30000);
            result = AddMoveAndUpdateStateAndSaveToDatabase(newMove);
        }
Exemplo n.º 5
0
        private void AddMoveToHistory(GoMove move, GoMoveResult result)
        {
            try
            {
                if (move.MoveType == MoveType.Normal)
                {
                    var latestPiece = GetLatestNormalMovePieceFromHistory();
                    if (latestPiece != null)
                    {
                        latestPiece.IsNewPiece = false;
                        latestPiece.RaiseMultiplePropertiesChanged();
                    }
                }

                History.Insert(0, new GoMoveHistoryItem
                {
                    Move   = move,
                    Result = result,
                });

                if (move.MoveType == MoveType.Normal)
                {
                    var latestPiece = GetLatestNormalMovePieceFromHistory();
                    if (latestPiece != null)
                    {
                        latestPiece.IsNewPiece = true;
                        latestPiece.RaiseMultiplePropertiesChanged();
                    }
                }
            }
            catch (Exception)
            {
                // eat a winrt bug where the UI sees the inserted item and throws an exception if the user
                // already navigated away from the game page.
                throw;
            }
        }
Exemplo n.º 6
0
 public GoMoveResponse(GoResultCode resultCode, GoMove move, GoMoveResult result)
     : base(resultCode)
 {
     Move       = move;
     MoveResult = result;
 }
Exemplo n.º 7
0
        private GoMoveResult AddMoveAndUpdateStateAndSaveToDatabase(GoMove move)
        {
            try
            {
                var beforeBlack = State.BlackPositions;
                var beforeWhite = State.WhitePositions;

                GetStones(); // Gets the new _state.BlackPositions and _state.WhitePositions.

                GoMoveResult rval;
                if (move.Color == GoColor.Black)
                {
                    rval = new GoMoveResult(beforeWhite, State.WhitePositions);
                }
                else
                {
                    rval = new GoMoveResult(beforeBlack, State.BlackPositions);
                }

                if (State.GoMoveHistory == null)
                {
                    State.GoMoveHistory = new List <GoMoveHistoryItem>();
                }
                State.GoMoveHistory.Add(new GoMoveHistoryItem {
                    Move = move, Result = rval
                });

                // Change turn.
                State.WhoseTurn = State.WhoseTurn == GoColor.Black ? GoColor.White : GoColor.Black;

                switch (move.MoveType)
                {
                case MoveType.Resign:
                    State.Status = move.Color == GoColor.Black
                            ? GoGameStatus.WhiteWonDueToResignation
                            : GoGameStatus.BlackWonDueToResignation;
                    //var gameResult = CalculateGameResult();
                    //State.WinMargin = Decimal.Parse(gameResult.Substring(1));
                    break;

                case MoveType.Pass:
                    // If previous move was a pass also, calculate winner.
                    var  moveCount           = State.GoMoveHistory.Count;
                    bool previousMoveWasPass = moveCount >= 2 &&
                                               State.GoMoveHistory[moveCount - 2].Move.MoveType == MoveType.Pass;
                    if (previousMoveWasPass)
                    {
                        var gameResult2 = CalculateGameResult();
                        State.WinMargin = Decimal.Parse(gameResult2.Substring(1));
                        State.Status    = gameResult2.StartsWith("B") ? GoGameStatus.BlackWon : GoGameStatus.WhiteWon;
                    }
                    break;
                }

                rval.Status    = State.Status;
                rval.WinMargin = State.WinMargin;

                // Save to database.
                _goGRepository.SaveGameState(CurrentGameId, State);

                return(rval);
            }
            catch (DbEntityValidationException)
            {
                // Setting State to null will cause it to be loaded from database the next time
                // a client initiates an operation.
                State = null;
                throw;
            }
        }
Exemplo n.º 8
0
        public async Task <GoMoveResponse> PlayAsync(Guid gameid, GoMove move)
        {
            GoMoveResponse rval;

            GoMoveResult moveResult = null;

            try
            {
                await EnsureFuegoStarted();

                if (move.MoveType == MoveType.Resign)
                {
                    // Fuego doesn't support the command to resign.
                    _state.Operation = GoOperation.Resign;
                    SaveState();

                    moveResult = AddMoveAndUpdateState(move);
                }
                else
                {
                    string position;
                    switch (move.MoveType)
                    {
                    case MoveType.Normal:
                        position         = move.Position;
                        _state.Operation = GoOperation.NormalMove;
                        break;

                    case MoveType.Pass:
                        position         = "PASS";
                        _state.Operation = GoOperation.Pass;
                        break;

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

                    await Task.Factory.StartNew(
                        () =>
                    {
                        SaveState();

                        // This throws a GoEngineException on any failure.
                        ParseResponse(WriteCommand("play", (move.Color == GoColor.Black ? "black" : "white") + ' ' + position));

                        // Add to move history and persist new game state so user can
                        // see what happened.
                        moveResult       = AddMoveAndUpdateState(move);
                        _state.Operation = GoOperation.Idle;
                        SaveState();
                    });
                }

                Debug.Assert(moveResult != null, "moveResult != null");
                rval = new GoMoveResponse(GoResultCode.Success, move, moveResult);
            }
            catch (GoEngineException gex)
            {
                rval = new GoMoveResponse(gex.Code, null, null);
            }
            catch (Exception ex)
            {
                rval = new GoMoveResponse(GoResultCode.ServerInternalError, null, null);
            }
            return(rval);
        }