Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient method to represent chessboard

Tags:

java

chess

I have a chess program written in Java. Currently I am using a 2d char array to represent the chessboard. Upper case characters to represent white and lower case to represent black. Should I use an array of bytes to reduce the memory usage? Or maybe enums? Thank you.

like image 292
user3193535 Avatar asked Oct 24 '15 07:10

user3193535


People also ask

How do you represent a chess board?

One of the simplest ways to represent a board is to create an 8x8 two-dimensional array (or, equivalently, a 64 element one-dimensional array). Each array element would identify what piece occupied the given square, or alternatively, if the square is empty.

What according to you is the most suitable Datastructure to implement a chess game?

For a serious chess engine, using bitboards is an efficient way to represent a chess board in memory. Bitboards are faster than any array based representation, specially in 64-bit architectures where a bitboard can fit inside a single CPU register.

What is the algorithm for chess?

The core of the chess playing algorithm is a local min-max search of the gamespace. (or "ply" as it's as its referred to in computer chess terminology), all possible moves are examined, and the static board evaluation function is used to determine the score at the leafs of the search tree.

Does it matter which way a chess board is set up?

It doesn't affect the game. It's just that the rules state that that is the way you place a chess board.


1 Answers

In short: The most efficient and professional way is to use Bitboards.

Basically, there are 3 ways of representing a chessboard that are often used:

  • 8x8 2-dimensional array: Slow but easy to maintain

  • 10x12 1-dimensional array: Faster, a little bit trickier

  • Bitboards: They are the fastest method, also used in professional engines like Stockfish or Rybka. Basically, you need a 64-Bit uint for every figure type, where every bit stands for a single field. For more information, i recommend the chessprogramming wiki or google=>bitboards.

like image 88
xXliolauXx Avatar answered Sep 17 '22 18:09

xXliolauXx