示例#1
0
        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;
            }));
        }
示例#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;
            }
        }
示例#3
0
        /// <summary>
        /// This function loads the game pieces into the grid cells and prepares everything
        /// </summary>
        public void Reset()
        {
            currentTurn = Turn.Black;
            this.pnlBlackGraveyard.Children.Clear();
            this.pnlRedGraveyard.Children.Clear();

            int col = 0;

            for (int row = 0; row < grdBoard.RowDefinitions.Count; row++)
            {
                for (col = 0; col < grdBoard.ColumnDefinitions.Count; col++)
                {
                    Border b = new Border();
                    b.BorderThickness = new Thickness(1, 1, 1, 1);
                    b.BorderBrush     = Brushes.Black;
                    Grid.SetColumn(b, col);
                    Grid.SetRow(b, row);
                    this.grdBoard.Children.Add(b);
                }
            }

            for (int row = 0; row < grdBoard.RowDefinitions.Count; row++)
            {
                // put a piece in every other cell
                for (col = (col % 2 != 0 ? 0 : 1); col <
                     grdBoard.ColumnDefinitions.Count; col += 2)
                {
                    EmptySpace l = new EmptySpace();
                    l.Margin     = new Thickness(0, 0, 0, 0);
                    l.AllowDrop  = true;
                    l.Background = Brushes.Black;
                    l.Name       = "Label" + (row * col).ToString();
                    l.Drop      += new DragEventHandler(grdBoard_Drop);
                    l.col        = col;
                    l.row        = row;

                    Grid.SetColumn(l, col);
                    Grid.SetRow(l, row);
                    this.grdBoard.Children.Add(l);

                    if (row >= grdBoard.RowDefinitions.Count - 3)
                    {
                        RedChecker redChecker = new RedChecker();
                        redChecker.PreviewMouseLeftButtonDown +=
                            new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown);
                        redChecker.PreviewMouseMove +=
                            new MouseEventHandler(grdBoard_PreviewMouseMove);
                        redChecker.Cursor    = Cursors.Hand;
                        redChecker.AllowDrop = false;
                        redChecker.col       = col;
                        redChecker.row       = row;

                        Grid.SetColumn(redChecker, col);
                        Grid.SetRow(redChecker, row);
                        this.grdBoard.Children.Add(redChecker);
                    }
                    if (row < 3)
                    {
                        BlackChecker blackChecker = new BlackChecker();
                        blackChecker.PreviewMouseLeftButtonDown +=
                            new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown);
                        blackChecker.PreviewMouseMove +=
                            new MouseEventHandler(grdBoard_PreviewMouseMove);
                        blackChecker.Cursor    = Cursors.Hand;
                        blackChecker.AllowDrop = false;
                        blackChecker.col       = col;
                        blackChecker.row       = row;

                        Grid.SetColumn(blackChecker, col);
                        Grid.SetRow(blackChecker, row);
                        this.grdBoard.Children.Add(blackChecker);
                    }
                }
            }
        }
示例#4
0
        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;
                }
            }
        }