Esempio n. 1
0
        public JavaList <TElement> Clone()
        {
            var temp = new JavaList <TElement>();
            var node = _head;

            while (node != null)
            {
                temp.push_back(node.Value);
                node = node.Next;
            }

            return(temp);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            var pos = GetBoardPiecePos(e.X, e.Y);

            if (pos != -1)
            {
                try
                {
                    int piece = _boardGameEngine.GetPieceAtPosition(pos);

                    if (piece != CheckersBoardGameEngine.EmptyPiece &&
                        (((piece == CheckersBoardGameEngine.WhitePiece || piece == CheckersBoardGameEngine.WhiteKingPiece) &&
                          _boardGameEngine.GetCurrentPlayer() == CheckersBoardGameEngine.WhitePiece) ||
                         ((piece == CheckersBoardGameEngine.BlackPiece || piece == CheckersBoardGameEngine.BlackKing) &&
                          _boardGameEngine.GetCurrentPlayer() == CheckersBoardGameEngine.BlackPiece)))
                    {
                        if (_selectedPositions.IsEmpty())
                        {
                            _selectedPositions.push_back(pos);
                        }
                        else
                        {
                            var temp = (int)_selectedPositions.peek_tail();
                            if (temp == pos)
                            {
                                _selectedPositions.pop_back();
                            }
                            else
                            {
                                MessageBox.Show("WAT ARE U TRYIN' TO DO?");
                            }
                        }
                        Invalidate();
                        Update();
                    }
                    else
                    {
                        var isValidMove = false;
                        if (!_selectedPositions.IsEmpty())
                        {
                            CheckersBoardGameEngine tempBoardGameEngine;
                            if (_boardStack.Count == 0)
                            {
                                tempBoardGameEngine = _boardGameEngine.Clone();
                                _boardStack.Push(tempBoardGameEngine);
                            }
                            else
                            {
                                tempBoardGameEngine = (CheckersBoardGameEngine)_boardStack.Peek();
                            }
                            var from = (int)_selectedPositions.peek_tail();
                            if (tempBoardGameEngine.IsValidMove(from, pos))
                            {
                                tempBoardGameEngine = tempBoardGameEngine.Clone();
                                var isAttacking = tempBoardGameEngine.CanCurrentPlayerAttack();
                                tempBoardGameEngine.ApplyMove(from, pos);
                                if (isAttacking && tempBoardGameEngine.CanAttackPosition(pos))
                                {
                                    _selectedPositions.push_back(pos);
                                    _boardStack.Push(tempBoardGameEngine);
                                }
                                else
                                {
                                    _selectedPositions.push_back(pos);
                                    ApplyPlayerMove(_selectedPositions, _boardGameEngine);
                                    _boardStack = new Stack();
                                }
                                isValidMove = true;
                            }
                            else if (from == pos)
                            {
                                _selectedPositions.pop_back();
                                _boardStack.Pop();
                                isValidMove = true;
                            }
                        }
                        if (!isValidMove)
                        {
                            MessageBox.Show("CAN'T DO THAT BRAH");
                        }
                        else
                        {
                            Invalidate();
                            Update();
                        }
                    }
                }
                catch (InvalidBoardCoordinateException bad)
                {
                    Debug.WriteLine(bad.StackTrace);
                    Application.Exit();
                }
            }