Example #1
0
long total_score(char **names)
	{
	long score = 0L;
	for (int i = 0; i < NAME_COUNT; i++)
		score += word_score(i + 1, names[i]);
	return score;
	}
Example #2
0
/*----------------------------------------------------------------------
 *	return the score obtained from a given word, including
 *	subsidiary words in the cross-ways direction
 *----------------------------------------------------------------------*/
int play_score()
{
  int i;
  char *line = board[play.row]+play.col;
  int score = word_score();
  for (i=0; i<len; i++) {
    if (line[i] == EMPTY) {	/* if new tile being played */
      score += cross_score(play.row, play.col+i, letter_value(play.bword[i]));
    }
  }
  return score + play_bonus;		/* +50 if use all tiles */
}
int main(void) {
	srand(time(NULL));

	printf("The available commands are:\n"
	       "I N - Inject an NxN board. This command will scan NxN letters to form a board\n"
	       "G N - Generates a new NxN board where a letter doesn't appear more than N times\n"
	       "P - Print the current board\n"
	       "W N word - Insert a word into the dictionary with score N\n"
	       "A N word - Insert a word and all prefixes in the dictionary. Non-proper prefixes get a score of 0\n"
	       "R word - Delete a word\n"
	       "S word - Search for a word and return its score (-1 if no such word exists)\n"
	       "D - Dump the dictionary with the corresponding scores\n"
	       "B - Find the best word (word with the highest score)\n"
	       "Q - Quit\n"
	       "> ");

	char **board = NULL;
	size_t board_dim = 0;
	trie = new_trie();

	char op;
	while (scanf(" %c", &op) == 1) {
		if (op == 'I') {
			destroy_board(board, board_dim);
			scanf("%zu", &board_dim);
			board = inject_board(board_dim);
		} else if (op == 'P') {
			if (board == NULL) {
				printf("No board at the moment\n");
			} else {
				print_board(board, board_dim);
			}
		} else if (op == 'G') {
			destroy_board(board, board_dim);
			scanf("%zu", &board_dim);
			board = generate_board(board_dim);
		} else if (op == 'W') {
			size_t word_score;
			scanf("%zu%s", &word_score, word_buff);
			insert_word(trie, word_buff, word_score);
		} else if (op == 'A') {
			size_t word_score;
			scanf("%zu%s", &word_score, word_buff);

			size_t i;
			for (i = 1; word_buff[i] != '\0'; i++) {
				char c = word_buff[i];
				word_buff[i] = '\0';
				insert_word(trie, word_buff, 0);
				word_buff[i] = c;
			}
			insert_word(trie, word_buff, word_score);

		} else if (op == 'R') {
			scanf("%s", word_buff);
			delete_word(trie, word_buff);
		} else if (op == 'S') {
			scanf("%s", word_buff);
			int s = word_score(trie, word_buff);
			if (s == -1) {
				printf("No such word: %s\n", word_buff);
			} else {
				printf("score(%s) = %d\n", word_buff, s);
			}
		} else if (op == 'D') {
			print_known_words(trie);
		} else if (op == 'B') {
			char *w = find_best_word(board, board_dim);
			if (w == NULL) {
				printf("No words found\n");
			} else {
				printf("Best word: %s\n", w);
			}
			free(w);
		} else if (op == 'Q') {
			break;
		} else {
			fprintf(stderr, "Unrecognized operation: %c\n", op);
		}

		printf("> ");
	}

	destroy_trie(trie);
	destroy_board(board, board_dim);

	return 0;
}
int score(const char *word) {
	return word_score(trie, word);
}