java Part :
package first.learn.tictoctowgame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.content.ContextCompat;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int flag = 0;
int moveCount = 0;
int gameStatus = 0;
TextView result;
AppCompatButton btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9;
String bv_1, bv_2, bv_3, bv_4, bv_5, bv_6, bv_7, bv_8, bv_9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void checkResult(View view){
AppCompatButton clickedButton = (AppCompatButton) view;
if (!clickedButton.getText().toString().equalsIgnoreCase("") || gameStatus==1){
return;
}
moveCount++;
if (flag == 0){
clickedButton.setText("X");
flag = 1;
}else{
clickedButton.setText("O");
flag = 0;
}
if (moveCount > 4){
assignButtonText();
if (bv_1.equals(bv_2) && bv_2.equals(bv_3) && !bv_1.equals("")){
gameResult(btn_1, btn_2, btn_3, bv_1);
}else if(bv_4.equals(bv_5) && bv_5.equals(bv_6) && !bv_4.equals("")){
gameResult(btn_4, btn_5, btn_6, bv_4);
}else if(bv_7.equals(bv_8) && bv_8.equals(bv_9) && !bv_7.equals("")){
gameResult(btn_7, btn_8, btn_9, bv_7);
}else if(bv_1.equals(bv_4) && bv_4.equals(bv_7) && !bv_1.equals("")){
gameResult(btn_1, btn_4, btn_7, bv_1);
}else if(bv_2.equals(bv_5) && bv_5.equals(bv_8) && !bv_2.equals("")){
gameResult(btn_2, btn_5, btn_8, bv_2);
}else if(bv_3.equals(bv_6) && bv_6.equals(bv_9) && !bv_3.equals("")){
gameResult(btn_3, btn_6, btn_9, bv_3);
}else if(bv_1.equals(bv_5) && bv_5.equals(bv_9) && !bv_1.equals("")){
gameResult(btn_1, btn_5, btn_9, bv_1);
}else if(bv_3.equals(bv_5) && bv_5.equals(bv_7) && !bv_3.equals("")){
gameResult(btn_3, btn_5, btn_7, bv_3);
}else if(moveCount >= 9 ){
result.setText("Match Draw");
result.setBackgroundColor(getResources().getColor(R.color.red));
}
}
}
public void gameResult(AppCompatButton bt1, AppCompatButton bt2, AppCompatButton bt3, String winner){
bt1.setBackgroundColor(getResources().getColor(R.color.green));
bt2.setBackgroundColor(getResources().getColor(R.color.green));
bt3.setBackgroundColor(getResources().getColor(R.color.green));
result.setText(winner + " is Winner");
gameStatus = 1;
}
public void restartGame(){
btn_1.setText("");
btn_2.setText("");
btn_3.setText("");
btn_4.setText("");
btn_5.setText("");
btn_6.setText("");
btn_7.setText("");
btn_8.setText("");
btn_9.setText("");
result.setText("WELCOME");
flag = 0;
moveCount = 0;
gameStatus = 0;
result.setBackgroundColor(Color.parseColor("#000000"));
btn_1.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
btn_2.setBackgroundColor(getResources().getColor(R.color.orange));
btn_3.setBackgroundColor(getResources().getColor(R.color.orange));
btn_4.setBackgroundColor(getResources().getColor(R.color.orange));
btn_5.setBackgroundColor(getResources().getColor(R.color.orange));
btn_6.setBackgroundColor(getResources().getColor(R.color.orange));
btn_7.setBackgroundColor(getResources().getColor(R.color.orange));
btn_8.setBackgroundColor(getResources().getColor(R.color.orange));
btn_9.setBackgroundColor(getResources().getColor(R.color.orange));
}
public void assignButtonText(){
bv_1 = btn_1.getText().toString();
bv_2 = btn_2.getText().toString();
bv_3 = btn_3.getText().toString();
bv_4 = btn_4.getText().toString();
bv_5 = btn_5.getText().toString();
bv_6 = btn_6.getText().toString();
bv_7 = btn_7.getText().toString();
bv_8 = btn_8.getText().toString();
bv_9 = btn_9.getText().toString();
}
public void init(){
btn_1 = (AppCompatButton) findViewById(R.id.btn_1);
btn_2 = (AppCompatButton) findViewById(R.id.btn_2);
btn_3 = (AppCompatButton) findViewById(R.id.btn_3);
btn_4 = (AppCompatButton) findViewById(R.id.btn_4);
btn_5 = (AppCompatButton) findViewById(R.id.btn_5);
btn_6 = (AppCompatButton) findViewById(R.id.btn_6);
btn_7 = (AppCompatButton) findViewById(R.id.btn_7);
btn_8 = (AppCompatButton) findViewById(R.id.btn_8);
btn_9 = (AppCompatButton) findViewById(R.id.btn_9);
result = (TextView) findViewById(R.id.result);
}
public void restartGame(View view) {
restartGame();
}
}
xml part :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/result"
android:text="WELCOME"
android:layout_marginBottom="30dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/white"
android:background="@color/black"
android:paddingHorizontal="20dp"
android:paddingVertical="7dp"
android:layout_height="wrap_content"/>
<GridLayout
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:layout_width="wrap_content">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_1"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_2"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_3"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_4"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_5"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_6"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_7"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_8"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_9"
android:onClick="checkResult"
android:background="@color/orange"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white"/>
</GridLayout>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:text="Restart Game"
android:background="@color/black"
android:textColor="@color/white"
android:paddingHorizontal="20dp"
android:layout_marginTop="30dp"
android:onClick="restartGame"
android:layout_height="wrap_content"/>
</LinearLayout>
Color.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="orange">#DD2C00</color>
<color name="red">#D50000</color>
<color name="green">#F309653B</color>
</resources>
No comments:
Post a Comment