Help plz

This is a discussion on Help plz within the C Programming forums, part of the General Programming Boards category; Sorry, i think the above code was incoorect, this is tha problem: when I run it, and its player 2's ...

  1. #16
    Unregistered
    Guest
    Sorry, i think the above code was incoorect, this is tha problem:
    when I run it, and its player 2's go, it doesnt print the updated grid (does put in x & 0's, but doesnt print grid) 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*/

    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((rx<1)||(rx>3)||(cx<1)||(cx>3))
    {
    printf("Out of range grid is 3x3, enter 1-3, try again\n");
    continue; /*go back to top of infinite loop*/
    }
    if(oxo[rx][cx]=='X'||oxo[rx][cx]=='0')
    {
    printf("Cell already taken\n");
    continue; // go back to top of infinite loop
    }
    break; /*leave the infitite loop*/
    }
    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. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, here we go again..... I have corrected some of the errors in your source, enough to make it work. There were only a couple of minor alterations, which were to move around some of the }'s.

    This compiles, and does what it's supposed to, but there is room for a lot of improvement. Your earlier post said that Sebastiani's code looked complicated, but infact there's less to it than yours. In particular, your code is duplicated to handle the input and actions for player one and player two, when with a small amount of coding changes, you can have one set of statements doing both. This is then easier to design and maintain.

    Lastly, if your going to post code, I'll ask you again to use the code tags that have been spoken about in previous posts.

    Now just cut/paste this complete lot into a new file, and compile it. Run it, and ensure it works, then if you want, compare it line for line with your previous version to see where the differences are.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h> /* sets up 2 strings so names can be used */
    char	oxo[4][4];
    void	display_grid(void);
    
    /*
     =================================================
     =================================================
     */
    
    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("0's 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 */
    			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("X's 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 ((rx < 1) || (rx > 3) || (cx < 1) || (cx > 3))
    					{
    						printf("Out of range grid is 3x3, enter 1-3, try again\n");
    						continue;	/* go back to top of infinite loop */
    					}
    
    					if (oxo[rx][cx] == 'X' || oxo[rx][cx] == '0')
    					{
    						printf("Cell already taken\n");
    						continue;	/* go back to top of infinite loop */
    					}
    					break;			/* leave the infitite loop */
    				}
    
    				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 */
    			}
    		}
    
    		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(void)
    {
    	/*~~~~~~~~~*/
    	int row, col;
    	/*~~~~~~~~~*/
    
    	system ("clear");
    	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 all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Unregistered
    Guest

    Sorry

    Im ever so sorry, Salem & Hammer about not using the code tags, I wasnt too sure what u was talking about, I know that is no excuse, and from now on it will never happen again.

    Just noticed 1 glitch with the code, I really really appreciate ur help and dedication Hammer, I would understand if u never wanted to hear from me again but when asked for a co-ordinate, if users enter e.g 99, the program crashes. If you can help it would b grt.

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    .... and I thought I'd seen the last of this one!

    Anyways, I've checked the code I posted and can't find anything obvious that would cause a crash like that. The most likely cause would be a number greater than 3 being used to index the array. However, we have if ((rx < 1) || (rx > 3) || (cx < 1) || (cx > 3)), so we should be trapping this OK. (there's also a similar if statement for r0 and c0).

    I run the code as compiled by 2 different compilers, and can't make it crash in that manner.
    Code:
     - | - | - |
     ---------------
     - | - | - |
     ---------------
     - | - | - |
     ---------------
    0's Enter next move (two numbers): 99 9
    Out of range grid is 3x3, enter 1-3, try again
    0's Enter next move (two numbers):
    Did you cut/paste the complete source I gave you on my last post, or did you try and integrate my source with yours?
    Can you give more details about the steps you go through to make it crash.
    Just out of interest, what compiler and OS are you running?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Unregistered
    Guest
    Yes, I cut n pasted the code u provided, it crashes wen the user enters e.g. 12 (without a space between the co-ordinates), well it doesnt crash, it just goes to the next.

  6. #21
    Unregistered
    Guest
    Yes, I cut n pasted the code u provided, it crashes wen the user enters e.g. 12 (without a space between the co-ordinates), well it doesnt crash, it just goes to the next.

    I was also experienting, to try and make my program more user friendly, so wen the user chooses valid co-ordinates, the program prints:

    printf("the co-ordinstes u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed?\n", &r0 &c0);

    and givin the user a yes/no option, if the user chooses no, then it loops back to:

    printf("0's Enter next move (two numbers): ");

    but, wen i tried this, every time i pressed no, it carried on neway, is it possible to loop bak????

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >it crashes wen the user enters e.g. 12 (without a space between the co-ordinates), well it doesnt crash, it just goes to the next.
    I presume you mean it goes to the next line. That would be correct, you haven't yet entered two numbers, only one (twelve). The program requires the numbers to be split by white space (space key, tab, enter etc). Therefore, this is normal behaviour.

    >printf("the co-ordinstes u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed?\n", &r0 &c0);
    Change &r0 &c0 to r0, c0

    >but, wen i tried this, every time i pressed no, it carried on neway, is it possible to loop bak?
    Yes. You need something like this:
    Code:
    printf("the co-ordinates u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed?\n", r0, c0);
    {
    	int c;
    	getchar();
    	c = getchar();
    	if (c != 'y' && c != 'Y')
    		continue;
    }
    Put this after the if statement that is handling the "Cell already taken" bit. In other words, just before the break; line.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Unregistered
    Guest
    This (below) is what I did, and there was no sign of it, didnt print, and didnt ask y/n..

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h> /* sets up 2 strings so names can be used */
    char	oxo[4][4];
    void	display_grid(void);
    
    /*
     =================================================
     =================================================
     */
    
    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("0's 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 */
    				printf("the co-ordinates u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed? Y/N\n", r0, c0);
    				{
    				int c;
    				getchar();
    				c = getchar();
    				if (c != 'y' && c != 'Y')
    				continue;
    				}				}
    				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 */
    			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("X's 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 ((rx < 1) || (rx > 3) || (cx < 1) || (cx > 3))
    					{
    						printf("Out of range grid is 3x3, enter 1-3, try again\n");
    						continue;	/* go back to top of infinite loop */
    					}
    
    					if (oxo[rx][cx] == 'X' || oxo[rx][cx] == '0')
    					{
    						printf("Cell already taken\n");
    						continue;	/* go back to top of infinite loop */
    					printf("the co-ordinates u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed? Y/N?\n", r0, c0);
    {
    	int c;
    	getchar();
    	c = getchar();
    	if (c != 'y' && c != 'Y')
    		continue;
    }
    					}
    					break;			/* leave the infitite loop */
    				}
    
    				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 */
    			}
    		}
    
    		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(void)
    {
    	/*~~~~~~~~~*/
    	int row, col;
    	/*~~~~~~~~~*/
    
    	system ("clear");
    	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;
    }

  9. #24
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You almost got it right
    You have added the 2 new sections to the inside of an if statement, they should be just after it.

    Have another go

    Here is what your code should look like when you've corrected it: This includes code above and below the new bit, to show you how it fits in.
    Code:
    if (oxo[r0][c0] == 'X' || oxo[r0][c0] == '0')
    {
    	printf("Cell already taken\n");
    	continue;	/* go back to top of infinite loop */
    }
    printf("the co-ordinates u have chosen are %d and %d, are you sure this is the position u would like a '0' to be placed?\n", r0, c0);
    {
    	int c;
    	getchar();
    	c = getchar();
    	if (c != 'y' && c != 'Y')
    		continue;
    }
    break;			/* leave the infitite loop */
    Take careful note of the {'s and }'s.
    Congrats for using code tags, btw!
    I'm off for a lye down now, damn I think I need it. If you get stuck, maybe someone else will help you, otherwise, I'll be back later sometime.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #25
    Unregistered
    Guest

    Thumbs up

    Yea, worked grt, thx 4 all ur help!!!!!!! Cant thank u enuff, thanx for being patient with me.

    Take careful note of the {'s and }'s.
    Congrats for using code tags, btw!
    I'm off for a lye down now, damn I think I need it. If you get stuck, maybe someone else will help you, otherwise, I'll be back later sometime.
    Yea, the tags!! Yayyyy!!
    Yea, noticed ur from UK, wat a way 2 spend bank holiday....thanx again!!!

  11. #26
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Yea, worked grt, thx 4 all ur help!!!!!!! Cant thank u enuff, thanx for being patient with me.
    No problem, glad you finally got there, and hope you've learnt something along the way!

    >Yea, the tags!! Yayyyy!!
    Finally !!

    >Yea, noticed ur from UK, wat a way 2 spend bank holiday
    My favourite way, working on my PC (sad I know).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #27
    Unregistered
    Guest
    Got 1 tiny thing, well 2....
    ive tried to improve my code, to make it more user friendly:

    Instead of asking the user for vertical and horizontal coordinates together, I have decided to ask user for vertical coordinate first then horizontal coordinate (my code below does not work, ive a few things, but have been unsuccessful)

    Also, I would like to when this line appears on the screen:

    printf("the co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N? Press Q to quit game\n", rx, cx);

    but aint sure, how to incorporate this into my code?? To add this quit option...can ne1 help??

    Heres the code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h> /* sets up 2 strings so names can be used */
    char	oxo[4][4];
    void	display_grid(void);
    
    /*
     =================================================
     =================================================
     */
    
    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"); /* clears the screen */
    
    	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);	/* prints 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);	/* prints out */
    
    	system("clear"); /* clears screen */
    
    	display_grid();
    
    	count = 0;
    
    	while (count < 9)
    	{
    		if (count < 9)			
    		{
    			while (1)
    			{					/* start infinite loop, exits when user enters valid cell. */
    				printf(" %s (0's) enter vertical coordinate: ", name1); /* prints out */
    				while (scanf("%d", &r0) != 1)
    				{
    					printf("%d 1 number was not entered, please try again\n", name1);
    					getchar();	/* needed to remove non-numeric character from input stream, I believe */
    				}
    
    				{					/* start infinite loop, exits when user enters valid cell. */
    				printf(" %s (0's) enter horizontal coordinate: ", name1); /* prints out */
    				while (scanf("%d", &c0) != 1)
    				{
    					printf("%d, 1 number was not entered, please try again\n", name1);
    					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, vertical then space bar then horizontal co-ordinate, try again\n");
    					continue;	/* go back to top of infinite loop */
    				}
    
    				if (oxo[r0][c0] == 'X' || oxo[r0][c0] == '0')
    				{
    					printf("Cell already taken, please try again\n");
    					continue;	/* go back to top of infinite loop */
    				}
    				printf("The co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N\n", r0, c0);
    				{
    					int c;
    					getchar();
    					c = getchar();
    					if (c != 'y' && c != 'Y')
    						continue;
    				}
    				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 */
    			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(" %s (X's) enter vertical coordinate: ", name2);
    					while (scanf("%d", &rx) != 1)
    					{
    						printf("%d, 1 number was not entered, please try again\n", name2);
    						getchar();	/* needed to remove non-numeric character from input stream, I believe */
    					}
    					printf(" %s (X's) enter horizontal coordinate: ", name2);
    					while (scanf("%d", &cx) != 1)
    					{
    						printf("%d, 1 number was not entered, please try again\n", name2);
    						getchar();	/* needed to remove non-numeric character from input stream, I believe */
    
    					if ((rx < 1) || (rx > 3) || (cx < 1) || (cx > 3))
    					{
    						printf("Out of range grid is 3x3, enter 1-3 vertical then space bar then horizontal co-ordinate, try again\n");
    						continue;	/* go back to top of infinite loop */
    					}
    
    					if (oxo[rx][cx] == 'X' || oxo[rx][cx] == '0')
    					{
    						printf("Cell already taken\n");
    						continue;	/* go back to top of infinite loop */
    					}
    					printf("the co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N?\n", rx, cx);
    					{
    						int c;
    						getchar();
    						c = getchar();
    						if (c != 'y' && c != 'Y')
    							continue;
    					}
    					break;			/* leave the infitite loop */
    				}
    
    				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 */
    			}
    		}
    
    		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(void)
    {
    	/*~~~~~~~~~*/
    	int row, col;
    	/*~~~~~~~~~*/
    
    	system ("clear");
    	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;
    }

  13. #28
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    Talking O & X

    Hi,
    This is how I did it, its very simple , but it works.

    Cheers
    Q
    Attached Files Attached Files
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  14. #29
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Unregistered: in your last post there's a few minor things wrong.

    >printf("%d, 1 number was not entered, please try again\n", name2);
    The %d should be %s as you are dealing with a char variable. There are 4 of these statements to be changed.

    >{ /*Start infinite loop, exits when user enters valid cell. */
    There are 3 lines like this, there should only be 2. You have introduced an extra { into the prog. The one to remove is the one that is one line above ...(0's) enter horizontal coordinate....

    >Another missing }
    This time after
    printf("%d, 1 number was not entered, please try again\n", name2);
    getchar();
    } <-- add this bracket back in

    Thats the syntax bugs done, now to add the checking for quit, do this:
    Find the lines that are checking for y or Y. Add this line before each one:
    if (c != 'q' && c != 'Q') return (0);
    You'll find there's 2 of these to be added.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #30
    Unregistered
    Guest

    Unhappy

    This is what the code looks like (below), program runs, but wen the question:

    printf("the co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N?

    is displayed, if y is entered the program exits, if n is entered the program exits, which shud not happen, if n is entered the program shud loop bak and ask the user again, if y is entered it shud input on grid and display...I did exactly what u said Hammer, can u see what is goin wrong??

Page 2 of 3 FirstFirst 123 LastLast
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, 10: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, 01:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21