Here's a basic Tic-Tac-Toe.
Code @echo off
title TTT
setlocal enabledelayedexpansion
: start
set 1=1
set 2=2
set 3=3
set 4=4
set 5=5
set 6=6
set 7=7
set 8=8
set 9=9
set t=X
: board
cls
echo !7! !8! !9!
echo !4! !5! !6!
echo !1! !2! !3!
echo.
if !1!==X if !4!==X if !7!==X goto xwin
if !1!==O if !4!==O if !7!==O goto owin
if !2!==X if !5!==X if !8!==X goto xwin
if !2!==O if !5!==O if !8!==O goto owin
if !3!==X if !6!==X if !9!==X goto xwin
if !3!==O if !6!==O if !9!==O goto owin
if !1!==X if !2!==X if !3!==X goto xwin
if !1!==O if !2!==O if !3!==O goto owin
if !4!==X if !5!==X if !6!==X goto xwin
if !4!==O if !5!==O if !6!==O goto owin
if !7!==X if !8!==X if !9!==X goto xwin
if !7!==O if !8!==O if !9!==O goto owin
if !1!==X if !5!==X if !9!==X goto xwin
if !1!==O if !5!==O if !9!==O goto owin
if !3!==X if !5!==X if !7!==X goto xwin
if !3!==O if !5!==O if !7!==O goto owin
if not !1!==1 if not !2!==2 if not !3!==3 if not !4!==4 if not !5!==5 if not !6!==6 if not !7!==7 if not !8!==8 if not !9!==9 goto nowin
echo %t%, your go! Choose a number..
CHOICE /C 123456789 >nul
set %errorlevel%=%t%
if %t%==X (set t=O) else (set t=X)
goto board
: nowin
echo No one has won!
pause >nul
goto start
: xwin
echo X wins!
pause >nul
goto start
: owin
echo O wins!
pause >nul
goto start
The next part is to make the computer play the computer.
Edit: Player V CPU: https://hackforums.net/showthread.php?tid=2886755
(Sep 23, 2012 03:26 AM)Mr Liam Wrote: Here's a basic Tic-Tac-Toe.
Code @echo off
title TTT
setlocal enabledelayedexpansion
: start
set 1=1
set 2=2
set 3=3
set 4=4
set 5=5
set 6=6
set 7=7
set 8=8
set 9=9
set t=X
: board
cls
echo !7! !8! !9!
echo !4! !5! !6!
echo !1! !2! !3!
echo.
if !1!==X if !4!==X if !7!==X goto xwin
if !1!==O if !4!==O if !7!==O goto owin
if !2!==X if !5!==X if !8!==X goto xwin
if !2!==O if !5!==O if !8!==O goto owin
if !3!==X if !6!==X if !9!==X goto xwin
if !3!==O if !6!==O if !9!==O goto owin
if !1!==X if !2!==X if !3!==X goto xwin
if !1!==O if !2!==O if !3!==O goto owin
if !4!==X if !5!==X if !6!==X goto xwin
if !4!==O if !5!==O if !6!==O goto owin
if !7!==X if !8!==X if !9!==X goto xwin
if !7!==O if !8!==O if !9!==O goto owin
if !1!==X if !5!==X if !9!==X goto xwin
if !1!==O if !5!==O if !9!==O goto owin
if !3!==X if !5!==X if !7!==X goto xwin
if !3!==O if !5!==O if !7!==O goto owin
if not !1!==1 if not !2!==2 if not !3!==3 if not !4!==4 if not !5!==5 if not !6!==6 if not !7!==7 if not !8!==8 if not !9!==9 goto nowin
echo %t%, your go! Choose a number..
CHOICE /C 123456789 >nul
set %errorlevel%=%t%
if %t%==X (set t=O) else (set t=X)
goto board
: nowin
echo No one has won!
pause >nul
goto start
: xwin
echo X wins!
pause >nul
goto start
: owin
echo O wins!
pause >nul
goto start
The next part is to make the computer play the computer.
Nice, I look forward to play it with a computer.
In the land of the blind, the one-eyed man is king
Blood makes you related, but loyalty makes you family
This is really cool, thanks for the share OP. 👍
EDIT: I lost :(
Thank you for the share, a little nice game...
If you want a bad Computer-player you could work with the %random%%%10 variables just sayin
(Sep 23, 2012 03:26 AM)Mr Liam Wrote: Here's a basic Tic-Tac-Toe.
Code @echo off
title TTT
setlocal enabledelayedexpansion
: start
set 1=1
set 2=2
set 3=3
set 4=4
set 5=5
set 6=6
set 7=7
set 8=8
set 9=9
set t=X
: board
cls
echo !7! !8! !9!
echo !4! !5! !6!
echo !1! !2! !3!
echo.
if !1!==X if !4!==X if !7!==X goto xwin
if !1!==O if !4!==O if !7!==O goto owin
if !2!==X if !5!==X if !8!==X goto xwin
if !2!==O if !5!==O if !8!==O goto owin
if !3!==X if !6!==X if !9!==X goto xwin
if !3!==O if !6!==O if !9!==O goto owin
if !1!==X if !2!==X if !3!==X goto xwin
if !1!==O if !2!==O if !3!==O goto owin
if !4!==X if !5!==X if !6!==X goto xwin
if !4!==O if !5!==O if !6!==O goto owin
if !7!==X if !8!==X if !9!==X goto xwin
if !7!==O if !8!==O if !9!==O goto owin
if !1!==X if !5!==X if !9!==X goto xwin
if !1!==O if !5!==O if !9!==O goto owin
if !3!==X if !5!==X if !7!==X goto xwin
if !3!==O if !5!==O if !7!==O goto owin
if not !1!==1 if not !2!==2 if not !3!==3 if not !4!==4 if not !5!==5 if not !6!==6 if not !7!==7 if not !8!==8 if not !9!==9 goto nowin
echo %t%, your go! Choose a number..
CHOICE /C 123456789 >nul
set %errorlevel%=%t%
if %t%==X (set t=O) else (set t=X)
goto board
: nowin
echo No one has won!
pause >nul
goto start
: xwin
echo X wins!
pause >nul
goto start
: owin
echo O wins!
pause >nul
goto start
The next part is to make the computer play the computer.
My first try was already 2 times as long with only choice 1 hahaha. Whatever, first time with Batch. Looks nice :)
This is quite good, im looking forward for playing the computer :)
Very nice program but what does this line?
Code set %errorlevel%=%t%
(Sep 23, 2012 05:31 AM)Hepic Wrote: Very nice program but what does this line?
Code set %errorlevel%=%t%
%errorlevel% being the number they pressed.
!variable! means that these can be changed becayse are from errrolevel>?
(Sep 23, 2012 05:47 AM)Hepic Wrote: !variable! means that these can be changed becayse are from errrolevel>?
Set /? - Batch [url=//hackforums.net/misc.php?action=help Wrote:Help Docs[/url]]
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.
Basically if you change or declare a variable inside a parenthesis, you won't be able to see the change unless you do !variable! instead of %variable%, or try to access it from outside the parentheses.
Guys, check the thread again for the Player V CPU stuff.
|