Exemple #1
0
        /*
         * Outlines points to move to in red (GUI)
         */
        private void HighlightSpots(Piece p)
        {
            IEnumerable <Border> borders = MyGrid.Children.OfType <Border>();

            //generate brushes
            SolidColorBrush BlackBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0));
            SolidColorBrush RedBrush   = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0));

            foreach (Border border in borders)
            {
                border.BorderBrush = BlackBrush;
            }

            // this is the default view
            if (p.player.GetID() == 2)
            {
                List <System.Drawing.Point> endSpaces = p.getPossibleEndSpaces(board2);
                if (endSpaces != null)
                {
                    for (int i = 0; i < endSpaces.Count; i++)
                    {
                        TextBlock current        = blocks[7 - endSpaces[i].X][7 - endSpaces[i].Y];
                        Viewbox   currentViewbox = (Viewbox)current.Parent;
                        Border    currentBorder  = (Border)currentViewbox.Parent;
                        currentBorder.BorderBrush = RedBrush;
                    }
                }
            }

            if (p.player.GetID() == 1)
            {
                List <System.Drawing.Point> endSpaces = p.getPossibleEndSpaces(board1);

                if (endSpaces != null)
                {
                    for (int i = 0; i < endSpaces.Count; i++)
                    {
                        TextBlock current        = blocks[endSpaces[i].X][endSpaces[i].Y];
                        Viewbox   currentViewbox = (Viewbox)current.Parent;
                        Border    currentBorder  = (Border)currentViewbox.Parent;
                        currentBorder.BorderBrush = RedBrush;
                    }
                }
            }
        }
Exemple #2
0
        /*
         * Does operation to move a piece
         * Called on click. May or may not result in actual move
         * Triggers AI action if specified
         */
        private void MovePiece(Piece p)
        {
            if (previousPiece.player.GetID() == 1 &&
                (p.player.GetID() == 1 && p.position.X == previousPiece.position.X && p.position.Y == previousPiece.position.Y))
            {
                clearBorders();
            }
            else if (previousPiece.player.GetID() == 2 &&
                     (p.player.GetID() == 2 && p.position.X == previousPiece.position.X && p.position.Y == previousPiece.position.Y))
            {
                clearBorders();
            }
            // check if piece can move to that place
            else if (previousPiece.player.GetID() == 1)
            {
                //if not the right player
                if ((string)App.Current.Properties["ActivePlayer"] == "Black")
                {
                    previousPiece = null;
                    clearBorders();
                    return;
                }

                List <System.Drawing.Point> endSpaces     = previousPiece.getPossibleEndSpaces(board1);
                System.Drawing.Point        piecePosition = p.position;
                if (p.player.GetID() == 2)
                {
                    piecePosition = new System.Drawing.Point(7 - p.position.X, 7 - p.position.Y);
                }

                if (endSpaces != null && endSpaces.Contains(piecePosition))
                {
                    // this move is allowed!

                    Move m = new Move(previousPiece.position, piecePosition);

                    board1.MakeMove(m);

                    clearBorders();
                    UpdateBoard(board1);

                    previousPiece = null;

                    //AI's turn
                    if (AI.IsChecked == true)
                    {
                        //wait 1 second TBI
                        AIStep();
                        UpdateBoard(board1);
                    }
                }
                else
                {
                    HighlightSpots(p);
                }
            }
            else if (previousPiece.player.GetID() == 2)
            {
                //if not the right player
                if ((string)App.Current.Properties["ActivePlayer"] == "White")
                {
                    previousPiece = null;
                    clearBorders();
                    return;
                }

                List <System.Drawing.Point> endSpaces     = previousPiece.getPossibleEndSpaces(board2);
                System.Drawing.Point        piecePosition = p.position;
                if (p.player.GetID() != 2)
                {
                    piecePosition = new System.Drawing.Point(7 - p.position.X, 7 - p.position.Y);
                }
                if (endSpaces != null && endSpaces.Contains(piecePosition))
                {
                    Move m = new Move(previousPiece.position, piecePosition);

                    board2.MakeMove(m);

                    clearBorders();
                }
                else
                {
                    HighlightSpots(p);
                }
            }
            else if (previousPiece.player.GetID() == 0)
            {
                HighlightSpots(p);
            }

            UpdateBoard(board1);
            previousPiece = null;
        }