Thread: TTT Problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    TTT Problem

    Here I have a ttt code:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int turn, move;
    char name1[25], name2[25];
    char board [9]; // The tic-tac-toe Board
    
    
    void DrawBoard();
    void DrawBoard() //* Draws the board *//
    {
    printf("\n\n");
    printf(" %c | %c | %c \n", board[1], board[2], board[3]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[4], board[5], board[6]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[7], board[8], board[9]);
    printf("\n\n");
    }
    
    void CheckSpace(); 
    void CheckSpace() // Checks for double usage and places X's and O's
    {
    	if (turn == 1)
    	{board[move] = 'X';}
    else
    	{board[move] = 'O';}
    }
    
    
    int main()
    
    {
    BOOL StillPlaying=TRUE;
    int move=0;
    int turn=1;
    
    printf("Welcome to tic-tac-toe!!!\n\n");
    
    //players names
    printf("What is player 1's name: ");	
    	scanf("%s", name1);
    printf("\nWhat is player 2's name: ");
    	scanf("%s", name2);
    	
    while (StillPlaying)
    	{
    		DrawBoard();
    
    	if (turn == 1){
    		printf("Please make a move %s : ", name1);
    		scanf("%d", &move);
    		turn=2;
    		printf("\n");
    		}
    	else{
    		printf("Please make a move %s : ", name2);
    		scanf("%d", &move);
    		turn=1;
    		printf("\n");
    	} 
    
    	
    CheckSpace();
    
    }//While Winner
    
    return 0;	
    
    }// int main()
    When I put the piece of code which assigns X's and O's in a function it seems to to see it or read it right. If I move the code in the while loop its okay.. Why is that

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Well look at this... Its same code but I moved the X and O assign code in the while loop:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int turn, move;
    char name1[25], name2[25];
    char board [9]; // The tic-tac-toe Board
    
    
    void DrawBoard();
    void DrawBoard() //* Draws the board *//
    {
    printf("\n\n");
    printf(" %c | %c | %c \n", board[1], board[2], board[3]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[4], board[5], board[6]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[7], board[8], board[9]);
    printf("\n\n");
    }
    
    void CheckSpace(); 
    void CheckSpace() // Checks for double usage and places X's and O's
    {
    }
    
    
    int main()
    
    {
    BOOL StillPlaying=TRUE;
    int move=0;
    int turn=1;
    
    printf("Welcome to tic-tac-toe!!!\n\n");
    
    //players names
    printf("What is player 1's name: ");	
    	scanf("%s", name1);
    printf("\nWhat is player 2's name: ");
    	scanf("%s", name2);
    	
    while (StillPlaying)
    	{
    		DrawBoard();
    
    	if (turn == 1){
    		printf("Please make a move %s : ", name1);
    		scanf("%d", &move);
    		turn=2;
    		printf("\n");
    		}
    	else{
    		printf("Please make a move %s : ", name2);
    		scanf("%d", &move);
    		turn=1;
    		printf("\n");
    	} 
    
    	
    {
    	if (turn == 1)
    	{board[move] = 'X';}
    else
    	{board[move] = 'O';}
    }
    
    }//While Winner
    
    return 0;	
    
    }// int main()
    and it works just fine... but why wouldn't it work in a function thing

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and this is still wrong:

    >>printf(" %c | %c | %c \n", board[7], board[8], board[9]);

    Again, access your arrays from 0-8, not 1-9.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    again it will not work!!!
    Last edited by librab103; 07-03-2003 at 07:30 PM.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Well it will work I just tried it but to choose the upper left corner you would have to pick 0 instead of 1. How un-natrual that would be
    Last edited by librab103; 07-03-2003 at 07:31 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by librab103
    Well it will work i just tried it but to choose the upper left corner you would have to pick 0 and not 1. How un natrual that would be
    It doesn't matter if it's unnatural, it's how arrays work in C. Either don't use arrays, or modify the input:

    "Enter a value from 1 to 9:"
    value_entered: 8

    value_entered -= 1;

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Originally posted by quzah
    It doesn't matter if it's unnatural, it's how arrays work in C. Either don't use arrays, or modify the input:

    "Enter a value from 1 to 9:"
    value_entered: 8

    value_entered -= 1;

    Quzah.
    I am not going to add all that if the compiler i am using and the C language doesn't give me any errors.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by librab103
    I am not going to add all that if the compiler i am using and the C language doesn't give me any errors.
    Well you're stupid then. No compiler will warn you about running out of the boundries of your array. Quite frankly, the C language lets you address any memory address you want to. It's up to you to not be stupid.

    Honestly, don't bother asking for help if you're going to ignore what people tell you on how to fix your code. You obviously don't care if it works right. You just want the appearance of it working right.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Yea i fix the array and assign ' ' to all board spaces

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM