Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Last active August 29, 2015 14:01
Show Gist options
  • Save DeepSky8/ce6b415db218b78b8427 to your computer and use it in GitHub Desktop.
Save DeepSky8/ce6b415db218b78b8427 to your computer and use it in GitHub Desktop.
It's an api, you fool. Don't make the whole game!
TTT API requirements
object tttmeanderings {
//make a move (which takes a board state gives me back the updated board state)
//
//ask who won (which only takes a board state that represents a finished game and tells me that X's won, O's won, or it was a draw)
//
//take back a move (which takes a board state that has had at least one move made, and reverts the last move made)
//
//ask which player moved to a given square (which takes a board state and a location and informs me what player is there, if any)
case class Board(moves: List[Move])
case class Move(where: Position, who: Player)
abstract class Player
case object playerX extends Player
case object playerO extends Player
case object open extends Player
abstract class Position
case object topRight extends Position
case object topMid extends Position
case object topLeft extends Position
case object midRight extends Position
case object midMid extends Position
case object midLeft extends Position
case object botRight extends Position
case object botMid extends Position
case object botLeft extends Position
def move(board: Board, move: Move): Board = {
if(board.moves.isEmpty) {
Board(move :: board.moves)
} else {
checkMove(board, move)
}
}
def checkMove(board: Board, move: Move): Board = {
if(board.moves.head == Nil) {
Board(move :: board.moves)
} else {
if(board.moves.head == move) {
board
} else {
Board(move :: checkMove(board.moves.tail, move))
}
}
}
//def checkMove(board: Board, move: Move): Board = board.head match {
// case Nil => Board(move :: board.moves)
// case move => board
// case _ => Board(move :: checkMove(board.tail, move))
//}
def takeBack(board: Board): Board = {
Board(board.moves.tail)
}
//def whoWhere(position: Position, board: Board): Player = position match {
// case a if
// case board.moves.head._1 => board.moves.head._2
// case _ => whoWhere(position, board.moves.tail)
//}
//}
def playerIs(player: Player): String = player match {
case playerX => "This space is occupied by Player 1."
case playerO => "This space is occupied by Player 2."
case open => "This space currently not occupied."
}
def whoWhere(position: Position, board: Board): Player = {
if(board.moves.head == Nil) {
open
} else {
if(board.moves.head._1 == position) {
board.moves.head_.2
} else {
whoWhere(position, board.moves.tail)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment