0

I am trying to display a dialog box in the hello Android Sudoku example but when I run the app nothing happens. In the game.java I check if the puzzle is solved like this

/******Check to see if the game is complete**/
   public boolean isSolved()
   {
       for (int element : puzzle) {
           if (element == 0) return false;
        }
        return true;           
   }

Then in the PuzzleView in the onKeyDown method i try to detect if isSolved is true and if it is display the dialog

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (game.isSolved() == true) {
        Intent i = new Intent(mActivity, Congratulations.class);
        getContext().startActivity(i);
    } else {
        Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event=" + event);
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
            select(selX, selY - 1);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            select(selX, selY + 1);
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            select(selX - 1, selY);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            select(selX + 1, selY);
            break;
        case KeyEvent.KEYCODE_0:
        case KeyEvent.KEYCODE_SPACE:
            setSelectedTile(0);
            break;
        case KeyEvent.KEYCODE_1:
            setSelectedTile(1);
            break;
        case KeyEvent.KEYCODE_2:
            setSelectedTile(2);
            break;
        case KeyEvent.KEYCODE_3:
            setSelectedTile(3);
            break;
        case KeyEvent.KEYCODE_4:
            setSelectedTile(4);
            break;
        case KeyEvent.KEYCODE_5:
            setSelectedTile(5);
            break;
        case KeyEvent.KEYCODE_6:
            setSelectedTile(6);
            break;
        case KeyEvent.KEYCODE_7:
            setSelectedTile(7);
            break;
        case KeyEvent.KEYCODE_8:
            setSelectedTile(8);
            break;
        case KeyEvent.KEYCODE_9:
            setSelectedTile(9);
            break;
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            game.showKeypadOrError(selX, selY);
            break;
        default:
            return super.onKeyDown(keyCode, event);
        }
        return false;
    }
    return false;

}

I am learning java and Android development so please any help as to where I am going wrong will be much appreciated. If anyone needs more information please just ask and I will put it in an edit section for the question.

4

2 回答 2

0

WillNZ this is not answer, just to show you how put the log

/******Check to see if the game is complete**/
   public boolean isSolved()
   {
       for (int element : puzzle) {
           if (element == 0) return false;
        }
        Log.d("TAG", " isSolved() is true");
        return true;           
   }

Run your application and see if you can see " isSolved() is true" in your logcat.

于 2012-08-30T02:32:19.647 回答
0

In the end the way I checked to see if the game was complete was to have this in the game.class

/****** Check to see if the game is complete **/
public boolean isSolved() {
    for (int element : puzzle) {
        if (element == 0)
            return false;
    }
    return true;
}

And also

public boolean checkIsSolved()
{
    //check if the game is complete after each valid move
    if (isSolved() == true) { 
        Intent i = new Intent(this, Congratulations.class); 
        startActivity(i);} 
        else
        {
            return false;
        }
    return false;
}
于 2012-09-20T03:18:58.787 回答