0% found this document useful (0 votes)
22 views6 pages

Tictactoe

The document details code for a Tic Tac Toe game written in Android. It includes Java code for setting up buttons, tracking turns, checking for wins or draws, and resetting the board. Layout code is provided to display the buttons in a 3x3 grid.

Uploaded by

Mary Cris
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)
22 views6 pages

Tictactoe

The document details code for a Tic Tac Toe game written in Android. It includes Java code for setting up buttons, tracking turns, checking for wins or draws, and resetting the board. Layout code is provided to display the buttons in a 3x3 grid.

Uploaded by

Mary Cris
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/ 6

Main.

xml
package com.example.tictactoe;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
int chance = 0;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, reset;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
reset = findViewById(R.id.reset);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
reset.setOnClickListener(this);
}

@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
int viewId = view.getId();
if (viewId == R.id.btn1) {
if (btn1.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn1.setText("O");
result();
} else {
chance = 0;
btn1.setText("X");
result();
}
}
} else if (viewId == R.id.btn2) {
if (btn2.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn2.setText("O");
result();
} else {
chance = 0;
btn2.setText("X");
result();
}
}
}
else if (viewId == R.id.btn3) {
if (btn3.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn3.setText("O");
result();
} else {
chance = 0;
btn3.setText("X");
result();
}
}
}
else if (viewId == R.id.btn4) {
if (btn4.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn4.setText("O");
result();
} else {
chance = 0;
btn4.setText("X");
result();
}
}
}
else if (viewId == R.id.btn5) {
if (btn5.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn5.setText("O");
result();
} else {
chance = 0;
btn5.setText("X");
result();
}
}
}
else if (viewId == R.id.btn6) {
if (btn6.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn6.setText("O");
result();
} else {
chance = 0;
btn6.setText("X");
result();
}
}
}
else if (viewId == R.id.btn7) {
if (btn7.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn7.setText("O");
result();
} else {
chance = 0;
btn7.setText("X");
result();
}
}
}
else if (viewId == R.id.btn8) {
if (btn8.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn8.setText("O");
result();
} else {
chance = 0;
btn8.setText("X");
result();
}
}
}
else if (viewId == R.id.btn9) {
if (btn9.getText().toString().equals("")) {
if (chance == 0) {
chance = 1;
btn9.setText("O");
result();
} else {
chance = 0;
btn9.setText("X");
result();
}
}
}
else if (viewId == R.id.reset) {
resetBoard();
}
}
private void resetBoard() {
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
chance = 0;
}

private void result() {


// Check for a win or a draw and show a toast message
if (checkWin()) {
String winner = (chance == 1) ? "Player O" : "Player X";
Toast.makeText(this, winner + " wins!",
Toast.LENGTH_SHORT).show();
resetBoard();
} else if (isBoardFull()) {
Toast.makeText(this, "It's a draw!", Toast.LENGTH_SHORT).show();
resetBoard();
}
}

private boolean checkWin() {


// Implement your win logic here
// Return true if there is a win, false otherwise
// You'll need to check rows, columns, and diagonals
return false;
}

private boolean isBoardFull() {


// Check if the board is full
return !(btn1.getText().toString().equals("") ||
btn2.getText().toString().equals("") ||
btn3.getText().toString().equals("") ||
btn4.getText().toString().equals("") ||
btn5.getText().toString().equals("") ||
btn6.getText().toString().equals("") ||
btn7.getText().toString().equals("") ||
btn8.getText().toString().equals("") ||
btn9.getText().toString().equals(""));
}
}

Activity.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="match_parent"
android:orientation="vertical"
android:weightSum="3">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">

<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">

<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">

<Button
android:id="@+id/btn7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />

<Button
android:id="@+id/btn9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp" />
</LinearLayout>

<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="RESET"
android:textSize="25sp" />
</LinearLayout>

You might also like