-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 
5
  • 3
    Use a List instead, here is a huge tutorial that tells you everything about them tutorialsteacher.com/csharp/csharp-list Commented Oct 12, 2020 at 7:58
  • You could say there are a few duplicates of this question. Commented Oct 12, 2020 at 8:00
  • stackoverflow.com/questions/8032394/… Commented Oct 12, 2020 at 8:00
  • stackoverflow.com/questions/4870188/… Commented Oct 12, 2020 at 8:01
  • 1
    I'm assuming this is a basic assignment, which means you might not have been taught about List<T> 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.. Commented Oct 12, 2020 at 8:19

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.