Guess DistanceSet::get_best_guess(const Point &p1, const Point &p2) const { #if DONT_INCLUDE_ZERO_ANSWERS Guess g; int size = points.size(); for (int idx1 = 0; idx1 < size; idx1++) { for (int idx2 = 0; idx2 < size; idx2++) { if (idx1 == idx2) { continue; } Guess another = make_guess(idx1, idx2, p1, p2); if (another.is_better_than(g)) { g = another; } } } return g; #else int idx1 = get_closest_point(p1); int idx2 = get_closest_point(p2); return make_guess(idx1, idx2, p1, p2); #endif }
/** * analyse: * @analyser: An analyser initialized for some language. * @buffer: Buffer to be analysed. * @size: Size of @buffer. * * Analyses @buffer and finds its encoding. * * Returns: Encoding of @buffer. **/ static EncaEncoding analyse(EncaAnalyserState *analyser, unsigned char *buffer, size_t size) { analyser->result = ENCODING_UNKNOWN; /* Empty buffer? */ if (size == 0) { analyser->gerrno = ENCA_EEMPTY; return analyser->result; } assert(buffer != NULL); /* Initialize stuff. */ analyser->gerrno = 0; analyser->buffer = buffer; analyser->size = size; analyser->buffer2 = NULL; analyser->size2 = 0; analyser->gerrno = make_guess(analyser); if (analyser->gerrno) analyser->result = ENCODING_UNKNOWN; /* When buffer2 is not NULL, then it holds the original buffer, so we must * free the copy (i.e. buffer, not buffer2!). */ if (analyser->buffer2 != NULL) enca_free(analyser->buffer); return analyser->result; }
int main () { char buf[200]; int turn; int peg[4]; int guess[4]; do { printf ("Please enter a seed: "); if (NULL == fgets (buf, 200, stdin)) { printf ("\nProgram terminated.\n"); return 3; } } while (!set_seed (buf)); if (!start_game (&peg[0], &peg[1], &peg[2], &peg[3])) { printf ("\nGame start failed.\n"); return 3; } for (turn = 1; 10 >= turn; turn++) { printf ("Guess %d\n", turn); do { printf ("Enter your guess (#1 #2 #3 #4): "); if (NULL == fgets (buf, 200, stdin)) { printf ("\nProgram terminated.\n"); return 3; } } while (!make_guess (buf, &guess[0], &guess[1], &guess[2], &guess[3])); if (guess[0] == peg[0] && guess[1] == peg[1] && guess[2] == peg[2] && guess[3] == peg[3]) { printf ("You guessed correctly in %d guesses.\n", turn); return 0; } } puts ("You failed to guess correctly in 10 guesses.\n"); printf ("The solution was %d %d %d %d.\n", peg[0], peg[1], peg[2], peg[3]); return 0; }
/* Functions */ int main(int argc, char *argv[] ) { FILE *rules, *map; /* Pointers to rules/map file streams */ Grid board; /* Store info about the game board */ Ship ships[MAX_SHIPS]; /* Store info about each ship */ int **answer; /* Store the solution of the game */ unsigned int numShips = 0; /* Store the total numberof ships */ unsigned int xGuess, yGuess;/* Store the player's current guess */ /* Temp Variables */ int status = 0; /* exit status */ int i = 0, j = 0; /* loop indeces */ /* There must be at least three parameters */ if(argc < 3) { return params_missing(); } /* Save rules file into "rules", it's OK if "standard.rules" DNE * Rules file must be readable unless it is standard.rules */ if((rules = fopen(argv[1], "r")) == NULL) { if(!strcmp(argv[1], "standard.rules")) { fclose(rules); /* Looking for "standard.rules" but file DNE so create it * with the default contents */ rules = fopen(argv[1], "w"); fprintf(rules, "8 8\n5\n5\n4\n3\n2\n1\n\n"); fclose(rules); rules = fopen(argv[1], "r"); } else { return rules_missing(); } fclose(rules); } /* Save map file into "map" * Map file must be readable */ if((map = fopen(argv[2], "r")) == NULL ) { fclose(map); return maps_missing(); } /* Save and check rules file * Rules must conform to specification */ if((status = parse_rules(rules, ships, &board, &numShips))) { return status; } /* Initialize the answer array */ answer = (int**)malloc(board.width * sizeof(int*)); for(i = 0; i < board.width; i++) { answer[i] = (int*)malloc(board.height * sizeof(int)); for(j = 0; j < board.height; j++) { answer[i][j] = 1; } } /* Save and check map file * Map must conform to specification */ if((status = parse_map(map, ships, &board, &numShips))) { return status; } /* Populate the solution */ if((status = place_ships(ships, numShips, answer))) { return status; } /* Interaction Loop */ for(;;) { display_board(board.width, board.height, answer); display_prompt(); switch(get_prompt(&xGuess, &yGuess)) { case 0: make_guess(&xGuess, &yGuess, &board, answer); break; case 1: printf("Bad guess\n"); break; case -1: return bad_guess(); } } return 0; }