You are on page 1of 15

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Jnana Sangama, Santhibastawad Road, Machhe


Belagavi - 590018, Karnataka, India

ADVANCED JAVA & J2EE (17CS553)

ASSIGNMENT REPORT
ON
“Tic-Tac-Toe”
Submitted in the partial fulfillment of the requirements for the award of the degree of

BACHELOR OF ENGINEERING
IN
COMPUTER SCIENCE AND ENGINEERING
For the Academic Year 2019-2020
Submitted by
SIDDHANT SINGH 1JS17CS100
SHUBHAM SAHU 1JS17CS099
YASH GUPTA 1JS17CS122

Under the guidance of


Dr. Naveen N C
Professor and Head, Dept. of CSE, JSSATE

2019-2020
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
JSS ACADEMY OF TECHNICAL EDUCATION

JSS Campus, Dr.Vishnuvardhan Road, Bengaluru-560060


TABLE OF CONTENTS

ABSTRACT
1. INTRODUCTION
2. SYSTEM ANALYSIS
EXISTING SYSTEM
PROPOSED SYSTEM
3. SYSTEM DESIGN
4. IMPLEMENTATION
5. SCREENSHOTS
6. CONCLUSION
ABSTRACT

Tic-tac-toe (American English), noughts and crosses (British English), or Xs


and Os is a paper-and-pencil game for two players, X and O, who take turns
marking the spaces in a 3×3 grid. The player who succeeds in placing three of
their marks in a horizontal, vertical, or diagonal row is the winner..

The game can be generalized to an m,n,k-game in which two players alternate


placing stones of their own color on an m×n board, with the goal of
getting k of their own color in a row. Tic-tac-toe is the (3,3,3)-game.
[2] Harary's generalized tic-tac-toe is an even broader generalization of tic-
tac-toe. It can also be generalized as and game. Tic-tac-toe is the game where
n equals 3 and d equals 2.[3] If played properly, the game will end in a draw,
making tic-tac-toe a futile game.
INTRODUCTION

Overview

1. This report discusses the main components and the ways used to play tic tac toe game
2. This application is built on java platform using Android Studio.

Objective

Our project name is Tic-tac-Toe game. This game is very popular and is
fairly simple by itself. It is actually a two player game. In this game, there is
a board with n x n squares. In our game, it is 3 x 3 squares.

The goal of Tic-Tac-Toe is to be one of the players to get three same


symbols in a row horizontally, vertically or diagonally on a 3 x 3 grid.

SYSTEM ANALYSIS

Existing system features

Tic-Tac-Toe (or Noughts and crosses, Xs and Os) is a pencil-and-paper game for two
players, O and X, who take turns marking the spaces in a 3 x 3 grid. The player who
succeeds in placing three respective marks in a horizontal, vertical or diagonal row wins
the game.

New system feature

The computer implementation of the game TicTac-Toe has many features as compared to the
traditional way of playing it with paper and pencil. The various features are:
 The game has been made user friendly with proper use of android system.
 The user can play as many games without any interruption
 The user can choose any symbol he/she wants to
 The game has been made as a thorough expert system
 The player can win the game, draw the game or will loose the game.
SYSTEM DESIGN

Classes used:
Note: here, classes are mentioned as activities since they are associated with xml pages
for designing.

The activities used are:


▪ MainActivity – used for
initiating login page.

The functions used are:


• onClick()- to enter X
or O.
• checkForWin()- to
check whether player 1 or player 2 has won .
• onCreate()- to start
activity (to put initialisation code).
• player1Wins()-to
specify the options menu for an activity.
• draw()-when the
match is draw.
• updatePointsText()
• resetBoard()
• resetGame()- to
resest the game.

UI Designing:
XML which is extended markup language which comes built in with android studio has
been used for designing everything in this app.

IMPLEMENTATION
The implementation of TIC-TAC-TOE project includes various java
and xml file, Source codes of which are given:

SOURCE CODE:
Content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_view_p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Player 1: 0"
android:textSize="30sp" />

<TextView
android:id="@+id/text_view_p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_p1"
android:freezesText="true"
android:text="Player 2: 0"
android:textSize="30sp" />

<Button
android:id="@+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="reset" />

</RelativeLayout>

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

<Button
android:id="@+id/button_00"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

</LinearLayout>

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

<Button
android:id="@+id/button_10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

</LinearLayout>

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

<Button
android:id="@+id/button_20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_21"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

<Button
android:id="@+id/button_22"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:freezesText="true"
android:onClick="onClick"
android:textSize="60sp" />

</LinearLayout></LinearLayout>

MainActivity.java:
package com.example.tictaktoe;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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;

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

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

Button buttonReset = findViewById(R.id.button_reset);


buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetGame();
}
});
}

@Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
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[][] field = new String[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
field[i][j] = buttons[i][j].getText().toString();
}
}

for (int i = 0; i < 3; i++) {


if (field[i][0].equals(field[i][1])
&& field[i][0].equals(field[i][2])
&& !field[i][0].equals("")) {
return true;
}
}

for (int i = 0; i < 3; i++) {


if (field[0][i].equals(field[1][i])
&& field[0][i].equals(field[2][i])
&& !field[0][i].equals("")) {
return true;
}
}

if (field[0][0].equals(field[1][1])
&& field[0][0].equals(field[2][2])
&& !field[0][0].equals("")) {
return true;
}

if (field[0][2].equals(field[1][1])
&& field[0][2].equals(field[2][0])
&& !field[0][2].equals("")) {
return true;
}

return false;
}

private void player1Wins() {


player1Points++;
Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
updatePointsText();
resetBoard();
}

private void player2Wins() {


player2Points++;
Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
updatePointsText();
resetBoard();
}

private void draw() {


Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
resetBoard();
}

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

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putInt("roundCount", roundCount);
outState.putInt("player1Points", player1Points);
outState.putInt("player2Points", player2Points);
outState.putBoolean("player1Turn", player1Turn);
}

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

roundCount = savedInstanceState.getInt("roundCount");
player1Points = savedInstanceState.getInt("player1Points");
player2Points = savedInstanceState.getInt("player2Points");
player1Turn = savedInstanceState.getBoolean("player1Turn");
}
}
SCREENSHOTS:
CONCLUSION

The 'Tie Tac Toe game is most familiar among all the age groups. Intelligence can be a
proper or Any purpose-driven decision maker. This basic idea has been suggested many
times. An algorithm of playing Tie 1ac Toe has been presented and tested that works in
efficient way. Overall the system works without any bugs

You might also like