Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this code. There are 52 cards in a deck. 2-10, Jack, Queen, King, Ace (Ace is the highest card) It is a 2 player game, each player starts with 26 cards (half the deck) The game starts with each player flipping over 1 card and whomever has the highest card wins. If there is a tie, the next three cards in the deck are played face down and the 4th card dealt face up, whomever has the highest card wins. The game is played until one of the players has all of the 52 cards.
However, I haven't been able to split the 26 cards to each one of the players at the beginning of the game as expected.
Another issue is to finish the game just when one of the players have all the 52 cards. Could some body grant a solution ,please?
Thanks
Alexander Sanchez

C#
using System;
using System.Collections.Generic;

namespace PTCB12WARGAME2
{

    class Card
    {
        public string Name;
        public int Value;
        public string Suit;
        public override string ToString() { return string.Format("{0} of {1}", Name, Suit); }
        
    }
    class DeckOfCards
    {
        static void Main(string[] args)
        {

            List<card> DeckOfCards = new List<card>()
                {
                    new Card { Name="Ace", Value=1, Suit="Heart"},
                    new Card{Name="two",Value=2,Suit="Heart"},
                    new Card{Name="three",Value=3,Suit="Heart"},
                    new Card{Name="four",Value=4,Suit="Heart"},
                    new Card { Name="five", Value=5, Suit="Heart"},
                    new Card{Name="six",Value=6,Suit="Heart"},
                    new Card{Name="seven",Value=7,Suit="Heart"},
                    new Card{Name="eight",Value=8,Suit="Heart"},
                    new Card { Name="Nine", Value=9, Suit="heart"},
                    new Card{Name="ten",Value=10,Suit="Heart"},
                    new Card{Name="jack",Value=11,Suit="Heart"},
                    new Card{Name="queen",Value=12,Suit="Heart"},
                    new Card { Name="king", Value=13, Suit="heart"},
                    new Card { Name="Ace", Value=1, Suit="Spades"},
                    new Card{Name="two",Value=2,Suit="Spades"},
                    new Card{Name="three",Value=3,Suit="Spades"},
                    new Card{Name="four",Value=4,Suit="Spades"},
                    new Card { Name="five", Value=5, Suit="Spades"},
                    new Card{Name="six",Value=6,Suit="Spades"},
                    new Card{Name="seven",Value=7,Suit="Spades"},
                    new Card{Name="eight",Value=8,Suit="Spades"},
                    new Card { Name="Nine", Value=9, Suit="Spades"},
                    new Card{Name="ten",Value=10,Suit="Spades"},
                    new Card{Name="jack",Value=11,Suit="Spades"},
                    new Card{Name="queen",Value=12,Suit="Spades"},
                    new Card { Name="king", Value=13, Suit="Spades"},
                    new Card { Name="Ace", Value=1, Suit="Diamonds"},
                    new Card{Name="two",Value=2,Suit="Diamonds"},
                    new Card{Name="three",Value=3,Suit="Diamonds"},
                    new Card{Name="four",Value=4,Suit="Diamonds"},
                    new Card { Name="five", Value=5, Suit="Diamonds"},
                    new Card{Name="six",Value=6,Suit="Diamonds"},
                    new Card{Name="seven",Value=7,Suit="Diamonds"},
                    new Card{Name="eight",Value=8,Suit="Diamonds"},
                    new Card { Name="Nine", Value=9, Suit="Diamonds"},
                    new Card{Name="ten",Value=10,Suit="Diamonds"},
                    new Card{Name="jack",Value=11,Suit="Diamonds"},
                    new Card{Name="queen",Value=12,Suit="Diamonds"},
                    new Card { Name="king", Value=13, Suit="Diamonds"},
                    new Card { Name="Ace", Value=1, Suit="Clubs"},
                    new Card{Name="two",Value=2,Suit="Clubs"},
                    new Card{Name="three",Value=3,Suit="Clubs"},
                    new Card{Name="four",Value=4,Suit="Clubs"},
                    new Card { Name="five", Value=5, Suit="Clubs"},
                    new Card{Name="six",Value=6,Suit="Clubs"},
                    new Card{Name="seven",Value=7,Suit="Clubs"},
                    new Card{Name="eight",Value=8,Suit="Clubs"},
                    new Card { Name="Nine", Value=9, Suit="Clubs"},
                    new Card{Name="ten",Value=10,Suit="Clubs"},
                    new Card{Name="jack",Value=11,Suit="Clubs"},
                    new Card{Name="queen",Value=12,Suit="Clubs"},
                    new Card { Name="king", Value=13, Suit="Clubs"},

                };
            Console.WriteLine("Each player is flipping over 1 card and whomever has the highest card wins");
            Console.ReadLine();


            for (int i = 0; i < DeckOfCards.Count; i++)
            {
                Console.WriteLine(DeckOfCards[i]);

            }

            Random random = new Random();

            List<card> deckOfPlayer1 = new List<card>();
            List<card> deckOfPlayer2 = new List<card>();

            do
            {
                Console.WriteLine("Let's flip cards");
                string userAnswer = Console.ReadLine();
                if (userAnswer == "quit")
                    break;
                // pick a random card for player 1

                int r1;
                r1 = random.Next(0, DeckOfCards.Count);
                Card c1 = DeckOfCards[r1];
                deckOfPlayer1.Add(c1);
                DeckOfCards.RemoveAt(r1);
               

                // pick a random card for player 2

                int r2;
                r2 = random.Next(0, DeckOfCards.Count);
                Card c2 = DeckOfCards[r2];
                deckOfPlayer2.Add(c2);
                DeckOfCards.RemoveAt(r2);

                Console.WriteLine("Player1 has: {0}", c1);
                Console.WriteLine("Player2 has: {0}", c2);
                Console.WriteLine("Player1 has: {0}", r1);
                Console.WriteLine("Player2 has: {0}", r2);


                // now compare the cards
                if (c1.Value == c2.Value && deckOfPlayer1.Count > 4 || (deckOfPlayer2.Count > 4))
                {

                    Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
                        "4th card face up, whoever has the highest card wins");

                    Console.WriteLine(deckOfPlayer1[3]);

                    Console.WriteLine(deckOfPlayer2[3]);



                    if (c1.Value > c2.Value && c2.Value != 1)
                    {
                        deckOfPlayer1.Add(c2);
                        deckOfPlayer2.Remove(c2);
                        Console.WriteLine("Player1 wins");                     
                        Console.WriteLine("Player1 has: {0}", r1);
                        Console.WriteLine("Player2 has: {0}", r2);
                    }
                    else
                    {
                        Console.WriteLine("Player2 wins");
                        deckOfPlayer2.Add(c1);
                        deckOfPlayer1.Remove(c1);                                         
                        Console.WriteLine("Player1 has: {0}", r1);
                        Console.WriteLine("Player2 has: {0}", r2);
                    }

                }

                else if (c1.Value == 1 || (c1.Value > c2.Value && c2.Value != 1))
                {
                    deckOfPlayer1.Add(c2);
                    deckOfPlayer2.Remove(c2);
                    Console.WriteLine("Player1 wins");
                }
                else
                {
                    Console.WriteLine("Player2 wins");
                    deckOfPlayer2.Add(c1);
                    deckOfPlayer1.Remove(c1);
                }


            } while (deckOfPlayer1.Count < 52 || (deckOfPlayer2.Count < 52));

        }
    }
}


What I have tried:

I have tried to shorten my code a lot by creating an array of names and an array of suits, as I can simply use nested loops to populate your deck of cards list, but it hasn't work, either. Thanks

C#
var names = new [] { "Ace", "Two", "Three", "Four", "Five", "Six", 
     "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

var suits = new [] { "Hearts", "Clubs", "Diamonds", "Spades" };

var cards = new List<card>();

// Populate our deck of cards with two nested loops
foreach(var suit in suits)
{
    // We use a 'for' loop here instead of 'foreach' so we can use the index to 
    // populate the Value (which is always one more than the index of the name)
    for(int i = 0; i < names.Length; i++)
    {
        cards.Add(new Card { Name = names[i], Value = i + 1, Suit = suit });
    }
}
Posted
Updated 23-Apr-21 3:21am
v2
Comments
CPallini 23-Apr-21 8:54am    
"And it feels like déjà vu
The sun goes down..."
BillWoodruff 23-Apr-21 18:03pm    
You created a golem.
PIEBALDconsult 23-Apr-21 9:05am    
"Here's the deal, see..."

I suggest using enumerations for the card names and suit names. They could actually be combined into one enumeration if you're clever.
Then maybe look into the Monte Carlo algorithm.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
BillWoodruff 23-Apr-21 13:05pm    
this is a repost by the OP
You state
Quote:
However, I haven't been able to split the 26 cards to each one of the players at the beginning of the game as expected
and yet in response to a solution to How can I randomly deal 26 cards each to the two players of a 52 deck? Thanks[^] you state
Quote:
Thank you very much, Mr.Pallini! It worked, you are amazing!
So for the purposes of this post I am going to assume that you will follow CPallini's advice.

Your comparision logic seems flawed - the cards should be compared, not the number of cards in each player's hand. You then simply continue until 1 player has 52 cards in their hand OR "while both players still have cards" e.g.
C#
while (deckOfPlayer1.Count > 0 && deckOfPlayer2.Count > 0)
You are also not handling Aces properly. Instead of checking for Card.value = 1 (and then ignoring the Ace altogether) why not just give the Aces a value of 14 - one higher than a King.

Next look at the first part of your comparison code structure ...
C#
               // now compare the cards
                if (c1.Value == c2.Value && deckOfPlayer1.Count > 4 || (deckOfPlayer2.Count > 4))
                {

                    Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
                        "4th card face up, whoever has the highest card wins");

                    Console.WriteLine(deckOfPlayer1[3]);

                    Console.WriteLine(deckOfPlayer2[3]);



                    if (c1.Value > c2.Value && c2.Value != 1)
                    {
// CHill60 says: You will never get here because c1.Value == c2.Value
                        deckOfPlayer1.Add(c2);
                        deckOfPlayer2.Remove(c2);
                        Console.WriteLine("Player1 wins");                     
                        Console.WriteLine("Player1 has: {0}", r1);
                        Console.WriteLine("Player2 has: {0}", r2);
                    }
                    else
                    {
// CHill60 says: So Player 2 always wins when there is a match
                        Console.WriteLine("Player2 wins");
                        deckOfPlayer2.Add(c1);
                        deckOfPlayer1.Remove(c1);                                         
                        Console.WriteLine("Player1 has: {0}", r1);
                        Console.WriteLine("Player2 has: {0}", r2);
                    }

                }
Instead of that comparison you should be "playing 3 cards face down" - although quite what effect that will have when you are picking cards at random beats me.
 
Share this answer
 
Comments
Diasalva5 23-Apr-21 10:00am    
Good morning
I followed Mr. Pallini's advise and it really worked. The issue is that I tried to add more items to the programs like checking how many cards each one of the players have on hand at the beginning of the game and I noticed that they didn't have 26 each as planned. That is the main issue, everything else is ok. Thanks, MrChill60
Diasalva5 23-Apr-21 10:03am    
Mr. Chill60
I think your logic is better than mine, I am probably not handling Aces properly. Instead of checking for Card.value = 1 (and then ignoring the Ace altogether) I will just give the Aces a value of 14 - one higher than a King.
Thanks
Alexander
BillWoodruff 23-Apr-21 18:09pm    
+5 A good example of constructive feedback (as opposed to rewriting the code for the OP) where you discuss the OP's logic AND show the relevant code. This type of analysis is all too rarely seen here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900