Created
May 30, 2014 15:13
-
-
Save DeepSky8/495e08ae76b6876bee67 to your computer and use it in GitHub Desktop.
This is theoretically my completed Tic Tac Toe API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) { | |
def checkMove(move: Move): Boolean = moves.filter(_ == move).isEmpty | |
def move(move: Move): Board = moves match { | |
case Nil => Board(move :: moves) | |
case _ => if(checkMove(move)) Board(move :: moves) else this | |
} | |
def takeBack: Board = moves match { | |
case Nil => this | |
case _ :: t => Board(t) | |
} | |
} | |
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 | |
case object draw 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 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) | |
} | |
} | |
} | |
def playerMoves(board: Board, player: Player): List[Move] = { | |
if(board.moves.isEmpty) { | |
Nil | |
} else { | |
if(board.moves.head._2 = player) { | |
board.moves.head._2 :: playerMoves(board.moves.tail, player) | |
} else { | |
playerMoves(board.moves.tail, player) | |
} | |
} | |
} | |
def whoWon(board: Board): Player = { | |
def playerXWon(board: Board): Player = { | |
combinations(3, checkCheck(playerMoves(board, playerX))) | |
playerXMoves.head match { | |
case Nil => Nil | |
case (topLeft, topMid, topRight) => playerX | |
case (midLeft, midMid, midRight) => playerX | |
case (botLeft, botMid, botRight) => playerX | |
case (topLeft, midMid, botRight) => playerX | |
case (botLeft, midMid, topRight) => playerX | |
case (topLeft, midLeft, botLeft) => playerX | |
case (topMid, midMid, botMid) => playerX | |
case (topRight, midRight, botRight) => playerX | |
case _ => playerYWon(board) | |
} | |
} | |
def playerYWon(board: Board): Player = { | |
combinations(3, checkCheck(playerMoves(board, playerY))) | |
playerXMoves.head match { | |
case Nil => Nil | |
case (topLeft, topMid, topRight) => playerY | |
case (midLeft, midMid, midRight) => playerY | |
case (botLeft, botMid, botRight) => playerY | |
case (topLeft, midMid, botRight) => playerY | |
case (botLeft, midMid, topRight) => playerY | |
case (topLeft, midLeft, botLeft) => playerY | |
case (topMid, midMid, botMid) => playerY | |
case (topRight, midRight, botRight) => playerY | |
case _ => Nil | |
} | |
} | |
def checkCheck(list: List[Move]): List[Move] = list match { | |
case Nil => Nil | |
case (list.head == topLeft) => topLeft :: checkCheck(list.tail) | |
case (list.head == topMid) => topMid :: checkCheck(list.tail) | |
case (list.head == topRight) => topRight :: checkCheck(list.tail) | |
case (list.head == midLeft) => midLeft :: checkCheck(list.tail) | |
case (list.head == midMid) => midMid :: checkCheck(list.tail) | |
case (list.head == midRight) => midRight :: checkCheck(list.tail) | |
case (list.head == botLeft) => botLeft :: checkCheck(list.tail) | |
case (list.head == botMid) => botMid :: checkCheck(list.tail) | |
case (list.head == botRight) => botRight :: checkCheck(list.tail) | |
case _ => list | |
def combinations[A](size: Int, pool: List[A]): List[List[A]] = pool match { | |
case Nil => Nil | |
case h :: t => if(size <= 0) Nil else if (size == 1) { | |
list(h) :: combinations(size, t) | |
} else { | |
helper(h, combinations(size-1, t) ++ combinations(size, t) | |
} | |
} | |
def helper[A](distribute: A, combs: List[List[A]]): List[List[A]] = combs match { | |
case Nil => Nil | |
case h :: t => (distribute :: h) :: helper (distribute, t) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment