# Tricky varying of table names

Hello all,

I was delighted today to see there is a manual section called “Varying which table to look at” but I’m still one piece of insight short of fixing my problem.

My story has a game of chess. I’ve by no means programmed in the rules of chess, but the player can play a few moves to start a game. There are about 50 positions they might see, and each has a table of legal moves associated with it that is checked when the player tries to move a piece. My idea was to name each table “Table 0”, “Table 1”, etc., and also to have a number variable called “theory” which keeps track of which position the board is in (0, 1, etc.). When the player moves a piece, I want it to check the current value of theory and use that to pick out which table to refer to.

But I can’t see how to do this, short of including a separate “if” statement for every different theory number. (if theory is 0, use Table 0, if theory is 1, use Table 1, etc. but of course in actual code-form).

Basically I want to be able to refer to “Table [theory]”, but of course text substitutions only work in text. Any ideas?

This sounds snarky, but: store them in a table? (Or a list, which is basically a one-column table.)

I had a vague thought about making a table to match up theory numbers with table names or something… but I’m lost as to how that would actually work.

You can make a table whose first column is numbers and whose second column is table names. Then choose a row in it with (whatever you decide to call the first column) of the theory, extract the table name entry, and do what you need to it. An important thing to note is that when you want to talk about a table in some variable fashion, the kind you’re using is actually called “table name.”

Here’s a possibly somewhat opaque example of how it might work. (I just hacked the moving action to redirect it from moving from one position to another, so I didn’t have to define a new action.)

[code]“Les Tables of Tables”

The theory is a number that varies. The theory is 1.

Chessatorium is a room. “You see the board here in position [theory]. The allowable moves are [allowable moves].”

Table of first position
move result
north 2
west 3

Table of second position
move result
east 1
south 4

Table of third position
move result
north 1
east 2

Table of fourth position
move result
east 1
west 3

Table of theory tables
position corresponding table
1 Table of first position
2 Table of second position
3 Table of third position
4 Table of fourth position

To decide which table name is the corresponding table of (N - a number):
unless there is a position of N in the table of theory tables:
decide on table of first position; [this should never happen, it’s a failsafe]
choose a row with position of N in the table of theory tables;
decide on the corresponding table entry.

To say allowable moves:
Let L be a list of directions;
repeat through the corresponding table of the theory:
add the move entry to L;
say L.

Instead of going in Chessatorium: [note that the noun is the direction you’re going]
unless there is a move of the noun in the corresponding table of the theory:
say “That is not an acceptable move.”;
stop the action;
choose a row with move of the noun in the corresponding table of the theory;
now the theory is the result entry;
say “You move [noun] to position [theory]. You can now move [allowable moves].”[/code]

Of course this will get fairly annoying with fifty positions, but not as much as a giant if statement. And you could dispense with the first column, since you can always choose row N in the table of theory tables, but that seems like it’d be harder to read and more vulnerable to typos.

EDIT: By the way, in this old thread, I’m pretty sure the code I was trying to remember and couldn’t was

``Understand "pawn" as a pawn.``

And if you want to try out the code I posted above, follow the instructions for copy-pasting in that thread, because you want to keep those tab stops.

Thank you! I tried implementing that and it works well. I actually stumbled across another solution after tove brought up the idea of using a special table. I made ‘Keyword’ a table name that varies, and then toward the beginning of the action, wrote a ‘Now’ statement to make Keyword be a particular entry in my ‘Table of theory tables’. But I really need to learn more about how ‘To decide…’ works, so this is helpful, and it might end up being more elegant.

``choose a row with position of N in the table of theory tables;``

You can leave off the “position” column and just say “choose row N in the table of theory tables.”

Ah, you found those old posts! Yes, I started this chess project quite a while ago, and then got distracted from the world of IF. I’m trying to get back into it now! I hadn’t noticed you’re the same person who helped so much last time - my use of “Alice” as a temporary variable for repeats can be traced back to you.

What Zarf suggests will run faster, and if you do that you can also leave out the position column.

You could even cut out the middleman and store the corresponding table names within each movement table directly.

[code]“Les Tables of Tables”

The theory is a table name that varies. The theory is Table of first position.

Chessatorium is a room. “You see the board here in [position of theory]. The allowable moves are [allowable moves].”

Table of first position
move result
north Table of second position
west Table of third position

Table of second position
move result
east Table of first position
south Table of fourth position

Table of third position
move result
north Table of first position
east Table of second position

Table of fourth position
move result
east Table of first position
west Table of third position

To say allowable moves:
Let L be a list of directions;
repeat through the theory:
add the move entry to L;
say L.

Instead of going in Chessatorium: [note that the noun is the direction you’re going]
unless there is a move of the noun in the theory:
say “That is not an acceptable move.”;
stop the action;
choose a row with move of the noun in the theory;
now the theory is the result entry;
say “You move [noun] to [position of theory]. You can now move [allowable moves].”

To say position of (T - a table name):
let ord be word number 3 in “[T]”;
say “[ord] position”.[/code]

``````Table of e4/e5