Skip to content

Instantly share code, notes, and snippets.

@khajavi
Created October 29, 2020 06:02
Show Gist options
  • Select an option

  • Save khajavi/c3ba09e145389969097f67941abb9c91 to your computer and use it in GitHub Desktop.

Select an option

Save khajavi/c3ba09e145389969097f67941abb9c91 to your computer and use it in GitHub Desktop.

Revisions

  1. khajavi created this gist Oct 29, 2020.
    42 changes: 42 additions & 0 deletions TicTocToe.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    object TicTocToe {
    sealed trait Action
    object Action {
    case object X extends Action
    case object O extends Action
    }

    sealed trait Player
    object Player {
    case object X extends Player
    case object O extends Player
    }

    sealed trait State
    object State {
    case object NotStarted extends State
    case object Playing extends State
    case object Finished extends State
    }

    type Board = Array[Array[Action]]
    case class Game[S <: State](state: S, board: Board)
    type Cell = (Int, Int)

    trait PlayGround {

    /**
    * Who is the winner?
    */
    def winner[S <: State.Finished.type](game: Game[S]): Player

    /**
    * Who is the next player?
    */
    def nextPlayer(board: Board): Player

    //FIXME: who start the game?
    //FIXME: pick shouldn't work on Finished state
    def pick[S](game: Game[S]): Cell
    }

    }