[Haskell-beginners] A good data structure for representing a tic-tac-toe board?
emacstheviking
objitsu at gmail.com
Tue Mar 19 10:57:20 CET 2013
Roger,
Without writing any code, and still learning Haskell I personally don't see
anything intrinsically wrong with using a String, it's simple, it works and
what more can you ask from code than that!
I would go so far as to add "\n" after each line so that you can print the
status of the game to the console in one go, then checking for a winning
line would be as simple as writing a function that takes as its input the
String and a list of string offsets which make up a line, with the
assumption that [0] is the first board position, [2] is the end of the
first line and [3] contains the first "\n" character I would (pseudo-code /
made up from keyboard, pebcak may apply) go for this approach:
topLineWin = checkLine myBoard "O" [0,1,2] -- did O win ?
topleftDiagWin = checkLine myBoard "X" [0,5,10] -- did X win ?
botLineWin = checkLine my Board "X" [8,9,10] -- did X win ?
and so on. The implementation of checkLine is left as an exercise for the
reader as they say but by using partial application you can make the code
very readbable. I might even try this myself and post it back for a laugh.
It's surprising sometime the difference between what you think you know and
what you actually know!
Some coding hints and thinking as I go...
topLine = [0,1,2]
topleftDiagWin = [0,5,10]
etc.
winnerO = checkLine myString "O"
winnerX = checkLine myString "X"
checkLine topLine playerCode -- did the top line win?
...
By making the checkLine return a Bool then you can have a list of functions
that you could then pass through the Data.List.all and make it something
like this:
all [ winnerO topLine, winnerO botLine, ... etc]
Just to make the array number clear, here is the complete board:
"\n"
0 1 2 3
4 5 6 7
8 9 10 10
All the best, that's no coding and pure thought for that so YMMV!
Sean Charles.
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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130319/a571166b/attachment.htm>
More information about the Beginners
mailing list