Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Checking Variables  (Read 5507 times)

0 Members and 1 Guest are viewing this topic.

shiverbob

    Topic Starter


    Beginner

    Thanked: 1
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Checking Variables
« on: December 30, 2014, 01:09:52 PM »
It has been a long time since I have coded in Batch. There is a problem that I can't not figure out. I am making a Tic-Tac-Toe game, and when I have the player enter their choice via "set /p" and it goes through the process of setting the variable, which works fine. But what if they enter one that has already been selected. I do not know how to check the variable without having a bunch of if statements.

Thanks, Shiverbob

Code:
Code: [Select]
@echo off
setlocal enabledelayedexpansion
title Tic Tac Toe

:presets
::x o
set player=player
set 11=
set 12=
set 13=
set 21=
set 22=
set 23=
set 31=
set 32=
set 33=
set turn=x
goto start
:check


:turnCheck

if "!turn!" == "x" (
set turn=o
goto start
)
if "!turn!" == "o" (
set turn=x
goto start
)
goto turnCheck
:start
cls
echo  !player! versus CPU
echo ----------------------
echo 1 2 3
echo !11!л!21!л!31! 1
echo ллллл
echo !12!л!22!л!32! 2
echo ллллл
echo !13!л!23!л!33! 3
echo.
set /p k=!turn! Turn:
goto turnSet


:turnSet

if "!k!" == "11" (
set 11=!turn!
goto turnCheck
)
if "!k!" == "12" (
set 12=!turn!
goto turnCheck
)
if "!k!" == "13" (
set 13=!turn!
goto turnCheck
)
if "!k!" == "21" (
set 21=!turn!
goto turnCheck
)
if "!k!" == "22" (
set 22=!turn!
goto turnCheck
)
if "!k!" == "23" (
set 23=!turn!
goto turnCheck
)
if "!k!" == "31" (
set 31=!turn!
goto turnCheck
)
if "!k!" == "32" (
set 32=!turn!
goto turnCheck
)
if "!k!" == "33" (
set 33=!turn!
goto turnCheck
)
goto start
« Last Edit: December 30, 2014, 01:45:22 PM by shiverbob »
Pff computers are easy, math is hard.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #1 on: December 30, 2014, 04:36:42 PM »
Code: [Select]
@echo off
setlocal enabledelayedexpansion
title Tic Tac Toe

:presets
::x o
set player=player
set 11=
set 12=
set 13=
set 21=
set 22=
set 23=
set 31=
set 32=
set 33=
set turn=x
goto start
:check


:turnCheck

if "!turn!" == "x" (
set turn=o
goto start
)
if "!turn!" == "o" (
set turn=x
goto start
)
goto turnCheck
:start
cls
echo  !player! versus CPU
echo ----------------------
echo 1 2 3
echo !11!л!21!л!31! 1
echo ллллл
echo !12!л!22!л!32! 2
echo ллллл
echo !13!л!23!л!33! 3
echo.
set /p k=!turn! Turn:
goto turnSet


:turnSet

if NOT "!%k%!"==" " goto start
if "!k!" == "11" (
set 11=!turn!
goto turnCheck
)
if "!k!" == "12" (
set 12=!turn!
goto turnCheck
)
if "!k!" == "13" (
set 13=!turn!
goto turnCheck
)
if "!k!" == "21" (
set 21=!turn!
goto turnCheck
)
if "!k!" == "22" (
set 22=!turn!
goto turnCheck
)
if "!k!" == "23" (
set 23=!turn!
goto turnCheck
)
if "!k!" == "31" (
set 31=!turn!
goto turnCheck
)
if "!k!" == "32" (
set 32=!turn!
goto turnCheck
)
if "!k!" == "33" (
set 33=!turn!
goto turnCheck
)
goto start
You technically do not need to use delayed expansion for all your other IF commands but you need it for the one I added.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #2 on: December 30, 2014, 04:57:12 PM »
Here is a slightly more optimized version of your code.  I look forward to seeing your WIN logic.
Code: [Select]
@echo off
setlocal enabledelayedexpansion
title Tic Tac Toe

:presets
::x o
set player=player
FOR /L %%G IN (1,1,3) DO FOR /L %%H IN (1,1,3) DO SET "%%G%%H= "
set turn=x
goto start
:check


:turnCheck

if "%turn%"=="x" (
set turn=o
goto start
)
if "%turn%"=="o" (
set turn=x
goto start
)
goto turnCheck
:start
cls
echo  %player% versus CPU
echo ----------------------
echo 1 2 3
echo !11!л!21!л!31! 1
echo ллллл
echo !12!л!22!л!32! 2
echo ллллл
echo !13!л!23!л!33! 3
echo.
set /p k=%turn% Turn:

:turnSet

if NOT "!%k%!"==" " goto start

FOR /L %%G IN (1,1,3) DO (
FOR /L %%H IN (1,1,3) DO (
IF "%%G%%H"=="%k%" (
SET %k%=%turn%
goto turncheck
)
)
)
goto start

shiverbob

    Topic Starter


    Beginner

    Thanked: 1
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Checking Variables
« Reply #3 on: December 31, 2014, 12:37:20 PM »
My win solution requires if statements to be nested into each other. However, when I nest if statements together it crashes. Could it be the fact the the second if statement is false. It also crashes when i enter a different cord-nets.
And is it  possible to have a if "!var!" == "bah" || "bal"?

Code: [Select]
@echo off
setlocal enabledelayedexpansion
title Tic Tac Toe

:presets
::x o
set player=Logan
set 11=
set 12=
set 13=
set 21=
set 22=
set 23=
set 31=
set 32=
set 33=
set turn=x
goto start
:check


:turnCheck

if "!turn!" == "x" (
set turn=o
goto start
)
if "!turn!" == "o" (
set turn=x
goto start
)
goto turnCheck
:start
cls
echo  !player! versus CPU
echo ----------------------
echo 1 2 3
echo !11!л!21!л!31! 1
echo ллллл
echo !12!л!22!л!32! 2
echo ллллл
echo !13!л!23!л!33! 3
echo.
set /p k=!turn! Turn:
goto turnSet


:turnSet

if "!k!" == "11" (
set 11=!turn!
goto wincheck
)
if "!k!" == "12" (
set 12=!turn!
goto wincheck
)
if "!k!" == "13" (
set 13=!turn!
goto wincheck
)
if "!k!" == "21" (
set 21=!turn!
goto wincheck
)
if "!k!" == "22" (
set 22=!turn!
goto wincheck
)
if "!k!" == "23" (
set 23=!turn!
goto wincheck
)
if "!k!" == "31" (
set 31=!turn!
goto wincheck
)
if "!k!" == "32" (
set 32=!turn!
goto wincheck
)
if "!k!" == "33" (
set 33=!turn!
goto wincheck
)
goto start

:wincheck
:: Way one bottom row
:: o o
:: o o x
:: x x x
:: 13 23 33
::for /l %%g in (1,1,3) do (
::)

if "!13!" == "x" (
 echo hello
 pause
if "!23!" == "x"(
  echo hello
  pause
)
)

if "!win!" == "3x" goto xwin
if "!win!" == "3o" goto owin
goto turncheck

:owin
cls
echo o Has won!!
set /p c=Would you like to  have another game?(y/n):
if "!c!" == "n" exit
if "!c!" == "y" goto presets
goto owin
:xwin
cls
echo x Has won!!
set /p c=Would you like to  have another game?(y/n):
if "!c!" == "n" exit
if "!c!" == "y" goto presets
goto xwin


But when I used a for loop it would work however if you enter the last cordnets  and there is no winner yet it skips that and say whatever player is on there, they win.



« Last Edit: December 31, 2014, 12:53:18 PM by shiverbob »
Pff computers are easy, math is hard.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Checking Variables
« Reply #4 on: December 31, 2014, 03:04:05 PM »
is this what you need to do?

Code: [Select]
if "!13!" == "x" if "!23!" == "x" (
  echo hello
  pause
)


A problem in your code is that the ( needs a space before it here:

Code: [Select]
if "!23!" == "x"(
  echo hello
  pause
)

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #5 on: December 31, 2014, 04:59:43 PM »
You are overthinking it. In its simplest form you only need 8 IF statements and none of them need to be nested.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #6 on: December 31, 2014, 06:16:20 PM »
And stop using delayed expansion for your variables when you dont have to use it.

shiverbob

    Topic Starter


    Beginner

    Thanked: 1
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Checking Variables
« Reply #7 on: January 04, 2015, 12:52:50 PM »
Last Night I finished the game, with delayed-expansion. All works fine. The reason I added delayed-expansion was for my multiplayer add-on... It runs with nested if and is about 100 lines of code. Thanks for the help!
Pff computers are easy, math is hard.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #8 on: January 04, 2015, 04:13:32 PM »
Seems like it would be appropriate to post your final code otherwise people who read this thread in the future will see it as unfinished. Depending on what you were trying to do I still believe you could code it without using delayed expansion.
« Last Edit: January 04, 2015, 04:35:11 PM by Squashman »

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #9 on: January 06, 2015, 08:15:06 PM »
I am very bored
Apparently you are too busy.

In your other thread you talk about writing code together but you haven't bothered to share your final code.  If you are going to ask for help in writing code then it is polite to share the entire code after you are finished.

shiverbob

    Topic Starter


    Beginner

    Thanked: 1
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows XP
Re: Checking Variables
« Reply #10 on: January 11, 2015, 03:07:00 PM »
I don't have it. I gave it to my friend, and I didn't back it up sorry. And my multiplayer peice of code requires delayedExpansion and all it does is send it to a text file with the (echo whatever ) >> file.txt
Pff computers are easy, math is hard.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #11 on: January 11, 2015, 03:23:37 PM »
 ::)

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Checking Variables
« Reply #12 on: January 11, 2015, 05:14:52 PM »
And there you have it...
" Anyone who goes to a psychiatrist should have his head examined. "

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Checking Variables
« Reply #13 on: January 11, 2015, 06:09:07 PM »
This rates right up there with JacobRocks not enough time excuse.

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Checking Variables
« Reply #14 on: January 11, 2015, 07:15:03 PM »
Front runners for the Nebulous Request of the Month Award...
" Anyone who goes to a psychiatrist should have his head examined. "