Abhay TicTacToe1
Abhay TicTacToe1
xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X : 0"
android:textColor="@color/black"
android:id="@+id/player1Score"
android:textSize="24sp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"/>
<Button
android:id="@+id/musicButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="Off"
android:background="@drawable/audioima"
android:layout_marginLeft="300dp"
android:textColor="@android:color/white"
android:textSize="16sp" />
<Button
android:id="@+id/buttonReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonBack"
android:layout_marginLeft="210dp"
android:layout_marginTop="-65dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Reset"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 0 : 0"
android:textColor="@color/black"
android:id="@+id/player2Score"
android:textSize="24sp"
android:layout_marginTop="100dp"
android:layout_marginRight="50dp"
android:layout_alignParentRight="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button2"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button3"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button4"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button5"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button6"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button7"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button8"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
<Button
android:id="@+id/button9"
android:layout_width="61dp"
android:layout_height="65dp"
android:layout_margin="5dp"
android:textSize="24sp"
android:text="" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.tictactoe;
import android.app.Dialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player1ScoreTextView = findViewById(R.id.player1Score);
player2ScoreTextView = findViewById(R.id.player2Score);
musicButton = findViewById(R.id.musicButton);
resetButton = findViewById(R.id.buttonReset); // Initialize the
reset button
buttons[0][0] = findViewById(R.id.button1);
buttons[0][1] = findViewById(R.id.button2);
buttons[0][2] = findViewById(R.id.button3);
buttons[1][0] = findViewById(R.id.button4);
buttons[1][1] = findViewById(R.id.button5);
buttons[1][2] = findViewById(R.id.button6);
buttons[2][0] = findViewById(R.id.button7);
buttons[2][1] = findViewById(R.id.button8);
buttons[2][2] = findViewById(R.id.button9);
// Show the name input dialog only if it's the first creation of
the activity
if (savedInstanceState == null) {
showNameInputDialog();
} else {
// Restore the previous state
Player1 = savedInstanceState.getString("player1Name", Player1);
Player2 = savedInstanceState.getString("player2Name", Player2);
Player1Score = savedInstanceState.getInt("player1Score",
Player1Score);
Player2Score = savedInstanceState.getInt("player2Score",
Player2Score);
@Override
protected void onDestroy() {
super.onDestroy();
// Release the MediaPlayer resources
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("player1Name", Player1);
outState.putString("player2Name", Player2);
outState.putInt("player1Score", Player1Score);
outState.putInt("player2Score", Player2Score);
}
if (player1Turn) {
button.setText("X");
} else {
button.setText("O");
}
roundCount++;
if (checkForWin()) {
if (player1Turn) {
showWinnerDialog(Player1 + " wins!");
} else {
showWinnerDialog(Player2 + " wins!");
}
} else if (roundCount == 9) {
showWinnerDialog("Draw!");
} else {
player1Turn = !player1Turn;
}
}
return false;
}
TextView winnerMessageTextView =
dialog.findViewById(R.id.winnerMessage);
TextView playAgainTextView =
dialog.findViewById(R.id.playAgainTextView);
winnerMessageTextView.setText(message);
playAgainTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
resetBoard(); // Reset the board after clicking PLAY AGAIN
}
});
dialog.show();
}
roundCount = 0;
player1Turn = true;
}
EditText player1NameEditText =
dialog.findViewById(R.id.player1NameEditText);
EditText player2NameEditText =
dialog.findViewById(R.id.player2NameEditText);
Button okButton = dialog.findViewById(R.id.okButton);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newPlayer1Name =
player1NameEditText.getText().toString().trim();
String newPlayer2Name =
player2NameEditText.getText().toString().trim();
if (!newPlayer1Name.isEmpty()) {
Player1 = newPlayer1Name;
}
if (!newPlayer2Name.isEmpty()) {
Player2 = newPlayer2Name;
}
dialog.dismiss();
}
});
dialog.show();
}
}
Activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:gravity="center">
<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Play"
android:textSize="24sp"/>
<Button
android:id="@+id/buttonExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonStart"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Exit"
android:textSize="24sp"/>
</RelativeLayout>
</FrameLayout>
MenuActivity
package com.example.tictactoe;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
videoView = findViewById(R.id.videoView);
Button buttonStart = findViewById(R.id.buttonStart);
Button buttonExit = findViewById(R.id.buttonExit);
@Override
protected void onDestroy() {
super.onDestroy();
dialog_enter_name.xml
<EditText
android:id="@+id/player1NameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Player 1" />
<EditText
android:id="@+id/player2NameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" Player 2" />
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp" />
</LinearLayout>
Dialog_winner.xml
<TextView
android:id="@+id/winnerMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Winner!"
android:padding="20dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/playAgainTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="10dp"
android:text="PLAY AGAIN"
android:textColor="@android:color/black"
android:textSize="16sp"
android:gravity="center" />
</LinearLayout>