Batch

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Batch

Post by beagle on Thu Jul 03, 2008 11:24 am
([msg=6453]see Batch[/msg])

Is batch really a programming language? It's abilities are very limited, but it can still be used.(In Micro$oft Windows,anyway.) For example, I made a game in batch a couple days ago (It's a text adventure game) Here's the code if you want to play it (In case you don't know about batch, you save it notepad with extension .bat) Just copy and paste the code into notepad.

@echo off
color 2
:start
echo Welcome to my adventure game. This is a text-based game, meaning there are no graphics. Type "play" to start the game. Type "help" to view the commands you can use. ("n" means North, "s" is south, "e" is east, and "w" is west.) If you get stuck, you can use the "give up" command to start over. If you are a programmer, feel free to play around with the source code.
echo Have fun!
set /p a=Enter your command:
if %a%==play goto game
if %a%==help goto help
:help
echo Commands you can use:
echo ----------------------
echo n
echo e
echo s
echo w
echo give up
goto start
:game
echo You are in a cave. There are tunnels going north and east.
set /p n=
if %n%==n goto north
if %n%==w goto error
if %n%==e goto east
if %n%==s goto error
if %n%==give up exit
:error
echo You cannot go that way.
pause
cls
goto game
:east
echo You were attacked by bats and killed. Start over
pause
goto start
:north
echo You chose the correct path. You are out of the cave. You are standing in an open field. Where do you go?
set /p b=
if %b%==n goto nth
if %b%==s goto nth
if %b%==e goto e
if %b%==w goto nth
if %b%==give up exit
:nth
echo You had a heart attack and died. Game over
goto start
:e
echo You are now in a forest. The only paths are north and south.
set /p z=
if %z%==n goto die
if %z%==e goto error
if %z%==s goto path
if %z%==w goto error
if %z%==give up exit
:die
echo You tripped on a rock and died. Game over.
goto start
:path
echo You are still in the forest. The Only paths now are south and east
set /p x=
if %x%==n goto error
if %x%==e goto death
if %x%==s goto happy
if %x%==w goto error
if %x%==give up exit
:death
echo A snake bit you and you died. Game over.
pause
goto start
:happy
echo You are out of the forest now, but a tribe of Indians surrounded you. The only ways out are south and east.
set /p d=
if %d%==n goto error
if %d%==e goto indians
if %d%==s goto escape
if %d%==w goto error
if %d%==give up exit
:indians
echo They saw what you were up to and stabbed you with sharp rocks. Game over.
pause
goto start
:escape
echo You are standing at the edge of a cliff.
set /p 3=
if %3%==n goto fall
if %3%==e goto found
if %3%==s goto won
if %3%==w goto tripped
:fall
echo You walked off the cliff. Game over.
pause
goto start
:found
echo The Indians found you again and shot you with arrows. Game over
pause
goto start
:tripped
echo You tripped on a rock and fell off the cliff. Game over.
pause
goto start
:won
echo YOU WON THE GAME!! Congrats!
echo Please type your name:
set /p m=
echo WINNERS
echo ---------
echo 1 %m%
echo 2
echo 3
echo 4
echo 5
pause
cls
goto start





Have fun!
011000100110010101100001011001110110110001100101
beagle
Poster
Poster
 
Posts: 244
Joined: Wed Jul 02, 2008 2:37 pm
Location: Chico, CA
Blog: View Blog (0)


Re: Batch

Post by nathandelane on Thu Jul 03, 2008 2:21 pm
([msg=6480]see Re: Batch[/msg])

Hey that's pretty cool - batch is a general purpose shell scripting language - in and of itself it doesn't provide all of the constructs I'd like to have alone, so I usually install some other shell like Windows Powershell or even bash for windows. It seems to me that Batch was more or less a way to allow scripted activities in Windows and some extent of automation for Windows, but it does terribly, however it does include the standard pipes, redirects and command calling you would expect from a shell scripting engine. Just others are much more powerful and user-friendly. For example, Batch doesn't have a very obvious method of reading file data into a variable, but BASH, Powershell, and others do. Other good scripting engines on Windows (by default) include the VBScript and JScript engines, accessible by the cscript shell command. JScript is the more powerful of the two IMNSHO. You can also name your shell scripts with the .CMD extension in Win32 systems.

It's pretty cool that you made a game in Batch though - way to work the system ;)
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: Batch

Post by beagle on Thu Jul 03, 2008 8:30 pm
([msg=6507]see Re: Batch[/msg])

Thanks.
By the way, I visited your web site. I love it! It's really cool!
I think I'll try using BASH. It sounds neat. Better than batch, anyway. ;)
Thanks again,
011000100110010101100001011001110110110001100101
beagle
Poster
Poster
 
Posts: 244
Joined: Wed Jul 02, 2008 2:37 pm
Location: Chico, CA
Blog: View Blog (0)


Re: Batch

Post by nathandelane on Thu Jul 10, 2008 10:58 am
([msg=7065]see Re: Batch[/msg])

Thanks. If you have any troubles with bash, just let me know on here, and I'll see where I can help. Good luck to you.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: Batch

Post by Sharmz on Sun Jul 13, 2008 1:17 pm
([msg=7337]see Re: Batch[/msg])

I thought your game was really cool so I tried to write it in C because I needed a project to practice, I haven't come close
to mastering C yet so could someone look at my code so far and tell me how I can improve.
Code: Select all
#include<stdio.h>
main()
{
      int a; //assigns an int value to a
      char c;//assigns a char value to c
      printf("Welcome to someones adventure game, props to who wrote it in batch!/n");
      printf(" This is a text-based game, meaning there are no graphics./n");
      printf(" Type 1 to play and type 2 to play the game:/n");
      scanf("%d", a;
      if (a = 2){//starts if loop and initializes a
            printf("Commands you can use/n e for east/nw for west/ns for south/nn for north/nx to exit/n");
            }//ends if
            else{//starts else
                 printf("You are in a cave. There are tunnels going north and east./n");
                 }//ends else
      scanf("%c", c;//assigns a value to c
      switch (c){
             case 'e':
                  printf("You were attacked by bats and killed. Start over/n");
                  break;
             case 's':
                  printf("You cannot go that way./n");
                  break;
             case 'w':
                  printf("You cannot go that way./n");
                  break;
             case 'n':
                  printf("You chose the correct path. You are out of the cave. You are standing in an open field. Where do you go?/n");
                  break;
             case 'x':
                  return 0;
                  break;
                  }
                     
"You must be the change you wish to see in the world."
-Mohandas Karamchand Gandhi
"Be nice to nerds. Chances are you'll end up working for one."
-Bill Gates
Sharmz
New User
New User
 
Posts: 41
Joined: Thu May 29, 2008 1:18 am
Blog: View Blog (0)


Re: Batch

Post by Ntvu on Sun Jul 13, 2008 6:00 pm
([msg=7356]see Re: Batch[/msg])

Sharmz wrote:I thought your game was really cool so I tried to write it in C because I needed a project to practice, I haven't come close
to mastering C yet so could someone look at my code so far and tell me how I can improve.
Code: Select all
#include<stdio.h>
main()
{
      int a; //assigns an int value to a
      char c;//assigns a char value to c
      printf("Welcome to someones adventure game, props to who wrote it in batch!/n");
      printf(" This is a text-based game, meaning there are no graphics./n");
      printf(" Type 1 to play and type 2 to play the game:/n");
      scanf("%d", a;
      if (a = 2){//starts if loop and initializes a
            printf("Commands you can use/n e for east/nw for west/ns for south/nn for north/nx to exit/n");
            }//ends if
            else{//starts else
                 printf("You are in a cave. There are tunnels going north and east./n");
                 }//ends else
      scanf("%c", c;//assigns a value to c
      switch (c){
             case 'e':
                  printf("You were attacked by bats and killed. Start over/n");
                  break;
             case 's':
                  printf("You cannot go that way./n");
                  break;
             case 'w':
                  printf("You cannot go that way./n");
                  break;
             case 'n':
                  printf("You chose the correct path. You are out of the cave. You are standing in an open field. Where do you go?/n");
                  break;
             case 'x':
                  return 0;
                  break;
                  }
                     



The newline character in C is \n, not /n. You should fix that and you could also add a loop to keep it going until the user presses 'x' or whatever the exit key is. And I think for scanf that you have to use scanf("%d", &a); instead of scanf("%d", a) but I'm not completely sure about that. I'm not really that used to using printf and scanf, as I use C++ instead.
Ntvu
Poster
Poster
 
Posts: 159
Joined: Wed Jun 04, 2008 2:46 pm
Blog: View Blog (0)


Re: Batch

Post by Sharmz on Sun Jul 13, 2008 11:47 pm
([msg=7379]see Re: Batch[/msg])

Is there a different way I could write the whole scenario of which way to go? the switch statement doesn't quiet cut it? and how can i make it start over from the beginning when the user inputs something?
thanks!
"You must be the change you wish to see in the world."
-Mohandas Karamchand Gandhi
"Be nice to nerds. Chances are you'll end up working for one."
-Bill Gates
Sharmz
New User
New User
 
Posts: 41
Joined: Thu May 29, 2008 1:18 am
Blog: View Blog (0)


Re: Batch

Post by BhaaL on Mon Jul 14, 2008 11:03 am
([msg=7391]see Re: Batch[/msg])

It would probably make sense to add a 2-dimensional (or pseudo-2-dimensional is enough actually) array with predefined answers.

Code: Select all
char *field[][] = {
  { null, null, null, null, null },
  { null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
  { null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
  { null, "You fell off a cliff", "You fell off a cliff", "You win", null },
  { null, null, null, null, null }
};
int xPosition = 2;
int yPosition = 2;


And then simply loop, and see where you can go:
Code: Select all
char direction;

while (direction != 'x') {
  printf("Where do you want to go? 'x' to quit\n");
  scanf("%c", &direction);
  switch (direction) {
    case 'n':
      if (field[xPosition][xPosition - 1] == null) printf("You cannot go there\n");
      else {
        yPosition--;
        printf("%s\n", field[xPosition][yPosition]);
      }
      break;
    case 'e': case 'w': case 's': //appropriate stuff like 'n'
    case 'x':
      printf("Ok, quitting.\n");
      break;
  }
}


You could extend that loop to allow more commands, like starting over ('r', resetting x/yPosition), or even make it read whole words/sentences, to make a more/less "real" parser game out of it (like "go north", "take red flower" etc)
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Batch

Post by Sharmz on Mon Jul 14, 2008 12:51 pm
([msg=7402]see Re: Batch[/msg])

Can you please explain your code a little?, I'm new to C and haven't really covered arrays yet. This is a really good way to learn. Thanks a lot for your time & patience with me :?

BhaaL wrote:It would probably make sense to add a 2-dimensional (or pseudo-2-dimensional is enough actually) array with predefined answers.

Code: Select all
char *field[][] = {
  { null, null, null, null, null },
  { null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
  { null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
  { null, "You fell off a cliff", "You fell off a cliff", "You win", null },
  { null, null, null, null, null }
};
int xPosition = 2;
int yPosition = 2;


And then simply loop, and see where you can go:
Code: Select all
char direction;

while (direction != 'x') {
  printf("Where do you want to go? 'x' to quit\n");
  scanf("%c", &direction);
  switch (direction) {
    case 'n':
      if (field[xPosition][xPosition - 1] == null) printf("You cannot go there\n");
      else {
        yPosition--;
        printf("%s\n", field[xPosition][yPosition]);
      }
      break;
    case 'e': case 'w': case 's': //appropriate stuff like 'n'
    case 'x':
      printf("Ok, quitting.\n");
      break;
  }
}


You could extend that loop to allow more commands, like starting over ('r', resetting x/yPosition), or even make it read whole words/sentences, to make a more/less "real" parser game out of it (like "go north", "take red flower" etc)
"You must be the change you wish to see in the world."
-Mohandas Karamchand Gandhi
"Be nice to nerds. Chances are you'll end up working for one."
-Bill Gates
Sharmz
New User
New User
 
Posts: 41
Joined: Thu May 29, 2008 1:18 am
Blog: View Blog (0)


Re: Batch

Post by BhaaL on Tue Jul 15, 2008 11:16 am
([msg=7473]see Re: Batch[/msg])

Sharmz wrote:Can you please explain your code a little?, I'm new to C and haven't really covered arrays yet. This is a really good way to learn. Thanks a lot for your time & patience with me :?

Sure, no problem.

Code: Select all
//physically, an array is nothing more than a series of variables.
//another way to show the following array would be:
//char *field_0_0 = null;
//char *field_0_1 = null;
//...
//char *field_2_2 = "A red flower blooms here";
//etc.
//it simply groups together stuff that belongs together logically.
char *field[][] = {
  { null, null, null, null, null },
  { null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
  { null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
  { null, "You fell off a cliff", "You fell off a cliff", "You win", null },
  { null, null, null, null, null }
};
//variables for later, keep track where we are.
//if the field value is null, we hit a wall.
int xPosition = 2;
int yPosition = 2;


Code: Select all
char direction; //char the user entered, one of n s w e

while (direction != 'x') {
  printf("Where do you want to go? 'x' to quit\n");
  scanf("%c", &direction);
  //%c reads a character, &direction because we need
  //the address of it (look into pointers for more info)
  switch (direction) {
    case 'n':
      //check the field at yPosition - 1 (which is north of it. imagine that array as coordinate system spanning left/down)
      if (field[xPosition][yPosition - 1] == null) printf("You cannot go there\n");
      else {
        //field is not null, lets move there. yPosition-- decrements yPosition by one.
        yPosition--;
        printf("%s\n", field[xPosition][yPosition]);
      }
      break;
    case 'e': case 'w': case 's': //appropriate stuff like 'n'
    case 'x':
      printf("Ok, quitting.\n");
      break;
  }
}
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests