-1

this is part of code in javascript for a Tic Tac Toe game. this part is checking win for diagonal in both directions. The diagonals work in X's winning but they do not work in O's winning. I basically wrote the same thing so I'm not sure what's the mistake I made. Did I make some syntactic mistake?

 function CheckWin_Diagonal(rowClicked, colClicked) {
        if (rowClicked == colClicked) {
            for (c = 0; c < rows; c++) {
                if (intBoard[c][c] != X_CLASS)
                    break;
                if (c == rows - 1) {
                    alert("PLAYER X WIN!!");
                    document.location.reload()
                }
            }
        }
        else {
            if (rowClicked == colClicked) {
                for (c = 0; c < rows; c++) {
                    if (intBoard[c][c] != CIRCLE_CLASS)
                        break;
                    if (c == rows - 1) {
                        alert("PLAYER O WIN!!");
                    }
                }
            }
        }
    }
    
    
    function CheckWin_Diagonal2(rowClicked, colClicked) {
        if (rowClicked + colClicked == cols - 1) {
            for (r = 0; r < cols; r++) {
                if (intBoard[r][(cols - 1) - r] != X_CLASS) 
                    break;
                
                if (r == cols - 1) {
                    alert("PLAYER X WIN!!");
                    document.location.reload()
                  
                }
            }
            
        }
        else {
            if (rowClicked + colClicked == cols - 1) { 
                for (r = 0; r < cols; r++) {
                    if (intBoard[r][(cols - 1) - r] != CIRCLE_CLASS)
                        break;
                    if (r == cols - 1) {
                        alert("PLAYER O WIN!!");
                    }
                }
    
            }
        }
        
    }
  • Please add the code that defines all the undefined variables in your code. – trincot Jan 18 '22 at 15:27
  • I wouldn't jump to the conclusion that you've made a *syntax error* unless you're actually seeing a *syntax error* in your browser's development console. Are you? If you're not seeing any error at all in the console then now is a good time to start [using a debugger](https://stackoverflow.com/q/25385173/328193) to step through the code as it executes, observe the runtime values of the variables, and identify the specific operation which is producing an unexpected result. – David Jan 18 '22 at 15:27

2 Answers2

2

I haven't seen the logic at all, but you are doing:

function CheckWin_Diagonal(rowClicked, colClicked) {
        if (rowClicked == colClicked) {
           /** logic **/
        }
        else {
            if (rowClicked == colClicked) {
                /** logic **/
            }
        }
    }

Obviously the code in the second if will never execute because it's the same condition as the first one (so it'll never get there).

You are basically saying:

IF SOMETHING HAPPENED DO THIS, OTHERWISE IF THE SAME THING HAPPENED DO THAT

So THAT will never happen, because you trapped the condition in the first IF and there's no OTHERWISE for the exact same condition

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • Thanks, this is the first code I write so I do not understand exactly how it works. So how can I creat a circle win without opening a new function? is there any simple way? – Roni ev Jan 18 '22 at 15:48
  • Suggestion: Instead of explicitly checking whether a given square is an X, check whether the squares all have the same value. If they do, then whatever that value is wins. And if that's unworkable somehow, pass in a third argument that indicates whether you're checking X or O. – ray Jan 18 '22 at 16:29
1

Your else blocks run the same if condition that triggered the else in the first place, guaranteeing they'll never run.

ray
  • 26,557
  • 5
  • 28
  • 27