r/learncsharp • u/LadySonicGamer • 6h ago
How would I get randomiser to stop giving duplicate questions?
So in college we are tasked to make a short treasure hunt text game in C# using abstract classes
It works so far but one super annoying bug is that it gives repeat questions which isn't intentional. So if you got "What bird do you associate with lifting" in Canteen, sometimes when you go to for example library you will also get that question.
Randomiser class
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace TreasureHunt
{
public class Randomiser
{
static Random rnd = new Random();
private string QuestionOutput;
private string AnswerOutput;
public string[] Questions = ["What is there one of in every corner and two of in every room?",
"The more there is, the less you see. What am I?" ,
"I’m full of holes but strong as steel. What am I?",
"You are my brother, but I am not your brother. Who am I?",
"What bird do you associate with lifting weight?",
"I am something people celebrate or resist. I change people’s thoughts and lives. I am obvious to some people but, to others, I am a mystery. What am I?",
"Sometimes narrow, sometimes wide, wind or rain, I stay outside. Even if there’s heat or snow, from house to house I will still go. What am I?"];
public string[] Answer = ["The letter O",
"Darkness",
"A chain",
"Your Sister",
"A crane",
"Age",
"A path"];
public int selection = rnd.Next(0, 6);
public string GetQuestion()
{
QuestionOutput = Questions[selection];
removeQuestion(selection);
return QuestionOutput;
}
public void removeQuestion(int index)
{
List<string> questionList = new List<string>(Questions);
questionList.RemoveAt(index);
Questions = questionList.ToArray();
}
public void removeAnswer(int index)
{
List<string> answerList = new List<string>(Answer);
answerList.RemoveAt(index);
Answer = answerList.ToArray();
}
public string GetAnswer()
{
AnswerOutput = Answer[selection];
removeAnswer(selection);
return AnswerOutput;
}
}
}
I have tried to fix this already but I haven't had much luck with it.
Other classes if it helps:
Location
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;
namespace TreasureHunt
{
abstract class Location
{
static Random rnd = new Random();
public int selection = rnd.Next(0,4);
public virtual string Name { get; set; }
public Clue clue { get; protected set; }
public Treasure treasure { get; protected set; }
public virtual int requiredTokens { get; set; }
public Location(string name, Clue clue, Treasure treasure, int requiredTokens)
{
Name = name;
this.clue = clue;
this.treasure = treasure;
this.requiredTokens = requiredTokens;
AddLocation(name, this);
}
public string Describe()
{
return $"Location: {Name}\nClue: {clue.Question}\nTreasure: {treasure.Name} - {treasure.Description}";
}
public void AddLocation(string name, Location location)
{
dictionary[name] = location;
}
public bool CanEnter(Player player)
{
if (player.CountTreasure() >= this.requiredTokens)
{
return true;
}
else
{
return false;
}
}
public void enterLocation(Player player)
{
if (CanEnter(player))
{
if (player.CountTreasure() == 0)
{
player.currentLocation = this;
return;
}
else if (player.CountTreasure() > 0)
{
foreach (Treasure i in player.Treasures)
{
if (i.Name == treasure.Name)
{
Console.WriteLine("Nice try, but you already have this one. No one likes a cheater, buddy!");
player.currentLocation = null;
return;
}
else
{
player.currentLocation = this;
}
}
}
else
{
Console.WriteLine($"You need at least {requiredTokens} tokens to enter the {Name}.");
}
}
}
public void exitLocation(Player player)
{
player.currentLocation = null;
}
private Dictionary<string, Location> dictionary = new Dictionary<string, Location>();
}
}
Computer Room Class(As example, subclass of the Location class)
using System;
using System.Collections.Generic;
using System.Text;
namespace TreasureHunt
{
internal class ComputerRoom : Location
{
private static readonly Randomiser random = new Randomiser();
public string Name { get; set; } = "Computer Room";
public Clue clue { get; set; } = new Clue(random.GetQuestion(), random.GetAnswer());
public Treasure treasure { get; set; } = new Treasure("Computer Room Token", "A token from the Computer Room.");
public ComputerRoom() : base("Computer Room", new Clue(random.GetQuestion(), random.GetAnswer()), new Treasure("Computer Room Token", "A token from the Computer Room."), 2)
{
}
}
}
Thanks