Friday, February 9, 2007

Homework assignment 4

I've put up homework assignment 4 on the class web page. This one is a complete change of pace, being a small programming project to write a tic-tac-toe playing program. Feel free to ask questions, solicit hints, etc. in the comments to this post.

6 comments:

TurtleButterSnug said...

Just so you know, One thing I discovered is that while the book implies that TextIO.inputLine produces a string, it a actually produces a string option. So if you do TextIO.inputLine TextIO.stdIn, for example, you will need to case between NONE and SOME str. Just found it frustrating, so I figured I'd give y'all the heads up

Dave said...

Yes, that's right about TextIO.inputLine -- you have to check the returned value with a case expression over the option type. It will return NONE if the read operation encounters an end of file (EOF). In that case it is probably best to abort (I got into an infinite loop by trying to read again).

By the way, my initial version of the basic tic-tac-toe program with human players is 102 lines of code, using a datatype to represent the board (instead of arrays, vectors, or lists). My main functions are

chwin : board -> bool
(checks whether a board if a win)

place : mark * pos * board -> board
(places a new mark on the board, checking whether the position is already occupied)

readMove : unit -> pos
(read's a move, with various input validation checks)

printBrd : board -> unit
(prints a 2-dimensional display of the board)

move: mark * board -> unit
(the main interactive play loop)

tic_tac_toe : unit -> unit
(the top-level function starting a game)

Dave said...

In my last comment, the name of the move function was may be confusing.

The move function is the main loop of the program that takes the next player, specified by mark (which should be X or O, not E for empty) and the current board. It will prompt for and read the next move specification, update the board accordingly, check for a win (or for a draw if the board is full), and either end the game or call itself recursively the the other players mark and the new board.

TurtleButterSnug said...

With regard to your infinite loop comment, my read function works properly if I call it from within the interpreter like 'use "tictactoe.sml"', but if I try sml < tictactoe.sml, I get into an infinite loop. is it posible to get around this? Basically, Im just calling TextIO.readline on stdIn.

Piyo said...

With regards to the first comment, does this mean that the "print" names both a function that takes strings and a function that takes string options?

Dave said...

The print function only takes strings:

print : string -> unit

One thing to note about input functions provided by TextIO is that some (input, inputN, inputAll) return a string (TextIO.vector = string), while others (input1, inputLine) return a string option.

Note also that the IO signatures IMPERATIVE_IO and TEXT_IO include the types vector and element. For the TextIO structure, these are defined to be string and char, respectively.