0

I have no idea why this is going wrong this is what I have so far

void init(void) { // TODO int index = d * d - 1; for(int i=0; i < d; i++){ for(int j = 0; j < d; j++){ board[i][j] = index; index--; } } if(iseven(d)){ board[d-1][d-2] = 2; board[d-1][d-3] = 1; } }

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    // TODO
    for(int i=0; i < d; i++){
        for(int j = 0; j < d; j++){
            printf(" %2i |", board[i][j]);
        }
        printf("\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */

int DashI = 2;
int DashJ = 2;
bool move(int tile)
{
    // Find the numbers in the board and then take their positions
    int tileI;
    int tileJ;
    int DashI;
    int DashJ;
    bool tilefound = false;
    bool tilemoved = false;
    for(int i=0; i < d; i++){
        for(int j=0; j <d; j++){
            if(board[i][j] == tile){
                tileI = i;
                tileJ = j;
                tilefound = true;
            } else if(board[i][j] == 0){
                DashI = i;
                DashJ = j;
            }
        }
    }

    // Swap the positions of the tile and the dash.
    if(tilefound){
        if((DashI == tileI && abs(DashJ - tileJ) == 1) || (DashJ == tileJ && abs(DashI - tileI) == 1)){
            board[DashI][DashJ] = tile;
            board[tileI][tileJ] = 0;
            tilemoved = true;
            //printf("TILE POSITION: %i,%i | BLANK POSITION: %i,%i", DashI, DashJ, tileI, tileJ);
        }
    }


    if (tilefound && tilemoved){
        return true;
    }
    return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    // TODO
    int inorder = false;
    for(int i=0; i < d; i++){
        for(int j=0; j < d; j++){
            if(board[i][j] != board[d-1][d-1] && board[i][j] < board[i+1][j+1]){
                inorder = true;
            } else {
                inorder = false;
            }
        }
    }


    if (inorder){
        return true;
    } 
    return false;

}

bool iseven(int number){
    if (number % 2 == 0){
        return true;
    }
    return false;
}

I tried to use this input ./fifteen 3 < ~cs50/pset3/3x3.txt but it gives me illegal move everytime! Resulting output

0

Simplify! Think about init. You start with some number and assign each board location a number. Now think about won. It is sort of the opposite. You start with a number and test each board location for a number. You should be able to determine "order" using simple arithmetic; testing the value of neighboring tiles is complicating the issue. Don't forget that the last tile is the "oddball" :)

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .