0% found this document useful (0 votes)
9 views8 pages

CoCaro

The document contains an Android layout and Java code for a Tic-Tac-Toe game. The layout includes buttons for player moves and a display for the game result, while the Java code manages game logic, including player turns and win conditions. The game allows players to start a new game or play again after a round ends.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

CoCaro

The document contains an Android layout and Java code for a Tic-Tac-Toe game. The layout includes buttons for player moves and a display for the game result, while the Java code manages game logic, including player turns and win conditions. The game allows players to start a new game or play again after a round ends.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

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

public class MainActivity extends AppCompatActivity {


int Winer = -1;
int startGame=0;
Button btPlayAgain,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9;
TextView txtShowresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnhXa();
btPlayAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (startGame == 1) {
PlayAgain();
startGame=0;
btPlayAgain.setText("Bắt đầu");
}
else if (startGame == 0)
{
btPlayAgain.setText("Chơi lại");
startGame=1;
}

}
});
}

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;

if (Winer == -1 && startGame == 1)


{
PlayGame(CellID,btSelected);
}

}
int ActivePlayer=1; // 1 : first, 2 second
ArrayList<Integer> Player1 = new ArrayList<Integer>();
ArrayList<Integer> Player2 = new ArrayList<Integer>();

void PlayGame(int CellID, Button btselected){

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

int sum = Player1.size() + Player2.size();


if (sum == 9 && Winer == -1)
{
Winer = 0;
}

}
}

You might also like