Click here to Skip to main content
15,439,117 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;

namespace PTCB12WARGAME2
{

    class Card
    {
        public string Name;
        public int Value;
        public string 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"},

                };

            //Display deck of 52 cards

            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].Name);
            }

            Console.WriteLine("Let's flip cards");
            string userAnswer = Console.ReadLine();

            //Player1
            List<Card> DeckOfCards2 = new List<Card>();
            DeckOfCards2.Add(DeckOfCards[0]);
                               
            for (int i = 0; i < 25 ; i++)
            {
                
                
                Random random1 = new Random();
                int Player1 = random1.Next();
                DeckOfCards2.Add(DeckOfCards[i]);

            }

            for (int i = 0; i < DeckOfCards2.Count; i++)
            {
                Console.WriteLine($"Player1 have: {Player1}");
            }

            //Player2

            List<Card> DeckOfCards3 = new List<Card>();
            DeckOfCards3.Add(DeckOfCards2[0]);
            
            for (int i = 0; i <25 ; i++)
            {
                Random random2 = new Random();
                int Player2 = random2.Next();
                DeckOfCards3.Add(DeckOfCards[i]);
            }


            for (int i = 0; i < DeckOfCards3.Count; i++)
            {
                Console.WriteLine($"Player2 have: {Player2}");
            }


            //Game

            //{
            //    Console.WriteLine($"Player1 have {DeckOfCards2} cards");
            //    Console.WriteLine($"Player2 have  {DeckOfCards3} cards");
            //}



            if (Player1 > Player2)
            {
                Console.WriteLine($"Player1 WON with {Player1}");
                Console.WriteLine($"Player2 ended with  {Player2}");
            }

            else if (Player2 > Player1)
            {
                Console.WriteLine($"Player2 WON with {Player2}");
                Console.WriteLine($"Player1 ended with  {Player1}");
            }


            else if (Player1 == Player2)
            {
                Console.WriteLine("Player1 and Player2 have the same number, the game is a tie ");
            }


            Console.ReadLine();



        }
    }
}


What I have tried:

I have tried by using the random outside the for loop
Posted
Updated 16-Apr-21 8:05am
v2
Comments
BillWoodruff 17-Apr-21 12:53pm    
You do not separate the construction of the player decks from testing for a "winner" ... that seems odd.
BillWoodruff 18-Apr-21 5:49am    
If you want to see a code example that demonstrates simplifying creating the two decks, and guarantees each player does not have duplicate cards in their hand: let me know. Note that CPallini's solution does prevent duplicates.
CHill60 23-Apr-21 9:05am    
How does solution 1 not prevent duplicates? It removes the card from the deck after it has been dealt to a player. Am I missing something?
BillWoodruff 23-Apr-21 9:12am     CRLF
I say, in the previous comment: "CPallini's solution does prevent duplicates." You are missing ... something :)
CHill60 23-Apr-21 9:43am    
I seem to be inserting missing things! What a numpty am I. Sorry!

Try
C++
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"},

                };

            //Display deck of 52 cards

            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]);
            }
            
            Console.WriteLine();
            
            // create just ONCE the random object
            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);
            	// remove the card from the deck
            	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);
            	// remove the card from the deck
            	DeckOfCards.RemoveAt(r2);
           
            	Console.WriteLine("Player1 has: {0}", c1);
            	Console.WriteLine("Player2 has: {0}", c2);
							
            	
            	// now compare the cards
            	if (c1.Value > c2.Value)
            	{
            		Console.WriteLine("Player1 wins");
            	}
            	else if ( c1.Value < c2.Value)
            	{
            		Console.WriteLine("Player2 wins");
            	}	
            	else
            	{	
            		Console.WriteLine("Its a draw");            
            	}
            } while ( DeckOfCards.Count > 0);
        
        }
    }
}
 
Share this answer
 
Comments
Diasalva5 16-Apr-21 20:14pm    
Thank you very much, Mr.Pallini! It worked, you are amazing!
CPallini 18-Apr-21 5:30am    
You are welcome.
BillWoodruff 17-Apr-21 11:02am    
And, when the OP gets a job, are you going to rewrite his code for him ?
CPallini 18-Apr-21 5:07am    
Yes, it is a joint venture.
BillWoodruff 18-Apr-21 5:43am    
Some of us take teaching seriously; others enjoy showing off.
I think you want to "shuffle", then deal. More "natural".

How do I shuffle a deck of cards in C#?[^]
 
Share this answer
 
Comments
Diasalva5 16-Apr-21 20:13pm    
Thank you.
BillWoodruff 17-Apr-21 11:00am    
Voted #1: irrelevant ... if you are sampling the deck randomly, you do not need to shuffle.
CPallini 18-Apr-21 5:06am    
It is an alternative. My 5.
BillWoodruff 18-Apr-21 5:52am    
You're right, it is an alternative, and it is more on-topic than Schmitz' usual rep-bloating spew: I am raising my vote to #3.
CPallini 18-Apr-21 6:00am    
I think you are a bit rude and sometimes completely wrong on your judgements.

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