Two player Tic Tac Toe game in Android Studio

Hello Guys,

    In this tutorial we are developing tic-tac-toe game using java in android-studio. Everyone know about these game. Tic - Tac - Toe game is classical pen-papper game for 2 players. In these game each player chooses between X and 0. In a move a player can choose any position from 3x3 grid. The Main goal is to get three consecutive X or 0 in a horizontal , vertical or diagnoal direction. Tic - Tac - Toe game  MainScreen looking like below picture: 




Let's get  started.

Step 1: Create a New Project.

Step 2: Add Image for Project.

  • All image download And Save them in Drawable folder in resources. Go to app -> drawable -> res and paste them.
Step 3: Implement activity_main.xml file. 

  • In this activity we are implement layout for Tic - Tac - Toe game. In this layout we are added reset button for reset game.
<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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<LinearLayout
android:gravity="center"
android:layout_marginBottom="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:freezesText="true"
android:id="@+id/textview_p1"
android:layout_marginRight="20dp"
android:padding="5dp"
android:text="Player 1: 0"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#42559f"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:freezesText="true"
android:id="@+id/textview_p2"
android:layout_marginLeft="20dp"
android:padding="5dp"
android:text="Player 2: 0"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#42559f"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<ImageButton
android:id="@+id/reset_r1"
android:layout_marginBottom="20dp"
android:layout_width="69dp"
android:layout_height="58dp"
android:layout_gravity="center_horizontal"
android:background="#fd414040"
android:scaleType="fitXY"
android:src="@drawable/reset" />


<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">

<Button
android:freezesText="true"
android:id="@+id/button_00"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_01"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_02"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

</LinearLayout>

<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">

<Button
android:freezesText="true"
android:id="@+id/button_10"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_11"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_12"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

</LinearLayout>

<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">

<Button
android:freezesText="true"
android:id="@+id/button_20"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_21"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

<Button
android:freezesText="true"
android:id="@+id/button_22"
android:background="#792db917"
android:layout_margin="5dp"
android:textColor="#fff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="70sp"/>

</LinearLayout>

</LinearLayout>
       
Step 4: Implement MainActivity.java file.
  • In this file we are implementing reset button for reset game and updating score for player 1 and player 2.
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
implements View.OnClickListener{
private Button[][] buttons = new Button[3][3];

private int roundCounts;
private int activityCount = 1;

private boolean player1turn = true;

private int player1points;
private int player2points;

private TextView textplayer1;
private TextView textplayer2;


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

textplayer1 = findViewById(R.id.textview_p1);
textplayer2 = findViewById(R.id.textview_p2);

textplayer1.setBackgroundColor
(Color.rgb(200,20,60));

for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
String Buttonid = "button_"+i+j;
int resid = getResources().
getIdentifier(Buttonid ,"id" ,
getPackageName());
buttons[i][j] = findViewById(resid);
buttons[i][j].setOnClickListener(this);
}
}

ImageButton resetbutton = findViewById(R.id.reset_r1);
resetbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resetGame();
}
});

}

@Override
public void onClick(View view) {
if(!((Button) view).getText().toString().equals(" "))
{
return;
}
if(player1turn)
{
((Button) view).setText("X");
}else {
((Button) view).setText("0");
}

roundCounts++;
activityStaus();

if(checkForWins())
{
if(player1turn){
player1wins();
}
else
{
player2wins();
}
}else if(roundCounts == 9)
{
draw();
}
else{
player1turn = !player1turn;
}
}

public boolean checkForWins(){
String [][] fields = new String[3][3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++){

fields[i][j] = buttons[i][j].getText().toString();

}
}

for(int i = 0; i < 3; i++)
{
if(fields[i][0].equals(fields[i][1]) &&
fields[i][0].equals(fields[i][2])
&& !fields[i][0].equals(" "))

{
return true;
}
}

for(int i = 0; i < 3; i++)
{
if(fields[0][i].equals(fields[1][i]) &&
fields[0][i].equals(fields[2][i])
&& !fields[0][i].equals(" "))

{
return true;
}
}

if(fields[0][0].equals(fields[1][1]) &&
fields[0][0].equals(fields[2][2])
&& !fields[0][0].equals(" "))

{
return true;
}

if(fields[0][2].equals(fields[1][1]) &&
fields[0][2].equals(fields[2][0])
&& !fields[0][2].equals(" "))

{
return true;
}


return false;
}

public void player1wins(){
player1points++;
Toast.makeText(this,
"Player 1 Wins",
Toast.LENGTH_SHORT).show();
UpdatepointTable();
resetBoard();
}

public void player2wins() {
player2points++;
Toast.makeText(this,
"Player 2 Wins",
Toast.LENGTH_SHORT).show();
UpdatepointTable();
resetBoard();
}

public void draw()
{
Toast.makeText(this,
"Draw!",
Toast.LENGTH_SHORT).show();
resetBoard();
}

public void UpdatepointTable()
{
textplayer1.setText("Player 1: "+ player1points);
textplayer2.setText("Player 2: "+ player2points);
}

public void resetBoard(){
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
buttons[i][j].setText(" ");
}
}

roundCounts = 0;
activityCount = 1;
activityNum();
player1turn = true;
}

public void resetGame() {
resetBoard();
player1points = 0;
player2points = 0;
UpdatepointTable();
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

player1points = savedInstanceState.
getInt("Player1points");
player2points = savedInstanceState.
getInt("Player2points");
roundCounts = savedInstanceState.
getInt("RoundCounts");
player1turn = savedInstanceState.
getBoolean("Player1turn");
activityCount = savedInstanceState.
getInt("ActivityCount");

activityNum();
}

public void activityStaus() {
activityCount++;
if(activityCount % 2 == 0)
{
textplayer1.setBackgroundColor
(Color.rgb(58,133,0));
textplayer2.setBackgroundColor
(Color.rgb(200,20,60));
}
else
{
textplayer1.setBackgroundColor
(Color.rgb(200,20,60));
textplayer1.setBackgroundColor
(Color.rgb(58,133,0));
}
}

public void activityNum(){
if(activityCount % 2 == 0)
{
textplayer1.setBackgroundColor
(Color.rgb(58,133,0));
textplayer2.setBackgroundColor
(Color.rgb(200,20,60));
}else{
textplayer1.setBackgroundColor
(Color.rgb(200,20,60));
textplayer1.setBackgroundColor
(Color.rgb(58,133,0));
}
}
}
 

Output :





If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏


Post a Comment

0 Comments