Пример #1
0
        void RemovePiece(object sender, EventArgs e)
        {
            if (capturedPiece != null)
            {
                capturedPiece.Visibility = Visibility.Hidden;

                if (capturedPiece is RedChecker)
                {
                    RedChecker deadman        = new RedChecker();
                    Storyboard AddToGraveyard =
                        deadman.Resources["AddToGraveyard"] as Storyboard;
                    this.pnlBlackGraveyard.Children.Add(deadman);
                    AddToGraveyard.Begin();
                }
                else if (capturedPiece is BlackChecker)
                {
                    BlackChecker deadman        = new BlackChecker();
                    Storyboard   AddToGraveyard =
                        deadman.Resources["AddToGraveyard"] as Storyboard;
                    this.pnlRedGraveyard.Children.Add(deadman);
                    AddToGraveyard.Begin();
                }
                else if (capturedPiece is RedKingChecker)
                {
                    RedKingChecker deadman        = new RedKingChecker();
                    Storyboard     AddToGraveyard =
                        deadman.Resources["AddToGraveyard"] as Storyboard;
                    this.pnlBlackGraveyard.Children.Add(deadman);
                    AddToGraveyard.Begin();
                }
                else if (capturedPiece is BlackKingChecker)
                {
                    BlackKingChecker deadman        = new BlackKingChecker();
                    Storyboard       AddToGraveyard =
                        deadman.Resources["AddToGraveyard"] as Storyboard;
                    this.pnlRedGraveyard.Children.Add(deadman);
                    AddToGraveyard.Begin();
                }
                grdBoard.Children.Remove(capturedPiece);
            }
        }
Пример #2
0
        // Here, the source is the drop target which in this case is a Label. This is needed to get
        // a reference to the underlying grid cell. That way we know the cell to which to add the new
        // image.
        void grdBoard_Drop(object sender, DragEventArgs e)
        {
            // /red is user
            string from = "", to = "";             //hit = "";

            // use the label in the cell to get the current row and column
            EmptySpace l = e.Source as EmptySpace;
            int        r = Grid.GetRow((EmptySpace)e.Source);
            int        c = Grid.GetColumn((EmptySpace)e.Source);

            okToMove = false;
            from     = currentPiece.row + "," + currentPiece.col;
            to       = r + "," + c;

            CheckerPiece checker;

            if (currentPiece is RedChecker || currentPiece is RedKingChecker)
            {
                if (currentTurn != Turn.Red)
                {
                    // Should never be here
                    System.Windows.Forms.MessageBox.Show("It's not your turn");
                    return;
                }

                illegalTouchCounter = 0;

                // It's red's turn...
                if (currentPiece is RedKingChecker ||
                    (currentPiece is RedChecker && r == RED_LAST_ROW))
                {
                    checker = new RedKingChecker();
                }
                else
                {
                    checker = new RedChecker();
                }


                if (currentPiece is RedChecker || currentPiece is RedKingChecker)
                {
                    if ((l.row == currentPiece.row - 1 &&
                         (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1))
                        ||
                        ((l.row == currentPiece.row + 1 && currentPiece is RedKingChecker) &&
                         (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1)))
                    {
                        okToMove = true;
                    }
                }

                //now check to see if user captured anything
                //if the logic is hard to follow,
                //it basically checks the location if the supposedly hit agent(black) checker
                //based on the fact that user (red) piece could be crown, and assign a
                //crowned or normal hit piece, if any, to the opponentPiece variable.
                //>>
                CheckerPiece opponentPiece = null;                 //was black

                if (c == currentPiece.col + 2)
                {
                    if (r == currentPiece.row - 2)
                    {
                        if (grdBoard.Children.OfType <BlackChecker>()
                            .Where(p => p.row == currentPiece.row - 1 &&
                                   (p.col == currentPiece.col + 1)).SingleOrDefault() != null)
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackChecker>()
                                            .Where(p => p.row == currentPiece.row - 1 &&
                                                   (p.col == currentPiece.col + 1)).SingleOrDefault();
                        }
                        else
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackKingChecker>()
                                            .Where(p => p.row == currentPiece.row - 1 &&
                                                   (p.col == currentPiece.col + 1)).SingleOrDefault();
                        }
                    }
                    else if (r == currentPiece.row + 2 && currentPiece is RedKingChecker)
                    {
                        if (grdBoard.Children.OfType <BlackChecker>()
                            .Where(p => p.row == currentPiece.row + 1 &&
                                   (p.col == currentPiece.col + 1)).SingleOrDefault() != null)
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackChecker>()
                                            .Where(p => p.row == currentPiece.row + 1 &&
                                                   (p.col == currentPiece.col + 1)).SingleOrDefault();
                        }
                        else
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackKingChecker>()
                                            .Where(p => p.row == currentPiece.row + 1 &&
                                                   (p.col == currentPiece.col + 1)).SingleOrDefault();
                        }
                    }
                }
                else if (c == currentPiece.col - 2)
                {
                    if (r == currentPiece.row - 2)
                    {
                        if (grdBoard.Children.OfType <BlackChecker>()
                            .Where(p => p.row == currentPiece.row - 1 &&
                                   (p.col == currentPiece.col - 1)).SingleOrDefault() != null)
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackChecker>()
                                            .Where(p => p.row == currentPiece.row - 1 &&
                                                   (p.col == currentPiece.col - 1)).SingleOrDefault();
                        }
                        else
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackKingChecker>()
                                            .Where(p => p.row == currentPiece.row - 1 &&
                                                   (p.col == currentPiece.col - 1)).SingleOrDefault();
                        }
                    }
                    else if ((r == currentPiece.row + 2 && currentPiece is RedKingChecker))
                    {
                        if (grdBoard.Children.OfType <BlackChecker>()
                            .Where(p => p.row == currentPiece.row + 1 &&
                                   (p.col == currentPiece.col - 1)).SingleOrDefault() != null)
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackChecker>()
                                            .Where(p => p.row == currentPiece.row + 1 &&
                                                   (p.col == currentPiece.col - 1)).SingleOrDefault();
                        }
                        else
                        {
                            opponentPiece = grdBoard.Children.OfType <BlackKingChecker>()
                                            .Where(p => p.row == currentPiece.row + 1 &&
                                                   (p.col == currentPiece.col - 1)).SingleOrDefault();
                        }
                    }
                }
                //<<

                //>> now remove if any black or black king was hit
                if (opponentPiece != null && Math.Abs(l.row - currentPiece.row) == 2)
                {
                    int validCol = (opponentPiece.col > currentPiece.col) ?
                                   currentPiece.col + 2 : currentPiece.col - 2;
                    if (((r == currentPiece.row - 2) ||
                         (Math.Abs(r - currentPiece.row) == 2 &&
                          currentPiece is RedKingChecker)) && c == validCol)
                    {
                        Storyboard PieceCaptured =
                            opponentPiece.Resources["PieceCaptured"] as Storyboard;
                        capturedPiece = opponentPiece;

                        if (PieceCaptured != null)
                        {
                            PieceCaptured.Completed += new EventHandler(RemovePiece);
                            PieceCaptured.Begin();
                        }
                        okToMove = true;
                    }
                }
                if (okToMove)
                {
                    //currentTurn = Turn.Black;
                    this.UserPlayed(this, new CheckerEventArg
                    {
                        moveDesc = from + "//" + to
                    });
                }
            }

            else // that is, if touched checker is black or black king <><><><><><><><><<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
            {
                //when user touches agent stuff...
                if (currentTurn != Turn.Black)
                {
                    this.UserTouchedAgentChecker(this,
                                                 new UserTouchedAgentStuffEventArg
                    {
                        howManyTimes = ++illegalTouchCounter
                    });
                    //System.Windows.Forms.MessageBox.Show("Wait, that's mine!");
                    return;
                }

                //should never be here
                //>>Few lines below only for debugging (until "<<")
                checker = new BlackChecker();
                if (l.row == currentPiece.row + 1 &&
                    (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1))
                {
                    okToMove = true;
                }
                CaptureHumanCellInAgentMoveIfAny(r, c, l);
                if (okToMove)
                {
                    currentTurn = Turn.Red;
                }
                //<<
            }

            //if (okToMove
            //    && (checker is BlackChecker || checker is BlackKingChecker))
            //{
            //    MoveChecker(r, c, checker);
            //}

            //not doing the move until confirmed by Java side
            if (okToMove &&
                (checker is RedChecker || checker is RedKingChecker))
            {
                latestR = r; latestC = c;
                LatestRedTryingToMove = checker;
            }
        }