Thread: Storing matrix positions and using them in a struct?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    36

    Storing matrix positions and using them in a struct?

    Hey guys long time no see, i was hoping you could help me with this Homework/ tic-tac-toe game im doing, i just erased what i had because i got so frustrated but no worries i still have work to show but im really not asking someone to write any code, im currently reading and looking for info on structs in "the art and science of C" by Eric S. Roberts but what he says just confuses me more can someone tell me how i can use a struct to read in a matrix for the tic tac toe board positions (that i have now converted to a decimal system and then i should be able to figure our how i can use a struct to read in player info and i'll worry about checking for a winner after each turn later but im thinking i can just use a function call after each turn checking for all possible sequences of the winner? if i could just get some tips in the right way to use a struct more so than what i am doing right now that would be great, thanks guys and or girls

    So here's the remainder of my code that i liked and was at least some what confident in
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main()
    
    {
    printf("_1_|_2_|_3_\n_4_|_5_|_6_\n_7_|_8_|_9_\n");
    
    
    
    
    
    return 0;
    }
    
    struct player1{
    
    int a;
    printf("enter position number");
    scanf("%d", a);
    
    }x;
    
    struct player2{
    
    int a;
    printf("enter position number");
    scanf("%d", a);
    
    }y;
    again thanks guy but i do just wanna make it clear because i know there isn't muchcode left, i am not looking for someone to do my hw just looking for some tips to speed things up as i look for answers in my books
    Have a good one, Litzkrieg

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't put any code inside a struct definition. A struct is a way to make a collection of different types of variables, and pass them around as a single thing. We have a tutorial here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy. And here's a sample declaration of a struct type to contain a player's name and array of moves:
    Code:
    #define MAX_MOVES   5  // you can make at most 5 moves in a tic-tac-toe game
    #define MAX_NAME    20  // if your name is more than 20 chars long, too bad!
    
    struct player {
        char name[MAX_NAME + 1];  // +1 for the null terminator
        int moves[MAX_MOVES];  // store up to 5 moves
        int n_moves;  // keep track of how many moves have been made so far
    };
    
    struct player p1, p2;  // declare two player structs, p1 and p2
    That plus some tutorials should get you started. Maybe look for another book if that one is too confusing.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Note: In the above, I was assuming you would store the matrix coordinate as an integer from 0-8 or 1-9. You could also create a struct to store x-y coordinates if you wanted, and make an array of those. I should have also mentioned that you should put your struct definitions between the #include files and your functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing # from .dat file in Matrix using Pointers
    By dakarn in forum C Programming
    Replies: 5
    Last Post: 11-29-2008, 06:46 PM
  2. storing a matrix
    By cdkiller in forum C++ Programming
    Replies: 9
    Last Post: 03-25-2006, 08:07 PM
  3. storing into an ARRAY OF STRUCT
    By jave in forum C Programming
    Replies: 5
    Last Post: 10-23-2005, 02:25 AM
  4. help plz~~~~~~storing a matrix in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-05-2002, 12:04 AM
  5. storing struct ??!!¡Ö¡Õ
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-24-2001, 09:23 AM