Пример #1
0
//function to add new nodes to the tree
void addnew( struct node **q,int data )
{
	if( *q == NULL )
	{
	   *q = getnode( );
	   (*q)->left = NULL;
	   (*q)->right =NULL;
	   (*q)->data = data;
	   return;
	}

	if( (*q)->data > data )
	 addnew( &(*q)->left , data );
	else
	 addnew( &(*q)->right , data );
}
Пример #2
0
void main( )
{
	struct node *R;
	int n , i , data;
	R = NULL;

	printf("Enter number of nodes to create \n");
	scanf("%d", &n);
	//randomly generating the nodes and adding them into the tree
	for(i=1; i<=n ;i++)
	{
		data = rand()%100;
		addnew( &R , data );
	}
	
	printf("\n Inorder traversal \n");
	inorder( R );

	printf("\n Preorder traversal \n");
	preorder( R );

	printf("\n Postorder traversal \n");
	postorder( R );



}
Пример #3
0
void printArray(int *a,int n) 
{
	int i;
	for(i=0;i<=n;i++)	
		addnew(&root , a[i]);
	preorder(root);
}
Пример #4
0
int main(int argc,char *argv[]){
    int i=0,j=0,turn=0,diff=3,swap=0,k,h=1,bload=0;
    char human[]="white",computer[]="black";
    char ***b;
    char n=11;
    
    for(i=1;i<argc;i++){ /*Will make changes according to the commands given when the program was called. */
        if(!(strcmp(argv[i],"-s"))) /*Enable swap rule. */
            swap=1;
        else if(!(strcmp(argv[i],"-b"))){ /* Player plays as black. */
            strcpy(human,"black");
            strcpy(computer,"white");
        }
        else if(!(strcmp(argv[i],"-n"))){ /*Changes the size if input is appropriate. */
            /*Checks if there is something after "-n" in order not to go over the limits in case of wrong input.Also if the next word is a number. */
            if((i+1>=argc)||((*argv[i+1]<'0')||(*argv[i+1]>'9'))){
		fprintf(stderr,"Not given a new size after \"-n\".\nShutdown.\n");
		return 0;
	    }
	    if((atoi(argv[i+1])>=4)&&(atoi(argv[i+1])<=26)) /*If the given size has an acceptable value. */
                n=atoi(argv[++i]);
            else {
                fprintf(stderr,"Unacceptable size input(should be 4=<size=<26),shutdown.\n");
                return 0;
            }
        }
        else if(!(strcmp(argv[i],"-d"))){ /*Change the difficulty,if given level is appropriate */
	    if((i+1>=argc)||((*argv[i+1]<'0')||(*argv[i+1]>'9'))){ /*Checks if there is something after "-d" in order not to go over the limits in case of wrong input. */
		fprintf(stderr,"Not given a new difficulty level after \"-d\".\nShutdown.\n");
		return 0;
	    }
            if(atoi(argv[i+1])>0) /*If the new difficulty is over 1. */
                diff=atoi(argv[++i]);
            else {
                fprintf(stderr,"Unacceptable difficulty level input(should be >0),shutdown.\n");
                return 0;
            }
        }
        else /*If unknown parameter */
            fprintf(stderr,"Unacceptable parameters given.Program keeps running.Feel free to \"quit\" and start again.\n");
    }
    
    if(!(addnew(&b,n))) /*Beginning the game */
        return -1; 

    printf("Difficulty:%d\n",diff);
    printf("Size:%d\n",n);
    addnext(&b,++turn,n); /*Increasing turn and adding the turn's board. */
     /*Checks if the first digit of the human string is 'w'(means that human="white")and giving the appropriate value to h. */
    if (*human=='w')h=0;
    else h=1;
    
    /*Doing it so that for turn=0 the board is full of ' '.For turn=1 board is initially the same but changes before the next one is added. */
    
    /*Loop ends when a player gives a "quit" command or the functions encounter an error. */
    while(1){
        if((turn+h)%2){ /*If its the player's/computer's turn print the appropriate message. */
            printf("%s player(human) plays now.\n",human);    
        }
        else{
            printf("%s player(computer) plays now.\n",computer);            
        }
        printf("Turn:%d\n",turn);
        printgame(n,b,turn); /*Print the game status after each move. */
        
        do{                           /*Calls stringread until it returns 2(play/cont) */
            if(done(n,b,human,computer,turn-1,h)){
                turn=-1;
            }
            
            
            printf(">");
            k=stringread(&b,&turn,&diff,&n,human,computer,&swap,&h,&bload);
            if (k==0){                       /*If it returns 0 the program has encountered an error and needs to be terminated. */
                fprintf(stderr,"Fatal error.\n");
                return -1;
            }else if(k==-1)
                break;
            
        }while(k !=2);
        if(k==-1) /*In that case player quit. */
            break;
        
        if(!((turn+h)%2)){ /*If its the opponents turn calls the AI function,plays the move and prints the appropriate message */
            
            if(AI(n,diff,turn,b,NULL,&i,&j,1,*computer,*human,-1200,swap)){ /*1 is returned if*/
                swapit(b,n,turn);
            }
            else{
                b[turn][i][j]=*computer;
                printf("Move played: %c%d\n",'A'+j,i+1);
            }
        }
        
        if(!(addnext(&b,++turn,n)))
            return -1;
    }
    for (i=0;i<turn;i++){      /*Free what needs to be freed. */      
            for(j=0;j<n;j++)
                free(b[i][j]);
            free (b[i]);
    }
    free(b);
    printf("Application shut down.\n");
    return 1;
        
}