Thread: which is the better way

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    which is the better way

    As the title suggests i have come up with 2 ways of doing something which is the better way of doing it considering program structior memory allocation and processing speed.

    first way
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    typedef struct
    {
        int position_x;
        int position_y;
        bool king;
        bool taken;
    }peice;
    
    int main()
    {
        peice white[12], black[12];
        int i, row = 0, collum = 0;
    
        for (i = 0; i < 12; i++)
        {
            white[i].position_x = collum;
            white[i].position_y = row;
            white[i].king = false;
            white[i].taken = false;
            collum += 2;
            if (collum == 8)//first row done
            {
                collum = 1;
                row = 1;
            }
            if (collum == 9)//second row done
            {
                collum = 0;
                row = 2;
            }
    //*  turns lines 36-38 on and off by commenting out
            printf("white's co-ordinates are: %d:%d\n", white[i].position_x, white[i].position_y);
            printf("white is king: %d\n", white[i].king);
            printf("white is taken:%d\n", white[i].taken);
    //*/
        }
        collum = 1;
        row = 7;
        for (i = 0; i < 12; i++)
        {
            black[i].position_x = collum;
            black[i].position_y = row;
            black[i].king = false;
            black[i].taken = false;
            collum += 2;
            if (collum == 9) //first row done
            {
                collum = 0;
                row = 6;
            }
            if (collum == 8)//second row done
            {
                collum = 1;
                row = 5;
            }
    //* turns lines 60-62 on or off by commenting out
        printf("black's co-ordinates are: %d:%d\n", black[i].position_x, black[i].position_y);
        printf("black's king is: %d\n", black[i].king);
        printf("black taken is: %d\n", black[i].taken);
    //*/
        }
        return 0;
    }
    2nd way

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    typedef struct
    {
        int position_x;
        int position_y;
        bool king;
        bool taken;
    }peice;
    
    int main()
    {
        peice white[12], black[12];
        int i, white_row = 0, white_column = 0, black_row = 7, black_column = 1;
    
        for (i = 0; i < 12; i++)
        {
            white[i].position_x = white_column;
            white[i].position_y = white_row;
            white[i].king = false;
            white[i].taken = false;
            black[i].position_x = black_column;
            black[i].position_y = black_row;
            black[i].king = false;
            black[i].taken = false;
            white_column += 2;
            black_column += 2;
            if (white_column == 8) // 1st rows done
            {
                white_column = 1;
                white_row = 1;
                black_column = 0;
                black_row = 6;
            }
            if (white_column == 9) // 2nd rows done
            {
                white_column = 0;
                white_row = 2;
                black_column = 1;
                black_row = 5;
            }
    //*
            printf("whites co-ordinates: %d:%d blacks co-ordinates: %d:%d\n", white[i].position_x, white[i].position_y, black[i].position_x, black[i].position_y);
            printf("white king: %d black king: %d\n",white[i].king, black[i].king);
            printf("white taken: %d black taken: %d\n",white[i].taken, black[i].taken);
    //*/
        }
    
        return 0;
    }
    all comments welcome
    coop

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Because white and black do not influence the other, and there does not seem to be any difference in the strategy between the two, I'd (currently) go with the second one.

    However, it really depends on where you want the code to go from here... What are you planning the programme to do? How do you want it to grow?

    Is there a way that you want the programme to behave? i.e. taking turns?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Not the same, only a demo showing you can improve your code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    enum board_piece { NONE=0, BLACK, WHITE };
    
    static void setup_checkers_board( enum board_piece * );
    static void draw_board( enum board_piece * );
    
    int main( void )
    {
      // The board is configured like this (without pieces):
      //
      //  this is index 0.
      //  |
      //  V
      //  █▒█▒█▒█▒
      //  ▒█▒█▒█▒█
      //  █▒█▒█▒█▒
      //  ▒█▒█▒█▒█
      //  █▒█▒█▒█▒
      //  ▒█▒█▒█▒█
      //  █▒█▒█▒█▒
      //  ▒█▒█▒█▒█
      //         ^
      //         |
      //         this is index 63.
      //
      // In a black background the upper left corner (index 0) is "white".
      enum board_piece board[64];
    
      setup_checkers_board( board );
      draw_board( board );
    
      return EXIT_SUCCESS;
    }
    
    void setup_checkers_board( enum board_piece *boardptr )
    {
      int i, j;
    
      // clear board.
      memset( boardptr, 0, 64 * sizeof *boardptr );
    
      for ( i = 0, j = 1; i < 12; i++, j += 2 )
      {
        if ( i == 4 ) j--;
        if ( i == 8 ) j++;
    
        boardptr[j] = BLACK;
        boardptr[63-j] = WHITE;
      }
    }
    
    void draw_board( enum board_piece *boardptr )
    {
      int i;
    
      fputs( "\x1b[2J\x1b[0;0H"      // clearscreen using ANSI (not Windows!).
             "Checkers board:\n"
             "+--------+\n"
             "|", stdout );
    
      for ( i = 0; i < 64; i++ )
      {
        if ( i && ! (i % 8) )
          fputs( "|\n|", stdout );
    
        switch (boardptr[i])
        {
          case WHITE: putchar('W'); break;
          case BLACK: putchar('B'); break;
          default: putchar(' ');
        }
      }
    
      puts( "|\n+--------+" );
    }
    I'm using this model for the board:

    which is the better way-untitled-jpg
    Last edited by flp1969; 04-24-2019 at 07:45 AM.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    lol is it that obvious that im making a game of checkers!! i must admit that i did pinch the idea from one of the tutorials i read on here using a checkers board as a way of visualising a [8][8] array.

    thanks flp thats helpfull

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Yes, it is that obvious that you're making a checkers game

    As for why flp1969's version is preferred, think about how the data is going to be used before you think about how you could represent it. When making any kind of board game, the thing you'll be doing most is looking up which pieces are on the board and where they are. You can store all your pieces in a list of all pieces on the board along with their positions and other information, but that would make things difficult. If all you want to know is if there is a piece at location 4,4, you'd have to look through all the pieces in two separate lists. Efficiency isn't really a concern here, this is 2019 and it's checkers, but you can usually tell you're using the wrong data structures if you're spending a lot of time and effort doing simple tasks. An 8x8 array of pieces would be much easier to work with than lists for a lot of reasons.

    To add to flp1969's code, it's missing one important aspect to tell if a certain move will be valid or not. You had it in your code, it's whether a piece has been kinged or not. You can start by replacing his enum with a piece structure that holds the color and whether the piece is a king. Or expand the enum to include RED_KING and BLACK_KING. You don't need the position of each piece, since its position in the 8x8 array will already represent that. From there, you can start building up. Write a function that determines whether a move is a valid move. For example, a move from 1,1 (that's second row from the top, second column) to 0,2. You need to look up what piece, if any, is at 1,1. Determine its color, decide which direction it should be able to move based on its color and whether it's a king, check if there's any piece already at 0,2 and return true or false. This one function will be the core of the checkers game, after that you just need to work on the UI and scoring.

Popular pages Recent additions subscribe to a feed

Tags for this Thread