Example #1
0
bool CSchemaCache::Normalize(CSchemaPtr& pSchema, CXMLElement*& pXML) const
{
	pSchema = NULL;

	if ( pXML )
	{
		pSchema = SchemaCache.Get( pXML->GetAttributeValue( CXMLAttribute::schemaName ) );
		if ( ! pSchema )
		{
			// Schemas do not match by URN, get first element to compare
			// with names map of schemas (which are singulars)
			if ( CXMLElement* pElement = pXML->GetFirstElement() )
			{
				pSchema = Guess( pElement->GetName() );
				if ( pSchema )
				{
					// Strip envelope
					pElement->Detach();
					pXML->Delete();
					pXML = pElement;
				}
			}
			if ( ! pSchema ) // has no plural envelope
			{
				pSchema = Guess( pXML->GetName() );
			}
		}
	}

	return ( pSchema != NULL );
}
Example #2
0
/*
 *  Play the "animal" game, in which the program attempts to guess an animal
 *  that the user is thinking of by asking yes or no questions. Eventually,
 *  the program either will guess the user's animal or run out of questions
 *  to ask. In the latter case, the program will ask the user to provide a
 *  yes-or-no question that would distinguish between the user's animal and
 *  the program's best guess.
 *  The data structure of questions and guesses is essentially a binary tree,
 *  with each internal node having a "yes" branch and a "no" branch. Leaves
 *  of the tree represent animals to be guessed by the program. If the program
 *  fails to guess the user's animal, it replaces one of the leaves of the tree
 *  by a node containing the new question, whose children are the program's
 *  best guess and the animal provided by the user.
 *  The structure of the program is simple. It initializes the question/guess
 *  data structure, then plays games as long as the user is interested. In each
 *  game, the program starts at the top of the tree (the root) and progresses
 *  toward the bottom (the leaves) depending on the user's responses. Once it
 *  reaches a leaf, it either has won or lost, and handles the situation as
 *  described above.
 */
int main (int argc, char *argv[])
{
    char *treefile = NULL;
    TreeType tree;
    PositionType pos;
    char *newQuestion, *newAnswer;

    if (argc > 1) {
        treefile = argv[1];
    }
    tree = InitTree (treefile);

    printf("%s", "Think of an animal. I will try to guess what it is.\n"
		 "Please answer my questions with yes or no.\n");

    while (TRUE) {
        pos = Top (tree);
        while (!IsLeaf (tree, pos)) {
            pos = Answer(Question(tree,pos))? 
	       YesNode(tree,pos): NoNode(tree,pos);
        }
        if (Answer (Guess (tree, pos))) {
            printf ("I got it right!\n");
        } else {
            GetNewInfo (tree, pos, &newAnswer, &newQuestion);
            ReplaceNode (tree, pos, newAnswer, newQuestion);
        }

        if (!Answer ("Want to play again? ")) {
            WriteTree(tree, treefile);
            exit (0);
        }
    }
}
Example #3
0
/*
 *  Play the "animal" game, in which the program attempts to guess an animal
 *  that the user is thinking of by asking yes or no questions. Eventually,
 *  the program either will guess the user's animal or run out of questions
 *  to ask. In the latter case, the program will ask the user to provide a
 *  yes-or-no question that would distinguish between the user's animal and
 *  the program's best guess.
 *  The data structure of questions and guesses is essentially a binary tree,
 *  with each internal node having a "yes" branch and a "no" branch. Leaves
 *  of the tree represent animals to be guessed by the program. If the program
 *  fails to guess the user's animal, it replaces one of the leaves of the tree
 *  by a node containing the new question, whose children are the program's
 *  best guess and the animal provided by the user.
 *  The structure of the program is simple. It initializes the question/guess
 *  data structure, then plays games as long as the user is interested. In each
 *  game, the program starts at the top of the tree (the root) and progresses
 *  toward the bottom (the leaves) depending on the user's responses. Once it
 *  reaches a leaf, it either has won or lost, and handles the situation as
 *  described above.
 */
int main () {
    TreeType tree;
    PositionType pos;
    char *newQuestion, *newAnswer;
    tree = InitTree ();

    // unitTest();
    printf("%s", "Think of an animal. I will try to guess what it is.\n"
         "Please answer my questions with yes or no.\n");

    while (TRUE) {
        pos = Top (tree);
        while (!IsLeaf (tree, pos)) {
            pos = Answer (Question (tree, pos))?
            YesNode (tree, pos): NoNode (tree, pos);
        }
        if (Answer (Guess (tree, pos))) {
            printf ("I got it right!\n");
        } else {
            GetNewInfo (tree, pos, &newAnswer, &newQuestion);
            ReplaceNode (tree, pos, newAnswer, newQuestion);
        }
        if (!Answer ("Want to play again? ")) {
            exit (0);
        }
    }
    return 0;
}
Example #4
0
/*
 *  Form a question out of the string stored at position pos in the given
 *  animal tree.
 */
char *Question (TreeType tree, PositionType pos) {
    /* Your code goes here -- delete this line */
    if (IsLeaf(tree, pos)) {
        return Guess(tree, pos);
    } else {
        return tree[pos];
    }
}
void DisplayGuess(int level, int code[], int rightColorRightFeature, int rightColorWrongFeature, int *guessNum)
{
     char selection;
     
     if (rightColorRightFeature != level )
     {    
          printf("\nYour Score:\n");
          printf("\nRight Color, Right Feature: %d",rightColorRightFeature);
          printf("\n\nRight Color, Wrong Feature: %d\n",rightColorWrongFeature);
          system("pause");
          Guess(level,code);
     }
     else
     {
          printf("\n\n!!!!!! CONGRATULATIONS! You have won the game with %d Guesses !!!!!!\n\n",*guessNum - 1);
          outtextxy(365,180, "CONGRATULATIONS! You have won the game");     
          
          do
          {
              fflush(stdin);
              printf("\nWould you like to play again? (Y/N): ");
              scanf("%c",&selection);
              fflush(stdin);
          
              if (selection == 'Y' || selection == 'y')
              {
                   *guessNum = 1;
                   Menu();
              }
              else if (selection == 'N' || selection == 'n')
              {
                   printf("\n");
                   system("pause");
                   exit(1);     
              }
              else
              {
                   printf("\aWARNING: you should enter \'Y\' or \'N\'\n");
              }
          }while (selection != 'Y' && selection != 'y' && selection != 'N' && selection != 'n');
     }
}
void MakeCode (int level)
{     
     int i;
     
     srand(time(NULL));
     
     int code[level];
     
     for (i = 0; i < level; i++)
     {
         code[i] = 1 + (rand() % level);
         printf("%d",code[i]);
     }
    
 //    int code[5] = { 1,1,1,1,1 };
     
     printf("\n\n");
     
     Guess(level,code);
}
void Guess (int level, int code[])
{
     static int guessNum = 1;
     int i = 0, j;
     int guess[level];
     char selection;
     int x,y;
     
     outtextxy(410,150, "Now, Let\'s make your guess!");     
     
     if (guessNum == 13)
     {
         printf("\n\nYou have made 12 guesses already! You lost!\n\n");
         outtextxy(380,180, "GAME OVER! You have lost the game");
         printf("\nWould you like to play again? (Y/N): ");
         fflush(stdin);
         scanf("%c",&selection);
         fflush(stdin);
         if (selection == 'Y' || selection == 'y')
         {
              guessNum = 1;
              Menu();
         }
         else if (selection == 'N' || selection == 'n')
         {
              printf("\n");
              system("pause");
              exit(1);     
         }
         else
         {
              printf("\aWARNING: you should enter \'Y\' or \'N\'\n");
              Guess(level,code);
         }
     }
     
     printf("\nGuess %d:\n------------\n",guessNum);
     
     while (i != level)
     {
         switch (i)                               // according to i value (which goes to level), automatically running correct printf function..
         {
              case 0:
                   printf("Enter your guess for hat");
                   break;
                   
              case 1:
                   printf("Enter your guess for eyes");
                   break;
                   
              case 2:
                   printf("Enter your guess for nose");
                   break; 
                   
              case 3:
                   printf("Enter your guess for mouth");
                   break;
                   
              case 4:
                   printf("Enter your guess for clothes");
                   break; 
         }
         FillColor(i,11);                                            // changing color of current feature
         
         if (level == 5)                                             // according to level, running correct printf function..
         printf(" (1:red, 2:green, 3:blue, 4:purple, 5:yellow): ");
         else
         printf(" (1:red, 2:green, 3:blue): ");
         
         while (1)                                                  // taking mouse clicks or running scanf function
         {            
                guess[i] = -1;
    // ---------------------------------------------------------------------------------------------------------  
    // NOTICE:  Make "Comment" here, If you want to use Mouse while guessing          
    
    // <== REMOVE HERE              /*         
    
                if ( scanf("%d",&guess[i]) != -1 )
                {
                     if (guess[i] > level)
                     continue;
                     
                     else
                     {
                         clearmouseclick(WM_LBUTTONDOWN);     
                         break;
                     }
                }
                
    //                      */
    // ---------------------------------------------------------------------------------------------------------  
    
                if ( ismouseclick(WM_LBUTTONDOWN) )
                {
                      x = mousex();
                      y = mousey();
                      
                      if ( ( (x >= 380 && x <= 420) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 430 && x <= 470) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 480 && x <= 520) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 530 && x <= 570) && (y >= 80 && y <= 120) && level == 5 )  ||
                           ( (x >= 580 && x <= 620) && (y >= 80 && y <= 120) && level == 5 )  )
                      {
                                                 
                           clearmouseclick(WM_LBUTTONDOWN);      // if user clicks on correct place (buttons), take it..
                           break;
                      }
                      else
                      {
                           clearmouseclick(WM_LBUTTONDOWN);      // if user clicks on anywhere except buttons, wait to be clicked correctly..
                           continue;
                      }
                }
                

         }
      
                                                                   // Assigning guess[] array values according to the coordinates of mouse clicks
         if ( (x >= 380 && x <= 420) && (y >= 80 && y <= 120) )
         guess[i] = 1;
         if ( (x >= 430 && x <= 470) && (y >= 80 && y <= 120) )
         guess[i] = 2;
         if ( (x >= 480 && x <= 520) && (y >= 80 && y <= 120) )
         guess[i] = 3;
         if ( (x >= 530 && x <= 570) && (y >= 80 && y <= 120) )
         guess[i] = 4;
         if ( (x >= 580 && x <= 620) && (y >= 80 && y <= 120) )
         guess[i] = 5;
         
         
         if (guess[i] != -1)                                    
         printf("%d\n",guess[i]);                                  // this runs when Mouse is used.. 
    
         if (guess[i] == 1)                                        // Calling FillColor() function according to the guesses..
         FillColor(i,4);
         if (guess[i] == 2)
         FillColor(i,2);
         if (guess[i] == 3)
         FillColor(i,1);
         if (guess[i] == 4)
         FillColor(i,5);
         if (guess[i] == 5)
         FillColor(i,14);
           
         i++;                                                    // increasing i, which goes to level
     }
     guessNum++;                                                 // increasing guessNum
     
     CodeCheck(level,code,guess,&guessNum);                      // continue through CodeCheck()
}
Example #8
0
int HC(int N){
    int le,ri;
    int st,fi;
    st=1;
    fi=N;
    le=1;
    ri=N;
    
    //int pre=-1000;
 
    int pre=(le*2+ri)/3;
    Guess(pre);
 
    while(true){
        if(le==ri)
            return le;
        int mid=(le+ri)/2;
        int x=2*mid-pre;
        if(x==pre)
            x++;
        else if((x+pre)&1){
            if(x>pre){
                if(x<N)
                    x++;
                else if(x>pre+1&&x>1)
                    x--;
            }
            else{
                if(x>1)
                    x--;
                else if(x+1<pre&&x<N)
                    x++;
            }
        }
        if(x>=st&&x<=fi){
            int k=Guess(x);
            if(k==0)
                return (x+pre)/2;
            else if(k>0){
                if(x<pre){
                    ri=(x+pre)/2-((x+pre)%2==0);
                    pre=x;
                }
                else{
                    le=(x+pre)/2+1;
                    pre=x;
                }
            }
            else{
                if(x<pre){
                    le=(x+pre)/2+1;
                    pre=x;
                }
                else{
                    ri=(x+pre)/2-((x+pre)%2==0);
                    pre=x;
                }
            }
        }
        else{
            if(x<st){
                x=1;
            }
            else{
                x=N;
            }  
            if((x+pre)&1){
                if(x>pre){
                    if(x<N)
                        x++;
                    else if(x>pre+1&&x>1)
                        x--;
                }
                else{
                    if(x>1)
                        x--;
                    else if(x+1<pre&&x<N)
                        x++;
                }
            }
            int k=Guess(x);
            if(k==0)
                return (x+pre)/2;
            else if(k>0){
                if(x<pre){
                    ri=(x+pre)/2-((x+pre)%2==0);
                    pre=x;
                }
                else{
                    le=(x+pre)/2+1;
                    pre=x;
                }
            }
            else{
                if(x<pre){
                    le=(x+pre)/2+1;
                    pre=x;
                }
                else{
                    ri=(x+pre)/2-((x+pre)%2==0);
                    pre=x;
                }
            }
            //x=(le*2+ri)/3;
            //x=le;
            //Guess(x);
            //pre=x;
        }
    }
    return 0;
}