Need a help.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
silent
Posts: 44
Joined: 28 Oct 2011 14:40

Need a help.

#1 Post by silent » 30 Oct 2011 12:27

Hi,i wanna make a batch with asks and player has to answer,but i want to make them that every run of program they change,like a list of asks and its random,choosen from list.Does anyone know how to make something like this ? IF yes,please give me code.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need a help.

#2 Post by aGerman » 30 Oct 2011 19:58

What kind of list? Is it saved in another file? What should happen with the answers? What's your code so far?
You should give some more information.

Regards
aGerman

silent
Posts: 44
Joined: 28 Oct 2011 14:40

Re: Need a help.

#3 Post by silent » 31 Oct 2011 04:39

List can be located in other file ,.txt for example.It should look like this :
"echo 1.How old is Michael Jordan"
"echo a) 47"
"echo b) 38"
"echo c) 51"

and player needs to write "a",thats the true answer,but i want just a code for something that will randomly choose any text from a list. like %random% but it needs to choose some random text that is already wrote by me.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need a help.

#4 Post by aGerman » 31 Oct 2011 09:54

There are several possibilities. One of them:
Write a list.txt where the data is separated by semi colons. The first part is the right answer, the second part is the question, the next parts are the possible answers. E.g.:
list.txt

Code: Select all

a;How old is Michael Jordan?;a) 47;b) 38;c) 51
b;What player is Michael Jordan?;a) football;b) basketball


The following batch code is suitable to process that list:
game.bat

Code: Select all

@echo off
:: setlocal enabledelayedexpansion
set /a colLines=0, counter=0, right=0, false=0

REM *** number of lines in list.txt
for /f "tokens=2 delims=:" %%i in ('find /c /v "" "list.txt"') do set /a n=%%i

:loop
REM *** calculate a random line number
set /a rdm=%random% %% n + 1
REM *** check whether the line number was calculated before
echo %rdm%|findstr /x "%colLines%" >nul &&goto loop
REM *** append the line number to the collection of calculated numbers
set "colLines=%colLines% %rdm%"
REM *** get the line
<"list.txt" (
  for /l %%i in (1,1,%rdm%) do set /p "line="
)
REM *** split the line and display it
set "rightAnswer="
:loop2
for /f "delims=;" %%i in ("%line%") do (
  if not defined rightAnswer (
    set "rightAnswer=%%i"
  ) else (
    echo %%i
  )
  if "%line:*;=%"=="%line%" goto forward
  set "line=%line:*;=%"
  goto loop2
  )
)
:forward
REM *** choose the answer
echo(
set "answer="
set /p "answer=Choose the right answer: "
REM *** check whether the answer was right or false
if /i "%answer%"=="%rightAnswer%" (
  echo RIGHT
  set /a right+=1, counter+=1
) else (
  echo FALSE
  set /a false+=1, counter+=1
)
echo __________________________________________
REM *** check whether all questions are already processed
if %counter% lss %n% goto loop
REM *** display the result
echo    ~~~~~ Result ~~~~~
echo Number of questions     %n%
echo Number of right answers %right%
echo Number of false answers %false%
echo(
pause

Regards
aGerman

Post Reply