Building TickTackToe

This is a discussion on Building TickTackToe within the C Programming forums, part of the General Programming Boards category; Hey, this is for a tick tack toe game I'm creating, basically so far what i'm trying to get it ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Building TickTackToe

    Hey, this is for a tick tack toe game I'm creating, basically so far what i'm trying to get it to do is check if the array is empty, so later i can fill the array with the persons symbol (x) or (O). But yeah, it asks for the users input, i enter a number, and I get mixed results, depending on the number I enter, even if the arrays are ALL empty..

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int *iPointer1;
    int *iPointer2;
    int x;
    int y;
    char cBoard[3][3] = {0};
    int CheckSquare(int);
    
    int main()
    {
    	int iSquare;
    	iPointer1 = &x;
    	iPointer2 = &y;
    
    	printf("Enter a square number <1-9>: ");
    	scanf("&#37;d", &iSquare);
    	if (CheckSquare(iSquare) == 0)
    	{
    		printf("\nValid Choice, square was empty!\n");
    	}
    	else
    	{
    		printf("Square being used or invalid number.\n");
    	}
    return 0;
    }
    
    int CheckSquare(int iSquare)
    {
    	int z = 0;
    	for (*iPointer1=1; *iPointer1 <= 3; (*iPointer1)++);
    	{
    		for (*iPointer2=1; *iPointer2 <= 3; (*iPointer2)++)
    		{ 
    			z++;
    			if (z == iSquare)
    			{
    				if (cBoard[(*iPointer1)-1][(*iPointer2)-1] == 0)
    				{
    					printf("EMPTY");
    					return 0;
    				}
    				else
    				{	
    					printf("\nFULL\n");
    					return 1;
    				}
    			}
    		}
    	}
    printf("I failed..");
    }
    Here is what happens:

    Code:
    [mag1x@localhost ~]$ ./wtf
    Enter a square number <1-9>: 1
    EMPTY
    Valid Choice, square was empty!
    [mag1x@localhost ~]$
    It says empty for 1, 2 and 3, but if I enter 4 - 9 it says:

    Code:
    Enter a square number <1-9>: 6
    I failed..Square being used or invalid number.
    [mag1x@localhost ~]$
    Last edited by pobri19; 05-27-2008 at 01:45 AM.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    It's because you have another "scanf" in the CheckSquare function. You need to enter another number before it will continue.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Oh hehe silly mistake, was changing around the functions etc, must have missed that one :P But now there is another problem. When enter the number, it say it's being used, even if it isn't.

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Can you rewrite your CheckSquare?

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by manav View Post
    Can you rewrite your CheckSquare?
    Sure, all fixed, added a more detailed description of the new problem too.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    2
    Code:
    for (*iPointer2=1; *iPointer2 <= 3; (*iPointer2)++)
    		{ 
    			z++;
    			if (z == iSquare)
    			{
    				if (cBoard[(*iPointer1)-1][(*iPointer2)-1] == 0)
    				{
    					printf("EMPTY");
    					return 0;
    				}
    				else
    				{	
    					printf("\nFULL\n");
    					return 1;
    				}
    			}
    		}
    The use of "for" in here only counts up to 3 times. If the number entered
    exceeds three, it no longer goes through the "EMPTY/FULL check";

    A possible solution is to omit the x&y variables and make an array of 9 intergers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-08-2009, 06:33 PM
  2. Undertaking Website Building Project and Application Software Development
    By Programmer168 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-21-2009, 05:44 AM
  3. GUI building, is RAD the way to go?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-21-2007, 10:36 PM
  4. Computer building resources
    By Pantheon in forum Tech Board
    Replies: 4
    Last Post: 09-07-2006, 06:25 PM
  5. Warning when building!
    By electrolove in forum C Programming
    Replies: 21
    Last Post: 02-12-2003, 09:39 PM

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