Thread: Arrays

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    41

    Arrays

    I have a an array tic_tac_toe[9] with numbers 0-8 in the array. I want to get an user input and replace it with one of the humbers within the array and then display the array via the print_board function which prints the array. How would i do that. This is what i have but the arry doesnt reflect the new user inputed value.
    Code:
    void user_move(void)
    {
            char choice;
            static int i;
            printf("enter what box number that you\n");
            printf("would like to place you move in> \n");
            scanf("%d",&i);
    if (i < 0 || i > 8)
    {
                    printf("invalid move\n");
    }
    else
            tic_tac_toe[i] = 9;
            print_board();
    }
            
    void print_board(void)
    {       
            char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'};
    
            printf("  %c | %c | %c\n", tic_tac_toe[0], tic_tac_toe[1], tic_tac_toe[2]);
            printf("----+---+----\n");
            printf("  %c | %c | %c\n", tic_tac_toe[3], tic_tac_toe[4], tic_tac_toe[5]);
            printf("----+---+----\n");
            printf("  %c | %c | %c\n", tic_tac_toe[6], tic_tac_toe[7], tic_tac_toe[8]);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'};
    I don't see any way to change those numbers, since you go straight into printf. You have to communicate between functions. Pass that tic_tac_toe that you fiddled with in user_move off to print_board.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A variable defined in one function does not exist in another function. You must pass such a variable as an argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    To pass the tic_tac_toe[] i would need to get rid of the decliration in the function print_board and pass it to print_baord after it's changed in the function use_move?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    int handle_keys(char tic_tac_toe[NUMBER_OF_SQUARES]);
    int print_board(char tic_tac_toe[NUMBER_OF_SQUARES]);
    
    int main(void)
    {
       char tic_tac_toe[NUMBER_OF_SQUARES];
    
       while(handle_keys(tic_tac_toe))
       {
           print_board(tic_tac_toe);
       }
    }
    You can go ahead and define handle_keys() and print_board().

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >else
    >        tic_tac_toe[i] = 9;
    >        print_board();
    >}
    You are storing char in tic_tac_toe[], and also using &#37;c to print the board, so to see a 9, you would need to assign tic_tac_toe[i] = '9'.

    You could also change the printf()'s to use a %d. In this case what you have above will work. However you would need to initialize the board like this:
    Code:
            char tic_tac_toe[NUMBER_OF_SQUARES]={0, 1, 2, 3, 4, 5, 6, 7, 8};
    In which case you might as will make it an int array, unless memory is a problem:
    Code:
            int tic_tac_toe[NUMBER_OF_SQUARES]={0, 1, 2, 3, 4, 5,6, 7, 8};

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Ok here is the program. I'm trying to put the user's move, either X or O in the array postion that they chose and display the board again with the X or O in that positon. It isn't working though.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    #define NUMBER_OF_SQUARES 9 
    
    char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'}; 
    void print_board(char*tic_tac_toe); 
    void clear_board(); 
    char get_user_character(char); 
    void user_move(); 
    
    int main() 
    { 
    int use_move; 
    char choice; 
    char tic_tac_toe_board; 
    char user_representation; 
    
    clear_board(); 
    print_board( tic_tac_toe); 
    get_user_character(choice); 
    user_move(); 
    print_board(tic_tac_toe); 
    return 0; 
    } 
    
    
    void clear_board(void) 
    { 
    int i; 
    for (i = 0; i < 9; i++) 
    { 
    tic_tac_toe[i] = ' '; 
    } 
    } 
    
    void print_board(char *tic_tac_toe) 
    { 
    
    printf("  &#37;c | %c | %c\n", tic_tac_toe[0], tic_tac_toe[1], tic_tac_toe[2]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[3], tic_tac_toe[4], tic_tac_toe[5]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[6], tic_tac_toe[7], tic_tac_toe[8]);        
    } 
    
    char get_user_character(char choice) 
    { 
    printf("play with X or play with O\n"); 
    scanf("%c", &choice); 
    return (choice); 
    } 
    
    void user_move(void) 
    { 
    char choice; 
    static int i; 
    printf("enter what box number that you\n"); 
    printf("would like to place you move in> \n"); 
    scanf("%d",&i); 
    if (i < 0 || i > 8) 
    { 
    printf("invalid move\n"); 
    } 
    else 
    tic_tac_toe[i] = choice; 
    }
    my output is:
    | |
    ----+---+----
    | |
    ----+---+----
    | |
    play with X or play with O
    X
    enter what box number that you
    would like to place you move in>
    3
    | |
    ----+---+----
    | |
    ----+---+----
    | |
    Kinda wierd. can't figure out how to pass the X or O into the array and display it correctly.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    i changed my user_move to char user_move(char); and the function to char user_move(char choice)
    now my output is:

    | |
    ----+---+----
    | |
    ----+---+----
    | |
    play with X or play with O
    X
    enter what box number that you
    would like to place you move in>
    3
    | |
    ----+---+----
    &#194; | |
    ----+---+----
    | |


    It puts the character in the space where i want it but it puts an arbirtrary character not X.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    proper indentation never hurts

    Code:
    char choice;
    this var in user_move function is not initialized and contains garbage. It has nothing to do with the var in the main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put your TTT board in code tags to enable constant width font.

    Code:
     X         
    ---|---|---
         O
    ---|---|---
             X
    ---|---|---
    Doesn't seem to work quite right, but it's WAY better than not using code tags.

    Did you change your scanf()'s from &#37;d to %c for the char's?
    Last edited by Adak; 04-12-2008 at 01:54 PM.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    I changed my scanf()'s to &#37;c when i did that it doesnt allow me to input an positon ont he board. it bypasses the below code and prints invalid move and the new board....what could be wrong with that code?
    Code:
            scanf("%c", &i);

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    position is a number - so it should be read into int using &#37;d format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM