Thread: escape codes adding hidden characters after i remove them

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

    escape codes adding hidden characters after i remove them

    i have a function that adds escape codes to a string so it colours the piece selected red. if i try and move the piece to an invalid square or invalid move it then de-colours the piece
    Code:
    void colour_selected_piece(char board[][ROW_MAX + 1][MESSAGE], char code[], int current_player, int x, int y)
    {
    
        if (strlen(code) >= strlen(CLEAR_COLOUR))
        {
            //strcat(code, COLOUR_WHITE_PIECE);
            //printf(" %s", code);
    
            current_player ? update_board_array(board, strcat(code, COLOUR_BLACK_PIECE), x ,y) :
                             update_board_array(board, strcat(code, COLOUR_WHITE_PIECE), x, y);
        }
        else
        {
            strcpy(board[x][y], "test");
            current_player ? update_board_array(board, "b", x, y) : update_board_array(board, "w", x, y);
        }
        clear_screen();
        draw_board(board, current_player);
    }
    it all works as far as the screen says however if i try and select the piece again it says it isnt there. upon doing some strategic for loops and printf's i have discovered that the length of the string (should just be a single letter) at the given board location is 9. Obviously there is some escape code left over.

    how can i reset the length of the array for the string at that position to zero copying a string of "" didn't work
    many thanks
    coop

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to know the size of the "code" array inside the function you need to pass it into the function as a parameter. The strlen() function just gives you the length of the string not the size of the underlying array.

    Have you studied structures yet?


    How is board defined?

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    yes i have done structures i used them in my first verstion but had the board as an 8x8 array. this verstion i have done away with the structures of pieces and just stuck with the 8x8 array although it is now a 8x8x20 so i can house the escape codes on each square

    i dont think i am explaining my issue well
    Code:
     char myarray[8][8][20];
    
    strcpy(myarray[0][0], "w");
    printf("%lu", strlen(myarray[0][0]);
    should give the result of 1 yes??? but i get 9 because on a previous cycle of the function above i added escape codes to the string stored at myarray[0][0]
    many thanks
    coop

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    man i need shot. i dread to meet you kind people face to face you must think im an idiot lol. using CLEAR_CODE which is equivalent for the clear formating escape code and then comparing the length of the escape code to clear code is always going to be true it will never be less so my else statement never gets run.

    time for pizza i think im getting "code eyes"
    once again apologies for being dim
    coop

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    yes i have done structures i used them in my first verstion but had the board as an 8x8 array. this verstion i have done away with the structures of pieces and just stuck with the 8x8 array although it is now a 8x8x20 so i can house the escape codes on each squar
    You really should have stuck with the structure, for a couple of reasons.

    First if the "contents" of the square is contained in the structure you don't loose the size of the "COLOR" array when you pass that structure to a function.

    Second you can hold much more information much easier by just adding a new "field" in the structure.

    Third what happens if there is no piece at that location of the board, what would be it's COLOR?

    i dont think i am explaining my issue well
    If you want to know the sizes of your arrays you need to pass those sizes into the function as parameters.

    should give the result of 1 yes??? but i get 9 because on a previous cycle of the function above i added escape codes to the string stored at myarray[0][0]
    many thanks
    Since you used strcpy() the length of the string should be the same as the length of the string in the "source" of strcpy(). Remember strcpy() overwrites the content of the "destination". And note strlen() reports about the length of the string, not the size of the underlying array. If you try to stuff more information into the string than the array can hold the value returned from strlen() could return a value larger than the array, but you can't rely on this to happen since overflowing your array produces undefined behaviour.

    Remember that when you pass an array into a function you are actually passing a pointer, so you loose any "size" information.

    Code:
    void colour_selected_piece(char board[][ROW_MAX + 1][MESSAGE], char code[], int current_player, int x, int y)
    Why the addition for ROW_MAX ?

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    because its a 3d array for example 3x3x20 so to pass it as a parameter in a function i have to do char array [][3][20] yes?
    coop

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    sorry you wern't asking that.... i have rowmax defined as 7 because i use it to check piece dont move off the board

    however in hindsight it ould of been better to set it to 8 then subtract 1 from it where i need to

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes (there are also a couple of other syntax ie: *[3][20]) , but what good does that do you? You still loose the size of the array when you pass it into that function. You appear to want the size of the array, along with the length of the string. You need to insure that the length of your "new" string is small enough to fit into the size of your array.

    Question are you "modelling" a square on the board or a piece that is "in" a square on the board?

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i have an 8X8(x20) array each element of the 8x8 is one square on the board. so i can place either a w or a b in the desired element (with or without escape codes)

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i guess what i have is a grid of pointer each pointing to an array of up to 20 characters. the issue i was having is i look at what that array holds and compare it to either a w or a b. because my above function was wrong i had a strng of escape characters so when i compared it to "w" or "b" using a sttcmp it failed.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    i have an 8X8(x20) array each element of the 8x8 is one square on the board.
    What is the color of an unoccupied square?

    I really think you should be using a multidimensional array of "Pieces" not the array of char. A "Piece" can keep track of it's never changing color, wether it is a king/pawn, and whatever other information helps define a "Piece".

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    an empty "square" is a space. the only thing i was doing with colours was changing the colour of the piece selected on the screen. for example if i type in the co-ordinates 0,2 and there is a piece there i change it to red to show its been selected

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My point is that you should just be moving pieces on the board, not changing the color of the board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen with escape codes
    By cooper1200 in forum C Programming
    Replies: 4
    Last Post: 05-05-2019, 09:27 AM
  2. Escape Characters
    By GokhanK in forum C Programming
    Replies: 7
    Last Post: 02-24-2011, 08:01 PM
  3. Escape Codes
    By renurv in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2006, 02:01 PM
  4. Escape characters
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 05:38 PM
  5. Printer Escape Codes for Epson stylus color 460?
    By DanTheMan in forum C Programming
    Replies: 3
    Last Post: 02-14-2002, 10:30 PM

Tags for this Thread