digitalmars.D.learn - string[int[][]] ??
- dcoder (10/10) Jul 22 2010 Hello. I want to use associative arrays, but have a 2-d int array as my
- dcoder (7/17) Jul 22 2010 Ooops, I just realized that int[someX][someY] needs to evaluate to an in...
- awishformore (2/19) Jul 22 2010 How about string[][]? ;)
- dcoder (4/6) Jul 22 2010 yes, heheh... you are right.
- Steven Schveighoffer (13/32) Jul 22 2010 This is what I think you should use:
- dcoder (29/40) Jul 22 2010 board[[0,0]] = "Rook";
- awishformore (2/42) Jul 22 2010 string[8][8] positions; // fixed size array
- Steven Schveighoffer (11/56) Jul 22 2010 It may be represented properly inside the AA. There are currently some ...
- Heywood Floyd (5/21) Jul 23 2010 Changing the declaration to
- Stewart Gordon (19/25) Jul 24 2010 Further to what others have said, why use strings? There are only 12
- dcoder (14/28) Jul 25 2010 Wow, well you have certainly thought about this alot more than I have. ...
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
== Quote from dcoder (dcoder devnull.com)'s articleHello. 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! dcoderOoops, 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
On 22/07/2010 22:57, dcoder wrote:== Quote from dcoder (dcoder devnull.com)'s articleHow about string[][]? ;)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! dcoderOoops, 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
== Quote from awishformore (awishformore nospam.plz)'s articleyes, heheh... you are right. I'm over thinking things. Sorry about the noise.How about string[][]? ;)
Jul 22 2010
On Thu, 22 Jul 2010 16:57:59 -0400, dcoder <dcoder devenull.dev> wrote:== Quote from dcoder (dcoder devnull.com)'s articleThis 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 :) -SteveHello. 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! dcoderOoops, 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?
Jul 22 2010
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleThis 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: RookOr, 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
On 22/07/2010 23:21, dcoder wrote:== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articlestring[8][8] positions; // fixed size arrayThis 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: RookOr, 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
On Thu, 22 Jul 2010 17:21:15 -0400, dcoder <blah blah.com> wrote:== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleIt may be represented properly inside the AA. There are currently some bugs with AA's and using foreach.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: RookIf 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); -SteveOr, 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?
Jul 22 2010
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: RookChanging the declaration to string[int[]] board; makes it work (for me). BR /HF
Jul 23 2010
dcoder wrote:== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article<snip>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:<snip> Clearly a bug. Need to investigate further.... Stewart.
Jul 24 2010
== Quote from Stewart Gordon (smjg_1998 yahoo.com)'s articleFurther 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