Example #1
0
void main()
{
   unsigned int init_length, x, y, seed;
   unsigned long generation = 0;
   char gen_text[80];
   long bios_time, start_bios_time;

   cellmap current_map(cellmap_height, cellmap_width);
   cellmap next_map(cellmap_height, cellmap_width);

   // Get the seed; seed randomly if 0 entered
   cout << "Seed (0 for random seed): ";
   cin >> seed;
   if (seed == 0) seed = (unsigned) time(NULL);

   // Randomly initialize the initial cell map
   cout << "Initializing...";
   srand(seed);
   init_length = (cellmap_height * cellmap_width) / 2;
   do {
      x = random(cellmap_width);
      y = random(cellmap_height);
      next_map.set_cell(x, y);
   } while (--init_length);
   current_map.copy_cells(next_map); // put init map in current_map

   enter_display_mode();

   // Keep recalculating and redisplaying generations until a key
   // is pressed
   show_text(0, MSG_LINE, "Generation: ");
   start_bios_time = _bios_timeofday(_TIME_GETCLOCK, &bios_time);
   do {
      generation++;
      sprintf(gen_text, "%10lu", generation);
      show_text(1, GENERATION_LINE, gen_text);
      // Recalculate and draw the next generation
      current_map.next_generation(next_map);
      // Make current_map current again
      current_map.copy_cells(next_map);
#if LIMIT_18_HZ
      // Limit to a maximum of 18.2 frames per second, for visibility
      do {
         _bios_timeofday(_TIME_GETCLOCK, &bios_time);
      } while (start_bios_time == bios_time);
      start_bios_time = bios_time;
#endif
   } while (!kbhit());
   getch();    // clear keypress
   exit_display_mode();
   cout << "Total generations: " << generation << "\nSeed: " <<
         seed << "\n";
}
Example #2
0
template<typename T, typename O> void Fsm<T, O>::reduce()
{
	// remove transitions without output and to states without outgoing transitions
	/*for (int changed = 1; 0 < changed; --changed)
		for (size_t i = 0; i < transition_table.size(); ++i)
		{
			std::map<Char, Transition> new_transitions;
			for (typename std::map<Char, Transition>::const_iterator itt = transition_table[i].begin(); itt != transition_table[i].end(); ++itt)
				if (!this->transition_table[itt->second.nextState].empty() || !itt->second.output.empty())
					new_transitions.insert(*itt);
				else changed = 2;
			transition_table[i].swap(new_transitions);
		}*/

	//std::map<State,State> curr_map;
	std::vector<State> curr_map(this->transition_table.size());
	std::map<std::vector<bool>,std::set<State> > final_output_map;
	//typename std::map<State,std::map<T,State> >::const_iterator itt;
	//for(itt = transition_table.begin(); itt != transition_table.end(); itt = transition_table.upper_bound(itt->first))
	//	if(!this->final(itt->first))
	//		curr_map[itt->first] = 0;

	for(size_t i = 0; i < transition_table.size(); ++i)
		curr_map[static_cast<State>(i)] = 0;

	State state_counter = 1, last_state_counter = 0;

	// p = part state; t = terminal; n = new part state
	std::map<std::pair<State,std::map<Char,Transition> >,State> ptpn;
	typename std::map<std::pair<State,std::map<Char,Transition> >,State>::const_iterator iptpn;
	//std::vector<std::map<Char,Transition> > ptp;
	//std::vector<State> ptp_newpart;
	std::vector<State> next_map(this->transition_table.size());

	//const State invalidState = static_cast<State>(transition_table.size());

	while( state_counter != last_state_counter )
	{
		ptpn.clear();
		//ptp.clear();
		//ptp.resize(transition_table.size());

		//ptp.assign(transition_table.size(), std::map<Char,Transition>());
		//ptp_newpart.assign(transition_table.size(), invalidState);

		last_state_counter = state_counter;
		state_counter = 0;

		//typename std::map<State,State>::const_iterator isp;

		// build up part-transition table into ptpn
		//for(isp = curr_map.begin(); isp != curr_map.end(); ++isp)
		for(State state = 0; state < static_cast<State>(curr_map.size()); ++state)
		{
			std::pair<std::pair<State,Transitions>,State> ptp;
			ptp.first.first = curr_map[state];
			ptp.second = state_counter;
			//State part = curr_map[state];
			// building up transition table for this part
			//std::map<Char,Transition> tp;
			typename std::map<Char,Transition>::const_iterator its;
			for(its = transition_table[state].begin(); its != transition_table[state].end(); ++its)
				ptp.first.second[its->first] = Transition(curr_map[its->second.nextState], its->second.output);

			// looking for part with same outgoing transitions
			std::pair<typename std::map<std::pair<State,Transitions>,State>::iterator, bool> insResult =
				ptpn.insert(ptp);
			//if( (iptpn = ptpn.find(ptp)) != ptpn.end() )
			if (!insResult.second)
				// create state-new-part mapping in next map for this state
				next_map[state] = insResult.first->second;
				//next_map[state] = iptpn->second;
			else
				// insert new part-transition pair and assign new part
				next_map[state] = state_counter++;
				//ptpn[ptp] = next_map[state] = state_counter++;

		}

		// swap state->part mapping with new partitions
		curr_map.swap(next_map);
	}

	TransitionTable new_transition_table(state_counter);
	for(iptpn = ptpn.begin(); iptpn != ptpn.end(); ++iptpn)
		new_transition_table[iptpn->first.first] = iptpn->first.second;
		//transition_table.insert( iptpn->first );

	//ptp.resize(state_counter);

	transition_table.swap(new_transition_table);
	//transition_table.swap(ptp);
}
Example #3
0
int
write_map(struct map *parm, int (*f) (void *, int), char *arg)
{
    struct bitmap *map;
    int page;
    int bitno, lastno, thisno, prevno;
    int i, j;

    bitno = first_map(parm);
    bitno &= ~((1 << LSHIFT) - 1);

    lastno = last_map(parm);
    lastno -= bitno;
    lastno += ((1 << LSHIFT));
    lastno &= ~((1 << LSHIFT) - 1);
/* count, then startbitno, then bits. */
    if ((*f) (arg, lastno) == -1)
	return -1;
    /* word: number of bits */
    if ((*f) (arg, bitno) == -1)
	return -1;
    lastno += bitno;
    prevno = bitno;
    for (bitno = first_map(parm); bitno >= 0; bitno = next_map(parm, bitno)) {
	page = NUMBERTOPAGE(bitno);
	for (map = MAP(parm); map; map = map->m_next)
	    if (map->m_page == page)
		break;
	if (!map) {
#ifdef PRINT_MAP_ERROR
	    printf("write_map: botch #1: can't find page %d\n", page);
#endif
	    continue;
	}
	for (i = 0; i < MDATA; ++i) {
	    if (!map->m_data[i])
		continue;
	    thisno = TONUMBER(page, i, 0);
	    for (j = thisno - prevno; j > 0; j -= (1 << LSHIFT))
		if ((*f) (arg, 0) == -1)
		    return -1;
	    prevno = thisno;
	    for (;;) {
		if ((*f) (arg, map->m_data[i]) == -1)
		    return -1;
		prevno += (1 << LSHIFT);
		++i;
		if (i >= MDATA || !map->m_data[i])
		    break;
	    }
	    --i;
	}
	bitno = TONUMBER(page, i, 0) - 1;
    }
#ifdef PRINT_MAP_ERROR
    if (prevno != lastno)
	printf("write_map: botch #2: bitno=%d prevno=%d lastno=%d\n", bitno,
	       prevno, lastno);
#endif
    return 0;
}
Example #4
0
int
first_map(struct map *parm)
{
    return next_map(parm, -9999);
}
Example #5
0
int next_map(int turn, int color,int map[][MAP_SIZE_Y]){
	int i,j;
	int d_map[MAP_SIZE_X][MAP_SIZE_Y];

	map_case++;
	printf("case_n = %d\n",map_case);

	//自分の色から、相手の色を割り出す
	int next_color=0;
	if(color==1){
		next_color=2;
	}else{
		next_color=1;
	}

	cpy_map(d_map,map);

	//おけるところがあるかどうか
	double flg=0;

	//マップ全体のスペースの数
	int space=0;
	//勝率
	double win_par=0;

	//左上から、おけるところを検索
	for(i=0; i<MAP_SIZE_Y; i++){
		for (j = 0; j < MAP_SIZE_X; j++) {
			//おけるところがあったら、置く
			if(change(j,i,color,d_map)!=0){
				flg++;
				printf("AI...(%d,%d)\n",j,i);
				printf("turn=%d\n",turn);
				draw_map(d_map);
				win_par += next_map(turn+1,next_color,d_map);
				cpy_map(d_map,map);
			}
			if(map[j][i] == 0)	space++;
		}
	}

	//もし、すべてのますが埋まっていたら
	if(space==0){
		printf("End_game");
		printf("win : %d\n",winner(map));
		return winner(map);
	}

	//パスが発生した場合
	if(flg==0){
		puts("-----------pass--------------");
		//左上から、おけるところを検索
		for(i=0; i<MAP_SIZE_Y; i++){
			for (j = 0; j < MAP_SIZE_X; j++) {
				//おけるところがあったら、置く
				if(change(j,i,next_color,d_map)!=0){
					flg++;
					printf("AI...(%d,%d)\n",j,i);
					printf("turn=%d\n",turn);
					draw_map(d_map);
					win_par += next_map(turn,color,d_map);
					cpy_map(d_map,map);
				}
				if(map[j][i] == 0)	space++;
			}
		}
	}

	//どちらの色もおける場所がない
	if(flg==0){
		printf("space_bat_no_putaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
		printf("win : %d\n",winner(map));
		return winner(map);
	}

	return win_par/flg;
}