package object tictactoe {
  val Human = 'X'
  val Computer = 'O'  
  val BaseBoard = ('1' to '9').toList
  val WinnerLines = List((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6))
  val randomGen = new util.Random(System.currentTimeMillis)
}

package tictactoe {

class Board(aBoard : List[Char] = BaseBoard) {

  def availableMoves = aBoard.filter(c => c != Human && c != Computer)

  def availableMovesIdxs = for ((c,i) <- aBoard.zipWithIndex if c != Human && c != Computer) yield i

  def computerPlays = new Board(aBoard.updated(availableMovesIdxs(randomGen.nextInt(availableMovesIdxs.length)), Computer))

  def humanPlays(move : Char) = new Board(aBoard.updated(aBoard.indexOf(move), Human))

  def isDraw = aBoard.forall(c => c == Human || c == Computer)

  def isWinner(winner : Char) = 
    WinnerLines.exists{case (i,j,k) => aBoard(i) == winner && aBoard(j) == winner && aBoard(k) == winner}

  def isOver = isWinner(Computer) || isWinner(Human) || isDraw

  def print { 
    aBoard.grouped(3).foreach(row => println(row(0) + " " + row(1) + " " + row(2))) 
  }

  def printOverMessage { 
    if (isWinner(Human)) println("You win.")
    else if (isWinner(Computer)) println("Computer wins.")
    else if (isDraw) println("It's a draw.")
    else println("Not over yet, or something went wrong.")     
  }

}


object TicTacToe extends App {   

   def play(board : Board, turn : Char) {