Thread: Board Game

  1. #1
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28

    Board Game

    Hi, I'm supposed to make a program to solve a Board Game similar to a Chess Board. I want to store the information about pieces and their possible moves, during the game, in an integer matrix so each piece will have stored in an single integer all the information about it.

    The question is... how can I read/write small parts of an integer? ... something like... the first two digits will tell what kind of piece is occupying a particular position in the matrix, and the next three digits will tell the possible moves it can do.

    ty in advance...
    Last edited by Tiago; 04-10-2010 at 07:42 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you want "digits" or bits? If you want "digits", use simple division. Otherwise look into bitwise operations (they're in your book, and our FAQ).


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

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    the first two digits will tell what kind of piece is occupying a particular position in the matrix, and the next three digits will tell what possible moves does that piece have.
    I think a struct would be more appropriate in this situation, although it would use some more memory. If you're really concerned about memory usage you could use bitfields, but a normal struct ought to suffice.
    Consider this post signed

  4. #4
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    The goal is to have a really fast program, memory is also a problem since this matrix (the game board) can have any size, the program will also use tons of memory and time trying to find a solution.

    I'm going to use bitfields because uses less memory and should also do an efficient use of the CPU, and also the algorithm in the both cases (bitfields vs structures) should be similar, I believe.

    ty all...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Bitfields are great, they are not hard to use once you understand bit operations (which you might as well, if you are going to program in C), and are of course very very efficient both in terms of mem usage and ops. The standard library and most other APIs use them a lot.

    Code:
    #define FLAG1 1
    #define FLAG2 1<<1
    #define FLAG3 1<<2
    XOR, the "toggle" operator, is handy here. The most painful move is unsetting a bit if it is set:
    Code:
    int bitflags;
    [...]
    if (bitflags&SOMEFLAG) bitflags ^= SOMEFLAG;
    since you must check, you can't just zero a field regardless of whether it is set or not. Whereas |= is fine to set (on) without a check.
    Last edited by MK27; 04-10-2010 at 09:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 06:02 AM
  3. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM