samedi 29 décembre 2018

filling an array with random numbers in an instantiated class

I'm creating a bingo game where the users choose how many players will be playing and how many tickets every player will be having. I have two classes, one for players and another one for bingo tickets. When the users have written the amount of players and tickets i instantiate both classes. The BingoTicket class have a multi-dimensional array which gets initialised to [3,5] automatically. The problem that I have is that I want to fill that array with random numbers as the classes gets instantiated but as the classes aren't instantiated at the moment of writing the code I can't access the variables inside the class and I can't create that method inside that class either. Which would be the way to do it?

I tried creating a for-loop inside this BingoTicket's class and creating the method in the Main class but i can't do it either way.

This is the code inside Main class:

rightAnswer = false;
        Console.WriteLine("Please write how many tickets every 
player is going to have. (1 or 2 tickets per person)");
        while (rightAnswer == false)
        {
            try
            {
                numberOfTickets = int.Parse(Console.ReadLine());
            }
            catch (Exception)
            {
                Console.WriteLine("Please write a valid number.");
                continue;
            }
            if(numberOfTickets > 0 && numberOfTickets <= 2)
            {
                rightAnswer = true;
            }
            else
            {
                Console.WriteLine("Please write a valid number of tickets from 1 to 2.");
            }
        }
        // Instantiating as many tickets as the user wants.
        List<BingoTicket> bingoTickets = new List<BingoTicket>();

        for(int i = 0; i < numberOfTickets; i++)
        {
            BingoTicket bingoticket = new BingoTicket(i, (new int[3, 
            5]));

            bingoTickets.Add(bingoticket);
        }
    }

This is the class BingoTicket:

public class BingoTicket
    {
        private int ticketId = 0;
        private int[,] ticketNumbers = new int[10, 3];

        public int TicketId
        {
            get { return ticketId; }
            set { ticketId = value; }
        }

        public int[,] TicketNumbers
        {
            get { return ticketNumbers; }
            set { ticketNumbers = value; }
        }

        public BingoTicket(int ticketId, int[,] ticketNumbers)
        {
            this.ticketId = TicketId;
            this.ticketNumbers = TicketNumbers;
        }
    }

The expected result will be that when i instantiate as many tickets as the user wants, the int[,] ticketNumbers array gets automatically filled with random numbers.




Aucun commentaire:

Enregistrer un commentaire