Professional Documents
Culture Documents
Tic Tak Toe Codes
Tic Tak Toe Codes
In this section, you will learn about designing a simple UI for Tic Tac Toe game. We
are using a tablelayout (3 x 3) for game UI.
Let's create a Tic Tac Toe UI. We are adding 2 textviews (one for title & other for
displaying the turn), 1 button (for starting new game) and 1 tablelayout (on which
we will play our game).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" //It will fill the whole screen
android:layout_height="fill_parent" //It will fill the whole screen
android:orientation="vertical">
<TextView
android:id="@+id/titleText" //Defines the 'id' used to refer this element in
activity class.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" //It aligns the text at center
horizontally
android:text="@string/title" />
<Button
android:id="@+id/newGameBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:onClick="newGame"
android:text="New Game" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
//First Row:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
//Second Row
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
//Third Row
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
//First Column (R3C1)
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:width="100dp"
android:height="100dp"
android:text="O" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
/**
*/
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
setupOnClickListeners();
resetButtons();
@Override
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
return super.onOptionsItemSelected(item);
/**
*/
is_O_turn = false;
/**
*/
B.setText("");
B.setEnabled(true);
t.setText(R.string.title);
nextTurnView.setText("X's Turn");
/**
* This method that returns true when someone has won and false when
nobody has
* If someone has won the game, it will also displays that in form of
Toast.
*
* @return
*/
if (checkWinner(gameBoard, 3, 'X')) {
winner = 'X';
winner = 'O';
if (winner == '\0') {
} else {
// display winner
return true;
/**
* Kindly have a look at the image (given after this section) for more
details on this scction.
*/
int total = 0;
if (board[x][y] == player) {
total++;
}
return true;
int total = 0;
if (board[x][y] == player) {
total++;
return true;
/*
* i.e. starting x & y from 0 and compare when x & y are same.
*/
int total = 0;
total++;
}
if (total >= size) {
return true;
/*
*/
total = 0;
total++;
return true;
return false;
/**
*/
B.setEnabled(false);
/**
*/
private int x = 0;
private int y = 0;
this.x = x;
this.y = y;
@Override
B.setEnabled(false);
is_O_turn = !is_O_turn;
if (checkWin()) {
disableGameButtons();
nextTurnView.setText("X's Turn");
In this section, you will learn about making the UI more inttractive and
beautiful.
Note
Only the changes to the existing files (that we have discussed in above
tutorials) have been highlighted.
Let's style our button, textview & tablelayout. We'll give blue color to button,
white color to tablelayout & red text color to textview text. We'll also add
some margin to our input fields.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@color/colorWhite"
android:textColor="@color/colorRed"
android:text="@string/title" />
<Button
android:id="@+id/newGameBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:onClick="newGame"
android:background="@color/colorPrimary"
android:textColor="@color/colorWhite"
android:layout_marginBottom="10dp"
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/topLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:width="100dp"
android:height="100dp"
android:layout_margin="5dp"
android:background="@color/colorWhite"
android:text="O" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/nextTurn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@color/colorWhite"
android:textColor="@color/colorRed"
</LinearLayout>
</ScrollView>
Adding colors to 'X' & 'O':
package com.internshala.app.is_tictactoe;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
/**
*/
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
setupOnClickListeners();
resetButtons();
@Override
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
return super.onOptionsItemSelected(item);
/**
*/
is_O_turn = false;
resetButtons();
/**
*/
B.setText("");
B.setEnabled(true);
t.setText(R.string.title);
nextTurnView.setText("X's Turn");
/**
* This method that returns true when someone has won and false when
nobody has
* If someone has won the game, it will also displays that in form of
Toast.
* @return
*/
if (checkWinner(gameBoard, 3, 'X')) {
winner = 'X';
if (winner == '\0') {
} else {
// display winner
return true;
/**
*/
int total = 0;
if (board[x][y] == player) {
total++;
return true;
}
//Then we check all the columns
int total = 0;
if (board[x][y] == player) {
total++;
return true;
/*
* i.e. starting x & y from 0 and compare when x & y are same.
*/
int total = 0;
total++;
return true;
/*
*/
total = 0;
total++;
return true;
return false;
/**
*/
B.setEnabled(false);
}
}
/**
*/
private int x = 0;
private int y = 0;
this.x = x;
this.y = y;
@Override
B.setTextColor(is_O_turn ?
getResources().getColor(R.color.colorPrimary) :
getResources().getColor(R.color.colorRed));
B.setTextSize(20);
B.setEnabled(false);
is_O_turn = !is_O_turn;
if (checkWin()) {
disableGameButtons();
nextTurnView.setText("X's Turn");
}
We need to add colors.xml under values directory.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#1295c9</color>
<color name="colorWhite">#FFFFFF</color>
<color name="colorBlack">#000000</color>
<color name="colorRed">#db2a3b</color>
</resources>
<string name="app_name">IS-TicTacToe</string>
<string name="action_settings">Settings</string>
</resources>