I found an old minimal implementation of Boggle that I wrote a few years ago. I cleaned it up and tweaked it a bit further. It generate a correct boggle board using the American dice and checks input words against the board.

/* Mini boggle by Jonas Minnberg 200x */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static char board[49], *b = board + 8;

int check_board(char *w, int i, int v) {
    int *p, offs[] = {-9,-8,-7,-1,1,7,8,9,0};
    for(p = offs; *p; p++)
        if((!*w) || ((*w == b[*p+i]) && !(v&1<<(*p+i)) && check_board(w+1, *p+i, v|1<<i)))
        	return 1;
    return 0;
}

int main() {
    int i, dn=6, used[] = {4,3,4,2,2,1,0};
    char input[80], *dice = "aeiouyaeiouetdbtgprlhmnkcswfhsqxzjkv";

    srand(time(NULL));
    for(i=0; i<=49; i++) board[i] = 0;
    for(i=0; i<16; used[dn]--, dn=6, i++) {
        while(!used[dn]) dn = rand()%6;
        b[i+(i&12)] = dice[dn*6+rand()%6];
    }
    while(1) {
        for(i=0; i<4; i++) puts(&b[i*8]);
        putchar('>'); gets(input);
        for(i=0; i<16; i++)
            if(b[i+(i&12)] == *input && check_board(input+1, i+(i&12), 0))
                printf("Found at (%d,%d)\n", i%4+1, i/4+1);
    }
    return 0;
}

This is how it works:

# gcc boggle.c -oboggle
# ./boggle
osnm
saoj
aido
tteg
>moans
Found at (4,1)
osnm
saoj
aido
tteg
>soda
Found at (2,1)
osnm
saoj
aido
tteg
>godot
osnm
saoj
aido
tteg


blog comments powered by Disqus

Published

19 September 2011

Tags