Solved Shuffle through questions for trivia game

Hewlett-packard / Pavilion g6-2000 15.6" bl...
January 25, 2016 at 20:33:54
Specs: Windows 10, 4gb
I'm making a trivia game for me and a couple of friends, and I need a way to constantly shuffle the questions randomly, without repeating the question and stopping when all questions have been asked.

See More: Shuffle through questions for trivia game

Report •


#1
January 25, 2016 at 23:19:46
Using a blunderbuss won't help.

But it may try the patience of those able to help.

=====================

M2 Get custom script or take private lessons


Report •

#2
January 26, 2016 at 07:54:38
✔ Best Answer
Store the questions line-by-line in a file called "questions.txt" in the same folder as the script.

This worked in my tests, however it would leave out a question or two sometimes for some weird reason, I couldn't quite figure out why.

@Echo off
setlocal enabledelayedexpansion
cd "%~dp0"
if exist ingame_questions.txt del /q ingame_questions.txt > nul
copy questions.txt ingame_questions.txt > nul
set cnt=1
:loop
set lines=0
for /f "tokens=*" %%A in (ingame_questions.txt) do (
	set /a lines=!lines!+1
)
if !lines!==0 (
	echo No more questions.
	pause > nul
	exit 
)
set /a _lines=%lines%
set /a randomQuestion=%random% %% %_lines%
if %randomQuestion% LSS 1 (
	for /f "tokens=*" %%A in (ingame_questions.txt) do (
		echo Question %cnt%: %%~A
		pause > nul
		findstr /b /v /c:"%%~A" ingame_questions.txt >> _ingame_questions.txt
		del /q ingame_questions.txt > nul
		ren _ingame_questions.txt ingame_questions.txt
		goto break
	)
)
for /f "skip=%randomQuestion% tokens=*" %%A in (ingame_questions.txt) do (
	echo Question %cnt%: %%~A
	pause > nul
	findstr /b /v /c:"%%~A" ingame_questions.txt >> _ingame_questions.txt
	del /q ingame_questions.txt > nul
	ren _ingame_questions.txt ingame_questions.txt
	goto break
)
:break
set /a cnt=!cnt!+1
goto loop

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Report •
Related Solutions


Ask Question