Trying to 'reset' to starting values

I am very new to Inform, and in my first project I’ve taken on the perhaps too-difficult task of making a chess board. So I have ‘pieces’ as a kind, and pawns and knights etc. as kinds of pieces. Pieces have two values, position and color, where position is a1, a2, … a8, b1, b2 etc… I made an action that lets you “play e2 to e4”, which changes the position value of the piece on e2 to e4.

My problem is that I want an easy way to reset all the pieces back to their original values. There are lots of different conditions that cause the game to end, and I want to be able to point them all to a resetting function - maybe by putting in “try resetting” wherever I need it, and defining the action to do what it needs to do. But I don’t know how to write it. The reset needs to work regardless of the position of the pieces.

I think there are probably lots of clever ways to do this but I don’t understand Inform (especially values) well enough. Can anyone suggest an idea?

What about giving each piece an original_position value that you can reset their position value to when needed?

Sounds like it could help, but two things -

  1. Wouldn’t they original_position values have to have the same names as the position values, and wouldn’t that be problematic?

  2. I can’t seem to change the position value of any of my pieces to something set - like changing an e4 pawn to be an e2 pawn. Whenever I try to refer to the e4 pawn Inform tells me “this is not explicit enough”, I guess because all my pawns are the same thing. The only way I’ve been able to change the values is through player commands, I guess because when the player types e2 pawn, it is able to pick that out as one individual thing. But it never works in the source - if I start a line with ‘now the e2 pawn…’ it says I’m not explicit enough.

No. Something like this (untested, YMMV):

A chess piece is a kind of thing. A chess piece has a room called original_position. [I don't know are you using rooms? or doing something else?]

A king is a kind of chess piece.

A queen is a kind of chess piece.

[...etc]

When play begins:
    repeat with piece running through all chess pieces:
        now the original_position of the piece is the location of the piece.

Now when resetting:

repeat with piece running through all chess pieces:
    now the location of the piece is the original_position;

Not sure I quite understand, but in any case I want to do this all in one room. So I made a value:

Position is a kind of value. The positions are a1, a2, a3, a4, a5, a6, a7, a8, b1, b2, b3, b4, b5, b6, b7, b8, c1, c2, c3, c4, c5, c6, c7, c8, d1, d2, d3, d4, d5, d6, d7, d8, e1, e2, e3, e4, e5, e6, e7, e8, f1, f2, f3, f4, f5, f6, f7, f8, g1, g2, g3, g4, g5, g6, g7, g8, h1, h2, h3, h4, h5, h6, h7, and h8.

And a kind called pieces, and then particular pieces, like this:

A pawn is a kind of piece. On the board are a white a2 pawn, a white b2 pawn, a white c2 pawn, etc...

Now when I try to refer, in the code, to one of these pawns to change it’s value, I’m told I’m not being explicit enough. And if I try to create another value called StartingPosition, so that the pieces have ‘memory’ of where they came from, I can’t because it can’t have possible values a1, a2, etc. since they’re already used.

Overall, this looks pretty complicated as something to start with. However, defining original-position shouldn’t be too awful. You can say “Every pawn has a position called current-position” and also “Every pawn has a position called original-position”; that way, you can write a routine that sets the current-position of all the pawns to their original-positions, and invoke it whenever you need to.

I wasn’t quite sure about what you said about referring to “black b2 pawn” etc. in the source, but if you just write “on the board is a black b2 pawn” etc., you won’t actually create a pawn (let alone one with color black and position b2); you’ll just create a thing called “black b2 pawn.” At least that’s what happened when I tried to type it in. To avoid giving myself the screaming heebie-jeebies, I’d define the pieces with a table, as in section 15.16. Even then, many pitfalls await.

I put a semi-working thingy under the spoiler tag. If you want to try it out, hit the “quote” button as if you were replying to this post, then copy-paste the code out of that box; that’ll preserve the all-important tab stops.

[spoiler][code]Position is a kind of value. The positions are a1, a2, a3, a4, a5, a6, a7, a8, b1, b2, b3, b4, b5, b6, b7, b8, c1, c2, c3, c4, c5, c6, c7, c8, d1, d2, d3, d4, d5, d6, d7, d8, e1, e2, e3, e4, e5, e6, e7, e8, f1, f2, f3, f4, f5, f6, f7, f8, g1, g2, g3, g4, g5, g6, g7, g8, h1, h2, h3, h4, h5, h6, h7, and h8.

Color is a kind of value. The colors are black and white.
A pawn is a kind of thing. A pawn has a color. A pawn has a position called current-position. A pawn has a position called original-position.
[Note that the pawn has two different positions associated with it; this is legal.]
The printed name of a pawn is “[color of the item described] [current-position of the item described] pawn”.
[This means that, whenever the game talks about the pawn, it’ll say “white a5 pawn” or whatever instead of using the goofy names defined in the table.]

Chess club is a room. A board is a supporter in chess club.

Some pawns are defined by the table of chessfolk. [Unfortunately the compiler barfs if you say “table of pawns.” Hence this dumb name.]

Table of chessfolk
pawn color original-position
white-a pawn white a2
white-b pawn white b2
white-c pawn white c2
black-a pawn black a7
black-b pawn black b7
black-c pawn black c7 [Also, unfortunately, the compiler barfs if you name pawns things like “white b pawn” without the hyphen. Apparently, it then interprets “white” in the color column as referring to the white b pawn thing, instead of the color “white.” There may be a way around this namespace clash, but I just put in a hyphen.]

To reset the pawns: [This is the rule for restoring things to the original position. Alice is just a temporary variable name.]
repeat with Alice running through pawns:
now Alice is on the board; [this was the easiest way I could think of to put all the pawns on the board]
now the current-position of Alice is the original-position of Alice.

When play begins: reset the pawns. [This way we don’t actually have to specify the starting current-position in the table.]

[Now let’s implement some moves.]
Moving it to is an action applying to one thing and one position.
Understand “move [something] to [position]” as moving it to.
Check moving it to: if the noun is not a pawn, say “You can only move pawns.” instead. [You could also write rules restricting the moves to legal positions. Better you than me, I fear.]
Carry out moving it to:
now the current-position of the noun is the position understood.
Report moving it to:
say “You move the pawn to [current-position of the noun].” [If we were being really stylish, we’d save the old current-position so we could say which square you were moving it from as well. We’re not being really stylish.]

Understand the current-position property as describing a pawn. Understand the color property as describing a pawn. [This means that commands like “move black pawn” and “move a5 pawn” will be understood. I’m not sure, but I think “pawn” may be understood only because all the pawns have “pawn” in their names. There’s a better way to make that happen but I came down with a case of the fails and couldn’t find it. “white-a pawn” will probably also be understood, but there’s no risk of the player actually trying it.]

Moving it wrong is an action applying to one thing and one topic. Understand “move [something] to [text]” as moving it wrong. [This just catches cases where the player types in an invalid position.]
Instead of moving something wrong: say “You can’t move something to [topic understood]; you can only move things to positions on the board.”

Resigning is an action applying to nothing. Understand “resign” as resigning.

Carry out resigning: reset the pawns.
Report resigning: say “You move the pawns back to their original places.”[/code][/spoiler]

Thanks! - I had a lot of that code but there were a few key things I was missing. One was the “repeat with … running through the pieces” mechanism that you and the previous poster mentioned - I didn’t know anything about that before. But the real aha! was seeing that your pawns have two different position values, but only one is “understood…as describing the pawn”, so that there won’t be any confusion when the player refers to them.

The project won’t actually be too tough - the games will be four moves long at most, and only one or two in most lines.

Ah, I thought you maybe wanted to implement a lot of different possible moves, which seems like it would be well into blinding headache territory. Understanding via properties and relations is wonderful and weird stuff, anyway.

[Are you related to the baseball player, by any chance?]

I am not, but that he is the inspiration for the username.
Thanks again for the help, I’ve got the whole thing working very well now. I added PieceType to the chart so now I don’t need separate kinds for pawns and knights and such, and can use [PieceType of noun] in my descriptions - very happy!

Hope this necro is okay…I wrote the following to make a grid, for my own small game. This simply checks to make sure input is valid and maps the text to a number. You can also move around.

[spoiler]“a1-calc” by andrew

alphabet is a list of indexed text that varies.

alphabet is { “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j” };

gridsquares is a list of indexed text that varies.

startingsquare is a number that varies. endingsquare is a number that varies.

to make-grid:
let z be indexed text;
let w be text;
repeat with x running from 1 to number of entries in alphabet:
repeat with y running from 1 to number of entries in alphabet:
let z be “[entry y of alphabet][x]”;
add z to gridsquares;

when play begins:
make-grid;

carry out converting:
repeat with J running from 1 to number of entries in gridsquares:
if the topic understood matches the text “[entry J of gridsquares]”:
say “Ooh, a match: [J] matches [the topic understood].”;
the rule succeeds;
let temp be the number of entries in alphabet;
say “Sorry, that’s not a valid grid-square. Try [entry 1 of alphabet]1-[entry temp of alphabet][temp].”;

converting is an action applying to one topic.

understand the command “convert [text]” as something new.

understand “convert [text]” as converting.

rule for printing a parser error:
let validwords be 0;
repeat with J running from 1 to number of entries in gridsquares:
if word number 1 in the player’s command matches the text “[entry J of gridsquares]”:
say “Ooh, a match: [J] matches [word number 1 in the player’s command].”;
now startingsquare is J;
increment validwords;
repeat with J running from 1 to number of entries in gridsquares:
if word number 2 in the player’s command matches the text “[entry J of gridsquares]”:
say “Ooh, a match: [J] matches [word number 2 in the player’s command].”;
now endingsquare is J;
increment validwords;
expostulate instead;

to expostulate:
say “[startingsquare] to [endingsquare], bam!”;
the rule succeeds;

room 1 is a room.[/spoiler]

I have a feeling I missed a bunch of stuff here, and the code seems rather awkward and blunt. If anyone has some suggestions for touchups, I’d be interested. It may generalize better. But it maybe doesn’t help you do what you want.

I guess my concern is there any way to make the value of indexed text a variable name? Or be able to refer to it as something more easily? Or detect, say, c3 in my above code?

Also, and this would be easiest, is there an easy way to say “the index of (some text) in a list, if present”? Or would it be better to use a table?

Even if there’s no answer to my lead-on question, thanks very much for asking this in the first place–it reminded me of something I’d wanted to try for a while.