Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 08-10-2007, 10:42 PM   #1
Danny252
I have a custom title!

 
Join Date: Oct 2003
Location: Telford, England
Posts: 1,303
Default

So last night, at around 11, having realised I wasn't sleepy and was bored, I got out good old notepad and started typing away

An hour later I had an intro and a main menu for a game of Naughts and Crosses (Tic Tac Toe) coded... as a batch file.

I did have my reasons for choosing a batch file though:
1. I spend so much time hacking the school network through them anyway I actually know a fair bit
2. They're damn easy to make anyway (mm.. text editor)
3. It was simple enough to keep me interested

So all of 24 hours later, I popped out a finished product. A wonderful 6.84kb (of which about half is blank lines and notes so I actually remember what I was doing anyway...) I've got a couple of things I want to add.. maybe some options to change the colour, names for the players, an actual help menu... the sort of things that wouldn't be too hard.

How to play it... well, just chuck in the grid section you want to take. Format is letter-number, i.e. a1 is top left, c3 is bottom right.

I did come across a problem which stumped me for a while. I was trying to work out how to see if someone had won. Seeing as Batch Files don't support any sort of variable other than a variable, let alone an array, I knew it had to be some tedious way of looking at all the possible outcomes.

So I coded it to look at each one and check if all 3 in that line were equal. I then discovered it classed 3 "."s (I used period as a blank space) as a win. Oh dear... Now it checks if each line is equal to the symbol of the last go... it eats up a ton of lines though. Can anyone think of something more efficient?

Oh, I just remembered... I haven't coded in a stop for taking squares already taken or an actual tie code... So you can have wars with the other player by taking squares they already have... Right, that's the fixes for version 0.15 planned...

Code is below, feel free to try it:

Code:
REM Danny252's Naughts and Crosses
REM Version 0.2
REM Complete working version of game
REM More features planned in future

ECHO OFF

:INTRO

REM Intro, will eventually
REM Time delayed by the Pings

CLS
COLOR 0a
ECHO Dannei!! Productions Present...
PING -n 5 127.0.0.1 >NUL
CLS
ECHO A Danny252 Production... 
PING -n 5 127.0.0.1 >NUL
CLS
ECHO Welcome to Danny252's...
ECHO.
ECHO.
PING -n 5 127.0.0.1 >NUL
COLOR 1c
ECHO ##**#** ###** #** #** ###** #** #**#####** #### 
ECHO # # #**#** #**#** #**#** #**#** #****#****#
ECHO # # #**#####**#** #**#******#####****#**** ###
ECHO # # #**#** #**#** #**# ###**#** #****#********#
ECHO #**##**#** #** ###****###** #** #****#****####
ECHO.
PING -n 2 127.0.0.1 >NUL
COLOR 1e
ECHO****************###** ##**#**####
ECHO************** #** #**# # #**#** #
ECHO************** #####**# # #**#** #
ECHO************** #** #**# # #**#** # 
ECHO************** #** #**#**##**####
ECHO.
PING -n 2 127.0.0.1 >NUL
COLOR 1a
ECHO**###** ####****###****####** ####**#####** #### #
ECHO #******#** #**#** #**#******#******#******#**** # 
ECHO #******####** #** #** ###****###** ####****###**#
ECHO #******#** #**#** #******#******#**#**********#
ECHO**###** #** #** ###** ####** ####** #####**####**#
ECHO.
PING -n 6 127.0.0.1 >NUL

:MENU

REM Main Menu Display
REM Clears screen and sets color

CLS
COLOR 1a
ECHO Danny252's Naughts and Crosses
ECHO.
ECHO MAIN MENU
ECHO.
ECHO Options:
ECHO A: New Game
ECHO B: Replay Intro
ECHO Q: Quit 
ECHO.

REM Ask for choice

SET Choice=
SET /P Choice=

REM Select the first letter

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

REM Follow the command

IF /I '%Choice%'=='a' GOTO GAMESTART 
IF /I '%Choice%'=='b' GOTO INTRO
IF /I '%Choice%'=='q' GOTO EXIT
GOTO MENU

:GAMESTART

REM Game Setup
REM Clears screen and sets color

CLS
COLOR 1a

REM Sets up grid.
REM Letter is Column
REM Number is Row
REM N is Neutral (Empty)
REM O is Naught
REM X is Cross

SET a1=.
SET a2=.
SET a3=.
SET b1=.
SET b2=.
SET b3=.
SET c1=.
SET c2=. 
SET c3=.

REM Sets up Win/Lose
REM 0 is In play
REM 1 is Player 1
REM 2 is Player 2

SET win=0

REM Sets up Turn
REM Starts as player 1

SET player=1

REM Input Validity Check 
REM Valid=1
REM Invalid=0

SET valid=0

REM Turns played

SET turns=0

:GAMELOOP

REM Main Game
REM Clears screen to dump last turn

CLS

REM Set the shape for the round
REM And its name

IF %player%==1 (
****SET shape=O& SET shapename=Naughts
) ELSE (
****SET SHAPE=X& SET shapename=Crosses
)

REM Set the rows to the variables for each square 

SET rowa=%a1%%b1%%c1%
SET rowb=%a2%%b2%%c2%
SET rowc=%a3%%b3%%c3%

REM Print out the rows

ECHO.
ECHO** ABC
ECHO**1%rowa%
ECHO**2%rowb%
ECHO**3%rowc% 
ECHO.

REM Say who is player

ECHO The current player is
ECHO.
ECHO PLAYER %player% (Who is playing with %shapename%)

REM Ask for grid reference

ECHO.
ECHO Where would you like to go? (Column, Row) 
ECHO Type "Q" to return to the menu
SET Choice=
SET /P Choice=

REM Decode the info

IF NOT '%Choice%'=='' SET column=%Choice:~0,1%
IF NOT '%Choice%'=='' SET row=%Choice:~1,2% 

REM Check if input is valid for columns
REM Then check if it's quit
REM If input is valid skip remaining checks

:COLUMNCHECK

IF %column%==a (SET valid=1 & GOTO ROWCHECK) ELSE (SET valid=0) 
IF %column%==b (SET valid=1 & GOTO ROWCHECK) ELSE (SET valid=0)
IF %column%==c (SET valid=1 & GOTO ROWCHECK) ELSE (SET valid=0)
IF %column%==q (GOTO MENU) ELSE (SET valid=0)

REM Check if input is valid for rows 
REM If input is valid skip remaining checks

:ROWCHECK

IF %row%==1 (SET valid=1 & GOTO PASS) ELSE (SET valid=0)
IF %row%==2 (SET valid=1 & GOTO PASS) ELSE (SET valid=0)
IF %row%==3 (SET valid=1 & GOTO PASS) ELSE (SET valid=0) 

:PASS

REM If data is bad, don't input

IF %valid%==0 GOTO INVALID

REM Put it together as a variable name

SET input=%column%%row%

REM If taken, tell player and ignore input

IF %input%==a1 IF %a1% NEQ . GOTO TAKEN
IF %input%==a2 IF %a2% NEQ . GOTO TAKEN
IF %input%==a3 IF %a3% NEQ . GOTO TAKEN
IF %input%==b1 IF %b1% NEQ . GOTO TAKEN
IF %input%==b2 IF %b2% NEQ . GOTO TAKEN
IF %input%==b3 IF %b3% NEQ . GOTO TAKEN
IF %input%==c1 IF %c1% NEQ . GOTO TAKEN
IF %input%==c2 IF %c2% NEQ . GOTO TAKEN
IF %input%==c3 IF %c3% NEQ . GOTO TAKEN

REM Put in the shape (set at start of GAMELOOP)

IF %input%==a1 SET a1=%shape%
IF %input%==a2 SET a2=%shape%
IF %input%==a3 SET a3=%shape%
IF %input%==b1 SET b1=%shape%
IF %input%==b2 SET b2=%shape%
IF %input%==b3 SET b3=%shape%
IF %input%==c1 SET c1=%shape%
IF %input%==c2 SET c2=%shape%
IF %input%==c3 SET c3=%shape%

REM Check if anyone has won
REM First checks the start of each possible win for current player's symbol
REM Then checks if the rest in that line are the same
REM If so, it continues to 
REM Checks Columns Left to Right
REM Checks Rows Top to Bottom
REM Checks TL to BR
REM Checks BL to TR

IF %a1%==%shape% IF %a1%==%a2% IF %a2%==%a3% GOTO WIN
IF %b1%==%shape% IF %b1%==%b2% IF %b2%==%b3% GOTO WIN 
IF %c1%==%shape% IF %c1%==%c2% IF %c2%==%c3% GOTO WIN
IF %a1%==%shape% IF %a1%==%b1% IF %b1%==%c1% GOTO WIN
IF %a2%==%shape% IF %a2%==%b2% IF %b2%==%c2% GOTO WIN
IF %a3%==%shape% IF %a3%==%b3% IF %b3%==%c3% GOTO WIN 
IF %a1%==%shape% IF %a1%==%b2% IF %b2%==%c3% GOTO WIN
IF %a3%==%shape% IF %a3%==%b2% IF %b2%==%c1% GOTO WIN 

:NOWIN

REM Check if it's been 9 turns
REM If so, it's a draw

IF %turns%==8 GOTO DRAW

REM No one has won, continue play
REM Change player

IF %player%==1 (SET player=2) ELSE (SET player=1) 

REM Increment round
REM Done after checks so that invalid turns don't get counted

SET /a turns=%turns%+1 

GOTO GAMELOOP

:INVALID

REM Grid square doesn't exist
REM Tell the user and await awknowledgement

ECHO.
ECHO Sorry, that is not an avaliable square
ECHO Please try again
PAUSE

REM Go back to the game as it was before
GOTO GAMELOOP

:TAKEN

REM Tell the user that the square has already been taken
REM Similar to :INVALID

ECHO.
ECHO Sorry, that square has been taken already
ECHO Please try again
PAUSE

REM Go back to the game as it was before
GOTO GAMELOOP

:DRAW

REM No one has won after 9 turns
REM So it's a draw

CLS

REM Say that it's a draw

ECHO DRAW
ECHO.
ECHO No one has won, it's a draw!
ECHO.
ECHO Here's what the game looked like: 
ECHO.

REM Display final game status
REM Set the rows to the variables for each square

SET rowa=%a1%%b1%%c1%
SET rowb=%a2%%b2%%c2%
SET rowc=%a3%%b3%%c3%

REM Print out the rows

ECHO. 
ECHO** ABC
ECHO**1%rowa%
ECHO**2%rowb%
ECHO**3%rowc%
ECHO.

REM New Game, Main Menu or Quit
REM If invalid, returns to Menu

ECHO Press A to return to the menu, B for a new game or Q to quit 

REM Ask for choice

SET Choice=
SET /P Choice=

REM Select the first letter

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

REM Follow the command

IF /I '%Choice%'=='a' GOTO 
IF /I '%Choice%'=='b' GOTO GAMESTART
IF /I '%Choice%'=='q' GOTO EXIT

REM Otherwise return to Menu 

GOTO MENU

:WIN

REM A player has won one of the possibilities
REM Clear screen

CLS

REM Display "WINNER!" and flash colours

COLOR 0F
ECHO # # #**#####**##**#**##**#**#####**####**#
PING -n 2 127.0.0.1 >NUL
COLOR 1E
ECHO # # #****#****# # #**# # #**#******#** # #
PING -n 2 127.0.0.1 >NUL
COLOR 2D
ECHO # # #****#****# # #**# # #**####** ####**#
PING -n 2 127.0.0.1 >NUL
COLOR 3C
ECHO # # #****#****# # #**# # #**#******#** # 
PING -n 2 127.0.0.1 >NUL
COLOR 4B
ECHO**# #** #####**#**##**#**##**#####**#** # #
PING -n 2 127.0.0.1 >NUL
COLOR 5A
PING -n 5 127.0.0.1 >NUL

REM Change to normal color
COLOR 1a

REM Say who won

ECHO.
ECHO PLAYER %player% has won!!!
ECHO.
ECHO Well done!
ECHO.
ECHO Here's what the game looked like: 
ECHO.

REM Display final game status
REM Set the rows to the variables for each square

SET rowa=%a1%%b1%%c1%
SET rowb=%a2%%b2%%c2%
SET rowc=%a3%%b3%%c3%

REM Print out the rows

ECHO. 
ECHO** ABC
ECHO**1%rowa%
ECHO**2%rowb%
ECHO**3%rowc%
ECHO.

REM New Game, Main Menu or Quit
REM If invalid, returns to Menu

ECHO Press A to return to the menu, B for a new game or Q to quit 

REM Ask for choice

SET Choice=
SET /P Choice=

REM Select the first letter

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

REM Follow the command

IF /I '%Choice%'=='a' GOTO 
IF /I '%Choice%'=='b' GOTO GAMESTART
IF /I '%Choice%'=='q' GOTO EXIT

REM Otherwise return to Menu 

GOTO MENU

:EXIT

REM Exit game holder
__________________
I liked the old forum.. =/
Danny252 is offline                         Send a private message to Danny252
Reply With Quote
Old 08-10-2007, 11:02 PM   #2
Burger Meister
Om Teh Nom Noms
 
Burger Meister's Avatar

 
Join Date: Aug 2006
Location: North Bay, Canada
Posts: 308
Default

That's nifty. I never knew you could just write programs like that. Just a little spelling mistake... it's naughts and crosses, not noughts and crosses. Unless you were purposefully trying to spell it like that, I just thought I'd let you know!
Burger Meister is offline                         Send a private message to Burger Meister
Reply With Quote
Old 09-10-2007, 03:44 PM   #3
Danny252
I have a custom title!

 
Join Date: Oct 2003
Location: Telford, England
Posts: 1,303
Default

I noticed about 10 minutes later that I was writing it "Noughts" instead of "Naughts" on here, and then saying "Naughts" everywhere else...

Talk about weird O_o

Went through and redid some code at school today, to stop player's using already taken squares. I think I ironed out some compatibility problems with XP also (I was coding a fair bit on my Vista machine)

I'll see if I can get it to work out a Tie system. Probably just a matter of getting it to count to 9...

EDIT:

The joys of version 0.2!

It now actually plays, and it now knows what a win is, it knows you can't take a square that's already taken and it can see if it's a draw

Note: I doubt this'll work on pre-NT operating systems, due to a number of features that I believe were only introduced after Windows NT (i.e. 2000, XP, Vista)

Feel free to try though, if you don't love your pc

Latest code put in first post
__________________
I liked the old forum.. =/
Danny252 is offline                         Send a private message to Danny252
Reply With Quote
Old 21-01-2008, 10:01 PM   #4
Gargantuan Orangutan
Super Freak
 
Gargantuan Orangutan's Avatar

 
Join Date: Oct 2006
Location: Harmony, Canada
Posts: 183
Default

I'm not familiar with how Echo** would work. Is that something particular to Vista? Here are my results:



The code next to the window is where I get this output.


Quote:
So I coded it to look at each one and check if all 3 in that line were equal. I then discovered it classed 3 "."s (I used period as a blank space) as a win. Oh dear... Now it checks if each line is equal to the symbol of the last go... it eats up a ton of lines though. Can anyone think of something more efficient?
I'll have to look at it closer, but right now (as shown above) it's probably not working as you intended. But, FOR loops might come in handy for this.

Last edited by Gargantuan Orangutan; 21-01-2008 at 10:09 PM.
Gargantuan Orangutan is offline                         Send a private message to Gargantuan Orangutan
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming In Windows. chickenman Tech Corner 15 10-10-2005 03:53 PM
Programming In Linux chickenman Tech Corner 6 01-10-2005 09:53 AM
Have You Finished Police Quest 4? greenbolt Gaming Zone 5 06-02-2005 05:00 PM
Programming ultranewbie Old Suggestions 2 20-01-2005 08:22 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 08:08 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.