Represent a chessboard as a data structure

Hi!

If I want to represent a chessboard with a data structure, which data structure do I have to use?

I have found that someone use a TMap<FIntPoint, ...>, but I don’t think this is a good option.

Thanks.

Hi @ViaCognita!

Could we get some more specifics of what you properties you are trying to add to your data structure and what you are trying to implement specifically? If it’s just the location on the grid, would a Data Table be something more along the lines you are looking for?

Any additional information you can provide will help provide the answer you are looking for.

1 Like

maybe something like this?

UENUM() enum chess_piece { Pawn, Knight, Bishop, Rook, Queen, King };

UENUM() enum player_color { Black, White };


USTRUCT(BlueprintType) struct Fchess_cell
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	int32 Coord_X;

	UPROPERTY(BlueprintReadWrite)
	int32 Coord_Y;

	UPROPERTY(BlueprintReadWrite)
	TEnumAsByte<chess_piece> piece;

	UPROPERTY(BlueprintReadWrite)
	TEnumAsByte<player_color> Player;
};

and then you create an array for the cells:

	UPROPERTY(BlueprintReadWrite)
	TArray<Fchess_cell> myCells;

and get something like this:

image

3 Likes

Hi @Quetzalcodename !

Why do need that information?

I’m going to set an object (C++ class instance).

I’ve said a chessboard to don’t explain a lot about the game. But it is a board with some characters on it, and I need to calculate a path avoiding obstacles.

I’m asking because I could use a matrix to get the value at cell [x][y], but I don’t know if the TMap option is the best or there is another better options.

Thanks!

You can use a TArray<TArray<>> but, honestly, I’d probably just do it with the TMap<> for purposes of displaying the board and letting the user interact with it.

The thing on the inside of the map could be an AActor* for the space, which could in turn have a property for the AActor* for the piece.

If you’re actually building a chess AI/player, then you’re going to need a totally different set of data structures anyway! There’s a whole science behind that.

Hi @jwatte!

Thanks for your answer. My purpose is to use the Data Structure with the best performance.

Thanks!

@jwatte By the way, why do I need a TMap for purposes of displaying the board?

You need some idea of what the game state is, so you can easily look up whether a particular proposed move is valid, etc.

Performance for what? Encoding board states for min-max search uses very different data structures that encoding board states for interactivity.

In general, the board state for interactivity will not show up as a performance problem in a profiler – you could use a single linked list (which is not very performant) and it would still not show up on a profile, assuming you’re not doing anything else insane. However, for interactivity, there’s probably little difference between TMap<> and TArray<TArray<>> except the map is slightly easier to work with in code IMO.

If you’re trying to encode the board state for a chess AI algorithm of some sort (min-max search, etc) then you want something like bitboards or maybe read something like http://elib.mi.sanu.ac.rs/files/journals/yjor/44/yujorn44p265-284.pdf

But, to get more specific answers, you need to be more precise in what, specifically, you’re trying to do. “Make a chess game” isn’t specific, because there are so many parts of that, that each may want a different solution.

To search all the available movement

To search all the available movement

Read that paper I linked (from sanu.ac.rs) it’s a reasonable survey of encodings for high performance chess engines.

In general, you don’t want to use Unreal container data structures for that low-level implementation, because the chess-specific encodings are faster to search and put less cache pressure, so you can search more board states faster.

1 Like