0% found this document useful (0 votes)
17 views12 pages

Abhay TicTacToe1

Diploma third year important

Uploaded by

aayushagp999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Abhay TicTacToe1

Diploma third year important

Uploaded by

aayushagp999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Activity_main.

xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/ggg">

<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>

<!-- Modify the Exit Button to be a Back Button -->


<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="17dp"
android:text="Back"
android:layout_marginLeft="70dp"
android:textSize="24sp" />
</RelativeLayout>

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;

public class MainActivity extends AppCompatActivity {

private Button[][] buttons = new Button[3][3];


private boolean player1Turn = true;
private int roundCount;

private int Player1Score;


private int Player2Score;

private TextView player1ScoreTextView;


private TextView player2ScoreTextView;
private String Player1 = "Player 1";
private String Player2 = "Player 2";

private Button musicButton;


private Button resetButton; // New button for resetting
private boolean isMusicPlaying = true; // To track music state
private MediaPlayer mediaPlayer;

@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);

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
buttons[i][j].setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
onButtonClick((Button) v);
}
});
}
}
Button buttonBack = findViewById(R.id.buttonBack);
buttonBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); // Finish the current activity and return to the
menu
}
});

// Initialize the MediaPlayer for background music


mediaPlayer = MediaPlayer.create(this, R.raw.audio);
mediaPlayer.setLooping(true); // Set to loop the music
mediaPlayer.start();

// Set up the music button


musicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isMusicPlaying) {
mediaPlayer.pause();
musicButton.setText(" ON");
} else {
mediaPlayer.start();
musicButton.setText(" OFF");
}
isMusicPlaying = !isMusicPlaying;
}
});

// Set up the reset button


resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetGame();
}
});

// 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);

player1ScoreTextView.setText(Player1 + " : " + Player1Score);


player2ScoreTextView.setText(Player2 + " : " + 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);
}

private void onButtonClick(Button button) {


if (!button.getText().toString().equals("")) {
return;
}

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;
}
}

private boolean checkForWin() {


String[][] field = new String[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
field[i][j] = buttons[i][j].getText().toString();
}
}

for (int i = 0; i < 3; i++) {


if (field[i][0].equals(field[i][1]) && field[i]
[0].equals(field[i][2]) && !field[i][0].equals("")) {
return true;
}
}

for (int i = 0; i < 3; i++) {


if (field[0][i].equals(field[1][i]) && field[0]
[i].equals(field[2][i]) && !field[0][i].equals("")) {
return true;
}
}

if (field[0][0].equals(field[1][1]) && field[0][0].equals(field[2]


[2]) && !field[0][0].equals("")) {
return true;
}

if (field[0][2].equals(field[1][1]) && field[0][2].equals(field[2]


[0]) && !field[0][2].equals("")) {
return true;
}

return false;
}

private void showWinnerDialog(String message) {


if (message.equals(Player1 + " wins!")) {
Player1Score++;
player1ScoreTextView.setText(Player1 + " : " + Player1Score);
} else if (message.equals(Player2 + " wins!")) {
Player2Score++;
player2ScoreTextView.setText(Player2 + " : " + Player2Score);
}

final Dialog dialog = new Dialog(this);


dialog.setContentView(R.layout.dialog_winner);

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
}
});

// Position the dialog at the bottom


Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new
WindowManager.LayoutParams();
layoutParams.copyFrom(window.getAttributes());
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(layoutParams);
}

dialog.show();
}

private void resetBoard() {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
}
}

roundCount = 0;
player1Turn = true;
}

private void resetGame() {


resetBoard();
Player1Score = 0;
Player2Score = 0;
player1ScoreTextView.setText(Player1 + " : " + Player1Score);
player2ScoreTextView.setText(Player2 + " : " + Player2Score);
}

private void showNameInputDialog() {


final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_enter_names);

EditText player1NameEditText =
dialog.findViewById(R.id.player1NameEditText);
EditText player2NameEditText =
dialog.findViewById(R.id.player2NameEditText);
Button okButton = dialog.findViewById(R.id.okButton);

player1NameEditText.setHint(" Player 1");


player2NameEditText.setHint(" Player 2");

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;
}

player1ScoreTextView.setText(Player1 + " : " +


Player1Score);
player2ScoreTextView.setText(Player2 + " : " +
Player2Score);

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;

public class MenuActivity extends AppCompatActivity {


private VideoView videoView;

@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);

// Set up the Start button


buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MenuActivity.this,
MainActivity.class);
startActivity(intent);
}
});

// Set up the Exit button


buttonExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

// Set up the VideoView for playing background video


Uri videoUri = Uri.parse("android.resource://" + getPackageName() +
"/" + R.raw.nifty);
videoView.setVideoURI(videoUri);
videoView.setOnPreparedListener(mediaPlayer -> {
mediaPlayer.setLooping(true);
videoView.start();
});
}

@Override
protected void onDestroy() {
super.onDestroy();

// Release the VideoView resources


if (videoView != null) {
videoView.stopPlayback();
}
}
}

dialog_enter_name.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<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

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
> <!-- Optional: Add a background drawable -->

<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>

You might also like