Search code examples
haskellfunctional-programming

Haskell function for Tic Tac Toe


I trust you're all doing well. I'm reaching out with a humble request. I've been working on a series of Haskell exercises I discovered online, and things have been progressing smoothly. However, I've hit a bit of a roadblock with my current assignment, which involves implementing a Tic-Tac-Toe function. The task requires me to create the following function:

Task: (https://i.sstatic.net/D7OKb.png)

I was wondering if any of you might be available to offer some guidance or assistance on this matter?

I genuinely appreciate your time and support!

I have tried to create a separate function where I have created a playing field without the 'x' or 'o' filled with blank spaces.


Solution

  • The first question you should ask yourself is : what data structure should I use to store the state of my Tic Tac Toe game ? Let's call this type Board, I will let you find out what this should be.

    Once you've answered that question, you should implement small functions that do simple tasks for this datatype. For instance you could implement a function

    circPlay :: (Int, Int) -> Board -> Board
    

    that allows the circle player to play on a certain square. Then you'll probably realize that you should handle cases when the square is not on the board, etc. And that maybe you want as many players as you want, and you'll probably come up with more and more ideas to generalize the code you started to write.

    For instance instead of circPlay, you'll probably realise that it's better to use

    play :: Char -> (Int, Int) -> Board -> Board
    

    so that there are as many players possible as there are characters. Following this step by step, you will eventually be able to implement Tic Tac Toe. One small step after the other !