ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been writing a simple text-based rpg game for a project for my Linux-Scripting class and I would like to know if there's a standard for setting up a project in Bash. So far I've made a directory called rpg_lite that contains two files: one file that serves as the main file that sources the second file which contains all of my functions. If you had multiple associative arrays where would be the best place to put them and how would you organize this? Here's a snippet:
Code:
#!/usr/bin/env bash
. my_functions
inventory "$weapons" # prints available weapons
Since these are (apparently) definitions of game data (weapons and such), I'd probably store them in an external "data" file in the same place as the functions.
declare -A apparel=(
['Steel Platemail']=108
['Studded Cuirass']=102
['Heavy Platemail']=110
['Assassin Robes']=60
)
# Items => Damage
declare -A weapons=(
['Edged Shuriken']=25
['Steel Claymore']=90
['Imperial Sword']=73
['Tainted Knives']=15
)
# Holds => Cities
declare -A regions=(
['Eastern Palace']='Imperial City'
['Darkwood Basin']='Undead Asylum'
['Redwood Valley']='Thieves Guild'
)
inventory() {
declare -p $1 | awk -F '"' '{ for(i=2; i<=NF; i+=4) print $i,"=>",$(i+2) }'
}
get_magic() {
spells=()
while IFS= read -r line; do
spells+=("$line")
done < $HOME/current_spells
printf "%s\n" "${spells[@]}"
}
locations() {
for city in "${!regions[@]}"; do
printf "%s\t%s\n" "$city" "${regions[$city]}"
done | sort
}
check_key() {
local holds city=$1
shift
for holds; do
[[ $holds == $city ]] && return 0
done
return 1
}
traveling() {
locations
read -p 'Travel to which capitol? ' on_foot
if check_key "$on_foot" "${regions[@]}"; then
printf "Now entering: %s\n" "$on_foot"
else
echo 'You got lost and rested at an Inn..'
fi
}
list_menu() {
# Should be exclusive to the player's equipment
selection=( 'Apparel' 'Weapons' 'Spells' 'Exit' )
for (( i=0; i<${#selection[@]}; i++ )); do
printf "%2d. %s\n" "$((i+1))" "${selection[$i]}"
done
}
I started this awhile ago, and kind of just had fun with by adding things to it day by day. I tried not to repeat myself. I just couldn't figure out how to simulate attacking an enemy.
It's quite admirable that you're brave enough to do this in Bash though I would recommend making it work in other scripting languages like Ruby instead.
Anyhow if you plan to organize your code into multiple source files then you might find this tool helpful: Shell Script Loader. It would allow you to call your files elegantly like how it's done in most languages.
And you might try looking at other projects as well for design concepts that you might find useful incorporating in your project.
Last edited by konsolebox; 07-24-2013 at 08:02 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.