コード例 #1
0
ファイル: greedy.cpp プロジェクト: hdl/CS561-reversi
string Greedy::run_greedy(Board_info &current_board, char your_tile){
	vector<Board_info> new_board_vector;
	vector<Board_info>::iterator it;
	string result="";
	new_board_vector = get_new_boards_vector(your_tile, current_board, your_tile);
	if(new_board_vector.size() != 0){
		sort(new_board_vector.begin(), new_board_vector.end(), compare_max_min);
		result = Board_info::get_board_cells(new_board_vector[0].board);
	}else{
		result = "no children on greedy.cpp:118\n";
	}
	free_boards(new_board_vector);
	return result;
}
コード例 #2
0
ファイル: test.c プロジェクト: jimmahoney/codesnips
int main() {
  int good_queens[] = {4, 1, 3, 5, 7, 2, 0, 6};
  int bad_queens[] = {4, 3, 1, 5, 7, 2, 0, 6};
  board good_board = new_board_from_queens(8, good_queens);
  board bad_board = new_board_from_queens(8, bad_queens);
  boards solutions, diagonals;
  int n;

  printf("==== print_board(good_board) ====\n");
  print_board(good_board);
  printf("==== print_board_as_perm(good_board) ====\n");
  print_board_as_perm(good_board);
  printf("==== valid_diagonals ====\n");
  printf(" good board : %i \n", valid_diagonals(8, good_queens));
  printf(" bad board  : %i \n", valid_diagonals(8, bad_queens));
  free_board(good_board);
  free_board(bad_board);

  printf("==== small n behavior ====\n");
  for (n=0; n<4; n++){
    printf(" n=%i : ", n); 
    solutions = queens_search(n);
    print_boards_as_perms(solutions);
    free_boards(solutions);
  }

  printf("==== queens_search n=4 ====\n");
  solutions = queens_search(4);
  print_boards(solutions);
  free_boards(solutions);

  printf("==== search n=8 ====\n");
  solutions = queens_search(8);
  printf(" number of solutions = %i \n", solutions->count);
  free_boards(solutions);
  diagonals = queens_search_diagonals(8);
  printf(" number of queens_search_diagonals = %i \n", diagonals->count);
  free_boards(diagonals);

  diagonals = in_both_diagonals(solutions);  // same, another way
  printf(" number of in_both_diagonals = %i \n", diagonals->count);
  free_boards(diagonals);

  printf("==== queens_search_diagonals for various n ====\n", n);
  for (n=4; n<15; n++){
    reset_timer();
    diagonals = queens_search_diagonals(n);
    printf(" n = %2i ; count = %7i ; time = ", n, diagonals->count);
    print_elapsed(); printf("\n"); fflush(stdout);
    free_boards(diagonals);
  }

  printf("=== memory check ===\n");
  printf(" malloc() + calloc() - free() = %i \n\n", get_allocation_count());

  return 0;
}
コード例 #3
0
ファイル: alphabeta.cpp プロジェクト: hdl/CS561-reversi
Board_info Alphabeta::run_alphabeta(Board_info &current_board, int depth, int a, int b, char tile)
{
	Board_info best_child, temp_child;
	vector<Board_info> children;
	vector<Board_info>::iterator child;
	int break_flag=0;

	if (depth == 0){
		current_board.visited = 1;
		if(tile == your_tile){
			current_board.a=a;
			current_board.b=current_board.weight;
		}
		else{
			current_board.a=current_board.weight;
			current_board.b=b;
		}
		(DEBUG?cout:log) << xy2(current_board.x, current_board.y)<<","<<this->depth - depth<<","<< current_board.weight<<",";
		(DEBUG?cout:log) << ab2(a, b)<<endl;
	}else{
		if(current_board.visited==0){
			current_board.visited = 1;
			(DEBUG?cout:log) << xy2(current_board.x, current_board.y)<<","<<this->depth - depth<<",";
			(DEBUG?cout:log) << (tile == your_tile ?"-":"")<<"Infinity,";
			(DEBUG?cout:log) <<ab2(a,b) <<endl;
		}
	}

	if(depth == 0){
		return current_board;
	}

	children = get_new_boards_vector(your_tile, current_board, tile);
	// it's pocoutible get 0 child
	if(children.size() == 0){
		cout<<"no children"<<endl;
		current_board.print();
		exit(0);
	}else{
		sort(children.begin(), children.end(), compare_order);
	}

	best_child.a = a;
	best_child.b = b;
	if(tile == your_tile){
		for(child=children.begin(); child != children.end(); ++child){
			temp_child = run_alphabeta(*child, depth -1, best_child.a, best_child.b, other_tile);
			best_child = max_a(best_child, temp_child);
			(DEBUG?cout:log) << xy2(current_board.x, current_board.y)<<","<<this->depth - depth<<","<< best_child.weight<<",";
			if(best_child.b<=best_child.a){
				cout<<"prune form:"<<xy2(best_child.x, best_child.y)<<endl;
				break_flag = 1;
				(DEBUG?cout:log) <<ab2(current_board.a,best_child.b)<<endl;
				break;
			}else{
				(DEBUG?cout:log) <<ab2(best_child.a,best_child.b)<<endl;
			}
		}		
	}else{
		for(child=children.begin(); child != children.end(); ++child){
			temp_child = run_alphabeta(*child, depth -1, best_child.a, best_child.b, your_tile);	
			best_child = min_b(best_child, temp_child);
			(DEBUG?cout:log) << xy2(current_board.x, current_board.y)<<","<<this->depth - depth<<","<< best_child.weight<<",";
			if(best_child.b<=best_child.a){
				cout<<"prune form:"<<xy2(best_child.x, best_child.y)<<endl;
				break_flag = 1;
				(DEBUG?cout:log) <<ab2(best_child.a,current_board.b)<<endl;
				break;
			}else{
				(DEBUG?cout:log) <<ab2(best_child.a,best_child.b)<<endl;
			}
		}
	}
	free_boards(children);
	//weight is the only var to save/to callee 
	current_board.weight = best_child.weight;
	current_board.best_child_x = best_child.x;	
	current_board.best_child_y = best_child.y;
	if(break_flag==0){
		if(tile == your_tile)
			current_board.b = best_child.a;
		else
			current_board.a = best_child.b;
	}
	cout << xy2(current_board.x, current_board.y) <<":"<< xy2(current_board.best_child_x, current_board.best_child_y)<<endl;	
	return current_board;
}
コード例 #4
0
ファイル: comm.cpp プロジェクト: locke2002/ShruggingMan
/*
 * Clean all memory on exit to help find leaks
 * Yeah I know, one big ugly function -Druid
 * Added to AFKMud by Samson on 5-8-03.
 */
void cleanup_memory( void )
{
   int hash;

#ifdef IMC
   fprintf( stdout, "%s", "IMC2 Data.\n" );
   free_imcdata( true );
   imc_delete_info(  );
#endif

   fprintf( stdout, "%s", "Quote List.\n" );
   free_quotes(  );

   fprintf( stdout, "%s", "Random Environment Data.\n" );
   free_envs(  );

   fprintf( stdout, "%s", "Auction Sale Data.\n" );
   free_sales(  );

   fprintf( stdout, "%s", "Project Data.\n" );
   free_projects(  );

   fprintf( stdout, "%s", "Ban Data.\n" );
   free_bans(  );

   fprintf( stdout, "%s", "Auth List.\n" );
   free_all_auths(  );

   fprintf( stdout, "%s", "Morph Data.\n" );
   free_morphs(  );

   fprintf( stdout, "%s", "Rune Data.\n" );
   free_runedata(  );

   fprintf( stdout, "%s", "Connection History Data.\n" );
   free_connhistory( 0 );

   fprintf( stdout, "%s", "Slay Table.\n" );
   free_slays(  );

   fprintf( stdout, "%s", "Holidays.\n" );
   free_holidays(  );

   fprintf( stdout, "%s", "Specfun List.\n" );
   free_specfuns(  );

   fprintf( stdout, "%s", "Wizinfo Data.\n" );
   clear_wizinfo(  );

   fprintf( stdout, "%s", "Skyship landings.\n" );
   free_landings(  );

   fprintf( stdout, "%s", "Ships.\n" );
   free_ships(  );

   fprintf( stdout, "%s", "Overland Landmarks.\n" );
   free_landmarks(  );

   fprintf( stdout, "%s", "Overland Exits.\n" );
   free_mapexits(  );

   fprintf( stdout, "%s", "Mixtures and Liquids.\n" );
   free_liquiddata(  );

   fprintf( stdout, "%s", "DNS Cache data.\n" );
   free_dns_list(  );

   fprintf( stdout, "%s", "Local Channels.\n" );
   free_mudchannels(  );

   // Helps
   fprintf( stdout, "%s", "Helps.\n" );
   free_helps(  );

   // Commands 
   fprintf( stdout, "%s", "Commands.\n" );
   free_commands(  );

#ifdef MULTIPORT
   // Shell Commands 
   fprintf( stdout, "%s", "Shell Commands.\n" );
   free_shellcommands(  );
#endif

   // Socials 
   fprintf( stdout, "%s", "Socials.\n" );
   free_socials(  );

   // Languages 
   fprintf( stdout, "%s", "Languages.\n" );
   free_tongues(  );

   // Boards 
   fprintf( stdout, "%s", "Boards.\n" );
   free_boards(  );

   // Events 
   fprintf( stdout, "%s", "Events.\n" );
   free_all_events(  );

   // Find and eliminate all running chess games
   fprintf( stdout, "%s", "Ending chess games.\n" );
   free_all_chess_games(  );

   // Whack supermob 
   fprintf( stdout, "%s", "Whacking supermob.\n" );
   if( supermob )
   {
      supermob->from_room(  );
      charlist.remove( supermob );
      deleteptr( supermob );
   }

   // Free Characters 
   fprintf( stdout, "%s", "Characters.\n" );
   extract_all_chars(  );

   // Free Objects 
   fprintf( stdout, "%s", "Objects.\n" );
   extract_all_objs(  );

   // Descriptors 
   fprintf( stdout, "%s", "Descriptors.\n" );
   free_all_descs(  );

   // Deities 
   fprintf( stdout, "%s", "Deities.\n" );
   free_deities(  );

   // Clans 
   fprintf( stdout, "%s", "Clans.\n" );
   free_clans(  );

   // Races 
   fprintf( stdout, "%s", "Races.\n" );
   free_all_races(  );

   // Classes 
   fprintf( stdout, "%s", "Classes.\n" );
   free_all_classes(  );

   // Teleport lists 
   fprintf( stdout, "%s", "Teleport Data.\n" );
   free_teleports(  );

   // Areas - this includes killing off the hash tables and such 
   fprintf( stdout, "%s", "Area Data Tables.\n" );
   close_all_areas(  );

   // Get rid of auction pointer MUST BE AFTER OBJECTS DESTROYED 
   fprintf( stdout, "%s", "Auction.\n" );
   deleteptr( auction );

   // Title table 
   fprintf( stdout, "%s", "Title table.\n" );
   free_all_titles(  );

   // Skills 
   fprintf( stdout, "%s", "Skills and Herbs.\n" );
   free_skills(  );

   // Prog Act lists 
   fprintf( stdout, "%s", "Mudprog act lists.\n" );
   free_prog_actlists(  );

   // Questbit data
   fprintf( stdout, "%s", "Abit/Qbit Data.\n" );
   free_questbits(  );

   free_mssp_info();

   fprintf( stdout, "%s", "Checking string hash for leftovers.\n" );
   {
      for( hash = 0; hash < 1024; ++hash )
         hash_dump( hash );
   }

#if !defined(__CYGWIN__) && defined(SQL)
   fprintf( stdout, "%s", "Closing database connection.\n" );
   close_db(  );
#endif

   // Last but not least, close the libdl and dispose of sysdata - Samson 
   fprintf( stdout, "%s", "System data.\n" );
   dlclose( sysdata->dlHandle );
   deleteptr( sysdata );
   fprintf( stdout, "%s", "Memory cleanup complete, exiting.\n" );
}