Writing a text adventure game in BASH

Forum rules
Before you post please read how to get help
Post Reply
User avatar
UOOP
Level 1
Level 1
Posts: 6
Joined: Mon Jan 29, 2018 2:40 pm

Writing a text adventure game in BASH

Post by UOOP » Mon Jan 29, 2018 3:20 pm

So I am working on a text adventure game which I am writing in BASH. I am having some issues with trying to code my shop:

I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ? I want to put this within a while loop, something like:

while read -p "-->" var; do
if [ "$var" = "buy item" ]; then
if (add command to verify file is present in directory); then
echo "item purchased"
else
echo "item not available"
fi
fi
done

I have tried a few different commands and I will either end up getting item purchased every time, or item not available every time. Any ideas?
--Linux Convert--

fabien85
Level 6
Level 6
Posts: 1343
Joined: Tue Mar 11, 2014 4:30 pm

Re: Writing a text adventure game in BASH

Post by fabien85 » Mon Jan 29, 2018 4:54 pm

What you want is [ -f ]. Example :

Code: Select all

$ touch item
$ [ -f item ] && echo "item purchased" || echo "item not available"
item purchased
$ rm item
$ [ -f item ] && echo "item purchased" || echo "item not available"
item not available
Good luck with your game ! :D

User avatar
Termy
Level 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Writing a text adventure game in BASH

Post by Termy » Mon Jan 29, 2018 7:02 pm

You should also most definitely, absolutely, undeniably use at the very least some standard indentation. :P I wouldn't be happy until it looked like this, regarding fairly standard indentation:

Code: Select all

while read -p "-->" var; do
	if [ "$var" = "buy item" ]; then
		if (add command to verify file is present in directory); then
			echo "item purchased"
		else
			echo "item not available"
		fi
	fi
done
By the way, you may wish to add a space after the read prompt; makes things look less squished.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

User avatar
MintBean
Level 9
Level 9
Posts: 2947
Joined: Fri Aug 07, 2015 6:54 am
Location: Blighty

Re: Writing a text adventure game in BASH

Post by MintBean » Mon Jan 29, 2018 7:32 pm

Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.

whm1974
Level 4
Level 4
Posts: 237
Joined: Fri Jan 19, 2018 11:07 pm

Re: Writing a text adventure game in BASH

Post by whm1974 » Mon Jan 29, 2018 7:46 pm

MintBean wrote:Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.
There are many FOSS Infocom style text game engines available. I would use one of those.

User avatar
Termy
Level 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Writing a text adventure game in BASH

Post by Termy » Tue Jan 30, 2018 10:18 am

MintBean wrote:Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.
I have a very, very pleasant experience writing shell programs. Not sure why people hate on it so much, as it's much more flexible than people realise, and I'd argue very approachable. Shell is also ideal for embedded systems and as the glue which ties together other programs, not to say that shell (specifically, bash) isn't very capable on its own. I've written and still maintain many shell programs, and related tools (such as function libraries and bash completion scripts), some extensive and complex, some small and simple, and each one I find enjoyable to write and maintain. I'm not saying it's perfect, but it's another tool in the toolbox.

Point? How you feel doesn't dictate how everyone else feels. To each their own, basically.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

lmuserx4849

Re: Writing a text adventure game in BASH

Post by lmuserx4849 » Wed Jan 31, 2018 12:28 am

UOOP wrote: I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ?
I believe what you need is the test command, which can be implemented via [ or [[. To see doc, type help test or man bash and look for the section CONDITIONAL EXPRESSIONS

if [[ ! -e "${filename}" ]]; then echo "File does not exist: ${filename}"; else : ; fi

declare -- filename=''; while [[ ! -e "${filename}" ]]; do read -r -p 'Enter some data: ' filename; done

echo can be replaced with printf -- 'Files does not exist: %s\n' "${filename}"

Just for fun - caterpillar game written in bash :-)
Last edited by lmuserx4849 on Wed Jan 31, 2018 12:57 pm, edited 1 time in total.

User avatar
UOOP
Level 1
Level 1
Posts: 6
Joined: Mon Jan 29, 2018 2:40 pm

Re: Writing a text adventure game in BASH

Post by UOOP » Wed Jan 31, 2018 4:21 am

lmuserx4849 wrote:
UOOP wrote: I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ?
I believe what you need is the test command, which is can be implemented via [ or [[. To see doc, type help test or man bash and look for the section CONDITIONAL EXPRESSIONS

if [[ ! -e "${filename}" ]]; then echo "File does not exist: ${filename}"; else : ; fi

declare -- filename=''; while [[ ! -e "${filename}" ]]; do read -r -p 'Enter some data: ' filename; done

echo can be replaced with printf -- 'Files does not exist: %s\n' "${filename}"

Just for fun - caterpillar game written in bash :-)

Thanks for the feedback, lmuserx4849! Code worked perfectly :D
--Linux Convert--

User avatar
UOOP
Level 1
Level 1
Posts: 6
Joined: Mon Jan 29, 2018 2:40 pm

Re: Writing a text adventure game in BASH

Post by UOOP » Wed Jan 31, 2018 4:30 am

Termy wrote:You should also most definitely, absolutely, undeniably use at the very least some standard indentation. :P I wouldn't be happy until it looked like this, regarding fairly standard indentation:

Code: Select all

while read -p "-->" var; do
	if [ "$var" = "buy item" ]; then
		if (add command to verify file is present in directory); then
			echo "item purchased"
		else
			echo "item not available"
		fi
	fi
done
By the way, you may wish to add a space after the read prompt; makes things look less squished.
I don't know what happened, but I did have the code indented :D Probably not exactly like that, but it was.

When I switched to Linux, I started learning terminal commands and then found out you could put those commands within a script very easily. I tried a couple of simple scripts and got hooked. I'm the kind of guy that likes to work from the ground up, knowing what's behind the scenes. This is why I didn't bother trying to use anything else. Others have asked "why don't you use a generator?" Well that's no fun is it? I guess it's an "I built this by hand" type of thing. It takes a while to write something out if you don't create short cut notes (like I do :D). So far I have enjoyed it and I love the challenge.
--Linux Convert--

Post Reply

Return to “Scripts & Bash”