Thread: Help plz

  1. #1
    Unregistered
    Guest

    Help plz

    Hi,
    I have created this noughts and crosses game (see code below) and am having the following problems

    First thing I want to do is-at the moment when a co-ordinate is put in where a "X" or "O" has already been placed it just keeps asking until a valid set of co-ordinates is entered(no error message) - how would I go about putting a error message in telling the user that at the co-ordinates that they have entered that there is not a free place.

    Also, when the user is asked to enter co-ordinates, if a letter is put in the program crashes, i need to validate this so a error message is displayed - I have experimented with a few things, but have failed with the loops, if you could help it would be very much appreciated.

    CODE:

    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)

    {

    if(count<9)

    {

    do{

    printf("%s enter you next move, '0's\n",name1); /*asks for m move by name*/

    scanf("%d%d",&r0,&c0);

    system ("clear");

    }

    while((oxo[r0][c0]!='-')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'));

    {

    oxo[r0][c0]='0';

    }

    count++;

    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    if (count<9)

    {

    do{

    printf("%s enter you next move, 'X's\n",name2); /*asks for m move by name*/

    scanf("%d%d",&rx,&cx);

    system ("clear");

    }

    while((oxo[rx][cx]!='-')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0'));

    {

    oxo[rx][cx]='X';
    }

    count++;

    }

    display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }


  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I only changed two things, but I don't think it really helped...
    I am just posting this to help spread out your code
    btw: use the [ c o d e ] tags
    Code:
    #include <stdio.h> 
    #include <string.h> /*sets up 2 strings so names can be used*/ 
    char oxo[4][4]; 
    void display_grid(); 
    int main(void) 
    { 
    	int r1,c1,r0,c0,rx,cx,count; 
    	int name1[20],name2[20]; /*defines name1 & name2 are variables */ 
    	for(r1=1;r1<=3;r1++) 
    	{ 
    		for(c1=1;c1<=3;c1++) 
    		{ 
    			oxo[r1][c1]='-'; 
    			oxo[0][1]='1'; /**/ 
    			oxo[0][2]='2'; /*prints column numbers*/ 
    			oxo[0][3]='3'; /**/ 
    			oxo[1][0]='1'; /**/ 
    			oxo[2][0]='2'; /*prints row numbers*/ 
    			oxo[3][0]='3'; /**/ 
    		} 
    	} 
    
    	system ("clear"); 
    	printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/ 
    	scanf("%s",name1); /*reads name*/ 
    	printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */ 
    	printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/ 
    	scanf("%s",name2); /*reads name*/ 
    	printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */ 
    	system ("clear"); 
    	display_grid(); 
    	count=0; 
    	while(count<9) 
    	{ 
    		if(count<9) 
    		{ 
    			do{ 
    				printf("%s enter you next move, '0's\n",name1); /*asks for m move by name*/ 
    				scanf("%d%d",&r0,&c0); 
    				system ("clear"); 
    			} 
    			while((oxo[r0][c0]!='-')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'))
    			{ 
    				oxo[r0][c0]='0'; 
    			} 
    	
    			count++; 
    	
    		} display_grid(); 
    	
    	/*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/ 
    	
    		if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||
    		(oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')|| 
    		(oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')|| 
    		(oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')|| 
    		(oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')|| 
    		(oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')|| 
    		(oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')|| 
    		(oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0')) 
    		{ 
    			printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/ 
    
    			return; /*if '0' wins end game*/ 
    
    		} 
    
    		else /*if 0 not won program continues*/ 
    
    		count<9; 
    
    		if (count<9) 
    	
    		{ 
    	
    			do{ 
    	
    				printf("%s enter you next move, 'X's\n",name2); /*asks for m move by name*/ 
    	
    				scanf("%d%d",&rx,&cx); 
    	
    				system ("clear"); 
    
    			} 
    
    			while((oxo[rx][cx]!='-')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0')); 
    
    			{ 
    
    				oxo[rx][cx]='X'; 
    			} 
    
    			count++; 
    	
    		} 
    
    		display_grid(); 
    
    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/ 
    
    		if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')|| 
    		(oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')|| 
    		(oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')|| 
    		(oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')|| 
    		(oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')|| 
    		(oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')|| 
    		(oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')|| 
    		(oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X')) 
    		{ 
    
    			printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2); 
    /*if X wins print this*/ 
    
    			return; /*if X wins end game*/ 
    
    		} 
    
    		else /*if 0 not won program continues*/ 
    	
    		count<9; 
    
    	} 
    
    	if (count=9) /*when 9 moves have been made with no winner game stops*/ 
    
    	system ("clear"); 
    
    	printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/ 
    	return 0;
    } 
    
    void display_grid() 
    { 
    	int row, col; 
    	for(row=1;row<=3;row++) 
    	{ 
    		for(col=1;col<=3;col++) 
    		{ 
    			printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/ 
    		} 
    
    		printf("\n"); 
    		if(row<=3) 
    		{ 
    			printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/ 
    		} 
    	} 
    return; 
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Unregistered
    Guest

    Unhappy Thx

    Thx for ur help, but nobody answered my questions, can ne1 plzz help??

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In answer to your two questions:

    1 - how would I go about putting a error message in telling the user that at the co-ordinates that they have entered that there is not a free place.

    You just need to be a bit cleverer with the while loop. You are already testing to see if the chosen cell is occupied, so when you find it is, just do something simple like printf a message saying so.

    2 - when the user is asked to enter co-ordinates, if a letter is put in the program crashes

    This is a common problem, and is a result of the poor input checking provided by scanf(). You can search the board to find many conversations about this. This one is a recent version. If you wanted to stick with scanf() though, use it's return code to check for the actual number of successful variables read.

    There's also a few errors in the source. For example, you have a couple of lines with just count<9; on, which serve no purpose. Also, you have if (count=9) which is probably incorrect, although valid statement. Make sure you use == to check for equality, and = for assignment.
    Last edited by Hammer; 05-05-2002 at 04:06 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    1.

    while(count<9)
    {
    if(count<9)
    {
    do{
    printf("%s enter you next move, '0's\n",name1); /*asks for m move by name*/
    scanf("%d%d",&r0,&c0);
    system ("clear");
    }
    while((oxo[r0][c0]!='-')

    else
    if(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'))

    printf("cell is already taken\n");
    {
    oxo[r0][c0]='0';
    }

    count++;


    2.

    while(count<9)
    {
    if(count<9)
    {
    do{
    printf("%s enter you next move, '0's\n",name1); /*asks for m move by name*/
    if scanf("%d%d",&r0,&c0);
    printf("Co-ordinates entered were %d%d\n", rx cx);
    else
    printf("This is not a valid interger, please try again\n");

    system ("clear");
    }
    while((oxo[r0][c0]!='-')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'))
    {
    oxo[r0][c0]='0';
    }

    count++;


    I tried the above and it didnt work, am I doing it correctly??

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if scanf("%d%d",&r0,&c0);
    This produces a syntax error in my compiler.

    scanf returns the number of succesful variables read. So, you can do something like this
    Code:
    if (scanf("%d %d", &num1, &num2) !=2)
    {
         printf("We did not read 2 numbers\n");
         getchar(); /* needed to remove non-numeric character from input stream, I believe */
    
    }
    else
    {
         printf("Our 2 numbers are %d and %d\n", num1, num2);
    }
    Try incorporating something like that into your code.
    Once you have the 2 numbers, make sure that they are both within range (to stop the user entering 99 for example).
    Next, check the contents of the cell at that location. If it's a '-', just go back and ask again. If it's 'X' or 'O', printf your error message, then go back and ask again.

    As I said before, scanf isn't very good, you'll so learn other functions that do a better job, albeit they take more coding on your part.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unregistered
    Guest
    Sorry, I put what u said into my code but if i did that:

    while(count<9)
    {
    if(count<9)
    {
    do{
    if (scanf("%d %d", &num1, &num2) !=2)
    {
    printf("We did not read 2 numbers\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */

    }
    else
    {
    printf("Our 2 numbers are %d and %d\n", num1, num2);
    }
    ----------------
    How would I incorperate:
    while((oxo[r0][c0]!='-')

    else
    if(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'))


    and

    {
    oxo[r0][c0]='0';
    }

    count++;


    Sorry if im being a pain, but im fairly new to this, and am not very good.

  8. #8
    Unregistered
    Guest
    while(count<9)
    {
    if(count<9)
    {
    do{
    if(scanf("%d %d", &num1, &num2) !=2)

    printf("We did not read 2 numbers\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */

    {
    if(r0<1)||(r0>3)||(c0<1)||(c0>3)

    printf("Out of range grid is 3x3, enter 1-3, try again\n");

    {
    if(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0')

    printf("Cell already taken\n");

    }
    else
    {
    printf("Our 2 numbers are %d and %d\n", num1, num2);
    }

    -------------------------
    above is what I have tierd, but no luck, can ne1 help?? I think i may be doing the "{" and "}" in the wrong places, not sure thou?

  9. #9
    Unregistered
    Guest
    This is what I meant:


    while(count<9)
    {
    if(count<9)
    {
    do{
    if(scanf("%d %d", &num1, &num2) !=2)

    printf("We did not read 2 numbers\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */

    {
    if(r0<1)||(r0>3)||(c0<1)||(c0>3)

    printf("Out of range grid is 3x3, enter 1-3, try again\n");

    {
    if(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0')

    printf("Cell already taken\n");

    }
    else
    oxo[r0][c0]='0';

    count++;
    }

    -------------------------
    above is what I have tierd, but no luck, can ne1 help?? I think i may be doing the "{" and "}" in the wrong places, not sure thou?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I think i may be doing the "{" and "}" in the wrong places, not sure thou?
    It does look a little confusing, doesn't it! I'd suggest you read up on the structure of the if/else and while statements.

    Anyways, in my example code, I used variables named num1 and num2. They were only examples and you should replace them with the names from your program.

    >if(r0<1)||(r0>3)||(c0<1)||(c0>3)
    This is incorrect syntax. It should be encased in brackets.

    You'll need to encase the user input section in a loop of some kind. That way, you can give the user as many goes as necessary to enter their numbers.

    Here's some revised code. It's by no mean complete, but is something for you to look at, and will get you on the right track:
    Code:
    while(count<9)
    {
    	if(count<9)	// not sure what you're doing here, but I'll leave it in anyway.
    	{
    		while (1)
    		{	// start infinite loop, exits when user enters valid cell.
    			printf("Enter next move (two numbers): ");
    			while (scanf("%d %d", &r0, &c0) != 2)
    			{
    				printf("We did not read 2 numbers, try again\n");
    				getchar(); /* needed to remove non-numeric character from input stream, I believe */
    			}
    		
    			if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    			{
    				printf("Out of range grid is 3x3, enter 1-3, try again\n");
    				continue;	// go back to top of infinite loop
    			}
    			if ( oxo[r0][c0]=='X' || oxo[r0][c0]=='0')
    			{
    				printf("Cell already taken\n");
    				continue;	// go back to top of infinite loop
    			}
    			break;   // leave the infitite loop
    		}
    		oxo[r0][c0]='0';
    		count++;
    		
    		// program continues here..........
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Unregistered
    Guest
    Thx Hammer, that worked grt, but havin problems:
    Wen I did this:

    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &r0, &c0) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='0';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    if (count<9)

    {

    do{

    printf("%s enter you next move, 'X's\n",name2); /*asks for m move by name*/

    scanf("%d%d",&rx,&cx);

    system ("clear");

    }

    while((oxo[rx][cx]!='-')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0'));

    {

    oxo[rx][cx]='X';
    }

    count++;

    }

    display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }


    ---------I pasted in 4 only the '0', that worked grt, but wen i tried to copy and paste for the 'X' it didnt work (I changed sum bits, which where required to be change, see highlighted code, below).

    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &r0, &c0) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='0';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &rx, &cx) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='X';
    count++;



    } display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }

    ----------When I added the hightlighted code (above) it didnt work, I dont see what I am doin wrong, can you help??

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    
    
    #include <stdio.h> 
    #include <string.h>
    #include <conio.c>
    
    #define OCCUPIED '!'
    #define INFINITY 1
    
    
    int boardIsFull();
    void display_grid();
    
    char oxo[4][4]; 
    char enforcer[4][4];
    
     
    int main(void)
    { 
    int r1,c1,R,C;
    
    int invalid;
    
    
    char name1[20],name2[20];
    char *player;
    char token;
    
    
    for(r1 = 1; r1 <= 3; r1++)
     {
      for(c1 = 1; c1 <= 3; c1++)
       {
        oxo[r1][c1]='-';
        enforcer[r1][c1]= '-';
       }
     }
    
    oxo[0][1]='1';  
    oxo[0][2]='2';  
    oxo[0][3]='3';  
    oxo[1][0]='1';  
    oxo[2][0]='2';  
    oxo[3][0]='3';
    
     clrscr();
    
    printf("\n Player 1 what is your name?\n");  
    
    scanf("%s",name1);  
    
    printf("\nHello %s you place the '0's & you go first! \n",name1);  
    
    printf("\n Player 2 what is your name?\n");  
    
    scanf("%s",name2);  
    
    printf("\nHello %s you place the 'X's & you go second!\n",name2);  
    
     clrscr();
    
    
    
    
    player = name2; //...this will flip-flop in a second...
    
    
    
    
    
    
    while( INFINITY )
    {
    
    
     if(player == name2)
      {
       player = name1;
       token = '0';
      }
     else
      {
       player = name2;
       token = 'X';
      }
    
    
     do{
    
       display_grid();
    
       invalid = 0;
    
       printf("%s enter you next move, '%c's\n\n",player, token);
    
       scanf("%d%d",&R,&C);
    
       clrscr();
    
        if(R > 3 || C > 3)
         {
          printf("Sorry, that is an illegal move!\n\n");
    
          invalid = 1;
         }
    
         if(!invalid)
          {
    
             if( enforcer[R][C]!= OCCUPIED )
             {
              oxo[R][C]= token;
              enforcer[R][C] = OCCUPIED;
             }
             else
             {
              printf("Sorry, that position is already occupied.\n\n");
              invalid = 1;
             }
          }
    
       }while(invalid);
    
    
    
    
    
     display_grid();
    
    
    
    if ((oxo[1][1] == token && oxo[1][2] == token && oxo[1][3] == token )||
    (oxo[2][1] == token && oxo[2][2] == token && oxo[2][3] == token )||
    (oxo[3][1] == token && oxo[3][2] == token && oxo[3][3] == token )||
    (oxo[1][1] == token && oxo[2][2] == token && oxo[3][3] == token )||
    (oxo[1][2] == token && oxo[2][2] == token && oxo[3][2] == token )||
    (oxo[1][3] == token && oxo[2][3] == token && oxo[3][3] == token )||
    (oxo[1][1] == token && oxo[2][2] == token && oxo[3][3] == token )||
    (oxo[3][1] == token && oxo[2][2] == token && oxo[1][3] == token ) )
     {
      printf("\n GAME OVER...%s WINS.... GAME OVER\n",player);
      system("pause");
      return 0;
     }
    
     else
     if( boardIsFull() )
     {
      printf("\nGAME OVER...NO WINNER!!...GAME OVER\n");
      system("pause");
      return 0;
     }
    
    
    clrscr();
    }
    
    
     return 0;
    } 
    
    
    
    
    
    
    int boardIsFull()
    {
     int r, c;
    
     for(r = 1; r <= 3; r++)
     {
      for(c = 1; c <= 3; c++)
       {
        if(enforcer[r][c] != OCCUPIED)
         return 0;
       }
     }
    return 1;
    }
    
    
    
    void display_grid() 
    {
     int row, col;
    
      for(row=0;row<=3;row++)
      {
        for(col=0;col<=3;col++)
        {
         printf(" %c |",oxo[row][col]);
        }
      printf("\n");
      }
    printf(" ---------------\n");
    
    return;
    }
    Last edited by Sebastiani; 05-05-2002 at 08:44 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Unregistered
    Guest

    Few problems....

    Woo, ^^^ looks complicated, think ill still stick to my code:

    Thx Hammer, that worked grt, but havin problems:
    Wen I did this:

    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &r0, &c0) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='0';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    if (count<9)

    {

    do{

    printf("%s enter you next move, 'X's\n",name2); /*asks for m move by name*/

    scanf("%d%d",&rx,&cx);

    system ("clear");

    }

    while((oxo[rx][cx]!='-')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0'));

    {

    oxo[rx][cx]='X';
    }

    count++;

    }

    display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }


    ---------I pasted in 4 only the '0', that worked grt, but wen i tried to copy and paste for the 'X' it didnt work (I changed sum bits, which where required to be change, see highlighted code, below).

    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &r0, &c0) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='0';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &rx, &cx) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='X';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }

    ----------When I added the hightlighted code (above) it didnt work, I dont see what I am doin wrong, can you help??

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I've had a quick look, and....

    There's a } missing, the one that finishes the main() function. It should go before the line starting the display_grid() function void display_grid() and after the printf saying that no-one won.

    Also, within the section of code doing the reads for player 2, there's a few more rO & cO that need changing to rx & cx. You have already changed the scanf bits, but the tests after that use the wrong names.

    When you put code up here, please can you use code tags. Just surround your code with [ code ] /* code here */ [ /code ] (note: don't use spaces within the brackets). It makes it easier for people to read.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Unregistered
    Guest

    Unhappy

    Below, is the revised code, worked, but wen I run it, and its player 2's go, it doesnt print the updated gride and for the rest of the game it doesnt, can you see why this is happening?


    #include <stdio.h>
    #include <string.h> /*sets up 2 strings so names can be used*/
    char oxo[4][4];
    void display_grid();
    int main(void)

    {
    int r1,c1,r0,c0,rx,cx,count;
    int name1[20],name2[20]; /*defines name1 & name2 are variables */

    for(r1=1;r1<=3;r1++)

    {

    for(c1=1;c1<=3;c1++)

    {

    oxo[r1][c1]='-';
    oxo[0][1]='1'; /**/
    oxo[0][2]='2'; /*prints column numbers*/
    oxo[0][3]='3'; /**/
    oxo[1][0]='1'; /**/
    oxo[2][0]='2'; /*prints row numbers*/
    oxo[3][0]='3'; /**/

    }

    }

    system ("clear");

    printf("\n Player 1 what is your name?\n"); /*asks player 1 1s name*/

    scanf("%s",name1); /*reads name*/

    printf("\nHello %s you place the '0's & you go first! \n",name1); /*print out */

    printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

    scanf("%s",name2); /*reads name*/

    printf("\nHello %s you place the 'X's & you go second!\n",name2); /*print out */

    system ("clear");

    display_grid();

    count=0;

    while(count<9)
    {
    if(count<9) /*not sure what you're doing here, but I'll leave it in anyway*/
    {
    while (1)
    { /*start infinite loop, exits when user enters valid cell.*/
    printf("Enter next move (two numbers): ");
    while (scanf("%d %d", &r0, &c0) != 2)
    {
    printf("We did not read 2 numbers, try again\n");
    getchar(); /* needed to remove non-numeric character from input stream, I believe */
    }

    if((r0<1)||(r0>3)||(c0<1)||(c0>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[r0][c0]=='X'||oxo[r0][c0]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    oxo[r0][c0]='0';
    count++;


    } display_grid();

    /*These lines look to see if there is a row of 0s in any of the 8 ways possible program looks to see if 0s have won*/

    if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

    (oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
    (oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
    (oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
    (oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
    (oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
    (oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

    {

    printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); /*if 0 wins print this*/

    return; /*if '0' wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    if (count<9)

    {

    do{

    printf("%s enter you next move, 'X's\n",name2); /*asks for m move by name*/

    scanf("%d%d",&rx,&cx);

    system ("clear");

    }

    while((oxo[rx][cx]!='-')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0'));

    {

    oxo[rx][cx]='X';
    }

    count++;

    }

    display_grid();

    /*These lines look to see if there is a row of Xs in any of the 8 ways possible program looks to see if Xs have won*/

    if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

    (oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
    (oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
    (oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
    (oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
    (oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
    (oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

    {

    printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
    /*if X wins print this*/

    return; /*if X wins end game*/

    }

    else /*if 0 not won program continues*/

    count<9;

    }

    if (count=9) /*when 9 moves have been made with no winner game stops*/

    system ("clear");

    printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print information telling users out*/

    }

    void display_grid()

    {

    int row, col;

    for(row=1;row<=3;row++)

    {

    for(col=1;col<=3;col++)

    {

    printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid lines*/

    }

    printf("\n");

    if(row<=3)

    {

    printf(" ---------------\n"); /*prints & spaces horizontal g grid lines*/

    }

    }

    return;

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 11:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM