# Represent a chessboard as a data structure

**URL:** <https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034>\
**Category:** C++\
**Tags:** unreal-engine, UE4-27, CPP, question\
**Created:** [May 27, 2022, 3:54pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034 "2022-05-27T15:54:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViaCognita](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/viacognita/32/906601_2.png) [@ViaCognita](https://forums.unrealengine.com/u/ViaCognita)\
**Post date:** [May 27, 2022, 3:54pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/1 "2022-05-27T15:54:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Quetzalcodename](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Quetzalcodename](https://forums.unrealengine.com/u/Quetzalcodename)\
**Post date:** [May 27, 2022, 4:55pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/2 "2022-05-27T16:55:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eldany.uy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eldany.uy/32/821920_2.png) [@eldany.uy](https://forums.unrealengine.com/u/eldany.uy)\
**Post date:** [May 27, 2022, 4:57pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/3 "2022-05-27T16:57:17Z")

</div>

maybe something like this?

```auto
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:

```auto
	UPROPERTY(BlueprintReadWrite)
	TArray<Fchess_cell> myCells;

```

and get something like this:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/b/0/fb06af58b335e6ae0e6098d0b073bf0ce008fbfa.png)

---

<div class="post-metadata">

**Author:** ![ViaCognita](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/viacognita/32/906601_2.png) [@ViaCognita](https://forums.unrealengine.com/u/ViaCognita)\
**Post date:** [May 27, 2022, 8:14pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/4 "2022-05-27T20:14:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [May 27, 2022, 10:36pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/5 "2022-05-27T22:36:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ViaCognita](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/viacognita/32/906601_2.png) [@ViaCognita](https://forums.unrealengine.com/u/ViaCognita)\
**Post date:** [May 28, 2022, 6:54am UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/6 "2022-05-28T06:54:31Z")

</div>

Hi @jwatte!

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

Thanks!

---

<div class="post-metadata">

**Author:** ![ViaCognita](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/viacognita/32/906601_2.png) [@ViaCognita](https://forums.unrealengine.com/u/ViaCognita)\
**Post date:** [May 28, 2022, 3:58pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/7 "2022-05-28T15:58:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [May 28, 2022, 6:36pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/8 "2022-05-28T18:36:09Z")

</div>

> [@ViaCognita](#):
>
> 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.

> [@ViaCognita](#):
>
> My purpose is to use the Data Structure with the best performance.

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](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.

---

<div class="post-metadata">

**Author:** ![ViaCognita](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/viacognita/32/906601_2.png) [@ViaCognita](https://forums.unrealengine.com/u/ViaCognita)\
**Post date:** [May 29, 2022, 5:16am UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/9 "2022-05-29T05:16:08Z")

</div>

> [@jwatte](#):
>
> Performance for what?

To search all the available movement

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [May 31, 2022, 5:06pm UTC](https://forums.unrealengine.com/t/represent-a-chessboard-as-a-data-structure/568034/10 "2022-05-31T17:06:03Z")

</div>

> 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.
