'Is there a way that I can create a method to deal a card with the set up I have right now? I am trying to play a game called War

using System; using System.Linq;

namespace CodeforWar { class program { static void Main(string[] args) { Path(); const int rows = 4; const int cols = 13; int[,] deck = new int[rows, cols]; DeckCard(deck); DisplayDeck(deck); Console.WriteLine(); ShuffleDeck(deck); DisplayDeck(deck); Console.ReadKey();

    }

    public static void BegMenu()
    {
        Console.Clear();
        System.Console.WriteLine("Welcome!");
        Console.WriteLine("1: Play War");
        System.Console.WriteLine("2: Play Lazer");
        System.Console.WriteLine("3: See Scoreboard");
        System.Console.WriteLine("4: Exit Game");
    }
    static void Path()
    {
        BegMenu();
        string userInput = Console.ReadLine();
        
        while(userInput != "4")
        {
            if(userInput == "1")
            {
                War();
            }
            if(userInput == "2")
            {
                Lazers();
            }
            Console.Clear();
        }
    }
    static void War()
    { 
        Console.ReadLine();
        System.Console.WriteLine("Welcome to War");
        Console.WriteLine("Press a key to enter the game");
        Console.ReadKey();
    }
    public void DisplayDeck()
    {
        DisplayDeck();
    }

    static void Lazers()
    {
        Console.ReadLine();
        System.Console.WriteLine("Welcome to Lazers!");
        Console.WriteLine("Press a key to enter the game");
        Console.ReadKey();
    }

    static void DeckCard(int[,] arr)
    {
        for(int row = 0; row < 4; ++row)
            for(int col = 0; col
                < 13; ++col)
                    arr[row,
                        col] = 0;

    }
    static void DisplayDeck(int[,] arr)
    {
        for(int row = 0;  row < 4; ++row)
        {
            for(int col = 0; col < 13; ++col)
                Console.Write(arr[row, col] + " ");
            Console.WriteLine();
        }
    }
    static void ShuffleDeck(int [,] arr)
    {
        int row, col;
        for (int card = 1; card <= 52; ++card)
        {
            Random rand = new Random();
            row = rand.Next(4);
            col = rand.Next(13);
            if (arr[row, col] == 0)
                arr[row, col] = card;
            else
            {
                while (arr[row, col] != 0)
                {
                    row = rand.Next(4);
                    col = rand.Next(13);
                }
                arr[row, col] = card;
            }

        }
    }


    
        
    
    

    






}



    





    




































 

} I am trying to figure out how to give the user a card and then give them the option to guess high or lower. I haven't tried making a card deck before and I'm wondering with the current set up I have right now that it could work.



Solution 1:[1]

One important thing you need to consider is what happens to the card once it's been drawn from the deck? The current implementation, a two-dimensional array, looks like it will always contain all 52 cards in the deck, even after a number of cards have been drawn, because there's no mechanism for indicating that a particular card is no longer in the deck once it's been drawn. This is a bit like inserting the card back into the deck at a random position once it's been drawn and used.

Instead you might want to look at a generic type such as Queue. This will allow you to Dequeue a card from the top of the deck, and then it's no longer in the deck. At an appropriate point in the game you can put cards back onto the bottom of the deck by Queueing them.

Also, I'd suggest that the structure of your deck of cards doesn't really need to reflect the fact that a deck of cards typically consists of 4 suits with 13 cards each, you only need to take that fact into account when initialising the deck, for example:

var deck = new Queue<Card>();
foreach (var suit in possibleSuits) // heart, diamond, spade, club
{
    foreach (var val in possibleValues) // ace, 2-10, jack, queen, king
    {
        deck.Enqueue(new Card(suit, val));
    }
}

Going a bit further, you need a way of shuffling the deck, which Queue<T> doesn't give you, so at this point you might consider implementing your own Deck class, such as:

public class Deck
{
    private Queue<Card> cards = new Queue<Card>();

    public Deck()
    {
        // fill the queue as per previous snippet
    }

    public Card Draw()
    {
        return cards.Dequeue();
    }

    public void ReturnToDeck(Card card)
    {
        cards.Enqueue(card);
    }

    public void Shuffle()
    {
        // I'll let you think about how to do this ;-)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1