❔ What data structure to use?

Hello! I'm working on a chess game, in particular to a method that gets me the valid moves for that particular piece. I need to return a list of valid moves for that piece, but the problem is, i don't know what data structure to use to return those moves. The method takes in a row and a column, which in fact represent the position the piece is found at. I've been thinking about tuple but im not sure. I also thought about an array, however, i need to initialize to a size, but i can't know ahead of time how many valid moves a piece can have at a given time. Could anyone suggest something? Thanks a lot!
21 Replies
cathei
cathei2y ago
List<Square> where square is struct that has row and column
_giuraemanuel
_giuraemanuelOP2y ago
Under the normal circumstances, that would be the obvious approach. But i dont have a square class for this. I'm building the squares in the wpf project. So i have no Square class.
cathei
cathei2y ago
Are you saying you cannot make custom type?
_giuraemanuel
_giuraemanuelOP2y ago
this is the "square" so to speak... but its in the wpf project, the ui has access to the game engine logic, not viceversa.
_giuraemanuel
_giuraemanuelOP2y ago
and this is where im creating the board, using the "square"
_giuraemanuel
_giuraemanuelOP2y ago
and this is what im attempting to do in the board class
cathei
cathei2y ago
I'm saying make a struct in your game logic. As simple as:
public record struct Square(int Column, int Row);
public record struct Square(int Column, int Row);
_giuraemanuel
_giuraemanuelOP2y ago
I'm not sure i follow. I'm not exactly familiar with records, never used them before
cathei
cathei2y ago
record is just shorthand to reduce boilerplate code Are you familiar with struct?
_giuraemanuel
_giuraemanuelOP2y ago
somewhat, yes.. but i haven't been in a situation where i needed them
cathei
cathei2y ago
You could use tuple for your case, but it would be more simple if you have custom struct like Square You can use it as easy as:
var sq = new Square(1, 2);
Console.WriteLine($"Col: {sq.Column} Row: {sq.Row}");
var sq = new Square(1, 2);
Console.WriteLine($"Col: {sq.Column} Row: {sq.Row}");
_giuraemanuel
_giuraemanuelOP2y ago
Okay i guess. Thats one approach. Is there another i could use? I'd like to have some more options to choose from so i can try them out then decide. Maybe return an array of rows and cols? Being tired is not doing me any favors tired
cathei
cathei2y ago
List<(int col, int row)> is tuple way Idk why you would use array for it, especially multidimensional one would be horrific
_giuraemanuel
_giuraemanuelOP2y ago
because i wanna do this myself with minimal help from the outside. Doesnt have to be the most optimal approach right now. I just wanna do something that works. So if it works, it doesn't matter too much. At least not for now.
ero
ero2y ago
i mean isn't something like this usually just a Figurine[,] _board
_giuraemanuel
_giuraemanuelOP2y ago
it is ^
cathei
cathei2y ago
Make sense for board, but not for list of moves
_giuraemanuel
_giuraemanuelOP2y ago
Wdym, where?
cathei
cathei2y ago
Here you are trying to use int[3,3] I guess? While the code looks WIP
_giuraemanuel
_giuraemanuelOP2y ago
attempted something yes.. i was still confused about how to go about it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server