#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #include "splib.h" #ifndef FALSE #define TRUE (0==0) #define FALSE (0!=0) #endif static int debug = FALSE; static int html = FALSE; static int report = TRUE; static int megaanalysis = FALSE; #define MIN(a, b) (a < b ? a : b) #define WIDTH 15 #define HEIGHT 15 #define MAX_TILES 100 #define RACKSIZE 7 /* 7 for most, 9 for old-style German */ #define BINGO 50 #define FULLRACK "aaaaaaaaabbccddddeeeeeeeeeeeeffggghhiiiiiiiiijkllllmm\ nnnnnnooooooooppqrrrrrrssssttttttuuuuvvwwxyyz??" /* 1..WIDTH for board, 0 and (WIDTH+1) for tombstones */ typedef struct GAMESTATE { char board[HEIGHT+2][WIDTH+2]; char apparent_letter[HEIGHT+2][WIDTH+2]; int tiles_held /* = 0*/; char tiles[/*RACKSIZE*/MAX_TILES+1]; /* Empty, for static analysis */ char remaining_tiles[MAX_TILES+1]; /* Full, for static analysis */ int myscore /*= 0*/; int oppscore /*= 0*/; int vertical_score[HEIGHT+2][WIDTH+2]; char fullrack[MAX_TILES+1]; char *crosschecks[HEIGHT+2][WIDTH+2]; char *vertical_word[HEIGHT+2][WIDTH+2]; int vindex[HEIGHT+2][WIDTH+2]; char *boardfile /* = NULL*/; /* This should be in "main()" but for hacky reasons (HTML in playword()) it is global *for now* */ int bestscore; char playtiles[/*RACKSIZE*/MAX_TILES+1]; /* Empty, for static analysis */ char playword[/*RACKSIZE*/MAX_TILES+1]; /* Empty, for static analysis */ char playtilesleft[/*RACKSIZE*/MAX_TILES+1]; /* Empty, for static analysis */ char playtemplate[/*RACKSIZE*/MAX_TILES+1]; /* Empty, for static analysis */ int playplaced; int playLetter; int playnumber; int playorientation; int movesconsidered; } GAMESTATE; /* QUESTION: The first question I have is very basic. Let's say we've made a play that puts down 3 specific letters. What is the probability of having picked a rack which contained those letters? Is is just Combs(3, 100), or is it something like Combs(3, Combs(7, 100))? I suspect it makes a difference if you say that the rest of the rack is different letters, as opposed to you don't care what the rest of the rack is - i.e. you could allow duplicated letters. I think for this application I don't care what the other letters are. ANSWER: Well, it makes a heck of a lot of difference what the three letters are. The chances of having a rack with the three letters EIA is a whole lot greater than having JZX. What you'd do to figure this out is to first calculate the number of racks that would meet the criteria. First, you'd calculate the number of different ways you could form the 3-letter combination in question with the letters in the Scrabble set - in the case of EIA, it would be 12 x 9 x 9. (In the case of JZX, it would be 1.) Then, you'd multiply that number by the number of ways that you could put together the other four tiles, which is "97 choose 4" (i.e., the number of ways you could choose 4 items out of 97 items), which is 97!/(93! x 4!). Therefore, the numerator of your probability, in the case of EIA???? would be (12 x 9 x 9 x 97!)/(93! x 4!). And the denominator of the probability, as always, is "100 choose 7", or 100!/(93! x 7!) - the number of ways you could choose an opening rack from a full bag. Expanding the above and cancelling where appropriate gives: 12.9.9.97! ---------- 93!.4! -------------- => 12.9.9.97! * 93!.7! 12.9.9 * 7! ------------------- => -------------- 100! 93!.4! * 100! 4! * 100.99.98 ------ 93!.7! Let's do this again for a single 'z': 1.99! * 93!.7! 1.7! 7 --------------- => ------ => --- 93!.6! * 100! 6!.100 100 So the probability of chosing a Z with a draw of 7 tiles is 7/100. */ float fact(int i) { float tot = (float)1; for (;;) { if (i <= 0) return(tot); tot *= (float)i--; } } float drawprob(char *tile, char *bag, int drawsize) { int bagtot[256], tiletot[256]; /* I had to do these as floats because longs were overflowing */ float numerator = 1.0; float denominator = 1.0; float max = (float)strlen(bag); int maxtiles = strlen(tile); int i; for (i = 0; i < 256; i++) tiletot[i] = bagtot[i] = 0; while (*bag != '\0') bagtot[*bag++] += 1; while (*tile != '\0') tiletot[*tile++] += 1; for (i = 0; i < 256; i++) { if (tiletot[i] != 0) { numerator *= ( fact(bagtot[i]) / (fact(bagtot[i] - tiletot[i]) * fact(tiletot[i]))); /* eg 12.9.9, or 1 choose(1,12).choose(1,9).choose(1,9) or choose(1,1).choose(1,1).choose(1,1) or choose(2,2) */ } } for (i = 0; i < maxtiles; i++) { numerator *= (float)(drawsize-i); /* add the factorial in the numerator (eg 7!) */ denominator *= max; /* eg 100.99.98 or 100 */ max -= 1.0; } if (numerator/denominator > 1.0) return(1.0); return(numerator/denominator); } static int lettermult[HEIGHT+2][WIDTH+2] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,0}, {0,1,1,1,1,1,3,1,1,1,3,1,1,1,1,1,0}, {0,1,1,1,1,1,1,2,1,2,1,1,1,1,1,1,0}, {0,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,3,1,1,1,3,1,1,1,3,1,1,1,3,1,0}, {0,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,0}, {0,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,0}, {0,1,1,2,1,1,1,2,1,2,1,1,1,2,1,1,0}, {0,1,3,1,1,1,3,1,1,1,3,1,1,1,3,1,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,2,1,1,1,1,1,1,2,1,1,1,1,1,1,2,0}, {0,1,1,1,1,1,1,2,1,2,1,1,1,1,1,1,0}, {0,1,1,1,1,1,3,1,1,1,3,1,1,1,1,1,0}, {0,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, }; static int wordmult[HEIGHT+2][WIDTH+2] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,3,1,1,1,1,1,1,3,1,1,1,1,1,1,3,0}, {0,1,2,1,1,1,1,1,1,1,1,1,1,1,2,1,0}, {0,1,1,2,1,1,1,1,1,1,1,1,1,2,1,1,0}, {0,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,0}, {0,1,1,1,1,2,1,1,1,1,1,2,1,1,1,1,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,3,1,1,1,1,1,1,2,1,1,1,1,1,1,3,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,1,1,1,2,1,1,1,1,1,2,1,1,1,1,0}, {0,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,0}, {0,1,1,2,1,1,1,1,1,1,1,1,1,2,1,1,0}, {0,1,2,1,1,1,1,1,1,1,1,1,1,1,2,1,0}, {0,3,1,1,1,1,1,1,3,1,1,1,1,1,1,3,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; #define HORIZONTAL 1 #define VERTICAL 2 static float totprob[256]; static int tot[256]; static int itot[27] = /* 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 */ { 9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2, 1,6,4,6,4,2,2,1,2, 1}; static int score[256]; static int iscore[27] = /* a b c d e f g h i j k l m n o p q r s t */ { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, /* u v w x y z */ 1, 4, 4, 8, 4, 10}; /* Temporarily, until I write some code to handle rack leaves in my own idiosyncratic way, I'm going to use Mark Watkins' values for rack-leave on a per-tile basis. (Not actually used yet) */ static float watkins_rackvalue[256]; static float iwatkins_rackvalue[27] = /* a b c d e f g h i j */ { 1.0, -2.0, -0.5, 0.5, 2.0, -2.0, -3.0, 1.5, -1.0, -2.0, /* k l m n o p q r s t */ 0.0, 0.5, 0.5, 0.5, -0.5, -1.0, -9.0, 1.0, 8.0, 0.0, /* u v w x y z */ -4.0, -6.0, -3.0, 2.5, -0.5, 5.0}; static NODE *dawg; static INDEX nedges; /*--------------------- crosscheck code -------------------------*/ /* When placing a word horizontally, if it abuts to any tiles either above or below, we will have generated a wildcard string consisting of the tiles above, a "?", and the tiles below, from which we then find all the valid words allowed which match that wildcard string. From that we find which letters were valid at that position, and we return the set of valid letters to the scrabble code, so that the search for legal plays can be trimmed for speed. */ /* External interface procedure follows this internal procedure, which needs a lot of extra parameters that the interface isn't really interested in */ static int crosscheck_internal( NODE *dawg, INDEX i, char *word, char *res, char *set, int len, int *found ) { int endsword, last, ch, target; NODE node; INDEX link; static int wildch = '\0'; for (;;) { node = dawg[i++]; ch = (int)((node >> V_LETTER) & M_LETTER); last = ((node & (INDEX)M_END_OF_NODE) != 0); endsword = ((node & M_END_OF_WORD) != 0); link = node & M_NODE_POINTER; res[len] = ch; res[len+1] = '\0'; target = ((int)*word)&255; if (ch != 0) { if (ch == target || target == '?') { if (target == '?') { wildch = ch; } if (endsword && *(word+1) == '\0') { int i; (*found)++; /* Add ch to crosscheck set if not already there */ /* We just assume only 1 '?' per word, and last wildch was it */ if (strchr(set, wildch) == NULL) { i = strlen(set); set[i] = wildch; set[i+1] = '\0'; } } if (*(word+1) != '\0' && link != 0) (void) crosscheck_internal(dawg, link, word+1, res, set, len+1, found); } } if (last) break; } return(0==0); } /* This is a 'black box' procedure which can be replaced by anything which behaves the same even though implemented differently. In fact the internal code of this function is a 'quick hack' and probably should be replaced by something neater. INTERFACE: inputs: word list, wild-card string containing one "?" output: string of letters which the "? could represent or NULL if none found. (i.e this square is blocked) example: twl98, "c?t" -> "auo" (cat, cut and cot all matching the pattern) */ char *convert_wildcard_to_permitted_set(NODE *dawg, char *word) { char result[MAX_WORD_LEN]; static char set[MAX_WORD_LEN]; int i = 0; *set = '\0'; if (debug) fprintf(stderr, "wildcard: %s\n", word); (void)crosscheck_internal(dawg, (INDEX)ROOT_NODE, word, result, set, 0, &i); if (i == 0) return(NULL); if (debug) fprintf(stderr, " -> %s\n", set); return(set); } /*--------------------- placement code -------------------------*/ /* This finds words which can be played at this row,column position of the length requested. It does a wildcard match of a string such as "c[aiou]t[etrains]" which in this instance might return cats, cots, cuts. The search is pruned in two ways - the first, by which letters are allowed on certain squares, eg [aiou] may be a constraint from cross-check words (see above); and the second by the tiles in your rack (eg "etrains"). If you hold a blank, the wildcard might look like "c[aiou]t?" instead. The decision to do separate searches for different lengths of words was a deliberate one, to simplify the code. Some scrabble programs would prefer to do searches which looked more like "c[aiou]t*" allowing more letters to be added at the right. I chose not to do this for simplicity, and also because some other optimisations are possible when you are using fixed-length searches (although those have not yet been done). This procedure is also passed the tiles which we hold, because the wildcard itself does not describe only valid words. For instance, if we hold the tiles "act" then the wildcard might be "[act][act][act]" - but "tat" could not be played even though wildcard expansion listed it. By removing the tiles played at each point for a wild letter, we end up playing only "act" and "cat" here. One known design flaw: if we have the tiles "ob?" and want to pay the word "bob", then the fixed "b" will always be put down before the wild-card as a "b". This could be a mistake if the second "b" is on a triple-letter score, for example. */ void playword(GAMESTATE *game, char *tiles, char *word, char *tilesleft, char *template, int placed, int Letter, int number, int orientation) { int hscore = 0, vscore = 0; int row, col; int tmp, tmpch, i, nexttile = 0; int wmult = 1; char *s = word; tiles[placed] = '\0'; /* we treat the board as horizontal even if it's not */ row = (orientation == HORIZONTAL ? number : Letter-'A'+1); col = (orientation == HORIZONTAL ? Letter-'A'+1 : number); if (report) fprintf(stdout, (orientation == VERTICAL ? "place %s at %c%d %s (%s) => " : "place %s at %d%c %s (%s) => "), tiles, (orientation == VERTICAL ? Letter : number), (orientation == VERTICAL ? number : Letter), (orientation == VERTICAL ? "down" : "across"), template); assert((word != NULL) && (game->boardfile != NULL) && (game->fullrack != NULL) && (tiles != NULL)); if (report) { if (html) { fprintf(stdout, "<A HREF=\"play?word=%s&board=%s&bagwas=%s&rackwas=%s&letter=%c&number=%d&orientation=%d&game->myscore=%d&game->oppscore=%d\">%s</A>", word, game->boardfile, game->fullrack, tiles, Letter, number, orientation, game->myscore, game->oppscore, word); } else { fprintf(stdout, "%s", word); } } i = col; for (;;) { if (*s == '\0') break; if (game->board[row][i] == 0) { /* Place first letter from "tiles" here */ hscore += ((tmp = score[tmpch = tiles[nexttile++]]) * lettermult[row][i]); if (debug) fprintf(stdout, "row %d col %d: add %d%s to hscore for %c\n", row, i, tmp, (lettermult[row][i] == 2 ? "*2L" : ( lettermult[row][i] == 3 ? "*3L" : "") ), tmpch); wmult *= wordmult[row][i]; if (game->vertical_word[row][i] != NULL) { tmp = (game->vertical_score[row][i] + (score[tmpch] * lettermult[row][i])) * wordmult[row][i]; vscore += tmp; game->vertical_word[row][i][game->vindex[row][i]] = *s; /* Plug in the letter to the vword */ if (report) fprintf(stdout, "/%s(%d", game->vertical_word[row][i], tmp); if (wordmult[row][i] != 1) if (report) fprintf(stdout, ":%dWS", wordmult[row][i]); if (report) fprintf(stdout, ")"); game->vertical_word[row][i][game->vindex[row][i]] = '?'; /* restore */ } if (debug) if (game->vertical_word[row][i] != NULL) { fprintf(stdout, "Intersects with %s ", game->vertical_word[row][i]); game->vertical_word[row][i][game->vindex[row][i]] = *s; /* Plug in the letter to the vword */ fprintf(stdout, "forming %s, worth %d", game->vertical_word[row][i], tmp); if (wordmult[row][i] != 1) fprintf(stdout, " (%dWS)", wordmult[row][i]); game->vertical_word[row][i][game->vindex[row][i]] = '?'; /* restore */ fprintf(stdout, "\n"); } } else { /* The tile is already placed - game->apparent_letter takes care of blanks */ /* no bonuses */ tmp = score[tmpch = game->apparent_letter[row][i]]; hscore += tmp; if (debug) fprintf(stdout, "add %d to hscore for %c\n", tmp, tmpch); } s += 1; i += 1; } /* simple hscore, and vscores now all added */ if (debug) if (wmult != 1) fprintf(stdout, "Multiply score by %d\n", wmult); hscore *= wmult; assert(nexttile == placed); if (placed == RACKSIZE) { hscore += BINGO; if (debug) fprintf(stdout, "Add 50 for a bingo!\n"); } if (megaanalysis) { if (report) fprintf(stdout, ". SCORE %d\n", hscore+vscore); } else { if (report) fprintf(stdout, ", leaving %s. SCORE %d\n", tilesleft, hscore+vscore); } if (megaanalysis && report) { fprintf(stdout, "The probability of playing this move is P(\"%s\", \"%s\") = %1.7f\n", tiles, game->fullrack, drawprob(tiles, game->fullrack, 7 /*BUG*/)); } game->movesconsidered += 1; if (hscore+vscore > game->bestscore) { game->bestscore = hscore+vscore; strcpy(game->playtiles, tiles); strcpy(game->playword, word); strcpy(game->playtilesleft, tilesleft); strcpy(game->playtemplate, template); game->playplaced = placed; game->playLetter = Letter; game->playnumber = number; game->playorientation = orientation; } /* I guess the recursive call to 'minimax()' should go here??? */ } /* External interface procedure follows this internal procedure, which needs a lot of extra parameters that the interface isn't really interested in */ static void fix_scrabble( GAMESTATE *game, NODE *dawg, INDEX i, char *word, char *template, char *res, char *tilesleft, char *placedtiles, int placed, int len, int *found, int L, int n, int orientation ) { int endsword, last, ch, target; NODE node; INDEX link; for (;;) { node = dawg[i++]; ch = (int)((node >> V_LETTER) & M_LETTER); last = ((node & (INDEX)M_END_OF_NODE) != 0); endsword = ((node & M_END_OF_WORD) != 0); link = node & M_NODE_POINTER; res[len] = ch; res[len+1] = '\0'; target = *word; target &= 255; if (ch != 0) { if (ch == target) { /* matches a tile on the board already */ if (endsword && *(word+1) == '\0') { playword(game, placedtiles, res, tilesleft, template, placed, L, n, orientation); (*found)++; } if (*(word+1) != '\0' && link != 0) { (void) fix_scrabble(game, dawg, link, word+1, template, res, tilesleft, placedtiles, placed, len+1, found, L, n, orientation); } } else if (target == '?') { /* We matched a wildcard. If we have the correct letter, play it; otherwise play the blank */ if (endsword && *(word+1) == '\0') { char *s; int i; s = strchr(tilesleft, ch); if (s != NULL) { /* Do we have the actual tile? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; placedtiles[placed] = ch; playword(game, placedtiles, res, tilesleft+1, template, placed+1, L, n, orientation); (*found)++; } s = strchr(tilesleft, '?'); if (s != NULL) { /* If not, do we have a blank left to play? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; res[len] = toupper(ch); /* Signal blank to display routine */ /* This trick is not ideal. Would be better to have a parallel array to 'res' which is given the equivalent of 'game->apparent_letter' instead of uppercase. (i.e. the actual blank character. Note in almost every case I can think of, it is better to play the actual letter if you have it rather than the blank. The only exception is when you're playing the same letter twice in a word, once as the letter and once as the blank. In that case, you don't know which of the two positions may be a double or triple letter square. But if the blank is not needed in a word at all because you already have all the letters, why would you ever play it? */ placedtiles[placed] = '?'; /* blank */ playword(game, placedtiles, res, tilesleft+1, template, placed+1, L, n, orientation); (*found)++; } } if (*(word+1) != '\0' && link != 0) { char *s; int i; s = strchr(tilesleft, ch); if (s != NULL) { /* Do we have the actual tile? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; placedtiles[placed] = ch; (void) fix_scrabble(game, dawg, link, word+1, template, res, tilesleft+1, placedtiles, placed+1, len+1, found, L, n, orientation); } s = strchr(tilesleft, '?'); if (s != NULL) { /* If not, do we have a blank left? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; res[len] = toupper(ch); /* Signal blank to display routine */ placedtiles[placed] = '?'; /* Blank */ (void) fix_scrabble(game, dawg, link, word+1, template, res, tilesleft+1, placedtiles, placed+1, len+1, found, L, n, orientation); } } } else if (target == '[') { /* Is this letter in our set of valid letters? */ char choices[(WIDTH * (256+2)) + 1]; char *s, *saved = word; strcpy(choices, word+1); s = strchr(choices, ']'); /* We assume well-formed expressions */ *s = '\0'; word = strchr(word, ']'); if (strchr(choices, ch) != NULL) { if (endsword && *(word+1) == '\0') { char *s; int i; s = strchr(tilesleft, ch); if (s != NULL) { /* Do we have the actual tile? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; placedtiles[placed] = ch; playword(game, placedtiles, res, tilesleft+1, template, placed+1, L, n, orientation); (*found)++; } s = strchr(tilesleft, '?'); if (s != NULL) { /* If not, do we have a blank left? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; res[len] = toupper(ch); /* Signal blank to display routine */ placedtiles[placed] = '?'; /* Blank */ playword(game, placedtiles, res, tilesleft+1, template, placed+1, L, n, orientation); (*found)++; } } if (*(word+1) != '\0' && link != 0) { char *s; int i; s = strchr(tilesleft, ch); if (s != NULL) { /* Do we have the actual tile? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; placedtiles[placed] = ch; (void) fix_scrabble(game, dawg, link, word+1, template, res, tilesleft+1, placedtiles, placed+1, len+1, found, L, n, orientation); } s = strchr(tilesleft, '?'); if (s != NULL) { /* If not, do we have a blank left? */ i = *s; *s = tilesleft[0]; tilesleft[0] = i; res[len] = toupper(ch); /* Signal blank to display routine */ placedtiles[placed] = '?'; /* Blank */ (void) fix_scrabble(game, dawg, link, word+1, template, res, tilesleft+1, placedtiles, placed+1, len+1, found, L, n, orientation); } } } word = saved; } } if (last) break; } } /* This is a 'black box' procedure which can be replaced by anything which behaves the same even though implemented differently. In fact the internal code of this function (above) is a 'quick hack' and probably should be replaced by something neater. INTERFACE: inputs: word list, wild-card, rack, row & column outputs: should be list of words to play. (Currently the output is printed, not returned) The wild card can contain only fixed letters, [abcd...] (sets of letters), or "?" (single-character wild-card). There are no multi- character wild-cards (eg "*") - the word length is fixed. IMPORTANT NOTE: "cat?" is not the same as "c[a]t?" - the former expects ONE tile to be placed, the latter exects TWO. I.e. simple letters represent tiles already placed on the board. A cleaned-up interface would not need L/n (letter/number, ie row/col in format such as "H7 across" or "G5 down". Also note 'orientation' is wrongly being passed around as a global, not as a parameter) because this procedure should just return the list of words, and doesn't need to know where they are placed if it is not doing the placement itself. */ int scrabble_match(GAMESTATE *game, NODE *dawg, char *word, char *tiles, int L, int n, int orientation) { char result[MAX_WORD_LEN]; char placedtiles[MAX_WORD_LEN]; int i = 0; result[0] = '\0'; placedtiles[0] = '\0'; fix_scrabble(game, dawg, (INDEX)ROOT_NODE, word, word, result, tiles, placedtiles, 0, 0, &i, L, n, orientation); return(i); } /*--------------------------------------------------------------*/ /* This reads a board in the simple file format which Kevin Cowtan uses in his board-graphic image generator software. I just happened to have some cgi web page software which generates this format so I plan to reuse it to create a graphical front-end to solving scrabble problems. */ void read_board(GAMESTATE *game, char *fname) { int row, col; int c, rackletter; int tiles_left_in_bag; FILE *sample; char *s; sample = fopen(fname, "r"); if (sample == NULL) { fprintf(stderr, "Cannot open board file '%s'\n", fname); exit(0); } for (row = 1; row <= HEIGHT; row++) { if (debug) fprintf(stderr, "Row %02d: ", row); for (col = 1; col <= WIDTH; col++) { c = fgetc(sample); if (isalpha(c)) { /* Take care with locale for other language versions */ game->board[row][col] = c; game->apparent_letter[row][col] = tolower(c); if (debug) fprintf(stderr, "%c", tolower(c)); if (islower(c)) { rackletter = '?' /* BLANK */; } else { rackletter = tolower(c); } s = strchr(game->fullrack, rackletter); if (s == NULL) { fprintf(stderr, "Error: we appear to have too many %c's on the board\n", rackletter); fprintf(stderr, " %s\n", FULLRACK); fprintf(stderr, " %s\n", game->fullrack); exit(0); } memmove(s, s+1, strlen(s)); } else if (debug) fprintf(stderr, " "); } c = fgetc(sample); /* newline */ if (c != '\n') { fprintf(stderr, "Data format in .dat file\n"); exit(0); } if (debug) fprintf(stderr, "\n"); } game->tiles_held = 0; for (col = 0; col < RACKSIZE; col++) { c = fgetc(sample); if (c == '?') continue; if (c == '*') c = '?'; if (isalpha(c)) c = tolower(c); rackletter = game->tiles[game->tiles_held++] = c; s = strchr(game->fullrack, rackletter); if (s == NULL) { fprintf(stderr, "Error: we appear to have too many %c's in the rack\n", rackletter); fprintf(stderr, " %s\n", FULLRACK); fprintf(stderr, " %s\n", game->fullrack); exit(0); } memmove(s, s+1, strlen(s)); } game->tiles[game->tiles_held] = '\0'; fscanf(sample, " %d %d\n", &game->myscore, &tiles_left_in_bag); if (debug) { fprintf(stderr, "%0d Tiles: %s\n", game->tiles_held, game->tiles); } if ((tiles_left_in_bag != 0) && (tiles_left_in_bag != strlen(game->fullrack))) { fprintf(stderr, "Data file says %d tiles left. I'm showing %d left\n%s\n", tiles_left_in_bag, strlen(game->fullrack), game->fullrack); exit(0); } /* We don't really need this now since we're calculating it on the fly */ game->remaining_tiles[0] = '\0'; fgets(game->remaining_tiles, MAX_TILES+1, sample); /* Ignore error code */ game->remaining_tiles[MAX_TILES] = '\0'; fclose(sample); } /*--------------------------------------------------------------*/ /* This looks at the place where a tile is likely to be played, and whether there are tiles abutting above or below it, which would permit or deny a word to be placed there. This generates a wild-card string which is then expanded in the code above to generate the set of letters which are valid here. It is that set which is stored, not the wild card string or the words themselves. */ char *create_crosscheck_wildcard(GAMESTATE *game, int r, int c) { char crosscheck[WIDTH+2]; char *s; int rp; game->vertical_score[r][c] = 0; /* Already a tile here so crosscheck is meaningless */ if (game->board[r][c] != 0) return(NULL); /* none above and none below? */ if ((game->board[r-1][c] == 0) && (game->board[r+1][c] == 0)) return(NULL); /* what's above? */ rp = r-1; while (game->board[rp][c] != 0) rp -= 1; rp += 1; /* row of 1st letter in crosscheck word */ s = crosscheck; while (rp != r) { *s = game->apparent_letter[rp][c]; game->vertical_score[r][c] += score[*s]; /* 0 if blank */ s += 1; rp += 1; } *s++ = '?'; /* r */ game->vindex[r][c] = s-crosscheck-1; /* what's below? */ rp = r+1; while (game->board[rp][c] != 0) { *s = game->apparent_letter[rp][c]; game->vertical_score[r][c] += score[*s]; /* 0 if blank */ s += 1; rp += 1; } *s = '\0'; game->vertical_word[r][c] = strdup(crosscheck); return(strdup(crosscheck)); } /*--------------------------------------------------------------*/ /* Can we slot a word into the board at this row,col and length(*)? If so, then the very last thing we do before returning is to actually generate the words. This is not really good design - we should pass the wildcard which this generates up a level and have that level do the search. This procedure generates a wild-card string using fixed letters, "[...]" letter sets, and wildcards ("?") which when expanded returns valid words which can be played here. *: Note 'length' here doesn't mean word length. It means number of tiles to be placed. The word may be longer because of tiles already on the board that we are placing around. */ void slot_internal( GAMESTATE *game, int orig_r, int orig_c, int r, int c, int still_to_place, int placed, char *pattern, int touches_center, int touches_horiz, int touches_vert, int orientation) { int valid; int pp = strlen(pattern); int opp = strlen(pattern); char *s; /* No more loop. Just place one, and recurse. If I had done this with the scrabble program to begin with, I wouldn't have had the stupid bug to do with playing blanks as the same letter as another letter you hold in the rack... */ /* Ran off end of board with tiles still to place? */ if ((c > WIDTH) && (still_to_place > 0)) return; /* Covered with an earlier hook? */ if ((placed == 0) && (game->board[r][c-1] != 0) && (c != 1)) return; /* (special case for first col ... */ while (game->board[r][c] != 0) { /* There is already a tile on the board here. Careful with blanks */ pattern[pp++] = game->apparent_letter[r][c++]; pattern[pp] = '\0'; } if ((c) > WIDTH) return; /* Oops, we went over the edge */ /* If center square was empty this must be the first move */ if ((game->board[WIDTH/2+1][HEIGHT/2+1] == 0) && (((WIDTH/2)+1 == r) && (c == (WIDTH/2)+1)) ) { touches_center = (touches_center || TRUE); } if (game->board[r][c-1] != 0 || game->board[r][c+1] != 0) touches_horiz = (touches_horiz || TRUE); if (game->board[r-1][c] != 0 || game->board[r+1][c] != 0) touches_vert = (touches_vert || TRUE); if (game->crosschecks[r][c] == NULL) { /* no tiles above or below in this col */ if (strchr(game->tiles, '?') != NULL) { /* We hold a blank so it could be anything */ sprintf(pattern+pp, "?"); } else { /* restrict search space when possible, ie only those letters for which we hold tiles */ sprintf(pattern+pp, "[%s]", game->tiles); } } else if (*game->crosschecks[r][c] == '!') { /* If no valid crosscheck at all, then reject this slot - square is blocked */ pattern[opp] = '\0'; /* undo recursion */ return; } else if ((strcmp(game->crosschecks[r][c], "?") == 0) && (strchr(game->tiles, '?') == NULL)) { /* we don't hold a blank, so do a reduction-in-strength of a wild-card "?" to [abcdefg] (i.e. only the tiles we hold) */ sprintf(pattern+pp, "[%s]", game->tiles); } else if (strcmp(game->crosschecks[r][c], "?") == 0) { /* we do hold a blank, so allow anything */ sprintf(pattern+pp, "%s", game->crosschecks[r][c]); } else { /* This letter is constrained by specific cross-checks */ sprintf(pattern+pp, "[%s]", game->crosschecks[r][c]); } pp = strlen(pattern); pattern[pp] = '\0'; placed += 1; still_to_place -= 1; if (still_to_place != 0) { /* Should this test be moved up before some of the code above? */ slot_internal(game, orig_r, orig_c, r, c+1, still_to_place, placed, pattern, touches_center, touches_horiz, touches_vert, orientation); pattern[opp] = '\0'; /* undo recursion */ return; } /* after placing all tiles, are there still some letters abutting to the right? If so, add them to the string to be matched */ {int p = c+1; while (game->board[r][p] != 0) { /*hscore += ...[r][p];*/ touches_horiz = TRUE; pattern[pp++] = game->apparent_letter[r][p++]; /* Careful with blanks */ }} pattern[pp] = '\0'; valid = (touches_horiz || (touches_vert && (placed > 1)) || (touches_center && (placed > 1))); if (valid) scrabble_match(game, dawg, pattern, game->tiles, (orientation == HORIZONTAL ? orig_c : orig_r)-1+'A', (orientation == HORIZONTAL ? orig_r : orig_c), orientation); /* the x/y param could be done more intelligently */ } int slot(GAMESTATE *game, int r, int c, int l, int orientation) { char pattern[(WIDTH*(256+2))+1]; pattern[0] = '\0'; slot_internal(game, r, c, r, c, l, 0, pattern, FALSE, FALSE, FALSE, orientation); return(0); /* Needs to be true if any were placed */ } int main(int argc, char **argv) { int i; int row = 0, col = 0; int orientation = 0; int length = 0; char *dict = NULL; GAMESTATE *game; if (debug) report = TRUE; if (html) report = TRUE; game = calloc(1, sizeof(struct GAMESTATE)); if (argc != 3) { if (argc <= 2) { dict = "SOWPODS"; } else { fprintf(stderr, "syntax: %s board.dat dict?\n", argv[0]); exit(1); } } else dict = argv[2]; /* Default word list is TWL98 */ /* default param when debugging makes running gbd easier */ if (argv[1] == NULL) game->boardfile = "test5.dat"; else game->boardfile = argv[1]; if (!dawg_init(dict, &dawg, &nedges)) { fprintf(stderr, "%s: cannot open wordlist '%s'\n", argv[0], dict); exit(2); } /* Clear the arrays */ assert(WIDTH == HEIGHT); for (row = 0; row < (HEIGHT+2); row++) { for (col = 0; col < (WIDTH+2); col++) { game->board[row][col] = 0; game->apparent_letter[row][col] = 0; game->crosschecks[row][col] = NULL; game->vertical_word[row][col] = NULL; game->vindex[row][col] = 0; } } for (i = 0; i < 256; i++) { tot[i] = 2; /* Blank ;-) */ score[i] = 0; totprob[i] = 0.0; watkins_rackvalue[i] = 25.0; } for (i = 0; i < 26; i++) { tot['a'+i] = itot[i]; score['a'+i] = iscore[i]; watkins_rackvalue['a'+i] = iwatkins_rackvalue[i]; } /* hand-coded random board. This could be replaced by a more complex format which includes tile distributions, values, language etc */ strcpy(game->fullrack, FULLRACK); /* If no tiles given in file, do megamove analysis */ read_board(game, game->boardfile); if (game->tiles[0] == '\0') { megaanalysis = TRUE; game->tiles_held = strlen(game->fullrack); strcpy(game->tiles, game->fullrack); } fprintf(stdout, "\nUnseen tiles are: %s\n\n", game->fullrack); for (orientation = HORIZONTAL; orientation <= VERTICAL; orientation++) { /* We assume throughout that we are examining only horizontal plays, and just flip the board to handle vertical plays. We flip it back at the end, in case we want to update it with a play and write it back to file. (but we don't do that yet) */ for (row = 1; row <= HEIGHT; row++) { /* calculate game->crosschecks for this row before we look at placements for this row. We could actually do this for the whole board first, but it isn't necessary */ for (col = 1; col <= WIDTH; col++) { char *s; /* Clean up from previous tries */ /* This is about our only use of the heap and I bet if we were to make these fixed size statics it would speed up the code... */ if (game->crosschecks[row][col] != NULL) free(game->crosschecks[row][col]); if (game->vertical_word[row][col] != NULL) free(game->vertical_word[row][col]); game->vertical_word[row][col] = NULL; /* first of all get wildcard pattern, eg "w?rd" : */ game->crosschecks[row][col] = create_crosscheck_wildcard(game, row, col); /* Then convert it to a set of valid letters */ if (game->crosschecks[row][col] == NULL) { /* game->crosschecks don't make sense here */ game->crosschecks[row][col]= strdup("?"); } else { s = convert_wildcard_to_permitted_set(dawg, game->crosschecks[row][col]); free(game->crosschecks[row][col]); if (s == NULL) { /* No letter can currently be placed in this square legally */ game->crosschecks[row][col] = strdup("!"); /* "!" is easier to debug than empty string */ } else { game->crosschecks[row][col] = strdup(s); } } } /* Now do placements: try every square on this row as position for first tile to be placed down. Rest follow automatically */ for (col = 1; col <= WIDTH; col++) { /* if (debug) fprintf(stderr, "Testing row %d col %d\n", row, col); */ for (length = 1; length <= MIN(game->tiles_held, 7); length++) { /* Can I legally place this many tiles onto the board here? */ if (slot(game, row, col, length, orientation)) { /* would be nice to pass the wildcard string back to here and then expand it/search for plays, and make the plays separately. The hack above was for convenience only. */ /* record row,col,length and wildcard for move generator (maybe) */ } } } } /* Try every row on the board */ /* flip the board around the x=y diagonal */ for (row = 1; row <= HEIGHT; row++) { for (col = 1; col <= WIDTH; col++) { int i; if (row > col) { /* avoid doing twice (no-op) */ /* swap board and game->apparent_letter at row,col */ i = game->board[row][col]; game->board[row][col] = game->board[col][row]; game->board[col][row] = i; i = game->apparent_letter[row][col]; game->apparent_letter[row][col] = game->apparent_letter[col][row]; game->apparent_letter[col][row] = i; } } } } /* end of horiz/vert placement loop */ /* If want to make best play on board and save it, do it here */ report = TRUE; /* if orientation == VERTICAL it expects the board to have already been swapped... quick hack... */ if (game->playorientation == VERTICAL) { /* flip the board around the x=y diagonal */ for (row = 1; row <= HEIGHT; row++) { for (col = 1; col <= WIDTH; col++) { int i; if (row > col) { /* avoid doing twice (no-op) */ /* swap board and game->apparent_letter at row,col */ i = game->board[row][col]; game->board[row][col] = game->board[col][row]; game->board[col][row] = i; i = game->apparent_letter[row][col]; game->apparent_letter[row][col] = game->apparent_letter[col][row]; game->apparent_letter[col][row] = i; } } } } fprintf(stdout, "\n"); if (game->bestscore != 0) playword(game, game->playtiles, game->playword, game->playtilesleft, game->playtemplate, game->playplaced, game->playLetter, game->playnumber, game->playorientation); fprintf(stdout, "%d moves were considered for this play\n", game->movesconsidered); exit(0); }