[Haskell-beginners] A good data structure for representing a tic-tac-toe board?
Lyndon Maydwell
maydwell at gmail.com
Tue Mar 19 04:16:40 CET 2013
If taking the array approach, I'd recommend using a single array indexed by
the (x,y) position of the cell, this way neither direction has a greater
implied significance. Diagonals should also be easier.
Aside: Tony Morris wrote a very interesting exercise based on tic-tac-toe
and it is available on Hackage: http://hackage.haskell.org/package/TicTacToe
On Tue, Mar 19, 2013 at 3:49 AM, Peter Hall <peter.hall at memorphic.com>wrote:
> Start with a data type for the cell values, instead of Char. Then use an
> Array of Arrays, containing those values.
>
> data Cell = Empty | O | X
> type Board = Array Int Cell
>
> Finding winning "rows" and "columns" is easy. Diagonals are slightly more
> complicated.
>
> Peter
>
>
>
> On 18 March 2013 15:54, Costello, Roger L. <costello at mitre.org> wrote:
>
>> Hi Folks,
>>
>> Currently I am representing a tic-tac-toe board as a string, with 'X'
>> denoting player 1 and 'O' denoting player 2. For example, I represent this
>> 2x2 game board:
>>
>> 'X' |
>> -----------------------
>> | 'O'
>>
>> with this string: "X O"
>>
>> The nice thing about that representation is that it is each to identify
>> which cells are filled or empty, and it is easy to mark a cell with an 'X'
>> or 'O'.
>>
>> The problem with the representation is that it is difficult to determine
>> when a player has won.
>>
>> Can you recommend a representation that makes it easy to:
>>
>> 1. determine when a player has won
>> 2. identify cells that are filled or empty
>> 3. mark an empty cell
>>
>> /Roger
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130319/5ae4c620/attachment-0001.htm>
More information about the Beginners
mailing list