[Haskell-cafe] How to represent a board?

Alexander Vieth alex at lagoa.com
Fri Jun 20 15:33:25 UTC 2014


> In many languages a NxN array seems like a good pick. In Haskell, most would translate that into lists of lists. I know I have. However, traversing those lists to get a position, calculate where a piece or agent could go, etc., feels awkward and unefficient. Beside the point already made, we have no type safe guarantee that our 64x64 won't become a 63x63 + 65x1 board due to some misbehaving function.

Perhaps choosing a zipper-like representation, in which all adjacent places on your board can be found without any traversal, would make the usage less awkward and more efficient. As for the size, you can get a guarantee that it won't change, so long as you make a new type for your board and verify that no function on that type will produce a board of bogus size.

Alex

On 2014-06-20, at 11:24 AM, Rafael Almeida wrote:

> Hello,
> 
> There are many examples of games which use boards: chess, checkers, go, etc. Many people starting out at programming and/or game programming are very much tempted to code a board game. It looks like such a game would be easy to build at first sight.
> 
> Even more sophisticated games sometimes use a grid system, which looks a lot like boards as well. In a grid system the movements of a given agent are bound do discrete positions. Although the user may not see the board underneath pretty graphics, it is a board game.
> 
> Having in mind the sort of operations such games have to make on boards, I ask: what are the best representations for a board in Haskell?
> 
> In many languages a NxN array seems like a good pick. In Haskell, most would translate that into lists of lists. I know I have. However, traversing those lists to get a position, calculate where a piece or agent could go, etc., feels awkward and unefficient. Beside the point already made, we have no type safe guarantee that our 64x64 won't become a 63x63 + 65x1 board due to some misbehaving function.
> 
> It strikes me that list of lists is not a great board representation. I think STArrays are a better pick; from the perspective of translating an NxN array into Haskell, at least. However, maybe there is an even more elegant way to deal with a board in Haskell. I hope you can help me out figuring it out.
> 
> []'s
> Rafael
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list