Skip to content

Instantly share code, notes, and snippets.

@washort
Forked from mythmon/01 main.mt
Last active August 29, 2015 14:00
Show Gist options
  • Save washort/11200441 to your computer and use it in GitHub Desktop.
Save washort/11200441 to your computer and use it in GitHub Desktop.
blackjack game module proposal example
def [=> makeDeck, => makeHand] := import("blackjack")
traceln("Let's play blackjack!")
def deck := makeDeck()
deck.shuffle()
def hand := makeHand([deck.draw() for _ in 1..2])
traceln("My hand is: ")
for card in hand.cards:
trace(`${card.render()}, `)
traceln(`Its score is ${hand.getScore()}`)
# This file combines some of the roles of __init__.py, setup.py, and a
# build script. It contains an expression producing configuration
# data. A packaging/deployment system will run it, collect its data,
# and use it to produce a deployable bundle/archive/executable/etc.
# Or in a development/scripting environment, it can be executed at
# runtime by calling the unsafe scope's "import" function.
# This script will probably have a path object representing its
# filesystem location in scope; I'll call it "currentDir" for now.
def cardFile := currentDir.child("cards.mt")
def deckFile := currentDir.child("deck.mt")
def handFile := currentDir.child("hand.mt")
# This is somewhat deficient, there needs to be some mechanism for a
# package/library to describe its dependencies in a way that hints at
# what it wants while also allowing for those dependencies to be fully
# specified by whatever's loading it.
def itertools := require("com.mythmon.itertools")
# "load" may be a poor name; this does not execute code, just inspects
# the 'module' suite at the top of the file.
def [=> suits, => ranks, => makeCard] := load(cardFile)
def makeDeck := load(deckFile, [makeCard, suits, ranks, itertools])
def makeHand := load(handFile, [makeDeck, suits, ranks])
makeModule("blackjack", [=> makeHand, => makeDeck])
module (makeDeck, suits, ranks):
exports [makeHand]
def makeHand(cards):
to getCards():
return cards
to getScore():
# I'd love some sort of clean reduce syntax, but I can't think of one.
var sum := 0
for card in cards:
sum += card.rank.value
return sum
module:
exports [makeCard, suits, ranks]
def makeSuit(name):
return object suit:
to getName():
return name
def suits := [s => makeSuit(s) for s in ["hearts", "spades", "diamonds", "clubs"]]
def makeRank(name, value):
return object rank:
to getName():
return name
to getValue():
return value
def ranks := [r => makeRank(name, v + 1) for v => name in
["ace", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "jack", "queen", "king"]]
def makeCard(rank, suit):
return object card:
to getRank():
return rank
to getSuit():
return suit
to render():
return `${name(rank)} of ${name(suit)}`
to op__cmp(other):
return [rank, suit].op__cmp([other.getRank(), other.getSuit()])
module (makeCard, suits, ranks, itertools):
exports [makeDeck]
def makeDeck():
def cards := [].diverge()
for [rank, suit] in itertools.product(ranks, suits):
cards.push(makeCard(rank, suit))
return object deck:
to draw():
return cards.pop()
to add(c):
cards.push(c)
to shuffle():
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment