Translate

Wednesday, 6 February 2013

TicTacToe Simplified - Android game

The UI update, in this basic example, done in the onClick method should be done in a separate thread (in the adapter or any middle layer). I

f you are doing an Android project at school or university, this is the simplest and quickest solution for you.






@TargetApi(Build.VERSION_CODES.HONEYCOMB)

@Override

public void onClick(View v) {

if (gameOver==false){
ImageCell c=(ImageCell)v;

if (playerId==1){
c.cells[c.cellNumber]=1;
gameOver=c.isWinner(c.cellNumber);
c.setImageResource(R.drawable.x);
c.setScaleType(ScaleType.

FIT_XY);
}

if (playerId==2){
c.cells[c.cellNumber]=2;
c.setImageResource(R.drawable.o_tic);
c.setScaleType(ScaleType.FIT_XY);
gameOver=c.isWinner(c.cellNumber);
}

playerId++;

if (playerId>2)playerId=1; 
moveCount++;


Toast.makeText(v.getContext(),

"Player "+ playerId +", your turn !",Toast.LENGTH_SHORT).show();
}

if (gameOver==true || moveCount>=9){
Toast.makeText(v.getContext(),

"Game over!",Toast.LENGTH_SHORT).show();
gameOver=true;
finish();
}

No comments: