0

I have this card war game; 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 1 player has all of the cards; however, I haven’t been able to finish it correctly, it crashes every time that I hit enter after the statement "lets flipping cards," and I haven’t been able to make the players to get the 4th card because when using a third variable r3, it crashes likewise.

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>();
            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 });
                }
            }
            //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);
              

                // pick a random card for player 2
                int r2;
                r2 = random.Next(0, DeckOfCards.Count);
                Card c2 = DeckOfCards[r2];
                deckOfPlayer2.Add(c2);
               

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


                // now compare the cards

                if (c1.Value == c2.Value )
                {

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

                     int r3;
                    r3 = random.Next(0, DeckOfCards.Count);
                    Card c3 = DeckOfCards[3];
                    deckOfPlayer1.Add(c3);


                    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");
                     
                    }
                    else
                    {
                        Console.WriteLine("Player2 wins");
                        deckOfPlayer2.Add(c1);
                        deckOfPlayer1.Remove(c1);
                    }

                }

                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));

        }
    }
}
zmike
  • 1,090
  • 10
  • 24
Diasalva
  • 3
  • 5
  • 3
    "It crashes" is not a problem statement we can assist with. Be specific. – Ian Kemp Apr 22 '21 at 20:28
  • In addition to @IanKemp You might want to correct your code or at least explain the process. Is it normal that player can both take out ace of spade every single draw ? – Franck Apr 22 '21 at 20:33
  • 4
    You create, but never seem to populate `DeckOfCards`. – 500 - Internal Server Error Apr 22 '21 at 20:38
  • It crashes by making a circle with a red cross in front of Card c1 = DeckOfCards[r1]; and stating "Exception unhandled, SystemArgmentOutOfRangeException :Index was out of range. Must be non-negative and less than the size of the collection". – Diasalva Apr 22 '21 at 20:52
  • 1
    You seem to be declaring two variables that do the same thing: `DeckOfCards` and `cards`. Remove `cards` and use `DeckOfCards` in the nested `for-loop`. – zmike Apr 22 '21 at 20:52
  • 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 your deck are played face down and the 4th card dealt face up, whoemever has the highest card wins. The game is played until 1 player has all of the cards. – Diasalva Apr 22 '21 at 20:53
  • No, Mr. Franck, is not normal that player can both take out ace of spade every single draw. – Diasalva Apr 22 '21 at 20:56
  • zmike and 500 are onto it. You've got two lists of cards and are using the wrong list. – pcdev Apr 22 '21 at 23:32
  • I removed the variable cards. It worked. Thank you. – Diasalva Apr 23 '21 at 01:39

1 Answers1

0

First, please learn how to use interactive debugging. If all you know is "it crashed", then you're going to struggle to find and eliminate bugs in your code. Here's one tutorial you can start with.

In this instance, the problem is likely due to your use of two lists:

List<Card> DeckOfCards = new List<Card>();         // One here
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>();                      // And one here

You are populating the second one with cards.Add, but you are trying to access the first one everywhere else, for example with these lines:

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

Remove one of the lists, replace it with the other instance, and your code will have a better chance of running (disclaimer: I haven't compiled it to check for other issues)

pcdev
  • 2,852
  • 2
  • 23
  • 39