Created
February 22, 2021 16:03
-
-
Save tiwarinaman/86cd7a08c85e9bfe4a1c9d6565b7e95b to your computer and use it in GitHub Desktop.
MainActivity code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.techienaman.tictactoe; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.ImageView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| public class MainActivity extends AppCompatActivity { | |
| boolean gameActive = true; | |
| // Player Representation: | |
| // 0 - X | |
| // 1 - O | |
| int activePlayer = 0; | |
| // State meaning: | |
| // 0 - X | |
| // 1 - O | |
| // 2 - Null | |
| int gameState[] = {2, 2, 2, 2, 2, 2, 2, 2, 2}; | |
| // Game winning positions | |
| int winPositions[][] = { | |
| {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, | |
| {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, | |
| {0, 4, 8}, {2, 4, 6} | |
| }; | |
| public void playerTap(View view) { | |
| ImageView img = (ImageView) view; | |
| int tappedImage = Integer.parseInt(img.getTag().toString()); | |
| if (!gameActive) { | |
| gameReset(view); | |
| } | |
| if (gameState[tappedImage] == 2) { | |
| gameState[tappedImage] = activePlayer; | |
| img.setTranslationY(-1000f); | |
| if (activePlayer == 0) { | |
| img.setImageResource(R.drawable.x); | |
| activePlayer = 1; | |
| TextView status = findViewById(R.id.status); | |
| status.setText("O's Turn - Tap To Play"); | |
| } else { | |
| img.setImageResource(R.drawable.zero); | |
| activePlayer = 0; | |
| TextView status = findViewById(R.id.status); | |
| status.setText("X's Turn - Tap To Play"); | |
| } | |
| img.animate().translationYBy(1000f).setDuration(300); | |
| } | |
| // check if any player won | |
| for (int[] winPosition : winPositions) { | |
| if (gameState[winPosition[0]] == gameState[winPosition[1]] && | |
| gameState[winPosition[1]] == gameState[winPosition[2]] && | |
| gameState[winPosition[0]] != 2) { | |
| String winnerStr; | |
| gameActive = false; | |
| if (gameState[winPosition[0]] == 0) { | |
| winnerStr = "X has won"; | |
| } else { | |
| winnerStr = "O has won"; | |
| } | |
| // Update the status bar for the winner | |
| TextView status = findViewById(R.id.status); | |
| status.setText(winnerStr); | |
| } | |
| } | |
| } | |
| public void gameReset(View view) { | |
| gameActive = true; | |
| activePlayer = 0; | |
| for (int i = 0; i < gameState.length; i++) { | |
| gameState[i] = 2; | |
| } | |
| // Hiding and show Image after button click | |
| ((ImageView) findViewById(R.id.imageView0)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView1)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView2)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView3)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView4)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView5)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView6)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView7)).setImageResource(0); | |
| ((ImageView) findViewById(R.id.imageView8)).setImageResource(0); | |
| } | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment