www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string[int[][]] ??

reply dcoder <dcoder devnull.com> writes:
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
Jul 22 2010
parent reply dcoder <dcoder devenull.dev> writes:
== 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.
Jul 22 2010
next sibling parent reply awishformore <awishformore nospam.plz> writes:
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[][]? ;)
Jul 22 2010
parent dcoder <blah blah.com> writes:
== Quote from awishformore (awishformore nospam.plz)'s article



yes, heheh... you are right. I'm over thinking things. Sorry about the noise.
Jul 22 2010
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Jul 22 2010
parent reply dcoder <blah blah.com> writes:
== 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.
Jul 22 2010
next sibling parent awishformore <awishformore nospam.plz> writes:
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
Jul 22 2010
prev sibling next sibling parent Heywood Floyd <soul8o8 gmail.com> writes:
 
  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
Jul 23 2010
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
dcoder wrote:
 == Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 This is what I think you should use:
 string[int[2]]


 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:

Clearly a bug. Need to investigate further.... Stewart.
Jul 24 2010
parent dcoder <dcoder devnull.com> writes:
== Quote from Stewart Gordon (smjg_1998 yahoo.com)'s article
 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.)

Wow, well you have certainly thought about this alot more than I have. I am merely experimenting / learning the D language, and a good way to do that would be to come up with a pet project. So, I figure writing a chess program would be good, non-trivial project to do. I also want to practice my patterns, which I haven't had a real chance to do in years. So, I figure I want to really write a generalize board game program so that it can play: 1. checkers. 2. othello 3. tic tac toe 4. connect four 5. chess By deriving the right base classes. Anyways, thanks for the suggestions, I will keep them in mind.
Jul 25 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Jul 22 2010