0

So in this CS50 problem you're given a Sudoku board filled with numbers from 0-9. 1-9 slots needs to be made unchangeable, 0 slots are rendered as '.', and can be changed to a number by hitting any numbers by moving your cursor with up down left right arrow over the slot then hitting any numbers from 1--> 9, you're allowed to delete numbers in the original 0 spots.

Screenshot of the board.

I've successfully implemented: Moving the cursor Prevent user from inputting numbers on slots without '.' Deleting numbers in general.(user can still delete numbers from the original board)

Since the original 2d array g.board[][] is changed when user make changes to the board, I copied g.board[][] to g.ogboard[][] and check whenever the user attempts to delete a if the spot the cursor is over(y,x value) on g.ogboard[][] is a originally a blank(0) if it is, change current spot on g.board to 0 else don't.

I've no idea why this method doesn't work, numbers on the orignal board can still be deleted. here's the snippets of code I mention and the entire .c file on pastebin.

Pastebin: https://pastebin.com/raw/hbSz2ypp

Snippet:

//fill g.ogboard
for(int i = 0; i<9; i++)
{
    for(int j = 0; j<9; j++)
    {
        g.ogboard[j][i] = g.board[j][i];
    }
}

  //handles 1-9 input
  case '1'...'9':
            if(g.board[g.y][g.x] == 0)
            {
                 g.board[g.y][g.x] = ch - '0';
            }
            draw_numbers();
            break;

        //handles delete
        case '0':
        case '.':
        case KEY_ENTER:
        case KEY_DC:
        case KEY_BACKSPACE:
            if(g.ogboard[g.y][g.x] == 0)
            {
                g.board[g.y][g.x] = 0;
                draw_numbers();
            }
            break;
0

1 Answer 1

1

So this is about Sudoku, part of Chapter 4, Module 2 of CS50 AP (not CS50x).

You made a mistake with for (int j = 0; j < 9; j++), you used i instead of j, so you accidentally copy only the first row.

Also, it's placed in the wrong position. At the time you copy, there's nothing to copy. You probably should do that after loading the board (from disk I assume?).

2
  • I spotted it and forgot to edit the post sorry! It still refuse to work though.
    – Phan Tom
    Feb 28, 2018 at 16:42
  • Oh my, can't believe it was that simple thank you!
    – Phan Tom
    Feb 28, 2018 at 20:02

You must log in to answer this question.

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