user3093536
user3093536

Reputation: 95

Batch Difficulty Using Variables

The values a1 through a9 are equal to some number that is modulus 13 (0-12) and the values 0 through 12 are then equal to the card range 2-A. I am trying to call A from 0 from a3 or the 7 from 8 from a1 with this code:

for %%R in (1 3) do (for %%S in (!a%%R!) do if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
if !b%%R!==2 echo P1 has a !%%S! pair)
for %%T in (2 4) do (for %%U in (!a%%T!) do if !b%%T!==4 echo CP has a !%%U! 4 of a kind
if !b%%T!==3 echo CP has a !%%U! 3 of a kind
if !b%%T!==2 echo CP has a !%%U! pair)

But the output is simply either "P1 has a pair" or "CP has a 3 of a kind" without the value that I need. b1 through b4 are unimportant, they just signify the number of repeated cards there are to identify pairs and such.

How do I receive the output that I desire and also, how can I simplify this:

set 0=A
set 1=2
set 2=3
set 3=4
set 4=5
set 5=6
set 6=7
set 7=8
set 8=9
set 9=10
set 10=J
set 11=Q
set 12=K

Upvotes: 0

Views: 57

Answers (3)

Aacini
Aacini

Reputation: 67196

Your code have a couple problems. In first place, the second for command is not appropriately nested because it apply just to the first if command. This way, in the second and third if commands the %%S replaceable parameter is not defined. The way you write your code makes difficult to isolate this types of errors, so my first recommendation is to always use justification in nested commands and to place the closing parentheses in a way that clearly mark the end of each for or if construct. Below there is an example of such format.

In second place, a trace of your code results in these actions:

%%R = 1
%%S = !a%%R! = !a1! = a numeric value between 0 and 12
if !b1!==4 echo P1 has a !%%S! 4 of a kind

In the last line %%S has a numeric value, so !%%S! tries to replace the value of a variable whose name is a number! I think you forgot an array here, so I inserted card as the name of the array that store the card names as you show above:

@echo off
setlocal EnableDelayedExpansion

rem Define names of cards
set i=0
for %%a in (A 2 3 4 5 6 7 8 9 10 J Q K) do (
   set card!i!=%%a
   set /A i+=1
)

rem The values a1 through a9 are equal to some number that is modulus 13 (0-12)
for /L %%i in (1,1,9) do set /A a%%i=!random! %% 13

rem b1 through b4 ... signify the number of repeated cards there are to identify pairs and such.
set "b1=2"    // two cards of b1 type
set "b2=0"    // no cards of b2 type
set "b3=4"    // four cards of b3 type
set "b4=1"    // one card of b4 type

rem I am trying to call A from 0 from a3 or the 7 from 8 from a1 with this code:
for %%R in (1 3) do for %%S in (!a%%R!) do (
   if !b%%R!==4 echo P1 has a !card%%S! 4 of a kind
   if !b%%R!==3 echo P1 has a !card%%S! 3 of a kind
   if !b%%R!==2 echo P1 has a !card%%S! pair
)
for %%T in (2 4) do for %%U in (!a%%T!) do (
   if !b%%T!==4 echo CP has a !card%%U! 4 of a kind
   if !b%%T!==3 echo CP has a !card%%U! 3 of a kind
   if !b%%T!==2 echo CP has a !card%%U! pair
)

Output example:

C:\> test
P1 has a A pair
P1 has a Q 4 of a kind

You may also do a further simplification this way:

for %%R in (1 3) do for %%S in (!a%%R!) do (
   if !b%%R! gtr 1 echo P1 has a !card%%S! !b%%R! of a kind
)
for %%T in (2 4) do for %%U in (!a%%T!) do (
   if !b%%T! gtr 1 echo CP has a !card%%U! !b%%T! of a kind
)

In the last simplification, you may also define an array of "names of multiples" like "2=pair", "3=3 of a kind", etc. if you want to show a more precise result.

Upvotes: 1

dbenham
dbenham

Reputation: 130809

I can't vouch for the logic of your code. But you are missing a set of parentheses within each inner FOR loop. As currently written, %%S and %%U are only defined for one IF statement each. You need them each for three statements, like so:

for %%R in (1 3) do (
  for %%S in (!a%%R!) do (
    if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
    if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
    if !b%%R!==2 echo P1 has a !%%S! pair
  )
)
for %%T in (2 4) do (
  for %%U in (!a%%T!) do (
    if !b%%T!==4 echo CP has a !%%U! 4 of a kind
    if !b%%T!==3 echo CP has a !%%U! 3 of a kind
    if !b%%T!==2 echo CP has a !%%U! pair
  )
)

Or you could shorten it a bit as follows:

for %%R in (1 3) do for %%S in (!a%%R!) do (
  if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
  if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
  if !b%%R!==2 echo P1 has a !%%S! pair
)
for %%T in (2 4) do for %%U in (!a%%T!) do (
  if !b%%T!==4 echo CP has a !%%U! 4 of a kind
  if !b%%T!==3 echo CP has a !%%U! 3 of a kind
  if !b%%T!==2 echo CP has a !%%U! pair
)

Note that there is no need to keep changing the letters used for each independent FOR block. It would work just as well using

for %%R in (1 3) do for %%S in (!a%%R!) do (
  if !b%%R!==4 echo P1 has a !%%S! 4 of a kind
  if !b%%R!==3 echo P1 has a !%%S! 3 of a kind
  if !b%%R!==2 echo P1 has a !%%S! pair
)
for %%R in (2 4) do for %%S in (!a%%R!) do (
  if !b%%R!==4 echo CP has a !%%S! 4 of a kind
  if !b%%R!==3 echo CP has a !%%S! 3 of a kind
  if !b%%R!==2 echo CP has a !%%S! pair
)

Upvotes: 0

unclemeat
unclemeat

Reputation: 5197

To shorten the sequence of set commands -

setLocal enableDelayedExpansion
set i=0
for %%a in (A 1 2 3 4 5 6 7 8 9 10 J Q K) do (
    set !i!=%%a
    set /a i+=1
)

Upvotes: 1

Related Questions