|
Today I am teaching you how to make games in
notepad in easy steps if you follow my steps you will learn easily and you will
also enjoy my post and please don't forget to comments on my post......
1-Tic Tac Toe game in notepad .
- Open notepad
- Copy the code in notepad
@echo
off
setlocal
enabledelayedexpansion
Title
TQC's Noughts and Crosses
: main
call
:titleScreen
If
"%EXIT%"=="1" exit /b
Call :
firstSetup
: main_Loop
call
:resetBoard
Call :
gameLoop
Call :
endGameScreen %win%
If /I
%rematch%==Y (
GOTO
main_Loop
)
Exit
/b
: title
Screen
echo
Welcome to Noughts and Crosses.
Echo.
Echo.
Echo
Type EXIT to quit the game.
Echo.
: askP1Type
set
p1Type=
Set /p
p1Type="Player 1 - Human or computer? [H/C] - "
If /I
"! p1Type!"=="C" GOTO askP2Type
If /I
"! p1Type!"=="H" GOTO askP2Type
If /I
"! p1Type!"=="EXIT" (
Set
EXIT=1
Exit
/b
)
GOTO
askP1Type
: askP2Type
set
p2Type=
Set /p
p2Type="Player 2 - Human or computer? [H/C] - "
If /I
"! p2Type!"=="C" exit /b
if /i
"!p2Type!"=="H" exit /b
if /i
"!p2Type!"=="EXIT" (
set
EXIT=1
exit
/b
)
goto
askP2Type
:firstSetup
set
p1Score=0
set
p2Score=0
set
turn=
set
p1Char=X
set
p2Char=O
set
p1Score=0
set
p2Score=0
set
NL=^
::winList
contains all the possible winning lines, separated by the new line character.
set
winList=1 2 3 !NL! 4 5 6 !NL! 7 8 9 !NL! 1 4 7 !NL! 2 5 8 !NL! 3 6 9 !NL! 1 5 9
!NL! 3 5 7
exit
/b
:ResetBoard
for /l
%%I in (1,1,9) do (
set
G%%I=%%I
)
if
"%turn%"=="" (
set /a
turn=%random% %% 2 + 1
) else
(
set /a
turn= 3 - %turn%
)
set
turnCount=0
set
availableMoves=123456789
set
errorMessage=
set
win=0
exit
/b
:gameLoop
set /a
otherTurn= 3 - %turn%
if /i
"!p%turn%Type!"=="H" (
call
:humanTurn
) else
(
call
:computerTurn %turn% !p%turn%Char! !p%otherTurn%Char!
)
set /a
turnCount+=1
set
availableMoves=!availableMoves:%move%=!
if NOT
%win%==0 exit /b
if
%turnCount%==9 exit /b
set /a
turn= 3 - %turn%
goto
GameLoop
:humanTurn
call
:displayGrid
echo.
echo.%infoMessage%
set
infoMessage=
set
move=
set /p
move= Player %turn%'s turn. Enter the number of your move [1-9] -
if
!move! lss 1 (
set
infoMessage=Invalid move, enter a number between 1 and 9.
goto
humanTurn
)
if
!move! gtr 9 (
set
infoMessage=Invalid move, enter a number between 1 and 9.
goto
humanTurn
)
if NOT
"!G%Move%!"=="!Move!" (
set
infoMessage=That move has already been made, choose another.
goto
humanTurn
)
set
G%move%=!p%turn%Char!
call
:CheckWin %turn% !p%turn%Char!
exit
/b
:computerTurn
set
move=
for /f
"tokens=1-3 delims= " %%I in ("!winList!") do (
if
"!G%%I!!G%%J!!G%%K!"=="%2%2%%K" (
set
move=%%K
)
if
"!G%%I!!G%%J!!G%%K!"=="%2%%J%2" (
set
move=%%J
)
if
"!G%%I!!G%%J!!G%%K!"=="%%I%2%2" (
set
move=%%I
)
if NOT
"!move!"=="" (
set
G!move!=%2
set
win=%1
exit
/b
)
)
for /f
"tokens=1-3 delims= " %%I in ("!winList!") do (
if
"!G%%I!!G%%J!!G%%K!"=="%3%3%%K" (
set
move=%%K
)
if
"!G%%I!!G%%J!!G%%K!"=="%3%%J%3" (
set
move=%%J
)
if
"!G%%I!!G%%J!!G%%K!"=="%%I%3%3" (
set
move=%%I
)
if NOT
"!move!"=="" (
set
G!move!=%2
exit
/b
)
)
set /a
moveGuess= %random% %% ( 9 - %turnCount% )
set
move=!availableMoves:~%moveGuess%,1!
set
G%move%=!p%turn%Char!
exit
/b
:displayGrid
cls
echo.
echo
^| ^|
echo
%G1% ^| %G2% ^| %G3%
echo
-------------
echo
%G4% ^| %G5% ^| %G6%
echo
-------------
echo
%G7% ^| %G8% ^| %G9%
echo
^| ^|
exit
/b
:checkWin
for /f
"tokens=1-3 delims= " %%I in ("!winList!") do (
if
"!G%%I!!G%%J!!G%%K!"=="%2%2%2" (
set
win=%1
exit
/b
)
)
set
win=0
exit
/b
:endGameScreen
if NOT
%win%==0 (
set /a
p%win%Score+=1
)
call
:Displaygrid
echo.
echo.
if
%win% gtr 0 (
echo
Player %win% wins^!
) else
(
echo
It's a draw^!
)
echo.
echo.
echo
Current Scores
echo
--------------
echo
Player 1 Score - %p1Score% Player 2 Score - %p2Score%
echo.
set
rematch=
set /p
rematch="Would you like a rematch? [Y/N] - "
if /i
"!rematch!"=="Y" exit /b
if /i
"!rematch!"=="N" (
exit
/b
) else
(
goto
endGameScreen
)
save it as game.bat
0 comments:
Post a Comment