Reputation: 401
I am creating Tic Tac Toe game.This is the click event- when a button is clicked set background image ==num1
public void onClick(View event) {
// TODO Auto-generated method stub
if(btn1 ==event){
btn1.setBackgroundResource(num1);
}
if(btn2 ==event){
btn2.setBackgroundResource(num1);
}
if(btn3 ==event){
btn3.setBackgroundResource(num1);
}
And the three buttons show the same image and the image is set like this
if(!(turn % 2 ==0)){
num1=(R.drawable.nought);
}
else
num1=(R.drawable.cross);
So that problem is now I need to compare these images to check if they are all the same. How do i do that?
Upvotes: 0
Views: 158
Reputation: 520
There might be a way to check what the backgrounds are, but why don't you just save the values in a 2D array? -1 for unmarked, 0 for O's, 1 for X's or whatever. It might be better performance wise too (not that I expect tic-tack-toe to be super intensive to begin with).
Upvotes: 1
Reputation: 181
here I think there is a similar question.
Who I compare an background Image resource TextView with a R.drawable.bg_image for switch
you can store the id of every button.
Is there something like button.getBackgroundResource()?
You can overwrite ImageButton class and add the id of the backgroundResource.
Upvotes: 0