Skip to content

Instantly share code, notes, and snippets.

@nowl
nowl / gist:4752490
Last active September 22, 2019 23:48
Monte Carlo Tree Search implemented in Common Lisp to play Tic-Tac-Toe. Adapted from some of the example python code found on www.mcts.ai
;; adapted to lisp from the python code found here:
;; http://www.mcts.ai/?q=code/python
(defgeneric do-move (state move))
(defgeneric get-random-move (state))
(defgeneric get-moves (state))
(defgeneric get-result (state player-just-moved))
(defgeneric clone (state))
(defgeneric print-state (state))
(defgeneric player-just-moved (state))
@nowl
nowl / los_spiral.c
Created September 20, 2011 03:12
Spiral FOV test
#include "lapis.h"
#include <math.h>
#define DIAMETER 30
#define PI 3.1415926
struct los
{
float default_mina, default_maxa;
@nowl
nowl / random-word.lisp
Created September 9, 2011 03:23
Common Lisp code to generate random words based on next-letter frequency from a predefined corpus
(defparameter *valid-chars* '(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z))
(defparameter *table* nil)
(defparameter *norm-table* nil)
(defun build-table ()
(setf *table* (make-hash-table))
(with-open-file (in #p"corpus.txt" :direction :input)
(loop with lower-c with c with p while (not (eql c :eof)) do
(setf c (read-char in nil :eof)
@nowl
nowl / k-means.lisp
Created April 13, 2011 23:08
k-means clustering in Common Lisp
(defun classify (means data dist-func)
(let ((sets (loop for m in means collect '())))
(loop for d in data do
(let ((min 0)
(dist (funcall dist-func d (car means))))
(loop for m in (cdr means) for n from 1 do
(when (< (funcall dist-func d m) dist)
(setf min n
dist (funcall dist-func d m))))
(push d (nth min sets))))
@nowl
nowl / perlin.py
Created April 6, 2011 16:42
python Perlin Noise
class Perlin (object):
def __init__(self, seed=0):
self.seed = seed
self.hash = [208,34,231,213,32,248,233,56,161,78,24,140,71,48,140,254,245,255,247,247,40,
185,248,251,245,28,124,204,204,76,36,1,107,28,234,163,202,224,245,128,167,204,
9,92,217,54,239,174,173,102,193,189,190,121,100,108,167,44,43,77,180,204,8,81,
70,223,11,38,24,254,210,210,177,32,81,195,243,125,8,169,112,32,97,53,195,13,
203,9,47,104,125,117,114,124,165,203,181,235,193,206,70,180,174,0,167,181,41,
164,30,116,127,198,245,146,87,224,149,206,57,4,192,210,65,210,129,240,178,105,
228,108,245,148,140,40,35,195,38,58,65,207,215,253,65,85,208,76,62,3,237,55,89,
@nowl
nowl / gtk_file_dialog.c
Created March 12, 2011 06:06
Opening a file dialog with gtk
#include <gtk/gtk.h>
static gboolean
menu_close_program(GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
gtk_main_quit();
return TRUE;
@nowl
nowl / ppm.c
Created February 16, 2011 01:56
simple PPM output
static void
write_header(FILE *out, int width, int height, int max_color_val)
{
fprintf(out, "P3\n%d %d\n%d\n", height, width, max_color_val);
}
static void
write_color_val(FILE *out, int red, int green, int blue)
{
@nowl
nowl / perlin.lisp
Created February 16, 2011 01:55
Perlin Noise in Common Lisp (slightly optimized)
(defpackage #:perlin
(:use #:cl)
(:export #:perlin2d
#:*seed*))
(in-package #:perlin)
(declaim (optimize (speed 3) (safety 0) (debug 0)))
(defparameter *seed* 0)
@nowl
nowl / perlin.c
Created February 15, 2011 19:04
Perlin Noise in C
#include <stdio.h>
static int SEED = 0;
static int hash[] = {208,34,231,213,32,248,233,56,161,78,24,140,71,48,140,254,245,255,247,247,40,
185,248,251,245,28,124,204,204,76,36,1,107,28,234,163,202,224,245,128,167,204,
9,92,217,54,239,174,173,102,193,189,190,121,100,108,167,44,43,77,180,204,8,81,
70,223,11,38,24,254,210,210,177,32,81,195,243,125,8,169,112,32,97,53,195,13,
203,9,47,104,125,117,114,124,165,203,181,235,193,206,70,180,174,0,167,181,41,
164,30,116,127,198,245,146,87,224,149,206,57,4,192,210,65,210,129,240,178,105,
(defparameter *world*
(append
(loop for count below 50 collect
(let* ((r (random 256))
(g (random 256))
(b (random 256))
(angle (/ (* 6 3.14159 count) 50))
(rad (- 2 (* 2 (/ count 50.0))))
(x (* rad (sin angle)))
(z (* rad (cos angle)))