C# Class players/Player hand User input. RRS feed

  • Question

  • //I am working on a card game made of 32 cards. The cards values are 10(highest),9,8,7,king,queen, jack(lowest).

    //Its a card game of four players. there must be five classes including players and tournament.

    //The first class I made was card class:public class Cards { private string face; private string suit; public Cards(string cardFace, string cardSuit) { face = cardFace; suit = cardSuit; } public override string ToString() { return face + " van " + suit; } }

    //Then I have defined those cards inside another class which I called it Deck,

    //but what so ever there are no values attached to the cards,

    //inside the deck class there is also the shuffling activity,

    //which should happen after every round:

    public class Deck { private Cards[] deck; private int currentCard; private const int NUMBER_OF_CARDS = 32; private Random ranNum; public Deck() { string[] faces = { "Jack", "Queen", "King", "Ace", "seven", "eight", "Nine", "Ten" }; string[] suits = { "Diamonds", "Hearts", "Spades", "Clubs" }; deck = new Cards[NUMBER_OF_CARDS]; currentCard = 0; ranNum = new Random(); for (int count = 0; count < deck.Length; count++) deck[count] = new Cards(faces[count % 8], suits[count / 8]); } public void shuffel() { int currentCard = 0; for (int first = 0; first < deck.Length; first++) { int second = ranNum.Next(NUMBER_OF_CARDS); Cards temp = deck[first]; deck[first] = deck[second]; deck[second] = temp; } } public Cards DealCards() { if (currentCard < deck.Length) return deck[currentCard++]; else return null; } }

    //and here is the program class:public class Program { public static void Main(string[] args) { Deck deck1 = new Deck(); deck1.shuffel(); for (int i = 0; i < 16; i++) { Console.WriteLine(deck1.DealCards()); if ((i + 1) % 4 == 0) Console.WriteLine(); } Console.ReadLine(); } }

    //I am having troubles how to make it as user input the players name which are 4,

    //the dealt cards should go as each player will have four cards,

    //I could make with this code four hands, each hand has 4 cards.

    //but not assigned to a player's name,

    //as well there should be a user input for which card the user(as player1 or player2) wants to play,

    //all four players can be controlled by one user no problem. Any valuable help would highly be appreciated.

    Sunday, March 11, 2018 11:25 AM


All replies

  • You will have better luck asking in one of the forums that discusses the C# programming language.
    Sunday, March 11, 2018 5:14 PM
  • You have a class for Deck and Cards and that is a good start. Search the internet for samples of card classes. There are very many available. You probably should use an enum for the face and the suite.

    Sam Hobbs

    Sunday, March 11, 2018 10:01 PM