So I have an object with several integer attributes; let's say, for argument's sake, object “treasure_chest”, with integer attributes “doubloons”, “jewels”, “ingots”, “artifacts”, and “idols”. I want to list the attributes like this:
“The treasure chest has 230 doubloons and 6 ingots,” or “The treasure chest has 17 jewels, 2 artifacts, and 1 idol.”
I want to create an algorithm where
1) it won't list the attribute if it doesn't have a value above zero,
2) it will adjust to the singular if the value is one,
3) it will print a comma after the attribute in question UNLESS it's the LAST attribute to have a value above zero, in which case it will
4) print a period after the attribute in question, and
5) print an “and” before the number UNLESS it's the ONLY attribute to have a value above zero.
6) Also, I've implemented a feature where it won't place a comma if two and only two attributes have values above zero.
My game has several of these pseudo-containers; I'll spare you the explanation for why they work with the dynamics of the game, sate to say that they do. This is what I have right now (which I assign to a command), and it does technically work, but it doesn't feel very elegant. My own code is hurting my eyes. Add onto that that some of these pseudo-containers will have dozens of these attributes, and you're looking at a lot of work. If anyone has a clever alternate approach, I'm all ears.
// set to command "check"
if ((treasure_chest.doubloons = 0) and (treasure_chest.jewels = 0) and (treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
msg ("The treasure chest is empty. Poor, disappointed pirate!")
}
else {
inv_count = 0
if (treasure_chest.doubloons > 0) {
inv_count = inv_count + 1
}
if (treasure_chest.jewels > 0) {
inv_count = inv_count + 1
}
if (treasure_chest.ingots > 0) {
inv_count = inv_count + 1
}
if (treasure_chest.artifacts > 0) {
inv_count = inv_count + 1
}
if (treasure_chest.idols > 0) {
inv_count = inv_count + 1
}
OutputTextNoBr ("Inside the chest you see ")
if (treasure_chest.doubloons > 0) {
if (treasure_chest.doubloons = 1) {
OutputTextNoBr ("1 doubloon")
if ((treasure_chest.jewels = 0) and (treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
else {
OutputTextNoBr (treasure_chest.doubloons + " doubloons")
if ((treasure_chest.jewels = 0) and (treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
}
if (treasure_chest.jewels > 0) {
if (treasure_chest.jewels = 1) {
if ((treasure_chest.doubloons > 0) and ((treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0))) {
OutputTextNoBr (" and ")
}
OutputTextNoBr ("1 jewel")
if ((treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
else {
if ((treasure_chest.doubloons > 0) and ((treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0))) {
OutputTextNoBr (" and ")
}
OutputTextNoBr (treasure_chest.jewels + " jewels")
if ((treasure_chest.ingots = 0) and (treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
}
if (treasure_chest.ingots > 0) {
if (treasure_chest.ingots = 1) {
if (((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0)) and ((treasure_chest.artifacts = 0) and (treasure_chest.idols = 0))) {
OutputTextNoBr (" and ")
}
OutputTextNoBr ("1 ingot")
if ((treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
else {
if (((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0)) and ((treasure_chest.artifacts = 0) and (treasure_chest.idols = 0))) {
OutputTextNoBr (" and ")
}
OutputTextNoBr (treasure_chest.ingots + " ingots")
if ((treasure_chest.artifacts = 0) and (treasure_chest.idols = 0)) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
}
if (treasure_chest.artifacts > 0) {
if (treasure_chest.artifacts = 1) {
if (((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0) or (treasure_chest.ingots > 0)) and (treasure_chest.idols = 0)) {
OutputTextNoBr (" and ")
}
OutputTextNoBr ("1 artifact")
if (treasure_chest.idols = 0) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
else {
if (((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0) or (treasure_chest.ingots > 0)) and (treasure_chest.idols = 0)) {
OutputTextNoBr (" and ")
}
OutputTextNoBr (treasure_chest.artifacts + " artifacts")
if (treasure_chest.idols = 0) {
OutputTextNoBr (".")
}
else {
if (inv_count <> 2) {
OutputTextNoBr (", ")
}
}
}
}
if (treasure_chest.idols > 0) {
if (treasure_chest.idols = 1) {
if ((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0) or (treasure_chest.ingots > 0) or (treasure_chest.artifacts > 0)) {
OutputTextNoBr (" and ")
}
OutputTextNoBr ("1 idol.")
}
else {
if ((treasure_chest.doubloons > 0) or (treasure_chest.jewels > 0) or (treasure_chest.ingots > 0) or (treasure_chest.artifacts > 0)) {
OutputTextNoBr (" and ")
}
OutputTextNoBr (treasure_chest.idols + " idols.")
}
}
}
I'm including for you the bit of code I run at the beginning of this game to test this algorithm, which I also assign to a command.
// set to command "reroll"
doubloons_dice = DiceRoll("1d5")
jewels_dice = DiceRoll("1d5")
ingots_dice = DiceRoll("1d5")
artifacts_dice = DiceRoll("1d5")
idols_dice = DiceRoll("1d5")
if (doubloons_dice = 5){
treasure_chest.doubloons = DiceRoll("1d5") + 1
}
else if (doubloons_dice = 4){
treasure_chest.doubloons = 1
}
else{
treasure_chest.doubloons = 0
}
if (jewels_dice = 5){
treasure_chest.jewels = DiceRoll("1d5") + 1
}
else if (jewels_dice = 4){
treasure_chest.jewels = 1
}
else{
treasure_chest.jewels = 0
}
if (ingots_dice = 5){
treasure_chest.ingots = DiceRoll("1d5") + 1
}
else if (ingots_dice = 4){
treasure_chest.ingots = 1
}
else{
treasure_chest.ingots = 0
}
if (artifacts_dice = 5){
treasure_chest.artifacts = DiceRoll("1d5") + 1
}
else if (artifacts_dice = 4){
treasure_chest.artifacts = 1
}
else{
treasure_chest.artifacts = 0
}
if (idols_dice = 5){
treasure_chest.idols = DiceRoll("1d5") + 1
}
else if (idols_dice = 4){
treasure_chest.idols = 1
}
else{
treasure_chest.idols = 0
}