CoCaro
CoCaro
>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="2dp"
tools:layout_editor_absoluteY="93dp">
<TableRow
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5pt">
<Button
android:id="@+id/bt1"
android:padding="0pt"
android:textSize="50dp"
android:layout_width="50pt"
android:layout_height="50pt"
android:layout_margin="3pt"
android:onClick="btClick"
app:backgroundTint="#BCB9B9" />
<Button
android:id="@+id/bt2"
android:textSize="50dp"
android:layout_width="50pt"
android:layout_height="50pt"
android:layout_margin="3pt"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
<Button
android:id="@+id/bt3"
android:layout_width="50pt"
android:textSize="50dp"
android:layout_height="50pt"
app:backgroundTint="#BCB9B9"
android:layout_margin="3pt"
android:onClick="btClick" />
</TableRow>
<TableRow
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5pt">
>
<Button
android:id="@+id/bt4"
android:textSize="50dp"
android:layout_width="50pt"
android:layout_height="50pt"
app:backgroundTint="#BCB9B9"
android:layout_margin="3pt"
android:onClick="btClick" />
<Button
android:id="@+id/bt5"
android:textSize="50dp"
android:layout_width="50pt"
android:layout_height="50pt"
android:layout_margin="3pt"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
<Button
android:id="@+id/bt6"
android:layout_width="50pt"
android:textSize="50dp"
android:layout_height="50pt"
android:layout_margin="3pt"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
</TableRow>
<TableRow
android:layout_gravity="center"
android:layout_marginLeft="5pt"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt7"
android:layout_width="50pt"
android:textSize="50dp"
android:layout_height="50pt"
android:layout_margin="3pt"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
<Button
android:id="@+id/bt8"
android:layout_width="50pt"
android:textSize="50dp"
android:layout_height="50pt"
android:layout_margin="3pt"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
<Button
android:id="@+id/bt9"
android:layout_width="50pt"
android:layout_height="50pt"
android:layout_margin="3pt"
android:textSize="50dp"
app:backgroundTint="#BCB9B9"
android:onClick="btClick" />
</TableRow>
<TableRow android:gravity="center"
android:layout_margin="5pt">
<Button
android:id="@+id/btPlayAgain"
android:padding="5pt"
android:text="Bắt Đầu">
</Button>
</TableRow>
<TableRow android:gravity="center"
android:layout_margin="5pt">
<TextView
android:layout_span="3"
android:id="@+id/txtShowresult"
android:textSize="30sp"
android:layout_width="match_parent"
android:text="AÂ"
android:gravity="center"
android:visibility="invisible"
></TextView>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.hutech.tictoetoygame;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
}
});
}
void PlayAgain(){
Player1.clear();
Player2.clear();
Winer=-1;
bt1.setText("");
bt1.setBackgroundColor(Color.rgb(188,185,185));
bt2.setText("");
bt2.setBackgroundColor(Color.rgb(188,185,185));
bt3.setText("");
bt3.setBackgroundColor(Color.rgb(188,185,185));
bt4.setText("");
bt4.setBackgroundColor(Color.rgb(188,185,185));
bt5.setText("");
bt5.setBackgroundColor(Color.rgb(188,185,185));
bt6.setText("");
bt6.setBackgroundColor(Color.rgb(188,185,185));
bt7.setText("");
bt7.setBackgroundColor(Color.rgb(188,185,185));
bt8.setText("");
bt8.setBackgroundColor(Color.rgb(188,185,185));
bt9.setText("");
bt9.setBackgroundColor(Color.rgb(188,185,185));
txtShowresult.setVisibility(View.INVISIBLE);
}
void AnhXa(){
btPlayAgain = (Button) findViewById(R.id.btPlayAgain);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
bt6 = (Button) findViewById(R.id.bt6);
bt7 = (Button) findViewById(R.id.bt7);
bt8 = (Button) findViewById(R.id.bt8);
bt9 = (Button) findViewById(R.id.bt9);
txtShowresult = (TextView) findViewById(R.id.txtShowresult);
}
public void btClick(View view) {
Button btSelected = (Button) view;
int CellID=0;
switch (btSelected.getId()){
case R.id.bt1:
CellID = 1;
break;
case R.id.bt2:
CellID=2;
break;
case R.id.bt3:
CellID=3;
break;
case R.id.bt4:
CellID=4;
break;
case R.id.bt5:
CellID=5;
break;
case R.id.bt6:
CellID=6;
break;
case R.id.bt7:
CellID=7;
break;
case R.id.bt8:
CellID=8;
break;
case R.id.bt9:
CellID=9;
break;
}
int ActivePlayer=1; // 1 : first, 2 second
ArrayList<Integer> Player1 = new ArrayList<Integer>();
ArrayList<Integer> Player2 = new ArrayList<Integer>();
if(ActivePlayer == 1)
{
btselected.setText("X");
btselected.setBackgroundColor(Color.GREEN);
btselected.setTextColor(Color.RED);
Player1.add(CellID);
ActivePlayer = 2;
}
else if (ActivePlayer ==2)
{
btselected.setText("O");
btselected.setTextColor(Color.WHITE);
btselected.setBackgroundColor(Color.BLUE);
Player2.add(CellID);
ActivePlayer = 1;
}
CheckWiner();
if (Winer == 1) {
txtShowresult.setVisibility(View.VISIBLE);
txtShowresult.setText("Player 1 thắng !");
//Toast.makeText(this,"Play 1 thắng !",Toast.LENGTH_LONG).show();
}
else if (Winer == 2) {
txtShowresult.setVisibility(View.VISIBLE);
txtShowresult.setText("Player 2 thắng !");
//Toast.makeText(this,"Play 2 thắng !",Toast.LENGTH_LONG).show();
}
else if (Winer == 0) {
txtShowresult.setVisibility(View.VISIBLE);
txtShowresult.setText("Hòa !");
//Toast.makeText(this,"Hòa !",Toast.LENGTH_LONG).show();
}
}
void CheckWiner(){
//Dòng 1
if(Player1.contains(1) && Player1.contains(2) &&
Player1.contains(3)){
Winer =1;
}
if(Player2.contains(1) && Player2.contains(2) &&
Player2.contains(3)){
Winer =2;
}
//Dòng 2
if(Player1.contains(4) && Player1.contains(5) &&
Player1.contains(6)){
Winer =1;
}
if(Player2.contains(4) && Player2.contains(5) &&
Player2.contains(6)){
Winer =2;
}
//Dòng 3
if(Player1.contains(7) && Player1.contains(8) &&
Player1.contains(9)){
Winer =1;
}
if(Player2.contains(7) && Player2.contains(8) &&
Player2.contains(9)){
Winer =2;
}
//Cột 1
if(Player1.contains(1) && Player1.contains(4) &&
Player1.contains(7)){
Winer =1;
}
if(Player2.contains(1) && Player2.contains(4) &&
Player2.contains(7)){
Winer =2;
}
//Cột 2
if(Player1.contains(2) && Player1.contains(5) &&
Player1.contains(8)){
Winer =1;
}
if(Player2.contains(2) && Player2.contains(5) &&
Player2.contains(8)){
Winer =2;
}
//Cột 3
if(Player1.contains(2) && Player1.contains(5) &&
Player1.contains(8)){
Winer =1;
}
if(Player2.contains(3) && Player2.contains(6) &&
Player2.contains(9)){
Winer =2;
}
//Chéo 1
if(Player1.contains(1) && Player1.contains(5) &&
Player1.contains(9)){
Winer =1;
}
if(Player2.contains(1) && Player2.contains(5) &&
Player2.contains(9)){
Winer =2;
}
//Chéo 2
if(Player1.contains(3) && Player1.contains(5) &&
Player1.contains(7)){
Winer =1;
}
if(Player2.contains(3) && Player2.contains(5) &&
Player2.contains(7)){
Winer =2;
}
}
}