Thread: stuck with how to solve an issue/back track moves

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

    stuck with how to solve an issue/back track moves

    I have an issue with my checkers game that im not sure of the best way to solve. Because of the for loop it will always favour taking the 1 1 square up and 1 square to the left in the case of white (1 square down and 1 to the right if black's turn). I have managed to code it so that if the "wrong choice" was taken at the start of the chain it can start from the beginning and i mark the original piece taken as an empty square. however that doesn't work if either a the original piece taken was correct and it came off the "path" else where or b it the original piece was wrong and it made the wrong turn elsewhere on the path.

    this is what i have so far
    Code:
    bool take_piece(char board[][ROW_MAX + 1][MESSAGE], bool is_king, int piece_coordinate_x, int piece_coordinate_y,
                                                        int move_coordinate_x, int move_coordinate_y, int current_player)
    {
        int x_coordinate, y_coordinate, multiplyer_x, multiplyer_y = current_player ? -1 : 1;
        char temp_board[COLUMN_MAX + 1][ROW_MAX + 1][MESSAGE];
        char *p_player_letter = current_player ? "b" : "w", *p_piece_letter = current_player ? "w" : "b";
        char *p_king_letter = current_player ? "W" : "B";
        bool found_piece_to_take = false;
    
        for (y_coordinate = ROW_MIN; y_coordinate <= ROW_MAX; y_coordinate++)
        {
            for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
            {
                strcpy(temp_board[x_coordinate][y_coordinate], board[x_coordinate][y_coordinate]);
                //printf("x is %d y is %d board is %s temp is %s\n", x_coordinate,y_coordinate, board[x_coordinate][y_coordinate], temp_board[x_coordinate][y_coordinate]);
            }
        }
        x_coordinate = piece_coordinate_x;
        y_coordinate = piece_coordinate_y;
        while (1)
        {
            for (multiplyer_x = -1; multiplyer_x <= 1; multiplyer_x += 2)
            {
                if ((strcmp(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], p_piece_letter) == 0) ||
                    (strcmp(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], p_king_letter) == 0))
                {
                    strcpy(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], EMPTY_SQUARE);// mark it empty so not found next time round
                    if (off_board((x_coordinate + (2 * multiplyer_x)), (y_coordinate + (2 * multiplyer_y))) == false)
                    {
                        if (valid_square(board, (x_coordinate + (2 * multiplyer_x)), (y_coordinate + (2 * multiplyer_y))) == true)
                        {
                            strcpy(temp_board[x_coordinate + (1 * multiplyer_x)][y_coordinate + (1 * multiplyer_y)], "t");
                            x_coordinate += 2 * multiplyer_x;
                            y_coordinate += 2 * multiplyer_y;
                            found_piece_to_take = true;
                            break;
                        }
                    }
                }
            }
            if (x_coordinate == move_coordinate_x && y_coordinate == move_coordinate_y)
            {
                break;
            }
            if (multiplyer_x >= 3)
            {
                if (found_piece_to_take == false)
                {
                    break;
                }
                else
                {
                    for (y_coordinate = ROW_MAX; y_coordinate >= ROW_MIN; y_coordinate--)
                    {
                        for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
                        {
                            if (strcmp(temp_board[x_coordinate][y_coordinate], "t") == 0)
                            {
                                strcpy(temp_board[x_coordinate][y_coordinate], EMPTY_SQUARE);
                                found_piece_to_take = false;
                                printf("\"x is %d y is %d board is %s temp is %s\"\n", x_coordinate,y_coordinate, board[x_coordinate][y_coordinate], temp_board[x_coordinate][y_coordinate]);
                            }
                        }
                    }
                    x_coordinate = piece_coordinate_x;
                    y_coordinate = piece_coordinate_y;
                }
            }
        }
        if (found_piece_to_take == true)
        {
            for (y_coordinate = ROW_MAX; y_coordinate >= ROW_MIN; y_coordinate--)
            {
                for (x_coordinate = COLUMN_MIN; x_coordinate <= COLUMN_MAX; x_coordinate++)
                {
                    if (strcmp(temp_board[x_coordinate][y_coordinate], "t") == 0)
                    {
                        update_board_array(board, EMPTY_SQUARE, x_coordinate, y_coordinate);
                    }
                }
            }
            update_board_array(board, EMPTY_SQUARE, piece_coordinate_x, piece_coordinate_y);
            update_board_array(board, p_player_letter, move_coordinate_x, move_coordinate_y);
            return true;
        }
        return false;
    }
    the way i see it is at the start of each single jump in the chain have two choices left and right. maximum amount of legal jumps on a board 8 x 8 is 3 so a total maximum of 8 choices (2^3). so either i can have some sort of array that logs the co-ordinates of the jumps and somehow backtracks through them. or somehow automatically calculating every route possible from point a to b in increments of 2 to both x and y co-ordinates stores them somehow then checks them off against the board to see if pieces are there to take.

    i think the latter is the better idea because when i come to deal with kings the possibilities increase dramatically so to try and predict how big the array would have to be is near impossible (i suppose the maximum number of pieces is 12 so 2^12)

    any advice greatly appreciated
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    is this even possible or am i banging my head against the wall expecting it not to hurt the next time
    coop

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    well after banging my head against the wall for 2 days all i have come up with is this and it still doesn't work it finds one route through then says there aren't any more "branches to find" when there is at least one more at the beginning of the route which should lead to it finding another 2 routes.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int board[8][8], start_x_coordinate, start_y_coordinate, finish_x_coordinate, finish_y_coordinate, temp_x, temp_y;
        int route[20][20][1], route_count = 0, move = 0, found_branch = 0;
        int a, x, y;
        int player = 0, multiplyer_y = player ? -1 : 1;
        int count_loop = 0;
    
        start_x_coordinate = 2;
        start_y_coordinate = 0;
        finish_x_coordinate = 4;
        finish_y_coordinate = 6;
        temp_x = start_x_coordinate;
        temp_y = start_y_coordinate;
        // inistalize board
        for (y = 7; y >= 0; y--)
        {
            for (x = 0; x < 8; x++)
            {
                if ((y % 2 == 0 && x % 2 == 0) || (y % 2 != 0 && x % 2 != 0))
                {
                    board[x][y] = 0; //good square
                    printf("%d ", board[x][y]);
                }
                else
                {
                    board[x][y] = 5; //invalid square
                    printf("%d ", board[x][y]);
                }
            }
            printf("\n");
        }
    
        // work out routes
        a = 1;
        while (a)
        {
            count_loop++;
            //check the diagonal squares are still on the board
            if (((temp_x - 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7)) &&
                ((temp_x + 2 >= 0 && temp_x + 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7)))
            {
                //record branch
                route[route_count][move][0] = 2;
                move += 1;
                //jump left
                route[route_count][move][0] = 0;
                temp_x -= 2;
                temp_y += 2 * multiplyer_y;
                move += 1;
            }
            else if((temp_x - 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7))
            {
                // jump left from whites point of view
                route[route_count][move][0] = 0;
                temp_x -= 2;
                temp_y += 2 * multiplyer_y;
                move += 1;
            }
            else if ((temp_x + 2 >= 0 && temp_x - 2 <= 7) && (temp_y + 2 * multiplyer_y >= 0 && temp_y + 2 * multiplyer_y <=7))
            {
                //jump right
                route[route_count][move][0] = 1;
                temp_x += 2;
                temp_y += 2 * multiplyer_y;
                move += 1;
            }
            else
            {
                repeat:
                if (move >= 0)
                {
                    for (x = move; x >= 0; x--)
                    {
                        if (route[route_count][x][0] == 2)
                        {
                            //last branch in the route found
                            found_branch = x;
                            break;
                        }
                    }
                    if (found_branch != 0)
                    {
                        temp_x = start_x_coordinate;
                        temp_y = start_y_coordinate;
                        move = 0;
                        for (x = 0; x <= found_branch; x++)
                        {
                            if (route[route_count][move][0] == 0)
                            {
                                temp_x -= 2;
                                temp_y += 2 * multiplyer_y;
                                move++;
                            }
                            else if (route[route_count][move][0] == 1)
                            {
                                temp_x += 2;
                                temp_y += 2 * multiplyer_y;
                                move++;
                            }
                            else
                            {
                                // just increment move to preserve branch record
                                move++;
                            }
                        }
                        // move is pointing at the move after the last branch so decrement move by 1 then jump right
                        route[route_count][move - 1][0] = 1;
                        temp_x += 2;
                        temp_y += 2 * multiplyer_y;
                        move += 1;
                    }
                    else
                    {
                        printf("no more branches\n");
                        break;
                    }
                }
                else
                {
                    printf("move was less than 1\n");
                    break;
                }
            }
            if (temp_x == finish_x_coordinate && temp_y == finish_y_coordinate)
            {
                printf("route found\n");
                found_branch = 0;
                route_count++;
                move = 0;
                goto repeat;
            }
        }
        printf("took %d goes to find a route\n", count_loop);
        return 0;
    }
    walking through a line at a time with the debugger proves that the first entry of route is a 2 it just doesn't find it.
    coop

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i give up every time i think i have cracked it and got it to work for specific co-ordinates i change the co-ordinates slightly (same moves just over and up one square it falls over and some odd thing happens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am stuck, can somebody help me solve this problem please
    By gameover6005 in forum C Programming
    Replies: 1
    Last Post: 03-23-2013, 07:29 AM
  2. What is error, kindly solve the issue and make me know my fault
    By kapil1089thekin in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2010, 11:27 PM
  3. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 02:02 AM
  4. Total moves
    By awkeller in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 11:24 AM
  5. I want to get back on the track..
    By Lameth in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2001, 05:30 AM

Tags for this Thread