Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
DeepSky8 / newClass.scala
Created April 8, 2014 16:38
This should call a new instance of the class - but the first line isn't liked
val wager = "Great! How much do you want to wager?"
val leave = "Well, thanks for coming out today, partner."
val again = "I didn't quite catch that, sonny. You'll have to speak up."
val get = readLine()
class wagers
println(wager)
class leaves
println(leave)
class agains
@DeepSky8
DeepSky8 / CoinFilpLeadIn.scala
Created April 8, 2014 18:18
Coin Flip game in infancy
object coinFlip {
val wager = "Great! How much do you want to wager?"
val leave = "Well, thanks for coming out today, partner."
val again = "I didn't quite catch that, sonny. You'll have to speak up."
val get = readLine()
def continue(p: String): String = p.toLowerCase match {
case y "yes" => wager
case n "no" => leave
@DeepSky8
DeepSky8 / CCCC.scala
Last active August 29, 2015 13:58
Current Collected Coinflip Contents
CCCC
object coinFlip {
val wagers = "Great! How much do you want to wager?"
val headsTails = "Do you want to bet heads or tails? "
val freddy = "Remember, Honest Freddy needs you to bet in increments of 25 credits: "
val freddy2 = "If you don't bet in exact increments of 25 credits, Freddy will just bet 25 credits for you."
val heads = "Do you want to bet heads or tails? "
val heads2 = "If Honest Freddy doesn't understand whether you picked heads or tails, he'll just bet heads for you."
@DeepSky8
DeepSky8 / NoStreamCrossing.scala
Last active August 29, 2015 13:59
There must be an easier, non-compiler-breaking way
def wager(bet: Int, heads: Int, total: Int, flip: Int): (Boolean, Int, Int) = {
if(total > 1000) = heads match {
case 0 => (false, 1, total-bet)
case 1 => (false, 0, total-bet)
} else {
@DeepSky8
DeepSky8 / IfAndCase.scala
Created April 16, 2014 14:58
You can't have an if inside a case
object WagerGameAction extends GameAction {
def execute(gameState: GameState): GameState = gameState match {
case Wager(stake) => {
println(wagers)
print(increments25)
print(increments25ForYou)
val betVal = readLine()
val realBet = confirm(scala.math.abs(betVal.toInt))
if (!afford(realBet, stake)) {
@DeepSky8
DeepSky8 / CoinFlipModular.scala
Created April 17, 2014 12:08
Updated, more modular CoinFlip betting game
object coinFlip {
val wagers = "Great! How much do you want to wager? "
val increments25 = "Remember, Honest Freddy needs you to bet in increments of 25 credits: "
val increments25ForYou = "If you don't bet in exact increments of 25 credits, Freddy will just bet 25 credits for you. "
val headsTails = "Do you want to bet heads or tails? "
val freddyBet = "If Honest Freddy doesn't understand whether you picked heads or tails, he'll just bet heads for you. "
val leave = "Well, thanks for coming out today, partner. "
val again = "I didn't quite catch that, sonny. You'll have to speak up. "
val ready = "Are you ready for another match of Outback CoinFlip? "
@DeepSky8
DeepSky8 / Framework.scala
Created May 7, 2014 03:07
Rough Sketching on a TicTacToe framework
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
@DeepSky8
DeepSky8 / tttapi.scala
Last active August 29, 2015 14:01
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)
@DeepSky8
DeepSky8 / problem26.scala
Last active August 29, 2015 14:01
Generate the combinations of K distinct objects chosen from the N elements of a list. In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient). For pure mathematicians, this result may be great. But we want to really gene…
scala> combinations(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, h', i', j', k', l'))
res0: List[List[Symbol]] = List(List('a, 'b, 'c), List('a, 'b, 'd), List('a, 'b, 'e), ...
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)
}
@DeepSky8
DeepSky8 / completedTTT.scala
Created May 30, 2014 15:13
This is theoretically my completed Tic Tac Toe API
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)