|
Answer» 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
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 startYou technically do not need to use delayed expansion for all your other IF commands but you need it for the one I added.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 startMy 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.
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 )You are overthinking it. In its simplest form you only need 8 IF statements and none of them need to be nested.And stop using delayed expansion for your variables when you dont have to use it.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!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.
Quote from: shiverbob on January 06, 2015, 08:01:36 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.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 And there you have it...This rates right up there with JacobRocks not enough time excuse. Front runners for the Nebulous Request of the Month Award...
|