LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2013, 03:57 PM   #1
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Rep: Reputation: Disabled
Organizing bash files


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
 
Old 07-23-2013, 01:49 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Lucien Lachance View Post
I would like to know if there's a standard for setting up a project in Bash.
BASH is a shell. It has no concept of "projects" so feel free to use what suits you (common sense).


Quote:
Originally Posted by Lucien Lachance View Post
If you had multiple associative arrays where would be the best place to put them
Wherever they're accessible for inclusion int he game? Not sure what the real reason is for asking this? Maybe add examples to illustrate?
 
1 members found this post helpful.
Old 07-23-2013, 11:23 AM   #3
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
What you said makes sense, I lost sight of that.
 
Old 07-23-2013, 01:20 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-23-2013, 02:42 PM   #5
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
Code:
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.
 
Old 07-24-2013, 08:00 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
  


Reply

Tags
bash, linux, rpg, scripting, shell scripting



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can anyone help me with fully understanding and organizing my bash startup scripts? Bowlslaw Slackware 2 08-07-2011 03:20 PM
Bash for organizing by alphabet IanGlenn Programming 8 12-08-2007 05:40 PM
organizing files endezeichen Linux - General 10 04-13-2004 03:21 PM
Organizing C code: .c files vs .h MadCactus Programming 11 01-14-2004 04:57 AM
organizing my files and directories ryancw Linux - Newbie 1 08-30-2003 09:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration