Skip to content

Instantly share code, notes, and snippets.

@darkf
darkf / ddrdd.txt
Created May 7, 2012 02:59
Data-driven rougelike description DSL
merchant Tom
inventory: 3x Pistol Rounds @ $5, 1x SMG Rounds @ $10
on talk {
say "Hello! Welcome to Tom's Ammunition Supply!"
}
end
enemy Boar
inventory: 1x Boar Meat
@darkf
darkf / derp.txt
Created May 17, 2012 02:45
Another terrible game object DSL
# Objects are values with at least the methods {init, update, draw}.
# state describes properties shared by all game states.
# game states can transition into others, or call methods by others.
# there is a global event system that can emit (dispatch) events (by string), and catch (observe) them.
state {
player : player
objects : [Object]
}
@darkf
darkf / game.py
Created May 18, 2012 20:32
Terrible text-based RPG
import random
def say(msg):
print ":", msg
def rnd(min, max):
return random.randint(min, max)
class Player:
def __init__(self):
type actor = {mutable hp : int; name : string}
type battle = {enemy : actor; mutable turn : int}
type attack = Slash of int
let player = {hp=100; name="Player"}
let runAttack by who atk =
match atk with
| Slash dmg -> printfn "%s slashes %s for %d" by.name who.name dmg; who.hp <- who.hp - dmg
@darkf
darkf / heh.txt
Created June 6, 2012 21:34
Pylike -> JS
struct vec2 (x, y);
def vec2_add(a, b) {
vec2(a.x + b.x, a.y + b.y);
}
def main() {
x = [1, 2, 3];
print("hi! length of x is #{len(x)}");
@darkf
darkf / sieve.c
Created July 28, 2012 07:39
Sieve of Eratosthenes in C99
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#define n 100
static bool A[n];
int main() {
int i, j;
@darkf
darkf / logic.py
Created July 30, 2012 06:22
Logic, how does it work?
def parse(expr):
lst = []
for line in expr.split('\n'):
s = line.split(' -> ')
if len(s) == 1:
lst.append(('def', line))
else:
lst.append(('imply', s[0], s[1]))
@darkf
darkf / rpn.fs
Created August 12, 2012 23:37
F# RPN calculator
type Atom =
| Num of int
| Op of char
let opToProc = function
| '+' -> (+)
| '*' -> (*)
| _ -> failwith "invalid operation"
let rec evalRPN (expr : Atom list) (stack : int list) =
@darkf
darkf / prog.erl
Created August 30, 2012 20:18
Bisect parallel factorial in Erlang
-module(prog).
-export([fact/2, worker/3, manager/1, main/0]).
fact(N, Start) -> lists:foldl(fun(L, R) -> L*R end, 1, lists:seq(Start, N)).
worker({Start, End}, I, Pid) ->
R = fact(End, Start),
Pid ! {finished, I, R}.
manager(Queue) ->
@darkf
darkf / prog.erl
Created August 31, 2012 01:50
Parallel (N-process) factorial in Erlang
-module(prog).
-export([main/0, take/2, drop/2, par/2, splitAt/2, product/1, manager/3, worker/3, fact/2]).
take(0, _) -> [];
take(_, []) -> [];
take(N, Lst) ->
[hd(Lst) | take(N-1, tl(Lst))].
drop(0, Lst) -> Lst;
drop(_, []) -> [];