Tic Tac Toe

1 Tic Tac Toe
1.1 Introduction
Problem 1. (25 pt.) A tic-tac-toe game is two players (X and O) playing on a 3 by
3 grid in turn, whichever player first assembles 3 pieces in a line (row, column or
diagonal) wins. And you are going to implement this game in haskell with the
following specification:
tictactoe :: IO () tictactoe =
undefined
tictactoeis an interactive IO operation, when invoked, it starts the game with the
following flow:
Step 1. Printing the board The board MUST be printed (on the screen
/ command line) in the following format:
An empty board looks like this:
.---.---.---. | | | | .---.---.---. | | | | .---.---.---. | | | | .---.---.---.
2
whereas anon-empty boardlookslikethis, byreplacing thecentral character of the
cell with X(capital) or O(capital letter O, NOT numberzero).
.---.---.---.
| X | O | | .---.---.---. | | O | | .---.---.---. | | | | .---.---.---.
PLEASE PAY ATTENTION TO THE FORMAT CHARACTER BY CHARACTER.
Step 2. Move Then, your program should print X MOVE or O MOVE at a new
line, according to current moving player.
Then it reads one line of user input, which consists of a pair of coordinate
. where denotes the number of row, denotes the number of
column, both indices are base 1 (1 2 denotes the first row and the second
column, NOT the second row and the third column)
A correct user input consists of two valid coordinates separated by an arbi- trary
amount of whitespaces.
For example the following lines are valid inputs:
1 2
2 3
However the following are not (#are followed by explanations):
2 # only one coordinate
12 2 # out of range coordinate (12)
1 2 xy # xy shouldn't appear
abdsaf # input that doesn't make any sense
Besides, the attempt of making a move on a non-empty cell is considered invalid
input.
Upon invalid input, your program should print INVALID POSITION followed by a
newline, reads user input again. This process repeats until a correct input is
3
Step 3. Loop Ifthe game resultis determined, then amessage is printed and
the program returns.
• If player X wins, prints XWINS. • If player O wins, prints OWINS. • If the board is full, but neither player wins, printsDRAW.
Otherwise return to step 1, printing the current state of the board.
1.2 Further Clarification
• Two sample inputs and outputs are attached in the appendix of this file.
• Player O always movesfirst.
• Yourprogramshouldhandle incorrectuserinputs asspecifiedin“Step 2”.
• The gradingofthisquestionwouldbemostly basedontheexactmatch of the
command line output of your program and standard output, so please pay
extra attention to the output format. Don’t let small typos cost you the
entire question.
• So for your convenience (and mine), all the printed letters areupper- case.
• Try your bestto keep your code clean, the clarity of code and the famil- iarity
of IO operation and monad shown in the code would probably be
considered.
Problem2. (25 pt) Implementthe Monadinstance of datatype Transform, and an
important function next
The definition of Transform is given:
newtype Transform a b = Transform { getTransform :: (b, a -> a)}