lundi 25 novembre 2019

Very simple card drawing game (Eliminating duplicates + resetting arrays)

a programming beginner here! I was challenging myself to create a drawing game(card drawing or in this case number drawing) in order to sharpen my coding skills, but it seemed a little too challenging for me as I am just a beginner.

The program I'm making is actually very simple. You have a deck of 15 cards(or in this case numbers) and the program asks you to draw 5 cards. While I am able to do this, I noticed there would sometimes be duplicates of a card you already drew, and as for any normal deck there would only be 1 copy of each card.

As for my code it's fairly short:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CardGame
{
    class Program
    {
        static void Main(string[] args)
        {
            string ans = " ";
            int[] Deck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; // the deck

            Console.WriteLine("Draw 5 cards? Y/N");


            while (!ans.Equals("N"))
            {

                ans = Console.ReadLine();

                if (ans.Equals("Y"))
                {
                    Draw(Deck);
                }

                else
                {
                    Console.WriteLine("Closing program");
                }
            }
        }

        static void Draw(int[] Deck) //Draw function
        {
            Random rand = new Random(); // randomizer
            int turncount = 1;

            while (turncount <= 5)
            {
                int pick = rand.Next(Deck.Count()); // random pick for each iteration
                Console.WriteLine(Deck[pick]);
                turncount++;
            }


        }


    }
}

So my expected output would be something like: 5,8,1,4,12 | 2,3,9,11,7 | 10,6,15,13,14

But what I did was spitting out something like: 1,5,5,3,7 | 15,12,1,15,3| 2,3,1,5,6

and I also an added challenge I wanted the deck to reset once you finish your 3 draws. So after 3 draws you can draw all of the cards again. But I have no idea on how i could do that at all.

As for the various methods that I tried, I tried implementing the Distinct function, so i could eliminate duplicates but, it was not really the result I was looking for as it would call my the elements in order like: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.

That about sums up my question. Hope you all could help this beginner and enlighten me!




Aucun commentaire:

Enregistrer un commentaire