Beispiel #1
0
//hall of fame mode
void hof(){
    char ch;
	int i, x;
	char *result;
	int n=0, m = 0;
	FILE *fptr; //create file pointer
	fptr = fopen("HallofFame.txt", "r");  //open HallofFame.txt
	while  ( ( ch = fgetc( fptr ) ) != EOF ){ //determine the number of lines in the file
		if(ch==10)n++;                        //we need that to know how big to make the array later
	}                                         //returns n which is the number of lines
	rewind(fptr);
	struct data entry[n];          //create a data array to hold the records from the HallofFame.txt file
	for(i=0; i<n; i++){            //loop n to read all records from the file
		char info[50];
        fscanf(fptr, "%s", &info); //read a line from the file
		result = strtok(info, ",");
		strcpy(entry[i].name,result); //store name
        result = strtok(NULL, ",");
		entry[i].diff=atoi(result);   //store level
		result = strtok(NULL, ",");
		entry[i].moves=atoi(result); //store steps
		result = strtok(NULL, ",");
		entry[i].time=atoi(result);  //store time from the file
	}
	fclose(fptr);         //close the file after reading all records
	ch = 'p';
	while(ch!=27){
        if(ch==49)sortName(entry, n, 1); //call the required sort method based on user's choice
        if(ch==50)sortDiff(entry, n, 1);
        if(ch==51)sortMoves(entry, n, 1);
        if(ch==52)sortTime(entry, n, 1);
        if(ch==53)sortName(entry, n, 0);
        if(ch==54)sortDiff(entry, n, 0);
        if(ch==55)sortMoves(entry, n, 0);
        if(ch==56)sortTime(entry, n, 0);
        system("cls");
        printf("\nPress 1 to sort by Name (Ascending)\nPress 2 to sort by Level (Ascending)\nPress 3 to sort by Steps (Ascending)\nPress 4 to sort by Time (Ascending)");
	    printf("\nPress 5 to sort by Name (Descending)\nPress 6 to sort by Level (Descending)\nPress 7 to sort by Steps (Descending)\nPress 8 to sort by Time (Descending)");
		printf("\nPress ESC to exit\n");
        ch = getch();
	}
	return;
}
Beispiel #2
0
bool Solver::solve(Grid& solution, int& steps, Grid& grid) {
  int index = selectIndex(grid);

  if (index == -1) {
    solution = grid;
    return true;
  }

  vector<int> moves(begin(grid[index]), end(grid[index]));
  sortMoves(grid, moves, index);

  for (auto& value : moves) {
    steps++;

    Grid nextGrid(grid);
    if (nextGrid.assign(index, value) && solve(solution, steps, nextGrid))
      return true;
  }

  return false;
}