Exemple #1
0
    public Card GetCardBelow(Card card)
    {
        Zone cardZone        = card.GetZone(gameState);
        int  cardColumnIndex = card.GetColumn(gameState);

        if (cardZone == Zone.Tableu)
        {
            CardColumn cardColumn = this.Tableu[cardColumnIndex];

            List <Card> faceUpCards   = cardColumn.faceUpCards;
            List <Card> faceDownCards = cardColumn.faceDownCards.Reverse().ToList();

            if (faceUpCards.Contains(card))
            {
                int index = faceUpCards.IndexOf(card);
                if (index - 1 >= 0)
                {
                    return(faceUpCards[index - 1]);
                }
                else
                {
                    if (faceDownCards.Count > 0)
                    {
                        return(faceDownCards[0]);
                    }
                }
            }

            if (faceDownCards.Contains(card))
            {
                int index = faceDownCards.IndexOf(card);
                if (index - 1 >= 0)
                {
                    return(faceDownCards[index - 1]);
                }
                else
                {
                    return(null);
                }
            }
        }
        if (cardZone == Zone.Waste)
        {
            var wastePileCards = this.WastePile.Reverse().ToList();
            if (wastePileCards.Contains(card))
            {
                int index = wastePileCards.IndexOf(card);
                if (index - 1 >= 0)
                {
                    return(wastePileCards[index - 1]);
                }
                else
                {
                    return(null);
                }
            }
        }

        return(null);
    }
Exemple #2
0
    private void SetUpTable(int noOfColumns, DeckShuffler shuffler)
    {
        if (noOfColumns > 9)
        {
            throw new Exception("Too Many Columns. Please choose a number <= 9");
        }

        //Init Tableu
        Tableu = new CardColumn[noOfColumns];
        for (int i = 0; i < noOfColumns; i++)
        {
            CardColumn newCardColumn = new CardColumn();

            List <Card> faceDownCards = shuffler.DrawCards(i);
            newCardColumn.faceDownCards = new Stack <Card>(faceDownCards);


            List <Card> faceUpCard = shuffler.DrawCards(1);
            newCardColumn.faceUpCards = faceUpCard;

            Tableu[i] = newCardColumn;
        }
        //Init Stock
        StockPile = new Stack <Card>(shuffler.DrawCards(shuffler.GetRemainigCardsCount()));
    }
    public object Clone()
    {
        CardColumn clone = new CardColumn();

        clone.columnIndex   = this.columnIndex;
        clone.faceDownCards = new Stack <Card>(this.faceDownCards.ToList().Select(c => c.Clone()).Cast <Card>().Reverse());
        clone.faceUpCards   = this.faceUpCards.Select(fp => fp.Clone()).Cast <Card>().ToList();

        return(clone);
    }
        public void NewCardColumnWith10Cards()
        {
            // Arrange
            var gameDeck = new Deck();

            // Act
            var gameColumn1 = new CardColumn(gameDeck, 10);

            // Assert
            Assert.AreEqual(gameColumn1.Column.Count, 10);
        }
        public void NewCardColumnNotNull()
        {
            // Arrange
            var gameDeck = new Deck();

            // Act
            var gameColumn1 = new CardColumn(gameDeck, 10);

            // Assert
            Assert.IsNotNull(gameColumn1);
        }
        public void GetCardsUpFromColumnWithNone_NotNull()
        {
            // Arrange
            var gameDeck   = new Deck();
            var gameColumn = new CardColumn(gameDeck, 10);

            // Act
            List <int> cardsUpList;

            cardsUpList = gameColumn.GetVisibleCards();

            // Assert
            Assert.IsNotNull(cardsUpList);
        }
Exemple #7
0
        public override string Execute(TrelloModel model)
        {
            var columnId = model.NextId();
            var column   = new CardColumn(columnId, ColumnName);

            model.Boards[BoardId].Columns.Add(column);

            RaiseEvent(new ColumnAdded
            {
                BoardId = BoardId,
                Column  = new ColumnView(column)
            });

            return(columnId);
        }
        public void GetCardsUpFromColumnWithOne()
        {
            // Arrange
            var gameDeck   = new Deck();
            var gameColumn = new CardColumn(gameDeck, 10);

            gameColumn.TurnCardUp(gameColumn.Column[5]);

            // Act
            List <int> cardsUpList;

            cardsUpList = gameColumn.GetVisibleCards();

            // Assert
            Assert.AreEqual(cardsUpList.Count, 1);
        }
Exemple #9
0
    //If null, no card needs to be flipped
    public Card GetCardToFlip()
    {
        if (this.from.zone == Zone.Tableu)
        {
            int selectedCardColumn = SelectedCard.GetColumn(gameSnapshot);
            //Update start tableu pile - unreference moved cards
            CardColumn startCardColum = gameSnapshot.tableu[selectedCardColumn];

            //Flip card below if needed
            List <Card> faceUpCards_afterMove = startCardColum.faceUpCards.TakeUntil(c => c == SelectedCard).ToList();
            if (faceUpCards_afterMove.Count == 0 && startCardColum.faceDownCards.Count > 0)
            {
                Card faceDownCardToFlip = gameSnapshot.tableu[selectedCardColumn].faceDownCards.Peek();
                return(faceDownCardToFlip);
            }
        }
        return(null);
    }
        public void GetCardsUpWithUpperLimit()
        {
            // Arrange
            var gameDeck   = new Deck();
            var gameColumn = new CardColumn(gameDeck, 10);

            for (int i = 0; i < 10; i++)
            {
                gameColumn.TurnCardUp(gameColumn.Column[i]);
            }

            // Act
            List <int> cardsUpList;

            cardsUpList = gameColumn.GetVisibleCards();

            // Assert
            Assert.AreEqual(cardsUpList.Count, 10);
        }
Exemple #11
0
    private void ExecuteMove(Move move)
    {
        Card selectedCard = move.movedCards.First();

        if (move.from.zone == Zone.Tableu)
        {
            int selectedCardColumn = selectedCard.GetColumn(move.gameSnapshot);
            //Update start tableu pile - unreference moved cards
            CardColumn startCardColum = this.Tableu[selectedCardColumn];
            startCardColum.faceUpCards = startCardColum.faceUpCards.TakeUntil(c => c == selectedCard).ToList();

            //Flip card below if needed
            Card cardToFlip = move.GetCardToFlip();
            if (cardToFlip != null)
            {
                Card faceDownCardToFlip = this.Tableu[selectedCardColumn].faceDownCards.Pop();
                startCardColum.faceUpCards.Add(cardToFlip);
            }
        }
        if (move.from.zone == Zone.Foundation)
        {
            //Update foundation pile - Remove moved card
            FoundationPiles[move.from.index].cards.Pop();
        }
        if (move.from.zone == Zone.Waste)
        {
            //Update waste Pile (linked to stock pile)- Remove selected card
            this.WastePile.Pop();
        }
        //--------------------
        if (move.to.zone == Zone.Tableu)
        {
            //Update destination tableu pile - reference moved cards
            CardColumn targetCardColumn = this.Tableu[move.to.index];
            targetCardColumn.faceUpCards.AddRange(move.movedCards);
        }
        if (move.to.zone == Zone.Foundation)
        {
            //Update foundation pile - Add moved card
            FoundationPiles[move.to.index].cards.Push(selectedCard);
        }
    }
Exemple #12
0
    public int GetCardColumn(Card card)
    {
        for (int i = 0; i < tableu.Length; i++)
        {
            CardColumn tableuColumn = tableu[i];
            if (tableuColumn.faceDownCards.Contains(card) || tableuColumn.faceUpCards.Contains(card))
            {
                return(i);
            }
        }
        for (int i = 0; i < foundationPiles.Length; i++)
        {
            FoundationPile foundationPile = foundationPiles[i];
            if (foundationPile.cards.Contains(card))
            {
                return(i);
            }
        }

        return(-1);
    }
Exemple #13
0
    public void MoveCard(Move move)
    {
        Sequence undoSequence = DOTween.Sequence();

        Card selectedCard = move.movedCards.First();
        int  targetColumn = move.to.index;

        CardView selectedCardView = this.cardData_to_cardView[selectedCard.ToString()];
        Vector3  selectedCardPos  = selectedCardView.positionBeforeDrag;

        Vector3 targetPos;

        CardView destinationCardView = null;

        if (move.from.zone == Zone.Tableu)
        {
            //Flip Card
            Card cardToFlip = move.GetCardToFlip();
            if (cardToFlip != null)
            {
                CardView cardViewToFlip = this.cardData_to_cardView[cardToFlip.ToString()];
                cardViewToFlip.TurnFaceUp(flipSpeed);

                undoSequence.AppendCallback(() => cardViewToFlip.TurnFaceDown(flipSpeed));
            }
        }

        if (move.from.zone == Zone.Waste)
        {
            //Update Waste Pile (Remove/Add card)

            //Move top two cards left, so we can show a third again.
            if (move.gameSnapshot.wastePile.Count > 3)
            {
                Card     topCard_wastePile     = move.gameSnapshot.wastePile.Peek();
                CardView topCardView_wastePile = this.cardData_to_cardView[topCard_wastePile.ToString()];

                CardView[] cardsToScoopRight = new CardView[2];
                cardsToScoopRight[0] = topCardView_wastePile.CardBelow;
                cardsToScoopRight[1] = topCardView_wastePile.CardBelow.CardBelow;
                foreach (CardView card in cardsToScoopRight)
                {
                    Vector3 cardPosition = card.transform.position;
                    card.transform.DOMove(cardPosition + new Vector3(stockPile_padding_x, 0, 0), flipSpeed);

                    undoSequence.Join(card.transform.DOMove(cardPosition, flipSpeed));
                }
            }
        }

        if (move.to.zone == Zone.Tableu)
        {
            //Move selected cards to new column
            bool isTargetColumnEmpty = move.gameSnapshot.tableu[targetColumn].faceUpCards.IsNullOrEmpty();
            if (isTargetColumnEmpty == false)
            {
                CardColumn targetCardColumn = move.gameSnapshot.tableu[targetColumn];
                Card       destinationCard  = targetCardColumn.faceUpCards.Last();
                destinationCardView = this.cardData_to_cardView[destinationCard.ToString()];

                int currZDepth = Mathf.RoundToInt(selectedCardView.transform.position.z * -1);

                targetPos = destinationCardView.transform.position - new Vector3(0, faceUp_padding_y, 0);


                selectedCardView.SetSortingOrderAndZDepth(targetCardColumn.TotalCardsCount());
                targetPos.z = selectedCardView.transform.position.z; //Update Z depth

                undoSequence.AppendCallback(() => selectedCardView.SetSortingOrderAndZDepth(currZDepth));
            }
            else
            {
                targetPos = this.tableuPositions[targetColumn];

                int currZDepth = Mathf.RoundToInt(selectedCardView.transform.position.z * -1);

                selectedCardView.SetSortingOrderAndZDepth(0);

                undoSequence.AppendCallback(() => selectedCardView.SetSortingOrderAndZDepth(currZDepth));
            }
        }
        else if (move.to.zone == Zone.Foundation)
        {
            //Move selected card to foundation
            targetPos = this.foundationPilesPositions[targetColumn];
            int cardsBelowSelection = move.gameSnapshot.foundationPiles[targetColumn].cards.Count;

            int currZDepth = Mathf.RoundToInt(selectedCardView.transform.position.z * -1);

            selectedCardView.SetSortingOrderAndZDepth(cardsBelowSelection);
            targetPos.z = selectedCardView.transform.position.z;

            undoSequence.AppendCallback(() => selectedCardView.SetSortingOrderAndZDepth(currZDepth));
        }
        else
        {
            throw new Exception("Move is invalid");
        }


        selectedCardView.MoveToPoint(targetPos);

        undoSequence.Join(selectedCardView.transform.DOMove(selectedCardPos, flipSpeed));
        undoSequence.Pause();
        movesUndo_sequences.Push(undoSequence);
    }
Exemple #14
0
    public void NotifyCardDropped(Card selectedCard, TablePosition dropPosition)
    {
        bool isValidMove             = false;
        Zone selectedCardZone        = selectedCard.GetZone(gameState);
        int  selectedCardColumnIndex = selectedCard.GetColumn(gameState);

        TablePosition from = new TablePosition(selectedCardZone, selectedCardColumnIndex);

        if (from.zone == dropPosition.zone && from.index == dropPosition.index)
        {
            isValidMove = false;
        }
        else if (dropPosition.zone == Zone.Tableu)
        {
            int        targetColumn     = dropPosition.index;
            CardColumn targetCardColumn = this.Tableu[targetColumn];
            Card       cardToDropOn     = targetCardColumn.faceUpCards.LastOrDefault();

            isValidMove = IsLegal_TableuMove(selectedCard, cardToDropOn);
        }
        else if (dropPosition.zone == Zone.Foundation)
        {
            isValidMove = IsLegal_FoundationMove(selectedCard, FoundationPiles[dropPosition.index]);
        }
        else
        {
            //Card was dropped on the upper part of the table, between foundation piles and deckPile, or on the same column as it started
            isValidMove = false;
        }

        if (isValidMove)
        {
            List <Card> cardsBeingMoved = new List <Card>();
            cardsBeingMoved.Add(selectedCard);
            //If the moved card comes from the tableu, there might other cards above that need to be moved as well.
            //This doens't happen for moves where the selected card comes from foundation piles, stock pile or waste pile.

            if (selectedCardZone == Zone.Tableu)
            {
                cardsBeingMoved.AddRange(this.Tableu[selectedCardColumnIndex].faceUpCards.SkipWhile(c => c != selectedCard).Skip(1));
            }
            GameState snapshot = gameState.Clone() as GameState;
            Move      move     = new Move(cardsBeingMoved, from, dropPosition, snapshot);
            //Notify subscribers
            foreach (ISolitaireEventsHandlers subscribedHandler in subscribedInstances)
            {
                subscribedHandler.NotifyLegalMove(move);
            }
            //Store move in history
            Debug.Log("Legal move");
            //Update Game data
            ExecuteMove(move);
            movesHistory.Push(move);
        }
        else
        {
            IllegalMove move = new IllegalMove(selectedCard);
            //Notify subscribers
            foreach (ISolitaireEventsHandlers subscribedHandler in subscribedInstances)
            {
                subscribedHandler.NotifyIllegalMove(move);
            }
            Debug.Log("Illegal move");
        }
    }