views:

268

answers:

1

Hello,

I am developing a card game for batch. [will give a link when done]

Basiclly what i want to do is 'set' '%random%' to '%numbs%.

set %numbs%=%random%

And then i want the user to type one value from the %random% numbers displayed

echo %numbs%
Set nuchoo=
set /p nuchoo=Number:

What my question now is about how to check if the value typed in by the user is in the %numbs% value. If it is,

GOTO :validnucho

if not,

GOTO :unvalidnucho

When it goes to :validnucho, it should delete that number from the %numbs% var. It does matter obviously if the %numbs% var contains 2x the number, so then it should delete only one of the two. The rest i know.

+1  A: 

This should get you started

Save the following as Instr.cmd

@echo off 

rem These two lines allow tracing to be turned on by entering 
rem set TRACE=ECHO on the cmd line
  if not defined TRACE set TRACE=rem
  %TRACE% on 

rem Allow use of delayed variable expansion using !  
  setlocal ENABLEDELAYEDEXPANSION 

  if  /I [%1]==[Test] ( 
    call :Test
    goto :eof
    )

:Instr 
  set mTestStr=%1

rem  set /a does arithmatic
  set /a TestIdx=0
  set Found=N
  set CopiedStr=

  :startLoop

rem extract character at index TestIndx to test   
    set TestedChar=!mTestStr:~%TestIdx%,1!
rem echo TestedChar if trace is on 
    %TRACE% %TestedChar%

    If [!TestedChar!]==[] (
      goto :exitInstr
    )

    If [%TestedChar%]==[%2] (
rem   Testedchar matchs search character, 
rem   so we have found the char we were looking for    
      if %Found%==Y (
        set CopiedStr=%CopiedStr%%TestedChar%
      ) 
      set Found=Y
    ) else (
      set CopiedStr=%CopiedStr%%TestedChar%
    ) 



rem Increment Index       
    SET /a TestIdx=TestIdx+1

  goto :startLoop

:exitInstr

rem endlocal but keep CopiedStr and Found
  endlocal & set CopiedStr=%CopiedStr% & set Found=%Found%

goto :eof

:Test    


  call :Instr abcde c
  call :Verify Y %Found% "call :Instr abcde c - Found"
  call :Verify abde, %CopiedStr% "call :Instr abcde c - CopiedStr"

  call :Instr abcde x
  call :Verify N %Found% "call :Instr abcde x - Found"
  call :Verify abcde, %CopiedStr% "call :Instr abcde x - CopiedStr"

  call :Instr abcdec c
  call :Verify Y %Found% "call :Instr abcdec c - Found"
  call :Verify abdec, %CopiedStr% "call :Instr abcdec c - CopiedStr"


    echo Tests Done

goto :eof   

:Verify
  set Expected=%1
  set Expected=%Expected:"=%

  set Actual=%2
  set Actual=%Actual:"=%

  set Msg=%3
  set Msg=%msg:"=%

  If not "%Expected%"=="%Actual%"   (
    echo Failed: Expected "%Expected%" but was "%Actual%"  - %Msg%
  ) else ( 
    echo OK Expected "%Expected%" - %Msg%
  )

goto :eof
Andy Morris
Can you explain it a little bit? especially those calls and ifs.
YourComputerHelpZ
and like everything of :startloop
YourComputerHelpZ
Hope the comments I've added inline help
Andy Morris
it does get me started, thanks, but how can i delete that number from the randoms?
YourComputerHelpZ
Don't think delete, think build a new string and miss out the chars you don't want.
Andy Morris
Where can i set the %random%?
YourComputerHelpZ
I've updated the code above to show. If add save it as Instr.cmd and add the line call Instr %Numbs% %NuChoo% you get the results in %Found% and %CopiedStr%
Andy Morris