Пример #1
0
 public BoardMemento(Card[,] cards, Card firstUpCard = null)
 {
     this.col         = cards.GetLength(0);
     this.row         = cards.GetLength(1);
     this.cards       = cards.Cast <Card>().ToArray();
     this.firstUpCard = firstUpCard;
 }
Пример #2
0
        private async Task CardsAsync()
        {
            game.ListCards = game.Shuffle();
            int index = 0;
            int y     = 70;

            for (int i = 0; i < cards.GetLength(0); i++)
            {
                int x = (Width - (cards.GetLength(1) * 80)) / 2;
                for (int j = 0; j < cards.GetLength(1); j++)
                {
                    cards[i, j] = new Card(
                        $"carta{index}", new Point(x, y),
                        new Size(80, 80), new Bitmap(game.ListCards[index]),
                        game.ListCards[index], index
                        );

                    index++;
                    x += 80;

                    cards[i, j].Click += (o, e) => CardClick(o);
                    Controls.Add(cards[i, j]);
                }
                y += 80;
            }

            await UntapAllCards(index);
        }
Пример #3
0
 void showCards()
 {
     for (int i = 0; i < cards.GetLength(0); i++)
     {
         string row = "";
         for (int j = 0; j < cards.GetLength(1); j++)
         {
             row += cards[i, j].id + " ";
         }
         Debug.Log(row);
     }
 }
        public int GetEmptyLocationOnColoumCard()
        {
            int output = 0;

            for (int i = 0; i < ColoumCard.GetLength(0); i++)
            {
                if (ColoumCard[i, 0] == null)
                {
                    output++;
                }
            }
            return(output);
        }
Пример #5
0
        private List <Point> GetBoardPositionAsList()
        {
            List <Point> boardLocations = new List <Point>();

            for (int x = 0; x < board.GetLength(0); ++x)
            {
                for (int y = 0; y < board.GetLength(1); ++y)
                {
                    boardLocations.Add(new Point(x, y));
                }
            }

            return(boardLocations);
        }
Пример #6
0
    public void generateGrid()
    {
        // creates a grid of cards in the environment
        Card[,] grid = Card.makeSquareGrid(difficulty, n);

        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                GameObject card = Object.Instantiate(cardObject, new Vector3(i * 8.4F, 0, j * 10.9F), Quaternion.identity);
                changeMatWithLoc(card, grid[i, j].getImgLoc());
            }
        }
    }
Пример #7
0
        public Tuple <int, int> GenerateRandomPosition()
        {
            Tuple <int, int> newPosition;
            int rowPos;
            int colPos;

            do
            {
                rowPos      = this.r_RandPosition.Next(m_CardsBoard.GetLength(0));
                colPos      = this.r_RandPosition.Next(m_CardsBoard.GetLength(1));
                newPosition = new Tuple <int, int>(rowPos, colPos);
            }while(this.r_FoundPositions.Contains(newPosition) || CardsBoard[rowPos, colPos].Open);

            return(newPosition);
        }
        //CONSTRUCTOR FOR DECK OF CARDS
        public DeckOfCards()
        {
            CardCollection = new Card[4, 13];

            //Give values to Cards
            //Loop through rows
            for (int i = 0; i < CardCollection.GetLength(0); i++)
            {
                //Loop through columns
                for (int j = 1; j <= CardCollection.GetLength(1); j++)
                {
                    Card c = new Card();
                    switch (j)
                    {
                    case 1:
                        c.CardName = "Ace";
                        break;

                    case 11:
                        c.CardName = "Jack";
                        break;

                    case 12:
                        c.CardName = "Queen";
                        break;

                    case 13:
                        c.CardName = "King";
                        break;

                    default:
                        c.CardName = j.ToString();
                        break;
                    }

                    c.Suit = suits[i];

                    CardCollection[i, j - 1] = c;
                } //end of for
            }     //end of for

            //set current card to 0
            currentCard = 0;
        }
Пример #9
0
    public void generateGrid()
    {
        // creates a grid of cards in the environment
        Card[,] grid = Card.makeSquareGrid(difficulty, n);

        // Get list of random cards #s to be init cards
        System.Random rand      = new System.Random();
        ArrayList     initCards = new ArrayList();

        for (int numCardsInited = 0; numCardsInited < n; numCardsInited++)
        {
            int randomNumber = rand.Next(0, n * n);
            if (!initCards.Contains(randomNumber))
            {
                initCards.Add(randomNumber);
            }
        }

        int currCard = 0;

        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                GameObject card = Object.Instantiate(cardObject, new Vector3(0, i * cardY, j * cardZ), Quaternion.Euler(90, 0, 90));
                changeMatWithLoc(card, grid[i, j].getImgLoc());
                Card cardScript = cardObject.AddComponent <Card>();
                cards[position++] = card;



                if (initCards.Contains(currCard))
                {
                    // If card # in initCards, set Init = true via Card.cs setter
                    cardScript.setInit(true);
                    Debug.Log(currCard);
                    initCards.Remove(currCard);
                }
                currCard++;
            }
        }
    }
Пример #10
0
        public string BoardToString() // Borde lägga till vid sidan rad och kulomn så ma  slipper räkna
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24");
            sb.AppendLine();
            for (int i = 0; i < gameBoard.GetLength(0); i++)
            {
                sb.Append(i);
                sb.Append(" ");
                if (i < 10)
                {
                    sb.Append(" ");
                }
                for (int j = 0; j < gameBoard.GetLength(1); j++)
                {
                    if (gameBoard[i, j] == null)
                    {
                        for (int p = 0; p < players.Count(); p++)
                        {
                            if (players[p].getX() == j && players[p].getY() == i)
                            {
                                sb.Append("|P|"); //Om jag tänkt rätt nu så kommer två spleare på samma ruta bara stå som |P|
                                break;
                            }
                            else if (p == players.Count - 1)
                            {
                                sb.Append("|_|");
                            }
                        }
                    }
                    else if (gameBoard[i, j].GetType() == typeof(Room))
                    {
                        sb.Append("|R|");
                    }
                }
                sb.Append("\n");
            }
            return(sb.ToString());
        }
Пример #11
0
        public void DealOnce(Deck gameDeck, Card[] garbage, int playerIndex)
        {
            // how many cards does the player have
            int cardsObtained = 0;
            int rowLength     = cardsDealt.GetLength(1);

            for (int i = 0; i < rowLength; i++)
            {
                if (cardsDealt[playerIndex, i] != null)
                {
                    cardsObtained++;
                }
            }

            //append card to the correct place
            cardsDealt[playerIndex, cardsObtained] = new Card(gameDeck.deck[0].Value, gameDeck.deck[0].Suit);



            //garbage
            int garbageLength = 0;

            for (int i = 0; i < garbage.Length; i++)
            {
                if (garbage[i] != null)
                {
                    garbageLength++;
                }
            }
            //new gamedeck

            Card[] newGameDeck = new Card[gameDeck.deck.Length - 1];
            int    count       = 0;

            for (int i = 0; i < gameDeck.deck.Length - 1; i++)
            {
                if (i == 0)
                {
                    // add it to the correct place
                    garbage[garbageLength + i] = new Card(gameDeck.deck[i].Value, gameDeck.deck[i].Suit);
                }
                else
                {
                    newGameDeck[count] = new Card(gameDeck.deck[i].Value, gameDeck.deck[i].Suit);
                    count++;
                }
            }

            //game deck has new length
            gameDeck.deck = newGameDeck;
        }
Пример #12
0
        public void StartNewSession()
        {
            selectedCards.Clear();
            won = false;
            IEnumerator <string> cardColors  = CardColors();
            IEnumerator <int>    cardNumbers = CardNumbers();

            for (int y = 0; y < cards.GetLength(1); y++)
            {
                for (int x = 0; x < cards.GetLength(0); x++)
                {
                    cardColors.MoveNext();
                    cardNumbers.MoveNext();

                    cards[x, y] = new Card(
                        cardColors.Current,
                        cardNumbers.Current.ToString());

                    cards[x, y].Clicked += OnCardClicked;
                }
            }

            cards.Shuffle();
        }
Пример #13
0
        public void MouseClick(int xForm, int yForm)
        {
            if (waiting || hasWon)
            {
                return;
            }
            int xGrid = (xForm - xBegin) / colWidth;
            int yGrid = (yForm - yBegin) / colHeight;

            if (xGrid >= 0 && xGrid < cards.GetLength(0) && yGrid >= 0 && yGrid < cards.GetLength(1) && numberOfShownCards < 2)
            {
                ++numberOfShownCards;
                if (numberOfShownCards == 1)
                {
                    currentCard1 = cards[xGrid, yGrid];
                }
                else if (numberOfShownCards == 2)
                {
                    currentCard2 = cards[xGrid, yGrid];
                    if (currentCard1.CompareTo(currentCard2))
                    {
                        currentCard1.Win();
                        currentCard2.Win();
                        counter.SuccessfulTurn(currentCard1.Uid, currentCard2.Uid);
                        hasWon = HasWon();
                        HideNotWonCards();
                    }
                    else
                    {
                        counter.Turn(currentCard1.Uid, currentCard2.Uid);
                        form.EnableTimerHideNotWonCards();
                    }
                }
                cards[xGrid, yGrid].TurnUp();
                form.Invalidate();
                if (hasWon)
                {
                    form.ShowWonDialog(counter);
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here

            //Updates the mouse cursor location and the mouse state
            mouseCursorLoc = new Rectangle((int)mouse.X, (int)mouse.Y, mouseCursor.Width, mouseCursor.Height);
            mouse          = Mouse.GetState();

            switch (currentGameState)
            {
            case GameStates.InGame:
                //Updates the cards remaining in deck
                cardsRemaining = deck.Count;

                //On and off button for music
                if (onVolBtn.ButtonClicked(mouse) && isVolOn && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    MediaPlayer.Pause();
                    isVolOn = false;
                }
                else if (onVolBtn.ButtonClicked(mouse) && !isVolOn && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    MediaPlayer.Resume();
                    isVolOn = true;
                }

                //Exits the game when button is pressed
                if (exit.ButtonClicked(mouse))
                {
                    Exit();
                }

                //Checks to see if the cards are laid down
                if (isCardPlaced && !isAddError && !isFaceError && !isPlusFaceError)
                {
                    for (int i = 0; i < tableCards.GetLength(1); i++)
                    {
                        for (int j = 0; j < tableCards.GetLength(0); j++)
                        {
                            //Checks if a card was clicked on and mouse is not currently clicked
                            if (tableCards[j, i].CardClicked(mouse) && prevMouse.LeftButton != ButtonState.Pressed)
                            {
                                //Checks whether card is a face or not
                                if (tableCards[j, i].value > 0)
                                {
                                    //Finds if selected card is not the first selected card and that first card was selected
                                    if (tableCards[j, i] != cardSelected1 && isFirstCardSelected)
                                    {
                                        //Selects the second card and checks if it adds to eleven
                                        cardSelected2       = tableCards[j, i];
                                        selectedCardsPos[1] = new Point(j, i);
                                        CheckCard();
                                    }

                                    //Checks if the card clicked was selected
                                    if (!isFirstCardSelected)
                                    {
                                        //Selects the card and sets the state whether it was clicked or not
                                        cardSelected1       = tableCards[j, i];
                                        selectedCardsPos[0] = new Point(j, i);
                                        isFirstCardSelected = true;
                                    }
                                    else
                                    {
                                        //Deselects the first card
                                        isFirstCardSelected = false;
                                    }
                                }
                                else
                                {
                                    //Checks if the pile number on face card is one
                                    if (pileNumber[j, i] == 1 && !isFirstCardSelected)
                                    {
                                        //Deselects any selected cards
                                        isFirstCardSelected = false;

                                        //Swaps the card
                                        tableCards[j, i] = SwapFaceCard(j, i);

                                        //Checks to see if game is won, lose or neither
                                        CheckForWinCondition();
                                    }
                                    else
                                    {
                                        //Checks which is the error that is occurring
                                        if (isFirstCardSelected)
                                        {
                                            //Plays sound, deselects card and sets error to true
                                            errorInstance.Play();
                                            isFirstCardSelected = false;
                                            isPlusFaceError     = true;
                                        }
                                        else if (pileNumber[j, i] > 1)
                                        {
                                            //Plays sound effect and sets face error to true
                                            errorInstance.Play();
                                            isFaceError = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    //Checks if mouse is clicked during error
                    if (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton != ButtonState.Pressed)
                    {
                        //Checks the type of error
                        if (isAddError)
                        {
                            isAddError = false;
                        }
                        if (isFaceError)
                        {
                            isFaceError = false;
                        }
                        if (isPlusFaceError)
                        {
                            isPlusFaceError = false;
                        }
                    }
                }
                break;

            case GameStates.WinCondition:
                //Checks whether user pressed any buttons
                if (yesButton.ButtonClicked(mouse) && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    Reset();
                }
                else if (noButton.ButtonClicked(mouse) && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    Exit();
                }
                break;

            case GameStates.LoseCondition:
                //Checks if user has pressed any buttons
                if (yesButton.ButtonClicked(mouse) && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    Reset();
                }
                else if (noButton.ButtonClicked(mouse) && prevMouse.LeftButton != ButtonState.Pressed)
                {
                    Exit();
                }
                break;
            }

            //Stores the previous mouse state
            prevMouse = mouse;

            base.Update(gameTime);
        }
Пример #15
0
        public bool PlayerPlays()
        {
            bool askCard = false;

            int  totalPoints;
            int  totalPlayerPointsP     = 0;
            int  totalPlayerPointsWAceP = 0;
            bool wAceP = false;

            foreach (Card card in cards)
            {
                if (card != null)
                {
                    if (card.Value == 14)
                    {
                        if (wAceP == false)
                        {
                            wAceP = true;
                            totalPlayerPointsWAceP += totalPlayerPointsP;
                            totalPlayerPointsWAceP += 11;
                            totalPlayerPointsP     += 1;
                        }
                        else
                        {
                            totalPlayerPointsWAceP += 11;
                            totalPlayerPointsP     += 1;
                        }
                    }
                    else
                    {
                        int cardValue;
                        if (card.Value > 10 && card.Value != 14)
                        {
                            cardValue = 10;
                        }
                        else
                        {
                            cardValue = card.Value;
                        }
                        totalPlayerPointsP += cardValue;

                        if (wAceP)
                        {
                            totalPlayerPointsWAceP += cardValue;
                        }
                    }
                }
            }



            if (totalPlayerPointsWAceP >= 16)
            {
                totalPoints = totalPlayerPointsWAceP;
            }
            else
            {
                if (totalPlayerPointsWAceP > totalPlayerPointsP)
                {
                    totalPoints = totalPlayerPointsWAceP;
                }
                else
                {
                    totalPoints = totalPlayerPointsP;
                }
            }

            if (isDealer == false)
            {
                // if the player is not the dealer

                // if total is less than or equal to 16 ask for another card else stay

                if (totalPoints <= 16)
                {
                    askCard = true;
                }
            }
            else
            {
                // if the player is the dealer
                // if current total point is higher than the others
                // if total is less than or equal to 16 ask for another card else stay

                int rowLength = cardsDealt.GetLength(0);
                int colLength = cardsDealt.GetLength(1);

                int[] playerPoints = new int[rowLength];

                for (int i = 0; i < rowLength; i++)
                {
                    int  totalPlayerPoints     = 0;
                    int  totalPlayerPointsWAce = 0;
                    bool wAce = false;

                    if (i != 0)
                    {
                        for (int j = 0; j < colLength; j++)
                        {
                            if (cardsDealt[i, j] != null)
                            {
                                if (cardsDealt[i, j].Value == 14)
                                {
                                    if (wAce == false)
                                    {
                                        wAce = true;
                                        totalPlayerPointsWAce += totalPlayerPoints;
                                        totalPlayerPointsWAce += 11;
                                        totalPlayerPoints     += 1;
                                    }
                                    else
                                    {
                                        totalPlayerPointsWAce += 11;
                                        totalPlayerPoints     += 1;
                                    }
                                }
                                else
                                {
                                    int cardValue;
                                    if (cardsDealt[i, j].Value > 10 && cardsDealt[i, j].Value != 14)
                                    {
                                        cardValue = 10;
                                    }
                                    else
                                    {
                                        cardValue = cardsDealt[i, j].Value;
                                    }
                                    totalPlayerPoints += cardValue;

                                    if (wAce)
                                    {
                                        totalPlayerPointsWAce += cardValue;
                                    }
                                }
                            }
                        }
                        if (totalPlayerPointsWAce >= 16)
                        {
                            playerPoints[i] = totalPlayerPointsWAce;
                        }
                        else
                        {
                            if (totalPlayerPointsWAce > totalPlayerPoints)
                            {
                                playerPoints[i] = totalPlayerPointsWAce;
                            }
                            else
                            {
                                playerPoints[i] = totalPlayerPoints;
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < colLength; j++)
                        {
                            if (cardsDealt[i, j] != null)
                            {
                                if (cardsDealt[i, j].Value == 14)
                                {
                                    if (wAce == false)
                                    {
                                        wAce = true;
                                        totalPlayerPointsWAce += totalPlayerPoints;
                                        totalPlayerPointsWAce += 11;
                                        totalPlayerPoints     += 1;
                                    }
                                    else
                                    {
                                        totalPlayerPointsWAce += 11;
                                        totalPlayerPoints     += 1;
                                    }
                                }
                                else
                                {
                                    int cardValue;
                                    if (cardsDealt[i, j].Value > 10 && cardsDealt[i, j].Value != 14)
                                    {
                                        cardValue = 10;
                                    }
                                    else
                                    {
                                        cardValue = cardsDealt[i, j].Value;
                                    }
                                    totalPlayerPoints += cardValue;

                                    if (wAce)
                                    {
                                        totalPlayerPointsWAce += cardValue;
                                    }
                                }
                            }
                        }


                        // if total with ace is higher than 16 choose that else choose the one that is higher
                        if (totalPlayerPointsWAce >= 16)
                        {
                            playerPoints[i] = totalPlayerPointsWAce;
                        }
                        else
                        {
                            if (totalPlayerPointsWAce > totalPlayerPoints)
                            {
                                playerPoints[i] = totalPlayerPointsWAce;
                            }
                            else
                            {
                                playerPoints[i] = totalPlayerPoints;
                            }
                        }
                    }
                }
                // if current total point is higher than the others

                /*foreach (int points in playerPoints)
                 * {
                 *  Console.WriteLine(points);
                 * }
                 *
                 * foreach (Card card in cardsDealt)
                 * {
                 *  if (card != null)
                 *  {
                 *      Console.WriteLine(card.Value);
                 *  }
                 * }*/

                int dealerPoint  = playerPoints[0];
                int comparePoint = 0;

                for (int i = 1; i < playerPoints.Length; i++)
                {
                    if (dealerPoint >= playerPoints[i])
                    {
                        comparePoint++;
                    }
                }

                // if compare points is higher than half of rounded table size
                int halfTableSize = Convert.ToInt16((playerPoints.Length - 1) / 2);

                if (comparePoint >= halfTableSize)
                {
                    askCard = false;
                }
                else
                {
                    if (dealerPoint < 16)
                    {
                        askCard = true;
                    }
                }
            }
            return(askCard);
        }
Пример #16
0
        private void initializeComponents()
        {
            const int marginBorders  = 12;
            const int squareCardSize = 100;

            // Button - Cards
            Dictionary <char, Image> cardImages = new Dictionary <char, Image>();

            for (int i = 0; i < m_ButtonCards.GetLength(0); i++)
            {
                for (int j = 0; j < m_ButtonCards.GetLength(1); j++)
                {
                    char cardSymbol = m_Game.GetCardValueByIndexes(i, j);

                    m_ButtonCards[i, j] = new Card();

                    if (!cardImages.ContainsKey(cardSymbol))
                    {
                        cardImages.Add(cardSymbol, m_ButtonCards[i, j].CardImage = GetRandomImage());
                    }
                    else
                    {
                        m_ButtonCards[i, j].CardImage = cardImages[cardSymbol];
                    }

                    m_ButtonCards[i, j].X                     = i;
                    m_ButtonCards[i, j].Y                     = j;
                    m_ButtonCards[i, j].CardSymbol            = cardSymbol;
                    m_ButtonCards[i, j].BackgroundImageLayout = ImageLayout.Stretch;
                    m_ButtonCards[i, j].Size                  = new Size(squareCardSize, squareCardSize);
                    m_ButtonCards[i, j].Left                  = (i * (squareCardSize + marginBorders)) + marginBorders;
                    m_ButtonCards[i, j].Top                   = (j * (squareCardSize + marginBorders)) + marginBorders;
                    m_ButtonCards[i, j].Click                += buttonCard_Click;
                    this.Controls.Add(m_ButtonCards[i, j]);
                }
            }

            // Label - Current Player
            m_LabelCurrentPlayer           = new Label();
            m_LabelCurrentPlayer.Text      = $"Current Player: {m_Game.PlayerTurn_Name}";
            m_LabelCurrentPlayer.BackColor = m_Game.PlayerTurn_Name.Equals(m_Game.Player1_Name) ? m_Player1Color : m_Player2Color;
            m_LabelCurrentPlayer.ForeColor = Color.Black;
            m_LabelCurrentPlayer.AutoSize  = true;
            m_LabelCurrentPlayer.Left      = marginBorders;
            m_LabelCurrentPlayer.Top       = m_ButtonCards[0, m_ButtonCards.GetLength(1) - 1].Bottom + marginBorders;
            this.Controls.Add(m_LabelCurrentPlayer);

            // Label - Player 1 Score
            m_LabelPlayer1Score           = new Label();
            m_LabelPlayer1Score.Text      = $"{m_Game.Player1_Name}: {m_Game.Player1_Score} Pairs";
            m_LabelPlayer1Score.BackColor = m_Player1Color;
            m_LabelPlayer1Score.ForeColor = Color.Black;
            m_LabelPlayer1Score.AutoSize  = true;
            m_LabelPlayer1Score.Left      = marginBorders;
            m_LabelPlayer1Score.Top       = m_LabelCurrentPlayer.Bottom + marginBorders;
            this.Controls.Add(m_LabelPlayer1Score);

            // Label - Player 2 Score
            m_LabelPlayer2Score           = new Label();
            m_LabelPlayer2Score.Text      = $"{m_Game.Player2_Name}: {m_Game.Player2_Score} Pairs";
            m_LabelPlayer2Score.BackColor = m_Player2Color;
            m_LabelPlayer2Score.ForeColor = Color.Black;
            m_LabelPlayer2Score.AutoSize  = true;
            m_LabelPlayer2Score.Left      = marginBorders;
            m_LabelPlayer2Score.Top       = m_LabelPlayer1Score.Bottom + marginBorders;
            this.Controls.Add(m_LabelPlayer2Score);

            // Form - This
            this.Text            = "Memory Game";
            this.ClientSize      = new Size(m_ButtonCards[m_ButtonCards.GetLength(0) - 1, m_ButtonCards.GetLength(1) - 1].Right + marginBorders, m_LabelPlayer2Score.Bottom + marginBorders);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition   = FormStartPosition.CenterParent;
            this.MaximizeBox     = false;
            this.MinimizeBox     = false;
            this.ShowIcon        = true;
            this.ShowInTaskbar   = true;
            this.FormClosing    += formGame_FormClosing;
        }