Hello all, I've been writing a multiple choice question version of the game Hangman. I've got it done except for stopping already asked questions from appearing again. I've got the 'asked' question numbers (the questions and answers are read in from a file into various arrays) added to a vector list as they are asked. My problem: I'm having difficulty setting up a search algorithm for the vector list to compare the randomly generated question numbers to the values in the vector. Here's a small snippet of my program where I'm having difficulty. I'm getting myself more confused by the minute trying to sort this out.

QuestionInfo[] questionObj = new QuestionInfo[10];
		
		for(int x = 0; x < 10; x++)
			questionObj[x] = new QuestionInfo(question[x], ansA[x], ansB[x], ansC[x], ansD[x], correctAns[x]);
	
		questionNum = (int)(Math.random() * 10);
		usedList.addElement(questionNum);
		QuestionInfo curQues = new QuestionInfo(questionObj[questionNum]);
		
		displayQuestion(questionObj, curQues, questionNum);

Recommended Answers

All 4 Replies

OK, here's what I've attempted so far. It is still not working and I'm not sure I have the search algorithm correct for vector lists. Can anyone point me in the right direction? If I need to post my code I can, it's just a big program. Thanks

QuestionInfo[] questionObj = new QuestionInfo[10];
		
		for(int x = 0; x < 10; x++)
			questionObj[x] = new QuestionInfo(question[x], ansA[x], ansB[x], ansC[x], ansD[x], correctAns[x]);
	   
		questionNum = (int)(Math.random() * 10);  
		while (isUsed)
		{
		searchResult = seqSearch(usedList, questionNum);
		if (searchResult == -1)
		{
		    isUsed = false;
      }//end if
		else
		    questionNum = (int)(Math.random() * 10);
		usedList.addElement(questionNum);
		}//end while
		QuestionInfo curQues = new QuestionInfo(questionObj[questionNum]);
		
		displayQuestion(questionObj, curQues, questionNum);
public int seqSearch(Vector<Integer> usedList, int questionNum)
	{
		 boolean found = false;
		 
		 for (Integer num : usedList)
		     if (num == questionNum)
			  {
			      found = true;
					break;
			  }
		 if (found)
		     return 1;
			  else
			     return -1;
	}

OK, here's what I've attempted so far. It is still not working and I'm not sure I have the search algorithm correct for vector lists. Can anyone point me in the right direction? If I need to post my code I can, it's just a big program. Thanks

QuestionInfo[] questionObj = new QuestionInfo[10];
		
		for(int x = 0; x < 10; x++)
			questionObj[x] = new QuestionInfo(question[x], ansA[x], ansB[x], ansC[x], ansD[x], correctAns[x]);
	   
		questionNum = (int)(Math.random() * 10);  
		while (isUsed)
		{
		searchResult = seqSearch(usedList, questionNum);
		if (searchResult == -1)
		{
		    isUsed = false;
      }//end if
		else
		    questionNum = (int)(Math.random() * 10);
		usedList.addElement(questionNum);
		}//end while
		QuestionInfo curQues = new QuestionInfo(questionObj[questionNum]);
		
		displayQuestion(questionObj, curQues, questionNum);
public int seqSearch(Vector<Integer> usedList, int questionNum)
	{
		 boolean found = false;
		 
		 for (Integer num : usedList)
		     if (num == questionNum)
			  {
			      found = true;
					break;
			  }
		 if (found)
		     return 1;
			  else
			     return -1;
	}

I don't see you reinitializing isUsed as true after it is set as false anywhere, so it looks to me like this while loop won't be gone through after the first time isUsed is set false. You probably want to initialize it to true every time you go through the for loop, before you get to the while loop. That way it goes through the while loop at least once for each value of x.

Also, there is a function called contains in the vector class, so to test whether a number is already in the vector, you can do this:

isUsed = usedList.contains(questionNum);

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#contains(java.lang.Object)

I need to implement a search algorithm, and it didn't seem to make a difference re-initiallizing unUsed to true outside of the loop, it does make sense however. Any other thoughts? would my entire program be more helpful?

I need to implement a search algorithm, and it didn't seem to make a difference re-initiallizing unUsed to true outside of the loop, it does make sense however. Any other thoughts? would my entire program be more helpful?

if (searchResult == -1)
{
    isUsed = false;
}//end if
else
    questionNum = (int)(Math.random() * 10);

usedList.addElement(questionNum);

Looks to me like line 8 is getting executed regardless of whether the if statement is true or not. You never check whether the number generated in line 6 is in the vector before inserting it into the vector, so you have the potential for duplicates. You may want to put line 8 inside your "if" statement block before line 4 so it is only executed when the if statement is true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.