예제 #1
0
파일: bullcow.c 프로젝트: mazonka/w
vect process(const vect * n, int g, int b, int c)
{
	int i,k;
	vect r;
	vect rm;

	const char * s2 = mks[g].x;

	r.sz = rm.sz = 0;

	for( i=0; i<n->sz; i++ )
	{
		int mb, mc;
		getb(n->x[i],s2,&mb);
		if( mb!=b ){ rm.x[rm.sz++]=i; continue; }
		
		getbc(n->x[i],g,&mb,&mc);
		if( mb!=b || mc!=c ) rm.x[rm.sz++]=i;
	}

	{
		for( k=0,i=0; i<n->sz; i++ )
		{
			if( k<rm.sz && rm.x[k]==i ) k++;
			else r.x[r.sz++] = n->x[i];
		}
	}

	return r;
}
예제 #2
0
파일: bullcow.c 프로젝트: mazonka/w
int processq(const vect* n, int g, int b, int c)
{
	int i;
	int r=n->sz;

	const char * s2 = mks[g].x;
	for( i=0; i<n->sz; i++ )
	{
		int mb, mc;
		getb(n->x[i],s2,&mb);
		if( mb!=b ){ --r; continue; }
		
		getbc(n->x[i],g,&mb,&mc);
		if( mb!=b || mc!=c ) --r;
	}

	return r;
}
예제 #3
0
파일: bullcow.c 프로젝트: mazonka/w
int main(int ac, char *av[])
{
	char a;
	char *t[10]={ "first", "second", "third", "fourth", "fifth", 
		"sixth", "seventh", "eighth", "ninth", "tenth" };

	int ctr=0;
	int rnd;
	int g;


	if( ac>1 ) hint = atoi(av[1]);

	initmks();
	init(&gn);

	printf("Usage: bullcow [help_number]\n\n");

	while(1)
	{
		printf("Have you thought a number? (type 'y' and enter) ");
		a = '\0';
		scanf("%c",&a);
		if( a=='y' || a=='Y' ) break;
	}

	ctr=0;
	while(1)
	{
		int b=0,c=0;

		if( gn.sz > 700 )
			g = gn.x[ clock()%gn.sz ];
		else
			g = guess();

		printf("My ");
		if( ++ctr<11 ) printf("%s",t[ctr-1]); else printf("%dth",ctr);
		printf(" guess is %s",mks[g].x);

		if( hint )
		{
			getbc(g,hint,&b,&c);
			printf("  [ for %s bulls=%d cows=%d ]",mks[hint].x,b,c);
		}

		printf("\n");

		printf("How many guessed in place (bulls): ");
		scanf("%d",&b);
		printf("How many guessed not in place (cows): ");
		scanf("%d",&c);

		gn = process(&gn,g,b,c);

		if( gn.sz==0 )
		{
			printf("Impossible - you made an error\n");
			break;
		}

		if( gn.sz==1 )
		{
			printf("Your number is %s",mks[gn.x[0]].x);
			break;
		}

	}
}
예제 #4
0
WORD* scanner(){
	// TODO allocate room for a new WORD
	// allocate char room while need
	WORD* myword = (WORD*)malloc(sizeof(WORD));

	myword->typenum = 10;
	myword->word = "";
	p_token = 0;
	m_getch();
	getbc();

	if(letter()){
		while(letter() || digit()){
			concat();
			m_getch();
		}
		retract();
		myword->typenum = reserve();
		myword->word = token;
		return(myword);
	}else if(digit()){
		while(digit()){
			concat();
			m_getch();
		}
		retract();
		myword->typenum = 11;
		myword->word = token;
		return(myword);
	}else switch(ch){
		case '=' : m_getch();
				   if(ch == '='){
					   myword->typenum = 39;
					   myword->word = "==";
					   return (myword);
				   }
				   retract();
				   myword->typenum = 25;
				   myword->word = "="; 
				   return(myword);
				   break;
		case '+' : myword->typenum = 13;
				   myword->word = "+";
				   return(myword);
				   break;
		case '-' :
				   myword->typenum = 14;
				   myword->word = "=";
			   	   return(myword);	   
			   	   break;
		case '*':
				   myword->typenum = 15;
				   myword->word = "*";
			   	   return(myword);
			   	   break;
		case '/':
				   myword->typenum = 16;
				   myword->word = "/";
			       	   return(myword);
			   	   break;
	
		case '(':
				   myword->typenum = 27;
				   myword->word = "(";
				   return(myword);
				   break;
		case ')':
				   myword->typenum = 28;
				   myword->word = ")";
				   return(myword);
				   break;
				   //TODO
		case '[':
				   myword->typenum = 30;
				   myword->word = "[";
				   return(myword);
				   break;
				   //TODO
		case ']':
				   myword->typenum = 31;
				   myword->word = "]";
				   return(myword);
				   break;
				   //TODO
		case '{':
				   myword->typenum = 32;
				   myword->word = "{";
				   return(myword);
				   break;
				   //TODO
		case '}':
				   myword->typenum = 33;
				   myword->word = "}";
				   return(myword);
				   break;
				   //TODO
		case ',':
				   myword->typenum = 34;
				   myword->word = ",";
				   return(myword);
				   break;
		case ':':
				   m_getch();
				   if(ch == '='){
				   	myword->typenum = 18;
					myword->word = ":=";
					return (myword);
				   }
				   retract();
				   myword->typenum = 17;
				   myword->word = ":";
				   return(myword);
				   break;
		case ';':
				   myword->typenum = 26;
				   myword->word = ";";
				   return(myword);
				   break;
		case '>':
				   m_getch();
				   if(ch == '='){
					   myword->typenum = 24;
					   myword->word = ">=";
					   return(myword);
				   }
				   retract();
				   myword->typenum = 23;
				   myword->word = ">";
				   return(myword);
				   break;
		case '<':
				   m_getch();
				   if(ch == '='){
					   myword->typenum = 22;
					   myword->word = "<=";
					   return(myword);
				   }else if(ch == '>'){
					   myword->typenum = 21;
					   myword->word = "<>";
					   return(myword);
				   }
				   retract();
				   myword->typenum = 20;
				   myword->word = "<";
				   return(myword);
				   break;
		case '!':
				   m_getch();
				   if(ch == '='){
					   myword->typenum = 40;
					   myword->word = "<";
					   return(myword);
				   }
				   retract();
				   myword->typenum = -1;
				   myword->word = "ERROR";
				   return(myword);
				   break;
		case '\0':
				   myword->typenum = 1000;
				   myword->word = "OVER";
				   return(myword);
				   break;
		case '#':	
				   myword->typenum = 0;
				   myword->word = "#";
				   return(myword);
		default:
				   myword->typenum = -1;
				   myword->word = "ERROR";
				   return(myword);
				   break;
		
	}

}