Created
May 7, 2014 03:07
-
-
Save DeepSky8/aa2d33a4443e9b864958 to your computer and use it in GitHub Desktop.
Rough Sketching on a TicTacToe framework
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
object tttmeanderings { | |
sealed trait GameState | |
case class Welcome extends GameState | |
case class Ready extends GameState | |
case class Player extends GameState | |
case class GetMove extends GameState | |
case class Convert extends GameState | |
case class Process extends GameState | |
case class CheckWin extends GameState | |
case class DrawBoard extends GameState | |
case class End extends GameState | |
//gameLoop | |
abstract class GameAction | |
def execute(gameState: GameState): GameState | |
def gameLoop(gameState: GameState): Unit = gameState match { | |
case Welcome => gameLoop(ActionWelcome.execute(Welcome)) | |
case Ready => gameLoop(ActionReady.execute(Ready)) | |
case Player(_) => gameLoop(ActionPlayer.execute(Player(_))) | |
case GetMove(player) => gameLoop(ActionGetMove.execute(GetMove(player))) | |
case Convert => gameLoop(ActionConvert.execute(Convert)) | |
case Process => gameLoop(ActionProcess.execute(Process)) | |
case CheckWinA => gameLoop(ActionCheckWin.execute(CheckWinA)) | |
case CheckWinB => gameLoop(ActionCheckWin.execute(CheckWinB)) | |
case WinA => gameLoop(ActionWinA.execute(WinA)) | |
case WinB => gameLoop(ActionWinB.execute(WinB)) | |
case DrawBoard => gameLoop(ActionDrawBoard.execute(DrawBoard)) | |
case End => println() | |
} | |
object ActionWelcome extends GameAction { | |
def execute (gameState: GameState): GameState = { | |
println("Welcome to Cosmic Tic-Tac-Toe. Prepare to battle the fiercest opponents for control of the solar system!") | |
case _ => Ready | |
} | |
} | |
object ActionReady extends GameAction { | |
def execute (gameState: GameState): GameState = { | |
println("Are you ready to begin?") | |
val ready = readLine() | |
ready.toLowerCase match{ | |
case yes => Player(true) | |
case y => Player(true) | |
case _ => End | |
} | |
} | |
} | |
object ActionPlayer(player: Boolean) extends GameAction { | |
def execute (gameState: GameState): GameState = player match { | |
case t => GetMove(player(!t)) | |
} | |
} | |
object ActionGetMove(player: Boolean) extends GameAction { | |
def execute (gameState: GameState): GameState = player match { | |
if( | |
val PlayerA = | |
println(" | |
case _ => Process | |
} | |
} | |
object ActionConvert extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match { | |
case _ => Process | |
} | |
} | |
object ActionProcess extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match { | |
case _ => CheckWinA | |
} | |
} | |
object ActionCheckWinA extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match { | |
case _ => WinA | |
case _ => CheckWinB | |
} | |
} | |
object ActionCheckWinB extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match { | |
case _ => WinB | |
case _ => DrawBoard | |
} | |
} | |
object WinA extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match{ | |
println(CongratulationsA) | |
End | |
} | |
} | |
object WinB extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match{ | |
println(CongratulationsB) | |
End | |
} | |
} | |
object ActionDrawBoard extends GameAction { | |
def execute (gameState: GameState): GameState = gameState match { | |
case _ => GetMove | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
gameLoop(Welcome) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment