Write a program that a will play a game of Misère tic-tac-toe. That is, the goal is to force your opponent to take three in a row.
Accept on standard input either an 'X' or an 'O'(the letter, not zero), to determine which side the program will be playing as. Then output a single digit for your move on your turn, and read a single digit on your opponents turn until the game is over (X always goes first). Once a winner is decided, output X or O for who won, or D for a draw. For example, if O gets 3 in a row, X wins.
Assume the board is numbered like so:
0|1|2 ----- 3|4|5 ----- 6|7|8
Ideally a solution will be optimal and never lose. Like tic-tac-toe, perfect play should always result in a draw. If the above protocol is adhered to, I can test submissions automatically against a variety of possible strategies.
Winner is shortest code. bonus points if it picks randomly from equally good moves to make it a bit more unpredictable.