View Full Version : Boggle board challenge
GnuVince
06-24-2002, 05:26 AM
Hi gals and guys! Today, Vince proposes an easy challenge: generating a boggle board of arbitrary size:
[vince@vincent: ~/prog/ocaml/boggle]% ./boggle 3
-------------
| U | A | F |
-------------
| H | S | J |
-------------
| N | M | B |
-------------
[vince@vincent: ~/prog/ocaml/boggle]% ./boggle 4
-----------------
| B | E | H | E |
-----------------
| P | J | B | F |
-----------------
| H | E | A | A |
-----------------
| P | V | L | A |
-----------------
[vince@vincent: ~/prog/ocaml/boggle]% ./boggle 5
---------------------
| B | U | F | L | G |
---------------------
| O | G | P | T | G |
---------------------
| O | Y | T | D | F |
---------------------
| T | S | P | T | R |
---------------------
| P | K | J | S | W |
---------------------
[vince@vincent: ~/prog/ocaml/boggle]%
One thing: all Q's must be replaced with Qu's. The lines and columns must be aligned and the lines (dashes and pipes) must be there. Enjoy! I'll post my code later, for now it's sleep time (4:30AM)
Strike
06-24-2002, 10:09 AM
Well, it's ugly if you have "Qu"s, but I'll clean it up in a bit. Here's the simple Python version:
import sys, random
class BadArguments(Exception): pass
if len(sys.argv) != 2:
print "Usage: boggle-board.py <size-of-board>"
raise BadArguments # as I was discussing with vomjom, errors are so
# passe - exceptions are where it's at ;)
size = int(sys.argv[1])
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Each letter has 4 -'s to itself, and then one on the end
print "----" * size + "-"
for x in range(size):
for y in range(size):
letter = random.choice(letters)
if letter == 'Q': letter = 'Qu'
print "| %s" % letter,
print "|"
print "----" * size + "-"
So does each letter have the same chance of occurring? Is that true to boggle? I've never played boggle.
Strike
06-24-2002, 10:55 AM
kmj: boggle has 25 6 sided dice with letters on each side. So, no, not every letter has the same chance of occurring as in real Boggle.
GnuVince
06-24-2002, 12:12 PM
kmj: Normally they don't have the same chances, but since:
(1) I don't know the ratios
(2) We want a board of any size
Just use random letters, it'll be all right.
Here's the O'Caml version:
l
let usage () =
Printf.eprintf "Usage: %s <size>\n" Sys.argv.(0);
exit 1
let print_line size =
print_char ' ';
print_string (String.make (size+1) '-');
print_char '\n'
let string_of_char c =
String.make 1 c
let convert = function
'Q' -> "Qu"
| c -> string_of_char c
let print_board size =
print_line (size*4);
for i = 1 to size do
for j = 1 to size do
Printf.printf " |%2s" (convert (Char.chr ((Random.int 25) + 65)));
done;
print_string " |\n";
print_line (size*4);
done
let _ =
Random.self_init ();
try
print_board (int_of_string Sys.argv.(1))
with
Failure "int_of_string"
| Invalid_argument(_) -> usage ()
javaman99
06-28-2002, 02:19 PM
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
void printSquare(int);
int main(int argc, char *argv[])
{
srand(time(NULL));
int dimension;
dimension = atoi(argv[1]);
for(int j = 0; j < dimension; j++)
{
printSquare(dimension);
cout << "|";
cout << endl;
}
cout << " ";
for (int i = 0; i < 4 * dimension - 1; i++)
printf("-");
return 0;
}
void printSquare(int dim)
{
int theChar;
cout << " ";
for (int i = 0; i < 4 * dim - 1; i++)
printf("-");
cout << endl;
for (int i = 0; i < dim; i ++)
{
theChar = (rand() % 25 + 65);
if (theChar == 81)
{
printf("| Qu");
}
else
{
printf("| %c ",theChar );
}
}
}
LiquidG
08-08-2002, 02:01 PM
Here's my C version ... since I'm still a bit rusty with C, could anybody point out how I could streamline this code some? And yes, I know the time to generate the output for this is long, but I read somewhere bout using higher order bits when using the srand function ...
Oh yeah, btw, just to clear all I did to compile the code was a:
gcc boggle_board.c - o boggle
and that the format to call the .o is ./<program name> <boggle-board size>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
/***GLOBAL DECLARATIONS***/
int bsize;
/*************************/
char alpha_generator()
{
/***VARIABLE DECLARATIONS***/
char random_char, alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int j;
/***Generate random number***/
sleep(1);
srand( time(NULL) );
j=(int) (25.0*rand()/(RAND_MAX+1.0));
random_char = alpha[j];
return random_char;
}
char* sequence(int boggle_size)
{
int elements = boggle_size * boggle_size;
char *seq = (char*) malloc (elements);
int i = 0;
do {
seq[i] = alpha_generator();
i++;
} while (i < (elements));
return seq;
}
void draw(int boggle_size, char *seq_ptr)
{
int i, s, r, num_dash = (4 * boggle_size) + 1;
/***Repeat for 'boggle_size' rows***/
for (i = 1; i <= boggle_size; i++) {
/***Print the horizontal dashs***/
for (s = 1; s <= num_dash; s++) {
printf("-");}
printf("\n");
/***Print the row characters and pipes***/
for (s = 1; s <= boggle_size; s++) {
printf("|");
/***Replace 'Q' with 'Qu' if necessary***/
if (*seq_ptr == 'Q') { printf(" Qu"); }
else { printf(" %c ", *seq_ptr); }
seq_ptr++;
}
printf("|\n");
}
/***Print last row of dashs***/
for (s = 1; s <= num_dash; s++) {
printf("-");}
printf("\n");
}
main(int argc,char *argv[])
{
char *ptr;
/***Verify proper program invocation***/
if (argc != 2) {
printf("\nImproper program call!\n\n");
exit(1); }
sscanf(argv[1], "%d", &bsize);
/***Verify POSITIVE WHOLE number boggle_size***/
if ( (bsize < strlen(argv[1])) || (bsize <= 0) ) {
printf("\nPlease enter a positive whole number boggle size!\n\n");
exit(1); }
ptr = sequence(bsize);
draw(bsize, ptr);
}
vBulletin® v3.7.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.