dcsimg
 

Creating a "Card Wars" Game in .NET

Thursday May 10th 2018 by Hannes DuPreez

Learn to program a card game called "Card Wars," in both C# and VB.NET.

Introduction

Hello again! As you may know, I always endeavour to write about topics that are interesting and fun. As you may also know: I love games—in another life, I would have been a game developer, but so much has changed since I have tried getting into the gaming development environment.

If you have been following my articles (or at least some!), you would know that I have written about a lot of games, mostly games involving logic. Lately, I have been writing about card games, because, well, there are only so many other games I can cover and remember. Today's topic also covers a card game known as Card Wars.

Card Wars

With Card Wars, you have to win all cards. In the beginning of the game, the card deck gets divided among the players. The players should place all their cards facing downwards; this creates a stack for each of the players. The players should reveal their cards simultaneously. The player with the highest card wins the battle. The winning player takes the card that lost.

If the cards shown are of the same value, a little face-off or a mini-war happens. The players should then place their next cards face down and then another card face-up. The player with the highest card wins the war and takes the other player's card. When a player runs out of cards, he or she loses.

Our Project

The little application that you will create today is very basic, which is the best way to learn game logic. If you do not understand how certain games work in the background and the logic to make it work, programming any game is difficult. We'll make this a two-player game, for simplicity's sake.

Open Visual Studio and create a Console application in either VB.NET or C#.

In the Main method of your application, create the following variables:

C#

        int intUCard;
        int intCCard;
        int intUTot = 0;
        int intCTot = 0;
        int intSuit;
        int intCount = 0;

        string strUSuit;
        string strCSuit;
        string strUCard;
        string strCCard;
        string strResult = "";

VB.NET

      Dim intUCard As Integer
      Dim intCCard As Integer
      Dim intUTot As Integer = 0
      Dim intCTot As Integer = 0
      Dim intSuit As Integer
      Dim intCount As Integer = 0

      Dim strUSuit As String
      Dim strCSuit As String
      Dim strUCard As String
      Dim strCCard As String
      Dim strResult As String = ""

Here, I created objects to hold the User's and Computer's chosen cards and totals. The other variables include the chosen suit and result. Still in the Main method, add the last few variables.

C#

         string[] strCardNames = { "Ace", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "Jack", "Queen", "King" };
         string[] strCardSuits = { "Clubs", "Diamonds", "Hearts",
            "Spades" };

         string[] strDrawnCards = new string[52];

         int[] intDeck = new int[52];

VB.NET

      Dim strCardNames As String() = {"Ace", "2", "3", "4", "5", _
         "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
      Dim strCardSuits As String() = {"Clubs", "Diamonds", _
         "Hearts", "Spades"}

      Dim strDrawnCards As String() = New String(51) {}

      Dim intDeck As Integer() = New Integer(51) {}

The last few variables contain the card names, suit, a Deck object, and a variable to host the drawn cards. Add the following line of code inside your Main method:

C#

         Shuffle(ref intDeck);

VB.NET

      Shuffle(intDeck)

This line calls the procedure named Shuffle. Let's add it now.

C#

      private static void Shuffle(ref int[] Deck)
      {

         int c;

         Random rndCards = new Random();

         for (c = 0; c < 52; c++)
         {

            Deck[c] = rndCards.Next(1, 52);

         }

      }

VB.NET

   Private Sub Shuffle(ByRef Deck As Integer())

      Dim c As Integer

      Dim rndCards As Random = New Random()

      For c = 0 To 52 - 1

         Deck(c) = rndCards.[Next](1, 52)

      Next

   End Sub

Aptly named, the Shuffle sub procedure stores the cards inside the deck at randomly generated locations. Back in the Main method, add the next For loop underneath the call to the Shuffle procedure.

C#

         for (int x = 0; x < 26; x++)
         {
            intUCard = ChooseCard(intDeck, ref intCount, out
               intSuit);
            strUSuit = strCardSuits[intSuit];
            strUCard = strCardNames[intUCard];

            intCCard = ChooseCard(intDeck, ref intCount, out
               intSuit);
            strCSuit = strCardSuits[intSuit];
            strCCard = strCardNames[intCCard];

            if (intUCard > intCCard)
            {

               intUTot += 1;
               strResult = "User wins.";

            }

            else if (intUCard < intCCard)
            {

               intCTot += 1;
               strResult = "Computer wins.";

            }

            else if (intUCard == intCCard)
            {

               intUTot += 1;
               intCTot += 1;

               strResult = "Tie.";

            }

            Console.WriteLine("Deal #{0}", x + 1);

            Console.WriteLine("User: {0} of {1}", strUCard,
               strUSuit);
            Console.WriteLine("Computer: {0} of {1}", strCCard,
               strCSuit);
            Console.WriteLine("{0}", strResult);
            Console.WriteLine("User Score: {0}", intUTot);
            Console.WriteLine("Computer Score: {0}", intCTot);
            Console.ReadKey();

         }

VB.NET

      For x As Integer = 0 To 25

         intUCard = ChooseCard(intDeck, intCount, intSuit)
         strUSuit = strCardSuits(intSuit)
         strUCard = strCardNames(intUCard)

         intCCard = ChooseCard(intDeck, intCount, intSuit)
         strCSuit = strCardSuits(intSuit)
         strCCard = strCardNames(intCCard)

         If intUCard > intCCard Then

            intUTot += 1
            strResult = "User wins."

         ElseIf intUCard < intCCard Then

            intCTot += 1
            strResult = "Computer wins."

         ElseIf intUCard = intCCard Then

            intUTot += 1
            intCTot += 1

            strResult = "Tie."

         End If

         Console.WriteLine("Deal #{0}", x + 1)
         Console.WriteLine("User: {0} of {1}", strUCard, strUSuit)
         Console.WriteLine("Computer: {0} of {1}", strCCard, _
            strCSuit)
         Console.WriteLine("{0}", strResult)
         Console.WriteLine("User Score: {0}", intUTot)
         Console.WriteLine("Computer Score: {0}", intCTot)

         Console.ReadKey()

      Next

The loop loops 26 times, which is 52 divided by 2. Each player (you and the computer) has 26 cards. With the help of the ChooseCard method, a card gets chosen for each player and then the score gets calculated based on the parameters I mentioned earlier. The result of the game gets displayed after each hand. Add the ChooseCard method now.

C#

      private static int ChooseCard(int[] Deck, ref int count,
         out int Suit)
      {
         int Card;

         Card = Deck[count];

         count++;

         Suit = (Card - 1) % 4;

         return (Card - 1) / 4 + 1;

      }

VB.NET

   Private Function ChooseCard(ByVal Deck As Integer(), ByRef _
         count As Integer, <Out> ByRef Suit As Integer) _
         As Integer

      Dim Card As Integer

      Card = Deck(count)
      count += 1

      Suit = (Card - 1) Mod 4
      Return (Card - 1) / 4 + 1

   End Function

The ChooseCard method also technically generates a random number based on a calculation that the player will receive.

Running the application will result in what's shown in Figure 1.

Running the game
Figure 1: Running the game

Conclusion

Each card game has the same components; how you use and manipulate them depends on the logic that goes with the game. Be on the lookout for another game coming soon. Until then, happy coding!

Home
Mobile Site | Full Site