Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 22, 2010 string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Hello. I want to use associative arrays, but have a 2-d int array as my index. so something like: string[int[][]] chessboard; chessboard[[0,0]] = "Rook"; chessboard[[0,1]] = "Knight"; Is this possible? I can declare chessboard without any compiler complaints, but I haven't figured out how to assign anything to it. Any help would be appreciated. thanks! dcoder |
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | == Quote from dcoder (dcoder@devnull.com)'s article
> Hello. I want to use associative arrays, but have a 2-d int array as my
> index. so something like:
> string[int[][]] chessboard;
> chessboard[[0,0]] = "Rook";
> chessboard[[0,1]] = "Knight";
> Is this possible? I can declare chessboard without any compiler complaints,
> but I haven't figured out how to assign anything to it.
> Any help would be appreciated.
> thanks!
> dcoder
Ooops, I just realized that int[someX][someY] needs to evaluate to an int, so that my statement above doesn't make any sense.
Still, is there anyway I can keep the spirit of a double index to a chessboard without having to create a Coordinate object and define relational orderings?
thanks.
dcoder.
|
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | On 22/07/2010 22:57, dcoder wrote:
> == Quote from dcoder (dcoder@devnull.com)'s article
>> Hello. I want to use associative arrays, but have a 2-d int array as my
>> index. so something like:
>> string[int[][]] chessboard;
>> chessboard[[0,0]] = "Rook";
>> chessboard[[0,1]] = "Knight";
>> Is this possible? I can declare chessboard without any compiler complaints,
>> but I haven't figured out how to assign anything to it.
>> Any help would be appreciated.
>> thanks!
>> dcoder
>
>
> Ooops, I just realized that int[someX][someY] needs to evaluate to an int, so that
> my statement above doesn't make any sense.
>
> Still, is there anyway I can keep the spirit of a double index to a chessboard
> without having to create a Coordinate object and define relational orderings?
>
> thanks.
>
> dcoder.
>
How about string[][]? ;)
|
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to awishformore | == Quote from awishformore (awishformore@nospam.plz)'s article
> >
> How about string[][]? ;)
yes, heheh... you are right.
I'm over thinking things.
Sorry about the noise.
|
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | On Thu, 22 Jul 2010 16:57:59 -0400, dcoder <dcoder@devenull.dev> wrote:
> == Quote from dcoder (dcoder@devnull.com)'s article
>> Hello. I want to use associative arrays, but have a 2-d int array as my
>> index. so something like:
>> string[int[][]] chessboard;
>> chessboard[[0,0]] = "Rook";
>> chessboard[[0,1]] = "Knight";
>> Is this possible? I can declare chessboard without any compiler complaints,
>> but I haven't figured out how to assign anything to it.
>> Any help would be appreciated.
>> thanks!
>> dcoder
>
>
> Ooops, I just realized that int[someX][someY] needs to evaluate to an int, so that
> my statement above doesn't make any sense.
>
> Still, is there anyway I can keep the spirit of a double index to a chessboard
> without having to create a Coordinate object and define relational orderings?
This is what I think you should use:
string[int[2]]
Although, I'm not sure if you can then do something like:
chessboard[[0,1]] = "Rook";
as the [0, 1] is typed as a dynamic array. If it does work, it may actually create [0,1] on the heap and then pass it as an int[2], which would suck.
Or, if you know how big your chessboard is (8x8 isn't a lot of memory), then:
string[8][8] chessboard;
is pretty straightforward :)
-Steve
|
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article > This is what I think you should use: > string[int[2]] > Although, I'm not sure if you can then do something like: > chessboard[[0,1]] = "Rook"; > as the [0, 1] is typed as a dynamic array. If it does work, it may > actually create [0,1] on the heap and then pass it as an int[2], which > would suck. board[[0,0]] = "Rook"; seems to work. thanks. But, the foreach loop looks strange. It looks like it takes the hash value of the key: string[int[2]] board; board[[0,0]] = "Rook"; board[[0,1]] = "Knight"; foreach( pos, val; board) { writefln( "%s: %s", pos, val); } Output: 2 9903680: Knight 2 9903696: Rook > Or, if you know how big your chessboard is (8x8 isn't a lot of memory), > then: > string[8][8] chessboard; > is pretty straightforward :) Yes it is :) Hehe.... Now, what if I wanted to do the following: class Board { string[][] positions; this( int x, y) { // set the size of positions } } I want positions to internally represent a chess board, a tic tac toe board, or a Connect Four board, etc... But, I want to fix the dimensions of the board when the board gets instantiated, so that I can have the compiler do all the work of bounds checking for me. I can create class variables maxx, maxy and access functions that check against the variables, but I'm wondering if there's a way to make the compiler do it for you. Is there a way? thanks. |
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | On 22/07/2010 23:21, dcoder wrote:
> == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article
>> This is what I think you should use:
>> string[int[2]]
>> Although, I'm not sure if you can then do something like:
>> chessboard[[0,1]] = "Rook";
>> as the [0, 1] is typed as a dynamic array. If it does work, it may
>> actually create [0,1] on the heap and then pass it as an int[2], which
>> would suck.
>
> board[[0,0]] = "Rook";
>
> seems to work. thanks. But, the foreach loop looks strange. It looks like it
> takes the hash value of the key:
>
> string[int[2]] board;
>
> board[[0,0]] = "Rook";
> board[[0,1]] = "Knight";
>
> foreach( pos, val; board) {
> writefln( "%s: %s", pos, val);
> }
>
>
> Output:
>
> 2 9903680: Knight
> 2 9903696: Rook
>
>
>
>
>
>
>
>> Or, if you know how big your chessboard is (8x8 isn't a lot of memory),
>> then:
>> string[8][8] chessboard;
>> is pretty straightforward :)
>
> Yes it is :) Hehe....
>
>
> Now, what if I wanted to do the following:
>
>
> class Board {
> string[][] positions;
>
> this( int x, y) {
> // set the size of positions
> }
> }
>
> I want positions to internally represent a chess board, a tic tac toe board, or a
> Connect Four board, etc...
>
> But, I want to fix the dimensions of the board when the board gets instantiated,
> so that I can have the compiler do all the work of bounds checking for me. I can
> create class variables maxx, maxy and access functions that check against the
> variables, but I'm wondering if there's a way to make the compiler do it for you.
>
> Is there a way?
>
> thanks.
>
string[8][8] positions; // fixed size array
|
July 22, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | On Thu, 22 Jul 2010 17:21:15 -0400, dcoder <blah@blah.com> wrote: > == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article >> This is what I think you should use: >> string[int[2]] >> Although, I'm not sure if you can then do something like: >> chessboard[[0,1]] = "Rook"; >> as the [0, 1] is typed as a dynamic array. If it does work, it may >> actually create [0,1] on the heap and then pass it as an int[2], which >> would suck. > > board[[0,0]] = "Rook"; > > seems to work. thanks. But, the foreach loop looks strange. It looks like it > takes the hash value of the key: > > string[int[2]] board; > > board[[0,0]] = "Rook"; > board[[0,1]] = "Knight"; > > foreach( pos, val; board) { > writefln( "%s: %s", pos, val); > } > > > Output: > > 2 9903680: Knight > 2 9903696: Rook It may be represented properly inside the AA. There are currently some bugs with AA's and using foreach. >> Or, if you know how big your chessboard is (8x8 isn't a lot of memory), >> then: >> string[8][8] chessboard; >> is pretty straightforward :) > > Yes it is :) Hehe.... > > > Now, what if I wanted to do the following: > > > class Board { > string[][] positions; > > this( int x, y) { > // set the size of positions > } > } > > I want positions to internally represent a chess board, a tic tac toe board, or a > Connect Four board, etc... > > But, I want to fix the dimensions of the board when the board gets instantiated, > so that I can have the compiler do all the work of bounds checking for me. I can > create class variables maxx, maxy and access functions that check against the > variables, but I'm wondering if there's a way to make the compiler do it for you. > > Is there a way? If you want it to be "compile-time-decided" but not hard-coded to 8x8: class Board(int width, int height) { string[width][height] positions; } Then you do: auto brd = new Board!(8, 8); -Steve |
July 23, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder |
>
> string[int[2]] board;
>
> board[[0,0]] = "Rook";
> board[[0,1]] = "Knight";
>
> foreach( pos, val; board) {
> writefln( "%s: %s", pos, val);
> }
>
>
> Output:
>
> 2 9903680: Knight
> 2 9903696: Rook
>
Changing the declaration to
string[int[]] board;
makes it work (for me).
BR
/HF
|
July 24, 2010 Re: string[int[][]] ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dcoder | dcoder wrote: > == Quote from Steven Schveighoffer (schveiguy@yahoo.com)'s article >> This is what I think you should use: >> string[int[2]] <snip> > board[[0,0]] = "Rook"; Further to what others have said, why use strings? There are only 12 possible chess pieces (black and white), plus blank, so probably the most efficient approach is char[8][8] board; and use uppercase letters for white and lowercase for black. This also makes it very easy to embed board positions in code, and to write/read positions to/from a file (but don't forget to validate). But there's something else to consider. Do you want to be able to represent: - whose turn it is to move? - availability of en passant capture? - possibility of castling? (One of my programs uses 'C' to represent a rook on which castling may still be possible, and 'R' for one on which it isn't.) > seems to work. thanks. But, the foreach loop looks strange. It looks like it > takes the hash value of the key: <snip> Clearly a bug. Need to investigate further.... Stewart. |
Copyright © 1999-2021 by the D Language Foundation