コード例 #1
0
ファイル: GameShape.xaml.cs プロジェクト: zessiez/always
        public void MoveChecker(int r, int c, CheckerPiece checker)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                checker.col = c;
                checker.row = r;

                // bind the mouse events
                checker.PreviewMouseLeftButtonDown +=
                    new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown);
                checker.PreviewMouseMove +=
                    new MouseEventHandler(grdBoard_PreviewMouseMove);
                checker.Cursor = Cursors.Hand;
                checker.AllowDrop = false;

                // add the piece to the board
                Grid.SetRow(checker, r);
                Grid.SetColumn(checker, c);
                this.grdBoard.Children.Remove(currentPiece);
                grdBoard.Children.Add(checker);
                Storyboard DropPiece = checker.Resources["DropPiece"] as Storyboard;
                if (DropPiece != null)
                {
                    DropPiece.Begin();
                }
            }));
        }
コード例 #2
0
ファイル: GameShape.xaml.cs プロジェクト: zessiez/always
        public void PlayAgentMove(string moveDesc)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                CheckerPiece checker = null;
                currentTurn = Turn.Black;
                List <string> descs = moveDesc.Split("//".ToArray()).ToList();
                string from = descs[0], to = descs[2];

                int fromx = int.Parse(from.Split(',')[0]),
                fromy = int.Parse(from.Split(',')[1]),
                tox = int.Parse(to.Split(',')[0]),
                toy = int.Parse(to.Split(',')[1]);

                //try black and black king, to find out which one it is
                if (grdBoard.Children.OfType <BlackChecker>()
                    .Where(p => (p.row == fromx) && (p.col == fromy)).SingleOrDefault() != null)
                {
                    currentPiece = grdBoard.Children.OfType <BlackChecker>()
                                   .Where(p => (p.row == fromx) && (p.col == fromy)).SingleOrDefault();

                    if (tox == BLACK_LAST_ROW)
                    {
                        checker = new BlackKingChecker();
                    }
                    else
                    {
                        checker = new BlackChecker();
                    }
                }
                else
                {
                    currentPiece = grdBoard.Children.OfType <BlackKingChecker>()
                                   .Where(p => (p.row == fromx) && (p.col == fromy)).SingleOrDefault();

                    checker = new BlackKingChecker();
                }

                EmptySpace tempEmptySpace = grdBoard.Children.OfType <EmptySpace>()
                                            .Where(p => (p.row == tox) && (p.col == toy)).SingleOrDefault();

                CaptureHumanCellInAgentMoveIfAny(tox, toy, tempEmptySpace);

                sound.Play();
                MoveChecker(tox, toy, checker);
                currentTurn = Turn.Red;
            }));
        }
コード例 #3
0
ファイル: GameShape.xaml.cs プロジェクト: zessiez/always
        private void StartDrag(MouseEventArgs e)
        {
            IsDragging = true;
            CheckerPiece src;

            if (e.Source.GetType() == typeof(RedChecker))
            {
                src = (RedChecker)e.Source;
            }
            else if (e.Source.GetType() == typeof(BlackChecker))
            {
                src = (BlackChecker)e.Source;
            }
            else if (e.Source.GetType() == typeof(RedKingChecker))
            {
                src = (RedKingChecker)e.Source;
            }
            else
            {
                src = (BlackKingChecker)e.Source;
            }

            currentPiece = src;

            //drag adroner (Not currently used)>>
            _dragAdorner = new DragAdorner(currentPiece, currentPiece, true, 1);
            //currentPiece.Visibility = System.Windows.Visibility.Hidden;
            AdornerLayerForDrag().Add(_dragAdorner);
            //<<

            this.CaptureMouse();

            DataObject data = new DataObject(
                System.Windows.DataFormats.Text.ToString(), "abcd");
            DragDropEffects de = DragDrop.DoDragDrop(
                this.grdBoard, data, DragDropEffects.All);

            IsDragging = false;
        }
コード例 #4
0
ファイル: GameShape.xaml.cs プロジェクト: zessiez/always
        // 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;
            }
        }
コード例 #5
0
ファイル: GameShape.xaml.cs プロジェクト: zessiez/always
        public void CaptureHumanCellInAgentMoveIfAny(int r, int c, EmptySpace l)
        {
            //If the logic is hard to follow, read below:
            //it basically checks the location of the supposedly hit user (red) checker
            //based on the fact that agent's (black) piece could be crown, and so assigns
            //crowned or normal hit piece, if any, to the opponentPiece variable.
            CheckerPiece opponentPiece = null;

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

            //>> now remove if any red or red king was hit
            if (opponentPiece != null && Math.Abs(currentPiece.row - l.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 BlackKingChecker)) && c == validCol)
                {
                    capturedPiece = opponentPiece;
                    Storyboard PieceCaptured =
                        opponentPiece.Resources["PieceCaptured"] as Storyboard;
                    if (PieceCaptured != null)
                    {
                        PieceCaptured.Completed += new EventHandler(RemovePiece);
                        PieceCaptured.Begin();
                    }
                    okToMove = true;
                }
            }
        }