WDI-SEA / game-project-issues

0 stars 0 forks source link

Can a program work backwards? Suggestions #30

Closed iwaggoner closed 3 years ago

iwaggoner commented 3 years ago

What's the problem you're trying to solve?

I am doing sudoku-solver, my issue is that I keep inserting numbers that aren't correct and breaking my code later on

Post any code you think might be relevant (one fenced block per file)

**This is my get number that places a number into the board, it calls correctBoard function that see if board is correct**

     // has to pass num from getNewNum, board, and current position
    // then test the row position gives
    // then tests col position is in
    // then test box based on postion
    // if passes all test returns true
    // if fails any test returns false
    const correctBoard = (board, num, currentPos) => {

        // checks all rows
        for(let i = 0;i<board[0].length;i++){
            if(board[currentPos[0]][i] == num){
                console.log('found false in row')
                return false
            }
        }
        // checks all cols
        for(let i = 0;i<board[0].length;i++){
            if(board[i][currentPos[1]] == num){
                console.log('found false in col')
                return false
            }
        }
        var startX = Math.floor(currentPos[0]/3)
        var startY = Math.floor(currentPos[1]/3)
            // set up double loop to collect each non 0 element in postion box
            for(let i = 0;i<startX+3;i++){
                for(let j = 0;j<startY+3;j++){
                    if(board[i][j] == num){
                        console.log('found false in box')
                        return false
                    } 
                }
            }
        return true
    }

    // takes in board and postion, then trys to inscert numbers 1-9 into position
    // once correct board comes back true it sets that number into that location and breaks loop ends function
    const getNewNum = (board, pos) => {
        console.log('getting new num')
            for(let i = 1;i<10;i++){
                if(correctBoard(board, i, pos)){
                    console.log('adding num to array after test for correct was true')
                    console.log(i)
                    board[pos[0]][pos[1]] = i
                    break
                }
                else {
                    console.log('correctBoard was false for', i)
                }
            }
    } 

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

I am not seeing a error but my board inputs the wrong number ie. 1 instead of 4 so later on in the program the board can't be solved any suggestions I have looked at solved version of board

What is your best guess as to the source of the problem?

I am very unsure

What things have you already tried to solve the problem?

I have a lot of console logs, and I am setting it if it can find any solutions then it dose nothing other wise it breaks my program

timmshinbone commented 3 years ago

I'm interested in what those console logs are putting out, can you copy and paste their responses here?

iwaggoner commented 3 years ago

So I was able to move past this by creating a function called test that calls getNewNum, and then inside getNewNum I call test, but right now my program just sets everyting to zero at this time.

iwaggoner commented 3 years ago

This is solved, I have changed my entire program, now my program finds unique solutions for each cell and then repeats its self until the board is filled out