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. I tried to create unique random numbers for each bingo ticket but i get the same numbers in each ticket.
I have created a variable ticketId for the Player and BingoTicket classes and my intention was to connect each ticket to a player, but I can't find the way to do it either. The ticket count doesn't seem to work and every ticket gets the same number
I tried creating a method inside the BingoTicket class that automatically creates random numbers for each array but I get the same numbers for every ticket. And for connecting the two classes I don't know where to start.
public static void Main(string[] args)
{
int numberOfPlayers = 0;
int numberOfTickets = 0;
bool rightAnswer = false;
Console.WriteLine("Please write how many players are going
to play. (1-4 players)");
while (rightAnswer == false)
{
try
{
numberOfPlayers = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Please write a valid number.");
continue;
}
if(numberOfPlayers > 0 && numberOfPlayers <= 4)
{
rightAnswer = true;
}
else
{
Console.WriteLine("Please write a valid number of
players from 1 to 4.");
}
}
// Instantiating as many players as the user wants.
List<Player> players = new List<Player>();
for (int i = 0; i <= numberOfPlayers; i++)
{
Player player = new Player(i, "Player" + i++);
players.Add(player);
}
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>();
numberOfTickets *= numberOfPlayers;
for(int i = 0; i < numberOfTickets; i++)
{
BingoTicket bingoticket = new BingoTicket(i, (new int[3,
5]));
bingoTickets.Add(bingoticket);
}
}
// == BINGOTICKET ==
public class BingoTicket
{
Random rnd = new Random();
void PrintArray(int[,] array)
{
var rowCount = array.GetLength(0);
var colCount = array.GetLength(1);
for (int row = 0; row < rowCount; row++)
{
for (int col = 0; col < colCount; col++)
Console.Write(String.Format("{0}\t", array[row,
col]));
Console.WriteLine();
}
}
private int ticketId = 0;
private int[,] ticketNumbers = new int[3, 5];
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;
for (int i = 0; i <= 4; i++)
{
ticketNumbers[0, i] = rnd.Next(1, 100);
}
for (int i = 0; i <= 4; i++)
{
ticketNumbers[1, i] = rnd.Next(1, 100);
}
for (int i = 0; i <= 4; i++)
{
ticketNumbers[2, i] = rnd.Next(1, 100);
}
Console.WriteLine("Player ticket number: " + TicketId);
PrintArray(ticketNumbers);
}
}
// == PLAYER ==
public class Player
{
private int ticketId;
private string name;
public int TicketId
{
get { return ticketId; }
set { ticketId = value; }
}
public string Name
{
get { return Name; }
set { Name = value; }
}
public Player(int ticketId, string name)
{
this.ticketId = ticketId;
this.name = name;
}
}
}
Aucun commentaire:
Enregistrer un commentaire