Beispiel #1
0
 public void ClearPot()
 {
     this.CardPile.Clear();
        potList.Clear();
        highestCard = null;
        highestPlayer = null;
 }
Beispiel #2
0
        /// <summary>
        /// Sorts the card by their suit
        /// </summary>
        /// <param name="pile"></param>
        /// <returns></returns>
        public static Pile SortBySuit(Pile pile)
        {
            System.Collections.ArrayList newHand = new System.Collections.ArrayList();

            System.Collections.ArrayList CardPile = (System.Collections.ArrayList)pile.CardPile;


            while (CardPile.Count > 0)
            {
                int        pos           = 0;                       // Position of minimal card.
                SpadesCard minSpadescard = (SpadesCard)CardPile[0]; // Minimal card.
                int        mincard       = (int)minSpadescard.CardIndex;
                for (int i = 1; i < CardPile.Count; i++)
                {
                    SpadesCard nextSpadescard = (SpadesCard)CardPile[i];
                    int        nextcard       = nextSpadescard.CardIndex;
                    if (Card.SuitFromCardIndex(nextcard) < Card.SuitFromCardIndex(mincard)
                        ||
                        (Card.SuitFromCardIndex(nextcard) == Card.SuitFromCardIndex(mincard) && Card.RankFromCardIndex(nextcard) < Card.RankFromCardIndex(mincard))
                        )
                    {
                        pos           = i;
                        mincard       = nextcard;
                        minSpadescard = nextSpadescard;
                    }
                }
                CardPile.RemoveAt(pos);
                newHand.Add(minSpadescard);
            }
            CardPile      = newHand;
            pile.CardPile = CardPile;

            return(pile);
        }
Beispiel #3
0
 public void ClearPot()
 {
     this.CardPile.Clear();
     potList.Clear();
     highestCard   = null;
     highestPlayer = null;
 }
Beispiel #4
0
        /// <summary>
        /// checks tha current Hand for the higher card
        /// </summary>
        /// <param name="card">card to check againts </param>
        /// <param name="h">Current hand of the player</param>
        /// <returns></returns>
        public static bool checkHandForHigherCard(SpadesCard card,Hand h)
        {
            if (card.Suit == CardSuit.Spades && card.Rank == CardRank.Ace) return false;

            foreach (SpadesCard c in h.CardPile)
            {

                if (hasSuit(card.Suit, h))
                {
                    if (c.Suit == card.Suit)
                    {
                        // same suit found
                        if (c.Rank == CardRank.Ace) return true;
                        else if (c.Rank > card.Rank) return true;

                    }
                    // check for spades also
                }
                else
                {
                    if (card.Suit != CardSuit.Spades)
                    {
                        if (c.Suit == CardSuit.Spades) return true; // if spades he has high card
                    }

                }

            }

            return false;
        }
Beispiel #5
0
        /// <summary>
        /// Event that occurs when the user double clicks the mouse on the form
        /// checks if it is valid card or not first
        /// If valid add to Pot
        /// updates card position
        /// updates turn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int activePlayerId = gameData.ActivePlayerId;

            if (gameData.CurrentPlayerList[boardBelongsTo].IsActivePlayer == false)
            {
                return;
            }

            // look for the card index from the mouse click
            int mouseX = e.X;
            int mouseY = e.Y;

            // now calculate the card position form player initial position of refrence
            // count how many cards are there in the pile
            int cardCount = gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count;

            // check for valid click
            // 71 is card width 97 is height
            if (cardCount > 0 && IsAnimating == false)
            {
                if (mouseX >= playerPositions[activePlayerId].x && mouseX <= ((playerPositions[activePlayerId].x + 56) + (15 * cardCount)))
                {
                    // check for y coordinate
                    if (mouseY >= playerPositions[activePlayerId].y && mouseY <= (playerPositions[activePlayerId].y + 97))
                    {
                        //valid coordinate
                        // calculate card index clicked
                        int index = (int)(mouseX - playerPositions[activePlayerId].x) / 15;

                        // if index is more than last index then make it last index
                        if (index > (gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count - 1))
                        {
                            index = gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count - 1;
                        }



                        if (GameRule.isValidCard(gameData.CurrentPlayerList[activePlayerId], (SpadesCard)gameData.CurrentPlayerList[activePlayerId].Hand.CardPile[index], gameData.CurrentPot))
                        {
                            // check if its a  valid card
                            // place the card in pot card
                            SpadesCard potCard = (SpadesCard)gameData.CurrentPlayerList[activePlayerId].Hand.CardPile[index];
                            // remove from hand
                            gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.RemoveAt(index);

                            potCard.CardPositionX = playerPositions[activePlayerId].x + 80;
                            potCard.CardPositionY = playerPositions[activePlayerId].y - 120;

                            gameData.CurrentPot.AddPot(potCard, gameData.CurrentPlayerList[activePlayerId]);



                            this.updateTurn(activePlayerId);
                        }
                    }
                }
            }
        }
Beispiel #6
0
 public void AddPot(SpadesCard c, player p)
 {
     this.AddCard(c);
        SpadesPot newPot = new SpadesPot();
        newPot.card = c;
        newPot.potter = p;
        setHighest(c,p);
        potList.Add(newPot);
 }
Beispiel #7
0
 public bool removeCard(SpadesCard c)
 {
     if (CardPile.Contains(c) == true)
     {
         CardPile.Remove(c);
         return true;
     }
     return false;
 }
Beispiel #8
0
        public void AddPot(SpadesCard c, player p)
        {
            this.AddCard(c);
            SpadesPot newPot = new SpadesPot();

            newPot.card   = c;
            newPot.potter = p;
            setHighest(c, p);
            potList.Add(newPot);
        }
Beispiel #9
0
        /// <summary>
        /// occurs when user clicks the mouse
        /// just does some little animation to lift the card up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_MouseClick(object sender, MouseEventArgs e)
        {
            int activePlayerId = gameData.ActivePlayerId;

            foreach (SpadesCard card in gameData.CurrentPlayerList[activePlayerId].Hand.CardPile)
            {
                card.MoveUp = false;
            }


            // look for the card index from the mouse click
            int mouseX = e.X;
            int mouseY = e.Y;

            // now calculate the card position form player initial position of refrence
            // count how many cards are there in the pile
            int cardCount = gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count;

            // check for valid click
            // 71 is card width 97 is height
            if (gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count > 0)
            {
                if (mouseX >= playerPositions[activePlayerId].x && mouseX <= ((playerPositions[activePlayerId].x + 56) + (15 * cardCount)))
                {
                    // check for y coordinate
                    if (mouseY >= playerPositions[activePlayerId].y && mouseY <= (playerPositions[activePlayerId].y + 97))
                    {
                        //valid coordinate
                        // calculate card index clicked
                        // 15 is the width of the card overlapping each other
                        int index = (int)(mouseX - playerPositions[activePlayerId].x) / 15;

                        // if index is more than last index then make it last index
                        if (index > (gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count - 1))
                        {
                            index = gameData.CurrentPlayerList[activePlayerId].Hand.CardPile.Count - 1;
                        }


                        SpadesCard c = (SpadesCard)gameData.CurrentPlayerList[activePlayerId].Hand.CardPile[index];
                        c.MoveUp = true;
                        gameData.CurrentPlayerList[activePlayerId].Hand.CardPile[index] = c;
                        this.Invalidate();
                    }
                }
            }
        }
Beispiel #10
0
        public void setHighest(SpadesCard c,player p)
        {
            // just set this as highest
               if (highestCard == null)
               {
               highestPlayer = p;
               highestCard = c;
               }
               else
               {
               // if already set highest check with it

               // if same suit then check value
               if (c.Suit == highestCard.Suit)
               {
                   // compare the values
                   if (c.Rank == CardRank.Ace)
                   {
                       highestCard = c;
                       highestPlayer = p;
                       return;
                   }
                   else if (c.Rank > highestCard.Rank && highestCard.Rank != CardRank.Ace)
                   {
                       highestCard = c;
                       highestPlayer = p;
                       return;
                   }
               }
               else
               {
                   // if not same suit check if spades has been played
                   if (c.Suit == CardSuit.Spades)
                   {
                       highestPlayer = p;
                       highestCard = c;
                       return;
                   }
               }

               }
        }
Beispiel #11
0
        public void setHighest(SpadesCard c, player p)
        {
            // just set this as highest
            if (highestCard == null)
            {
                highestPlayer = p;
                highestCard   = c;
            }
            else
            {
                // if already set highest check with it

                // if same suit then check value
                if (c.Suit == highestCard.Suit)
                {
                    // compare the values
                    if (c.Rank == CardRank.Ace)
                    {
                        highestCard   = c;
                        highestPlayer = p;
                        return;
                    }
                    else if (c.Rank > highestCard.Rank && highestCard.Rank != CardRank.Ace)
                    {
                        highestCard   = c;
                        highestPlayer = p;
                        return;
                    }
                }
                else
                {
                    // if not same suit check if spades has been played
                    if (c.Suit == CardSuit.Spades)
                    {
                        highestPlayer = p;
                        highestCard   = c;
                        return;
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// checks tha current Hand for the higher card
        /// </summary>
        /// <param name="card">card to check againts </param>
        /// <param name="h">Current hand of the player</param>
        /// <returns></returns>
        public static bool checkHandForHigherCard(SpadesCard card, Hand h)
        {
            if (card.Suit == CardSuit.Spades && card.Rank == CardRank.Ace)
            {
                return(false);
            }

            foreach (SpadesCard c in h.CardPile)
            {
                if (hasSuit(card.Suit, h))
                {
                    if (c.Suit == card.Suit)
                    {
                        // same suit found
                        if (c.Rank == CardRank.Ace)
                        {
                            return(true);
                        }
                        else if (c.Rank > card.Rank)
                        {
                            return(true);
                        }
                    }
                    // check for spades also
                }
                else
                {
                    if (card.Suit != CardSuit.Spades)
                    {
                        if (c.Suit == CardSuit.Spades)
                        {
                            return(true);                           // if spades he has high card
                        }
                    }
                }
            }



            return(false);
        }
Beispiel #13
0
 public void AddCard(SpadesCard c)
 {
     CardPile.Add(c);
     OnPotAdd(new PileEventArgs(c));
 }
Beispiel #14
0
 public void AddCard(SpadesCard c)
 {
     CardPile.Add(c);
     OnPotAdd(new PileEventArgs(c));
 }
Beispiel #15
0
 public PileEventArgs(SpadesCard c)
 {
     card = c;
 }
Beispiel #16
0
 public void AddCard(SpadesCard c, int index)
 {
     CardPile.Insert(index, c);
     OnPotAdd(new PileEventArgs(c));
 }
Beispiel #17
0
        /// <summary>
        /// Starts the turn of the player [ ActivePlayerID ]
        /// if its user turn , does nothing and waits for users input mouse clicks
        /// </summary>
        public void startTurn()
        {
            if (IsAnimating)
            {
                newToolStripMenuItem.Enabled = false;
            }
            else
            {
                newToolStripMenuItem.Enabled = true;
            }

            foreach (player p in gameData.CurrentPlayerList) //SpadesPlayer
            {
                p.IsActivePlayer = false;
            }

            // this is to prevent computer moves overlap
            if (IsAnimating == true)
            {
                return;
            }

            gameData.CurrentPlayerList[gameData.ActivePlayerId].IsActivePlayer = true;
            gameStatusLabel.Text = " Waiting for " + gameData.CurrentPlayerList[gameData.ActivePlayerId].Name + " ....";

            this.Invalidate();
            this.Update();


            if (gameData.CurrentPlayerList[gameData.ActivePlayerId] is AIPlayer)
            {
                // make a computer move

                // can control speed of the movement here // fast or slow computer moves
                // Thread.Sleep(10);

                Thread.Sleep(Convert.ToInt32(animationSpeed) * 50);


                SpadesCard card = gameData.CurrentPlayerList[gameData.ActivePlayerId].makeMove(gameData);

                if (playerPositions[gameData.ActivePlayerId].seat == "N")
                {
                    card.CardPositionX = playerPositions[gameData.ActivePlayerId].x + 80;
                    card.CardPositionY = playerPositions[gameData.ActivePlayerId].y + 120;
                }

                if (playerPositions[gameData.ActivePlayerId].seat == "W")
                {
                    card.CardPositionX = playerPositions[gameData.ActivePlayerId].x + 180;
                    card.CardPositionY = playerPositions[gameData.ActivePlayerId].y + 80;
                }
                if (playerPositions[gameData.ActivePlayerId].seat == "S")
                {
                    card.CardPositionX = playerPositions[gameData.ActivePlayerId].x + 80;
                    card.CardPositionY = playerPositions[gameData.ActivePlayerId].y - 120;
                }
                if (playerPositions[gameData.ActivePlayerId].seat == "E")
                {
                    card.CardPositionX = playerPositions[gameData.ActivePlayerId].x - 180;
                    card.CardPositionY = playerPositions[gameData.ActivePlayerId].y + 90;
                }

                gameData.CurrentPot.AddPot(card, gameData.CurrentPlayerList[gameData.ActivePlayerId]);
                gameData.CurrentPlayerList[gameData.ActivePlayerId].Hand.CardPile.Remove(card);
                string msg = (gameData.ActivePlayerId + " Played " + card.Rank + " of " + card.Suit + "\r\n");
                this.Invalidate();
                this.Update();
                updateTurn(gameData.ActivePlayerId);
            }
            else
            {
                return; // wait for human move
            }
        }
Beispiel #18
0
 public bool removeCard(SpadesCard c)
 {
     if (CardPile.Contains(c) == true)
     {
         CardPile.Remove(c);
         return true;
     }
     return false;
 }
Beispiel #19
0
 public bool containsCard(SpadesCard c)
 {
     return CardPile.Contains(c);
 }
Beispiel #20
0
        /// <summary>
        /// checks whether the card played by the player is valid or not in current scenario
        /// </summary>
        /// <param name="currentPlayer">The card player</param>
        /// <param name="playedCard">what card has the player chooosen to play </param>
        /// <param name="currentPot">pot of current running hand</param>
        /// <returns></returns>
        public static bool isValidCard(player currentPlayer,  SpadesCard playedCard, Pot currentPot)
        {
            // if pot is empty its the first card and is always valid
            if (currentPot.CardPile.Count == 0)
            {
                return true;
            }
            else
            {
                // get 1st played card

                SpadesCard first = (SpadesCard)currentPot.CardPile[0];

                // check if player has the same suit to play
                if (hasSuit(first.Suit, currentPlayer.Hand))
                {
                    if (first.Suit == playedCard.Suit)
                    {

                        if (first.Suit != CardSuit.Spades && currentPot.highestCard.Suit == CardSuit.Spades)
                        {
                            return true;
                        }
                        else
                        {
                            // check its higher than the higest card in the pile
                            if (playedCard.Rank == CardRank.Ace) return true;
                            else
                            {
                                if (playedCard.Rank > currentPot.highestCard.Rank) return true;
                                else
                                {
                                    // check if he has higher card in this hand
                                    if (checkHandForHigherCard(currentPot.highestCard, currentPlayer.Hand))
                                    {
                                        // player has other higher card
                                        return false;
                                    }
                                    else
                                    {
                                        return true;
                                    }

                                }
                            }
                        }

                    }
                    else return false;
                }
                // check if he has spades to play
                else if (hasSuit(CardSuit.Spades, currentPlayer.Hand))
                {

                    if (checkHandForHigherCard(currentPot.highestCard, currentPlayer.Hand))
                    {

                        if (currentPot.highestCard.Suit != CardSuit.Spades && playedCard.Suit == CardSuit.Spades) return true;
                        if (playedCard.Rank == CardRank.Ace && playedCard.Suit == CardSuit.Spades) return true;
                        if ((playedCard.Rank > currentPot.highestCard.Rank) && ((currentPot.highestCard.Suit == CardSuit.Spades) && (playedCard.Suit == CardSuit.Spades))) return true;

                        return false;
                    }

                  // if no higher card play any thing
                  return true;

                }
                // if nothing play anyhitng
                else return true;

            }
        }
Beispiel #21
0
 public PileEventArgs(SpadesCard c)
 {
     card = c;
 }
Beispiel #22
0
 public bool containsCard(SpadesCard c)
 {
     return CardPile.Contains(c);
 }
Beispiel #23
0
 public void AddCard(SpadesCard c, int index)
 {
     CardPile.Insert(index, c);
     OnPotAdd(new PileEventArgs(c));
 }
Beispiel #24
0
        /// <summary>
        /// checks whether the card played by the player is valid or not in current scenario
        /// </summary>
        /// <param name="currentPlayer">The card player</param>
        /// <param name="playedCard">what card has the player chooosen to play </param>
        /// <param name="currentPot">pot of current running hand</param>
        /// <returns></returns>
        public static bool isValidCard(player currentPlayer, SpadesCard playedCard, Pot currentPot)
        {
            // if pot is empty its the first card and is always valid
            if (currentPot.CardPile.Count == 0)
            {
                return(true);
            }
            else
            {
                // get 1st played card



                SpadesCard first = (SpadesCard)currentPot.CardPile[0];

                // check if player has the same suit to play
                if (hasSuit(first.Suit, currentPlayer.Hand))
                {
                    if (first.Suit == playedCard.Suit)
                    {
                        if (first.Suit != CardSuit.Spades && currentPot.highestCard.Suit == CardSuit.Spades)
                        {
                            return(true);
                        }
                        else
                        {
                            // check its higher than the higest card in the pile
                            if (playedCard.Rank == CardRank.Ace)
                            {
                                return(true);
                            }
                            else
                            {
                                if (playedCard.Rank > currentPot.highestCard.Rank)
                                {
                                    return(true);
                                }
                                else
                                {
                                    // check if he has higher card in this hand
                                    if (checkHandForHigherCard(currentPot.highestCard, currentPlayer.Hand))
                                    {
                                        // player has other higher card
                                        return(false);
                                    }
                                    else
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                // check if he has spades to play
                else if (hasSuit(CardSuit.Spades, currentPlayer.Hand))
                {
                    if (checkHandForHigherCard(currentPot.highestCard, currentPlayer.Hand))
                    {
                        if (currentPot.highestCard.Suit != CardSuit.Spades && playedCard.Suit == CardSuit.Spades)
                        {
                            return(true);
                        }
                        if (playedCard.Rank == CardRank.Ace && playedCard.Suit == CardSuit.Spades)
                        {
                            return(true);
                        }
                        if ((playedCard.Rank > currentPot.highestCard.Rank) && ((currentPot.highestCard.Suit == CardSuit.Spades) && (playedCard.Suit == CardSuit.Spades)))
                        {
                            return(true);
                        }



                        return(false);
                    }



                    // if no higher card play any thing
                    return(true);
                }
                // if nothing play anyhitng
                else
                {
                    return(true);
                }
            }
        }