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