Beth Ann Meredith
Beth Ann Meredith

Reputation: 191

Error on code for touchlistener - Android

I am a newbie here at Android, I am having trouble with this code block for touchListener for a TicTacToe game following an online tutorial.

The error I keep getting is:

The operator && is undefined for the argument type(s) boolean, void

The following code is located in the MainActivity.java. I get this error on the line highlighted with stars below:

    // Listen for touches on the board
private OnTouchListener mTouchListener = new OnTouchListener() { 
public boolean onTouch(View v, MotionEvent event) { 
// Determine which cell was touched 
int col = (int) event.getX() / mBoardView.getBoardCellWidth(); 
int row = (int) event.getY() / mBoardView.getBoardCellHeight(); 
int pos = row * 3 + col; 
    if (!mGameOver && setMove(TicTacToeGame.HUMAN_PLAYER, pos)) { //*****************


    // If no winner yet, let the computer make a move
    int winner = mGame.checkForWinner();
    if (winner == 0) { 
        mInfoTextView.setText(R.string.turn_computer); 

        setMove(TicTacToeGame.COMPUTER_PLAYER, pos);
        winner = mGame.checkForWinner();
    }   

 } 
return false; 
 } 
};

I think this is because the setMove() is void in the TicTacToeGame.java:

public void setMove(char player, int location) {
    if (location >= 0 && location < BOARD_SIZE &&
        mBoard[location] == OPEN_SPOT) {
        mBoard[location] = player;

    }

}

I have followed the tutorial precisely http://www.harding.edu/fmccown/classes/comp475-s10/tic-tac-toe-graphics.pdf

I would be very grateful of any help.

Many thanks,

Beth Ann

Upvotes: 0

Views: 90

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82563

In the PDF you linked to, setMove() has a boolean return type (Page 5, top):

private boolean setMove(char player, int location) { 
    if (mGame.setMove(player, location)) { 
        mBoardView.invalidate(); // Redraw the board
        if (player == TicTacToeGame.HUMAN_PLAYER) 
            mHumanMediaPlayer.start(); 
        else
            mComputerMediaPlayer.start(); 
        return true; 
    } 
    return false; 
}

Upvotes: 2

Related Questions