Skip to content

Instantly share code, notes, and snippets.

View randrews's full-sized avatar

Ross Andrews randrews

View GitHub Profile
@randrews
randrews / example.lua
Created March 9, 2011 03:07
lt - a very light Lua test utility
-- print("Setup code goes here")
test("always passes",
function()
assert(true, "Yay!")
end)
test("always fails",
function()
assert(false, "Boo.")
@randrews
randrews / dice.lua
Created March 9, 2011 04:01
Lua library for dice games
Die = {
roll = function(self)
self.value = math.random(self.sides)
end
}
Die["__index"] = Die
function D(sides, value)
local die = {sides = sides, value = value}
setmetatable(die, Die)
@randrews
randrews / facts.lua
Created April 1, 2011 05:46
Make Lua look like Prolog!
----------------------------------------------------------------------------------------------------
--- Making Lua look like Prolog:
---
--- Let's use metatables for something other than emulating prototype-based OO. By making the
--- __index metamethod create values for undefined things, we can make Lua look like Prolog!
--- We create empty tables for anything starting with a capital letter, functions that populate
--- those tables for lowercase things (to assign relationships) and if a name begins with "is_"
--- then it becomes a function that queries those tables.
----------------------------------------------------------------------------------------------------
@randrews
randrews / lua_map.c
Created April 23, 2011 22:16
Example of embedding Lua in C
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
int map_create(lua_State *lua);
int map_slice(lua_State *lua);
int main(int argc, char **argv){
lua_State *lua = lua_open();
@randrews
randrews / map.lua
Created May 15, 2011 20:45
Map generator, generates XPM files
-- Copyright (C) 2011 Ross Andrews
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@randrews
randrews / all_trees.lua
Created May 18, 2011 03:24
Enumerate all trees
-- Copyright (C) 2011 Ross Andrews
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@randrews
randrews / if1.lua
Created May 27, 2011 00:50
First part of an IF system in Lua
-- Object utilities
----------------------------------------
function dispatch(obj, message_name)
local m = getmetatable(obj)
if not m then return nil end
for _, operations in ipairs(m.behaviors) do
if operations[message_name] then return operations[message_name] end
end
@randrews
randrews / match_test.lua
Created June 10, 2011 06:14
Toy regular expression matcher in Lua
require "match"
test("Basic match",
function()
assert(("floob"):match("flo"))
assert(not ("floob"):match("nar"))
end)
test("Caret match",
function()
@randrews
randrews / console_fall.lua
Created June 16, 2011 04:50
Console version of Lua puzzle game
function make_board(size)
local board = { size = size }
setmetatable(board, { __tostring = board_tostring })
for n = 0, size * size - 1 do
board[n] = 0
end
return board
@randrews
randrews / fall.lua
Created June 18, 2011 22:02
First draft of Love puzzle game
function make_board(size)
local board = { size = size }
setmetatable(board, { __tostring = board_tostring })
for n = 0, size * size - 1 do
board[n] = 0
end
return board