-4

I am making a basic word scrambling and guessing game. To make sure the same word doesn't appear again (example words used) I need to delete the selected word. I have looked all over for solutions however none have worked. Any help is appreciated!

//word bank
  string[] wordBank = {
  "iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
  };

  //select random word
  Random wordRand = new Random();
  int index = wordRand.Next(wordBank.Length);
  string wordSelect = wordBank[index];
            
  //delete word so can't be used again 
Toxxii
  • 3
  • 3
  • 3
    Use a List instead, here is a huge tutorial that tells you everything about them https://www.tutorialsteacher.com/csharp/csharp-list – TheGeneral Oct 12 '20 at 07:58
  • You could say there are a few duplicates of this question. – Jamiec Oct 12 '20 at 08:00
  • https://stackoverflow.com/questions/8032394/how-to-delete-a-chosen-element-in-array/8032446 – Jamiec Oct 12 '20 at 08:00
  • https://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array – Jamiec Oct 12 '20 at 08:01
  • 1
    I'm assuming this is a basic assignment, which means you might not have been taught about `List` yet, so you might wish to avoid using it. Consider instead a strategy where you: keep a count of the numberOfWords (e.g. starts at 10), pick a random index between 0 and `numberOfWords-1` (pick an index between 0 and 9) - suppose your random index is 4, pull the word out at index 4 ("calculator"), copy the word at index `numberOfWords-1` over the top of it (so 4 is now "www"), and reduce the numberOfWords by 1, return the word pulled out ("calculator"). Next time random will choose from 0-8.. – Caius Jard Oct 12 '20 at 08:19

1 Answers1

0
List<string> wordBank = new List<string>  {
  "iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
  };

//select random word
Random wordRand = new Random();
int index = wordRand.Next(wordBank.Count);
string wordSelect = wordBank[index];

wordBank.RemoveAt(index);
//or
wordBank.Remove(wordSelect);
dovid
  • 6,354
  • 3
  • 33
  • 73
  • 1
    Please avoid code only answers, especially when helping someone in Programming 101; they'll benefit more from some tutoring, than they will from you just doing their homework for them – Caius Jard Oct 12 '20 at 08:10