Here are Tic Tac Toe game android App code below
Create a new project in Android Studio and name it “TicTacToeApp”.
Open the activity_main.xml file located in the res/layout directory. Replace the existing code with the following XML layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:columnCount="3" android:rowCount="3" android:useDefaultMargins="true"> <Button android:id="@+id/button1" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button2" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button3" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button4" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button5" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button6" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button7" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button8" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> <Button android:id="@+id/button9" style="@style/BoardButton" android:onClick="onBoardButtonClick" /> </GridLayout> <Button android:id="@+id/resetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="16dp" android:text="Reset" android:onClick="resetGame" /> <TextView android:id="@+id/statusTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="16dp" android:textSize="18sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> |
Open the MainActivity.java file located in the java/<your_package_name> directory. Replace the existing code with the following Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
package com.example.tictactoeapp; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button[][] buttons = new Button[3][3]; private boolean player1Turn = true; private int roundCount; private int player1Points; private int player2Points; private TextView textViewPlayer1; private TextView textViewPlayer2; private TextView textViewStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textViewPlayer1 = findViewById(R.id.text_view_p1); textViewPlayer2 = findViewById(R.id.text_view_p2); textViewStatus = findViewById(R.id.text_view_status); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { String buttonId = "button_" + i + j; int resourceId = getResources().getIdentifier(buttonId, "id", getPackageName()); buttons[i][j] = findViewById(resourceId); buttons[i][j].setOnClickListener(this); } } Button resetButton = findViewById(R.id.reset_button); resetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resetGame(); } }); } @Override public void onClick(View v) { if (!((Button) v).getText().toString().isEmpty()) { return; } if (player1Turn) { ((Button) v).setText("X"); } else { ((Button) v).setText("O"); } roundCount++; if (checkForWin()) { if (player1Turn) { player1Wins(); } else { player2Wins(); } } else if (roundCount == 9) { draw(); } else { player1Turn = !player1Turn; } } private boolean checkForWin() { String[][] board = new String[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = buttons[i][j].getText().toString(); } } for (int i = 0; i < 3; i++) { if (board[i][0].equals(board[i][1]) && board[i][0].equals(board[i][2]) && !board[i][0].isEmpty()) { return true; } if (board[0][i].equals(board[1][i]) && board[0][i].equals(board[2][i]) && !board[0][i].isEmpty()) { return true; } } if (board[0][0].equals(board[1][1]) && board[0][0].equals(board[2][2]) && !board[0][0].isEmpty()) { return true; } if (board[0][2].equals(board[1][1]) && board[0][2].equals(board[2][0]) && !board[0][2].isEmpty()) { return true; } return false; } private void player1Wins() { player1Points++; updatePointsText(); resetBoard(); textViewStatus.setText("Player 1 wins!"); } private void player2Wins() { player2Points++; updatePointsText(); resetBoard(); textViewStatus.setText("Player 2 wins!"); } private void draw() { resetBoard(); textViewStatus.setText("It's a draw!"); } private void updatePointsText() { textViewPlayer1.setText("Player 1: " + player1Points); textViewPlayer2.setText("Player 2: " + player2Points); } private void resetBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[i][j].setText(""); } } roundCount = 0; player1Turn = true; } private void resetGame() { player1Points = 0; player2Points = 0; updatePointsText(); resetBoard(); textViewStatus.setText(""); } } |
Open the AndroidManifest.xml file located in the app/src/main directory. Replace the existing code with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tictactoeapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Open the build.gradle (Module: app) file located in the root directory of the project. Ensure that the minSdkVersion is set to 21 or higher:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
apply plugin: 'com.android.application' android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.tictactoeapp" minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } lintOptions { checkReleaseBuilds false abortOnError false } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } |
That’s it! You can now build and run the app on an Android device or emulator, and it will function as a Tic Tac Toe game.