Thread: Compiling 2 codes in a code

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    Compiling 2 codes in a code

    Hi everyone!
    im new to here.
    anyway i'm trying to compile two codes using a switch statement.
    there's no error but the program cannot be run.
    can anyone help me?
    thanks in advance!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Where's the code? Post in here please, in code tags.

    EDIT: Wait, no, I found it
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main(){
    char userinput;
     printf("\nPlease enter C (play with computer) or U (play with other user)");
     scanf(" %c",userinput);
       for(userinput=1;userinput<100;userinput++){
        switch(userinput){
        case 'C': case 'c':
            computer();
            break;
        case 'U': case 'u' :
            user();
            break;
        default:
           exit(0);
    
    struct myDataType{
         int i;
         char ch;
    }
    inputValue();
    void run();
    int check(char sym[9], char ch[1],int count);
    struct myDataType inputValue(char sym[9],int count);
    void Display(char sym[9]);
    
    void user(){
       char reStart[1];
       again:
       run();
       printf("\nIf you want to play again, press 1: \nelse any:");
       scanf("%s",reStart);
       if(*reStart =='1'){
        goto again;
       }
       else
        exit(0);
    }
    
    void run(){
        int count;
        struct myDataType info;
        char symbol[9]={'1','2','3','4','5','6','7','8','9'};
        Display(symbol);
        again:
        info = inputValue(symbol,count);
        symbol[info.i] = info.ch;
        Display(symbol);
        if(check(symbol,info.ch, count)==1)
            ;
        else{count++;
        goto again;
        }
    }
    
    int check(char sym[9],char ch[1],int count){
    int i;
    for(i=0;i<3;i++)//for row
        if(sym[i]==ch&&sym[i+1]==ch&&sym[i+2]==ch){
         printf("the winner is : %c",ch);
         return 1;}
    for(i=0;i<3;i++) // for row
        if(sym[i]==ch&&sym[i+3]==ch&&sym[i+6]==ch){
         printf("the winner is : %c",ch);
         return 1;}
         if(sym[0]==ch&&sym[4]==ch&&sym[8]==ch){
         printf("the winner is : %c",ch);
         return 1;}
         else if(sym[2]==ch&&sym[4]==ch&&sym[6]==ch){
         printf("the winner is : %c",ch);
         return 1;}
         else if(count ==8){
            printf("The game is DRAW");
            return 1;
         } else return 0;
    
    }
    
    struct myDataType inputValue(char sym[9],int count){
        char value[1];
        int i;
        struct myDataType info;
        inputAgain:
    
        if(count%2 == 0){
            printf("\nEnter Your choice X :");
        }else{
               printf("\nEnter Your choice O: ");
        }
        scanf("%s", value);
        for(i=0;i<9;i++){
            if(*value==sym[i]){
                info.i = i;
                if(count%2==0)
                    info.ch = 'X';
                else
                    info.ch = 'O';
                break;
                } else{
                info.i=-1;
                info.ch = "";
                }
        }
        if(info.i ==-1){
        printf("\nInput is not valid");
        goto inputAgain;
        } return info;
        }
    
    void Display(char sym[9]){
         printf("T I C  T A C  T O E");
         printf("\nPlayer 1 symbol: X");
         printf("\nPlayer 2 symbol: O");
         printf( "\n\t\t\t |         |        ");
         printf("\n\t\t     %c   |     %c   |     %c   ",sym[0],sym[1],sym[2]);
         printf("\n\t\t---------|---------|----------");
         printf("\n\t\t     %c   |     %c   |     %c   ",sym[3],sym[4],sym[5]);
         printf("\n\t\t---------|---------|----------");
         printf("\n\t\t     %c   |     %c   |     %c   ",sym[6],sym[7],sym[8]);
         printf( "\n\t\t\t |         |        ");
    
    
    }
    
    int done = 0;
    int moveindex = 0;
    
    char board[3][3];
    void computer(){
    	int i, j, z, player_move = 1;
    	z = 1;
    	for ( i = 0; i < 3; i++ )
    		for ( j = 0; j < 3; j++ )
    		{
    			board[i][j] = (char)(z+48);
    			z++;
    		}
    
    	printf("Tic Tac Toe\n================\n");
    	do {
    		if (player_move)
    			get_player_movement();
    		else
    			computer_move();
    		player_move = !player_move;
    		if (moveindex == 9)
    			done = 2;
    		printf("move %d\n", moveindex);
    		moveindex++;
    
    	} while (done == 0);
    
    	if (done == 1)
    		printf("I win!\n");
    
    	if (done == 2)
    		printf("Draw!\n");
    
    	print_template();
    
        return 1;
    
    
    }
    
    void print_template(void)
    {
    	int i;
    	for (i = 0; i < 3; i++)
    	{
    		printf("   |   |   \n");
    		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
    		printf("   |   |   \n");
    		if ( i != 2 )
    			printf ("--- --- ---\n");
    	}
    }
    
    int get_input(int move)
    {
    	int row, col;
    	if ( move >= 1 && move <= 3 )
    		row = 0;
    	if ( move >= 4 && move <= 6 )
    		row = 1;
    	if ( move >= 7 && move <= 9 )
    		row = 2;
    	col = move % 3 - 1;
    	if ( col < 0 )
    		col = 2;
    	if ( board[row][col] == 'x' )
    		return 1;
    	if ( board[row][col] == 'o' )
    		return 2;
    	else
    		return 0;
    }
    
    
    void get_player_movement(void)
    {
    	int move, valid = 0;
    	int row, col;
    	print_template();
    
    	do {
    
    		printf("Choose a square:\nPlease enter (1-9) only \n");
    		scanf("%d", &move); // for row
    		if ( move >= 1 && move <= 3 )
    			row = 0;   //1 row
    		if ( move >= 4 && move <= 6 )
    			row = 1;    // 2nd row
    		if ( move >= 7 && move <= 9 )
    			row = 2;   // 3rd row
    		col = move % 3 - 1; //for column
    		if ( col < 0 )
    			col = 2;
    		if (get_input(move) == 0)
    		{
    			board[row][col] = (char)'x';
    			valid = 1;
    		}
    		else
    			printf("That square is occupied!\n");
    
    	} while (!valid);
    
    }
    
    void computer_move (void)
    {
    	int colcount[3] = { 0, 0, 0 };
    	int rowcount[3] = { 0, 0, 0 };
    	int i, j;
    	int can_lose;
    	int lose_pos[1][2];
    
    
    	if (moveindex <= 1)
    		if (get_input(5) == 0)
    		{
    			board[1][1] = (char)'o';
    			return;
    		}
    
    
    	for ( i = 0; i < 3; i++)
    	{
    		for ( j = 0; j < 3; j++)
    		{
    			if ( board[i][j] == 'o' )
    			{
    				colcount[j]++;
    				rowcount[i]++;
    			}
    			if ( board[i][j] == 'x' )
    			{
    				colcount[j]--;
    				rowcount[i]--;
    			}
    		}
    	}
    
    	can_lose = 0;
    
    	for ( i = 0; i < 3; i++ )
    	{
    
    		if (abs(colcount[i]) == 2)
    		{
    			for ( j = 0; j < 3; j++ )
    				if ( board[j][i] != 'x' && board[j][i] != 'o' )
    				{
    					if (colcount[i] > 0)
    					{
    						board[j][i] = 'o';
    						done = 1;
    						return;
    					} else
    					{
    						can_lose = 1;
    						lose_pos[0][0] = j;
    						lose_pos[0][1] = i;
    					}
    				}
    		}
    		if (abs(rowcount[i]) == 2)
    		{
    			for ( j = 0; j < 3; j++ )
    				if ( board[i][j] != 'x' && board[i][j] != 'o' )
    				{
    					if (rowcount[i] > 0)
    					{
    						board[i][j] = 'o';
    						done = 1;
    						return;
    					} else
    					{
    						can_lose = 1;
    						lose_pos[0][0] = i;
    						lose_pos[0][1] = j;
    					}
    				}
    		}
    	}
    
    	if (can_lose)
    	{
    		board[lose_pos[0][0]][lose_pos[0][1]] = 'o';
    		return;
    	}
    
    	if (get_input(5) == 2)
    	{
    		if ((get_input(1) == get_input(9) == 1) ||
    			(get_input(7) == get_input(3) == 1) )
    		{
    			if (get_input(6) == 0)
    			{
    				board[1][2] = 'o';
    				return;
    			}
    			if (get_input(2) == 0)
    			{
    				board[0][1] = 'o';
    				return;
    			}
    			if (get_input(4) == 0)
    			{
    				board[1][0] = 'o';
    				return;
    			}
    			if (get_input(8) == 0)
    			{
    				board[2][1] = 'o';
    				return;
    			}
    		}
    
    	}
    
    	if ( get_input(2) * get_input(4) > 0 && get_input(1) == 0 )
    	{
    		board[0][0] = 'o';
    		return;
    	}
    
    	if ( get_input(2) * get_input(6) > 0 && get_input(3) == 0 )
    	{
    		board[0][2] = 'o';
    		return;
    	}
    
    	if ( get_input(4) * get_input(8) > 0 && get_input(7) == 0)
    	{
    		board[2][0] = 'o';
    		return;
    	}
    
    	if ( get_input(6) * get_input(8) > 0 && get_input(9) == 0)
    	{
    
    		board[2][2] = 'o';
    		return;
    	}
    
    	if (	(get_input(1) == get_input(9) && get_input(1) != 0) ||
    			(get_input(5) == get_input(9) && get_input(9) != 0) ||
    			(get_input(5) == get_input(1) && get_input(5) != 0) )
    	{
    		if (get_input(1) == 0)
    		{
    			board[0][0] = 'o';
    			if (get_input(5) == 2)
    				done = 1;
    			return;
    		}
    		if (get_input(5) == 0)
    		{
    			if (get_input(1) == 2)
    				done = 1;
    			board[1][1] = 'o';
    			return;
    		}
    		if (get_input(9) == 0)
    		{
    			board[2][2] = 'o';
    			if (get_input(5) == 2)
    				done = 1;
    			return;
    		}
    	}
    
    	if (	(get_input(3) == get_input(7) && get_input(3) != 0) ||
    			(get_input(5) ==get_input(7) && get_input(5) != 0) ||
    			(get_input(5) == get_input(3) && get_input(3) != 0) )
    	{
    		if (get_input(3) == 0)
    		{
    			board[0][2] = 'o';
    			if (get_input(5) == 2)
    				done = 1;
    			return;
    		}
    		if (get_input(5) == 0)
    		{
    			board[1][1] = 'o';
    			if (get_input(3) == 2)
    				done = 1;
    			return;
    		}
    		if (get_input(7) == 0)
    		{
    			board[2][0] = 'o';
    			if (get_input(5) == 2)
    				done = 1;
    			return;
    		}
    	}
    
    	if ( get_input(5) == 1 )
    	{
    		if (moveindex == 1)
    			board[0][0] = (char)'o';
    		if (moveindex == 3)
    		{
    			if (get_input(9) == 1)
    				board[2][0] = (char)'o';
    			if (get_input(7) == 1)
    				board[0][2] = (char)'o';
    			if (get_input(3) == 1)
    				board[2][0] = 'o';
    		}
    		return;
    	}
    
    	for ( i = 0; i < 3; i++ )
    	{
    		for ( j = 0; j < 3; j++ )
    			if (board[i][j] != 'x' && board[i][j] != 'o')
    			{
    				board[i][j] = 'o';
    				return;
    			}
    	}
    }
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    Where's the code? Post in here please, in code tags.

    EDIT: Wait, no, I found it
    Code:
    void main(){ // internet find,
    good ole internet for finding things to do. like fixing old code. then getting someone else to "help"
    Last edited by userxbw; 12-12-2017 at 12:23 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it still may need a little more work. I just did enough to get it this far, and have not fully tested it .
    Code:
    userx@slackwhere:~/bin
    $ ./term2
    
    Please enter C (play with computer) or U (play with other user) u
    In user
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 1   |     2   |     3   
            ---------|---------|----------
                 4   |     5   |     6   
            ---------|---------|----------
                 7   |     8   |     9   
                 |         |        
    Enter Your choice X :x
    
    Input is not valid
    Enter Your choice X :1
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 X   |     2   |     3   
            ---------|---------|----------
                 4   |     5   |     6   
            ---------|---------|----------
                 7   |     8   |     9   
                 |         |        
    Enter Your choice O: 5
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 X   |     2   |     3   
            ---------|---------|----------
                 4   |     O   |     6   
            ---------|---------|----------
                 7   |     8   |     9   
                 |         |        
    Enter Your choice X :9
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 X   |     2   |     3   
            ---------|---------|----------
                 4   |     O   |     6   
            ---------|---------|----------
                 7   |     8   |     X   
                 |         |        
    Enter Your choice O: 2
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 X   |     O   |     3   
            ---------|---------|----------
                 4   |     O   |     6   
            ---------|---------|----------
                 7   |     8   |     X   
                 |         |        
    Enter Your choice X :3
    T I C  T A C  T O E
    Player 1 symbol: X
    Player 2 symbol: O
                 |         |        
                 X   |     O   |     X   
            ---------|---------|----------
                 4   |     O   |     6   
            ---------|---------|----------
                 7   |     8   |     X   
                 |         |        
    Enter Your choice O: ^C
    Last edited by userxbw; 12-12-2017 at 12:57 PM.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Oh no, I didn't pull that code out of the internet at random, the OP had posted an attachment but for some reason it disappeared right after downloading it...
    Devoted my life to programming...

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    Oh no, I didn't pull that code out of the internet at random, the OP had posted an attachment but for some reason it disappeared right after downloading it...
    And, I thought you had some kind of magical powers to find others homework assignments on the internet without exactly knowing what they were talking about.
    my other thought,
    there is no way he could have written that and not know how to fix it.

    not that he stated that he wrote it just saying.
    Last edited by userxbw; 12-12-2017 at 02:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-31-2017, 10:14 AM
  2. Compiling multiple source codes
    By JonathanS in forum C Programming
    Replies: 6
    Last Post: 09-21-2011, 01:41 AM
  3. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  4. Error codes when compiling, using template function
    By LurPak in forum C++ Programming
    Replies: 2
    Last Post: 09-03-2003, 04:12 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread