IrfanAhmed
public class MainActivity extends AppCompatActivity {

int activePlayer = 0; //0 for player red
int[] gameState = {2,2,2,2,2,2,2,2,2}; //2 means unplayed
int[][] winningLocations = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
boolean gameOver = false;
LinearLayout winnerLayout;
TextView winnerMsg;

public void gameLogic(View view){

ImageView tappedView = (ImageView) view;
int tappedLocation = Integer.parseInt(view.getTag().toString());


if(gameState[tappedLocation] == 2 && !gameOver) {

gameState[tappedLocation] = activePlayer;

tappedView.setTranslationY(-3000f);

if (activePlayer == 0) {

tappedView.setImageResource(R.drawable.chipsr);
activePlayer = 1;

} else if (activePlayer == 1) {

tappedView.setImageResource(R.drawable.chipsy);
activePlayer = 0;

}

tappedView.animate().translationYBy(3000f).setDuration(500);
String msg = "";


for (int[] winningPosition : winningLocations){

if(gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2){

if(activePlayer == 0)
msg = "Yellow is Winner!";


if(activePlayer == 1)
msg = "Red is Winner!";

winnerLayout.setVisibility(View.VISIBLE);

winnerMsg.setText(msg);

gameOver = true;
break; ////break loop if found winning player do not checks other location
}

} if(!gameOver){
for (int i : gameState){

if(i == 2){
// check if gameState all values are 2 continue run game else if
// all values are other value lik 0,1 than set gameOver to true
gameOver = false;
break;
}
else gameOver= true;
}
///here again check after verify every value of gameState now gameOver is true
/// msg will be display
if(gameOver){
msg="It's Draw!";

winnerLayout.setVisibility(View.VISIBLE);


winnerMsg.setText(msg);
//gameOver = true;
}

}

}

}

public void playAgain(View view){


winnerLayout.setVisibility(View.INVISIBLE);

gameOver = false;
activePlayer = 0;

for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}

GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0; i < gridLayout.getChildCount(); i++)
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

/////////////////////******************///////////////////////
winnerLayout = (LinearLayout) findViewById(R.id.winnerLayaout);
winnerLayout.setVisibility(View.INVISIBLE);
winnerMsg = (TextView) findViewById(R.id.textView);
/////////////////////*********************///////////////////

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
Quote 1 0
ibrahim
MashaALLAH IrfanAhmed brother... 
even mujhy 1 solution mil gaya ap ki coding se... thanks jazak ALLAH
Quote 0 0

Add a Website Forum to your website.

 
Urdu IT Accademy � 2013 | License Agreement