I want to make a simple card game but what's wrong with it?

Viewed 56

The game is played with 40 cards, 20 Red and 20 Green.

Cards: 1, 2, 3, 4, 5, ...., 20.

Colors: Red, Green

It is played with two players. The names of the players are taken.

The number of cards to be played is chosen. (N <= 20)

Players are given random N'er cards.

Player 1 and Player 2's N'er cards are compared in turn.

If one of the cards compared is green and one is red, the score of the player whose card is green is increased.

If the two compared cards have the same color, the score of the player with the higher card score is increased.

The cards and colors of Player 1 and Player 2 are written on the screen. Such as Y1, K1, Y3

Scores of the players are printed.

The player with a higher score is declared the winner.

using System;

namespace Card_Game
{
    class Program
    {
        static void Main(string[] args)
        {
            int player1score=0, player2score=0;
            string player1, player2;
            Console.WriteLine("enter player name 1");
            player1 = Console.ReadLine();
            Console.WriteLine("enter player name 2");
            player2 = Console.ReadLine();
           
            int number;
            Console.WriteLine("Enter how many cards you want to be given, " +
                "you can choose a maximum of 20, there are 40 cards in total in the game.");
            number = Convert.ToInt32(Console.ReadLine());
           
                Random random = new Random();
            int s1=0, s2=0;
            for (int i = 0; i < number; i++)
                {
               
                int player1card1 = random.Next(1, 3);
                /*if the number is 1 ,it means our card colour is green or number is 2,
                 * it means our card colour is red*/
                int player1card2 = random.Next(1, 21);
                int player2card1 = random.Next(1, 3);
                int player2card2 = random.Next(1, 21);
                Console.WriteLine("{0} player's card is given", player1);
                
                Console.WriteLine( "colour :"+ player1card1);

                Console.WriteLine("number:" + player1card2);
                Console.WriteLine("{0} player's card is given", player2);
                
                Console.WriteLine("colour :" + player2card1);
                
                Console.WriteLine("number :" + player2card2);
               
                if (player1card1 == 1 && player2card1 != 1)
                {
                    player1score  ++;
                }
                else if(player1card1 != 1 && player2card1 == 1)
                {
                    player2score  ++;
                }
                else
                {
                    if (player1card2 > player2card2)
                        player1score ++;
                    else
                        player2score ++;
                }

            }
            if (player1score > player2score)
                Console.WriteLine("Player {0} wins with {1} points Score of player {2} " +
                     "{3}", player1, player1score, player2, player2score);
            else if (player1score < player2score)
                Console.WriteLine("Player {0} wins with {1} points Score of player {2} " +
                     "{3}", player2, player2score,player1, player1score);
            else
                Console.WriteLine("the game is drawn with {0} points ", player1score);

        }
    }
}

What I want to do is to fulfill this function by generating 2 random numbers instead of defining individual cards.

If the first random number is 1, the card is green and the other card is red. But there are two problems here, the number of cards is 20 and when I define random numbers, the same numbers can come up. How can I set the equal number of cards?

1 Answers

Think about how a game of cards is normally played:

  1. First, the deck is created (when it's printed!)
  2. Then it's shuffled (but nobody has any cards yet!)
  3. The cards are handed out in order from the shuffled deck

So, the cards aren't determined when they're being dealt - their randomness is already assigned when they're shuffled. And when they're shuffled, they already exist! So, to make your approach similar to that, you should generate all the cards, and then order them randomly:

List<int> allCards = new List<int>();

// create the deck
for (int i = 0; i < 20; ++i) {
   allCards.Add(0); // one of each card
   allCards.Add(1);
}
var shuffledDeck = allCards.OrderBy(c => random.Next()).ToList(); // shuffle the deck

// get the cards for the persons hand
Related