Ejemplo n.º 1
0
        private void RemoveCapturedPieces(GoMoveResponse resp)
        {
            foreach (var piece in Pieces.Values)
            {
                if (piece.IsNewCapture)
                {
                    piece.IsNewCapture = false;
                    piece.RaiseMultiplePropertiesChanged();
                }
            }

            foreach (var capturedPosition in resp.MoveResult.CapturedStones.Split(' '))
            {
                if (!String.IsNullOrWhiteSpace(capturedPosition))
                {
                    if (Pieces.ContainsKey(capturedPosition))
                    {
                        var piece = Pieces[capturedPosition];
                        piece.Color        = null;
                        piece.IsNewCapture = true;
                        piece.RaiseMultiplePropertiesChanged();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void UndoMovesFromHistory()
        {
            if (Status == GoGameStatus.BlackWonDueToResignation || Status == GoGameStatus.WhiteWonDueToResignation)
            {
                History.RemoveAt(0);
            }
            else
            {
                Debug.Assert(History.Count < 2, "Can't undo more moves than have been made.");

                int count = 2;
                if (History.Count % 2 == 1)
                {
                    count = 1;
                }

                // Pop off the top move.
                for (int i = 0; i < count; i++)
                {
                    var his = History[0];
                    var pos = his.Move.Position;
                    if (his.Move.MoveType == MoveType.Normal)
                    {
                        if (Pieces.ContainsKey(pos))
                        {
                            var piece = Pieces[pos];
                            piece.IsNewPiece = false;
                            piece.Color      = null;
                            piece.RaiseMultiplePropertiesChanged();
                        }

                        var player = _players.First(p => p.Color == his.Move.Color);

                        // Restore any captured pieces.
                        foreach (var cap in his.Result.CapturedStones.Split(' '))
                        {
                            if (cap != String.Empty && Pieces.ContainsKey(cap))
                            {
                                player.Prisoners--;

                                var piece = Pieces[cap];
                                piece.IsNewPiece = false;
                                piece.Color      = his.Move.Color == GoColor.Black ? GoColor.White : GoColor.Black;
                                piece.RaiseMultiplePropertiesChanged();
                            }
                        }
                    }

                    History.RemoveAt(0);
                }

                var latestPiece = GetLatestNormalMovePieceFromHistory();
                if (latestPiece != null)
                {
                    latestPiece.IsNewPiece = true;
                    latestPiece.RaiseMultiplePropertiesChanged();
                }
            }
        }
Ejemplo n.º 3
0
 public bool CanPressedCommand(string position)
 {
     return(position != null &&
            IsBusy == false &&
            Status == GoGameStatus.Active &&
            Pieces != null &&
            Pieces.ContainsKey(position) &&
            Pieces[position].Color == null &&
            _players != null &&
            _players.Length == 2 &&
            _players[WhoseTurn].PlayerType == PlayerType.Human);
 }
Ejemplo n.º 4
0
        public async void ExecutePressedCommand(string position)
        {
            // Note: position is the Go position such as "A15" where the user pressed.

            if (!Pieces.ContainsKey(position))
            {
                return;
            }

            _savedColor            = Pieces[position].Color;
            Pieces[position].Color = _players[WhoseTurn].Color;
            Pieces[position].RaiseMultiplePropertiesChanged();

            MessageText = "Moving...";
            IsBusy      = true;
            var move = new GoMove(MoveType.Normal,
                                  _players[WhoseTurn].Color,
                                  position);
            GoMoveResponse resp = await DataRepository.PlayAsync(ActiveGame, move);

            IsBusy      = false;
            MessageText = null;

            if (resp.ResultCode == GoResultCode.Success)
            {
                AddMoveToHistory(resp.Move, resp.MoveResult);
                //Pieces[resp.Move.Position].IsNewPiece = true;
                RemoveCapturedPieces(resp);
                CurrentPlayer.Prisoners += resp.MoveResult.CapturedStones.Split(' ').Count(x => x != String.Empty);
                Pieces[position].RaiseMultiplePropertiesChanged();
                SwapTurns();

                SetState(resp.MoveResult.Status, resp.MoveResult.WinMargin);
                if (Status == GoGameStatus.Active)
                {
                    PlayCurrentUser();
                }
            }
            else if (resp.ResultCode == GoResultCode.CommunicationError)
            {
                await HandleCommunicationError("Moving...");
            }
            else
            {
                Pieces[position].Color = _savedColor;
                Pieces[position].RaiseMultiplePropertiesChanged();
                await DisplayErrorCode(resp.ResultCode);
            }

            RaiseCommandsChanged();
        }
Ejemplo n.º 5
0
        private PieceStateViewModel GetLatestNormalMovePieceFromHistory()
        {
            var latestMove = History.FirstOrDefault(hi => hi.Move.MoveType == MoveType.Normal);

            if (latestMove != null)
            {
                if (Pieces.ContainsKey(latestMove.Move.Position))
                {
                    var piece = Pieces[latestMove.Move.Position];
                    return(piece);
                }
            }

            return(null);
        }
Ejemplo n.º 6
0
 private void ClearHint()
 {
     if (_hint != null)
     {
         if (Pieces.ContainsKey(_hint))
         {
             var hintPiece = Pieces[_hint];
             if (hintPiece != null)
             {
                 hintPiece.IsHint = false;
                 hintPiece.RaiseMultiplePropertiesChanged();
             }
         }
         _hint = null;
     }
 }
Ejemplo n.º 7
0
        private void SetPieces(string p, GoColor goColor)
        {
            var split = p.Split(' ');

            for (int index = 0; index < split.Length; index++)
            {
                var pos = split[index];
                if (!String.IsNullOrWhiteSpace(pos) && Pieces.ContainsKey(pos))
                {
                    Pieces[pos].Color = goColor;
                }
                else if (!String.IsNullOrWhiteSpace(pos))
                {
                    // Can't set the sequence yet because index is not the correct value.
                    Pieces.Add(pos, new PieceStateViewModel(pos, null, goColor, false, false, false));
                }
            }
        }
Ejemplo n.º 8
0
        public async void ExecuteGetHint()
        {
            ClearHint();

            MessageText = "Getting hint...";
            IsBusy      = true;
            var resp = await DataRepository.HintAsync(ActiveGame, _players[WhoseTurn].Color);

            IsBusy      = false;
            MessageText = null;

            if (resp.ResultCode == GoResultCode.Success)
            {
                if (resp.Move.MoveType == MoveType.Normal)
                {
                    _hint = resp.Move.Position;
                    if (Pieces.ContainsKey(_hint))
                    {
                        Pieces[_hint].IsHint = true;
                        Pieces[_hint].RaiseMultiplePropertiesChanged();
                    }
                    //InvokeFleetingMessage("How about " + resp.Move.Position + '?');
                }
                else
                {
                    InvokeFleetingMessage("Perhaps you should " + resp.Move.ToString().ToLower() + '.', 1500);
                }
            }
            else if (resp.ResultCode == GoResultCode.CommunicationError)
            {
                await HandleCommunicationError("Syncronizing...");
            }
            else
            {
                await DisplayErrorCode(resp.ResultCode);
            }

            RaiseCommandsChanged();
        }
Ejemplo n.º 9
0
        public override string ToString()
        {
            var sb = new StringBuilder();

            for (byte row = 0; row < 8; row++)
            {
                sb.Append(row + " ");
                for (byte col = 0; col < 8; col++)
                {
                    var loc = new Loc(row, col);
                    if (Pieces.ContainsKey(loc))
                    {
                        sb.Append(GetName(Pieces[loc].Type) + " ");
                    }
                    else
                    {
                        sb.Append("  ");
                    }
                }
                sb.AppendLine();
            }
            return(sb.ToString());
        }
Ejemplo n.º 10
0
 public bool IsOccupied(Loc loc, bool white) =>
 Pieces.ContainsKey(loc) ? Pieces[loc].IsWhite == white : false;
Ejemplo n.º 11
0
        private async void PlayAI()
        {
            MessageText = _players[WhoseTurn].Name + " is thinking...";
            IsBusy      = true;
            var resp = await DataRepository.GenMoveAsync(ActiveGame, _players[WhoseTurn].Color);

            IsBusy      = false;
            MessageText = null;

            if (AbortOperation)
            {
                return;
            }

            if (resp.ResultCode == GoResultCode.Success)
            {
                ClearHint();

                switch (resp.Move.MoveType)
                {
                case MoveType.Normal:
                    CurrentPlayer.Prisoners += resp.MoveResult.CapturedStones.Split(' ').Count(x => x != String.Empty);
                    if (Pieces.ContainsKey(resp.Move.Position))
                    {
                        var p = Pieces[resp.Move.Position];
                        if (p != null)
                        {
                            p.Color      = CurrentPlayer.Color;
                            p.IsNewPiece = true;
                            AddMoveToHistory(resp.Move, resp.MoveResult);
                            RemoveCapturedPieces(resp);
                            SwapTurns();
                            PlayCurrentUser();
                        }
                    }
                    break;

                case MoveType.Pass:
                    AddMoveToHistory(resp.Move, resp.MoveResult);
                    await InvokeFleetingMessage(CurrentPlayer.Name + " passes.", 1500);

                    SwapTurns();
                    SetState(resp.MoveResult.Status, resp.MoveResult.WinMargin);
                    if (Status == GoGameStatus.Active)
                    {
                        PlayCurrentUser();
                    }
                    break;

                case MoveType.Resign:
                    Status      = CurrentPlayer.Color == GoColor.Black ? GoGameStatus.WhiteWon : GoGameStatus.BlackWon;
                    MessageText = CurrentPlayer.Name + " resigns.  You win!";
                    SwapTurns();
                    break;
                }
            }
            else if (resp.ResultCode == GoResultCode.CommunicationError)
            {
                await HandleCommunicationError(_players[WhoseTurn].Name + " is thinking...");
            }
            else
            {
                await DisplayErrorCode(resp.ResultCode);
            }

            RaiseCommandsChanged();
        }