OOPS PROJECT REPORT Sheraz 21

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

TIC TAC TOE GAME

INTRODUCTION TO OBJECT ORIENTED PROGRAMING


CE(102L)

Submitted by SHERAZ JABBAR

GROUP MEMBERS NAME:


SHERAZ JABBAR ROLLNO: 2020F-EL-021
ABDUL MOEEZ KK ROLL NO: 2020-EL-069
INDER KUMAR ROLL NO : 2020-EL-070

Semester Project Report


Department of Electrical Engineering
Sir Syed University Of Engineering and Technology, Karachi.
TABLE OF CONTENTS

Page ACKNOWLEDGMENT 3
ABSTRACT 3
INTRODUCTION 4
THEORETICAL BACKGROUND 4
METHODOLOGY 4

FLOW CHART 6
CONCLUSION AND RESULT 7

PROJECT CODE 8

PROJECT OUTPUT 13

REFERENCE 14

Department of Electrical Engineering Sir Syed University of Engineering and Technology


ACKNOWLEDGEMENT :

We are the students of SIR SYED UNIVERSITY OF ENGINEERING AND


TECHNOLOGY (ELECTRICAL ENGINEERING DEPARTMENT) who have
made this Project “TIC TAC TOE GAME” express our sincere praise and thanks to
ALLAH for his blessings which He showered on us throughout our lives, especially
for this project work

Our special Thanks to MISS RAIYAH and MISS NOREEN .

They have given us the chance to work on this project. They admire our abilities and
increased our knowledge about practical life. They not only gives us there precious
time but also shared there valuable thoughts about the topic. The supervision and
support that they gave truly help the progression and smoothness of the project. The
co-operation is much indeed appreciated.

ABSTRACT

The project title is ‘TICTAC TOE GAME’ In first step we are taking input from
users that which player is starting the game they have two write number between 1
and 2, because we have two players in this game player 1 symbol is ‘x’ and player 2
symbol is ‘o’. user have to decide themselves that who is payer 1 and who is player
2.
If player 1 is starting the game. It is necessary that player 1 must enter number
between 1 to 9 and symbol ‘x’ and if player 2 starting the game. It is necessary that
player 2 must enter number between 1 to 9 and symbol ‘o’. if Player 1 starts the
game and he enter any number between 1 to 9 with symbol ‘x’ the program starts

Department of Electrical Engineering Sir Syed University of Engineering and Technology


working on this and change that number with symbol ‘x’ in show function we have
in our project (in the form of array) after this turn is shifted towards player 2. And
player 2 have to enter number between 1 to 9 accept the number which is enter once
by opponent player before his turn with symbol ‘o’. The program starts working on
this and change that number with symbol ‘o’ in show function we have in our project
(in the form of array) after this turn is shifted towards player 1 and the process done
that player 1 done in his 1st turn.

INTRODUCTION

Tic-tac-toe (American English), noughts and crosses (Commonwealth 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.
Theoretical Background
An early variation of tic-tac-toe was played in the Roman Empire, around the
first century BC. It was called terni lapilli (three pebbles at a time) and instead
of having any number of pieces, each player only had three, thus they had to
move them around to empty spaces to keep playing.

METHODOLOGY :
Following are the steps that are involved in the project of TIC TAC TOE GAME.

Step no 1:
In first step we are taking input from users that which player is starting the game
they have two write number between 1 and 2, because we have two players in this
game player 1 symbol is ‘x’ and player 2 symbol is ‘o’. user have to decide
themselves that who is payer 1 and who is player 2.

Department of Electrical Engineering Sir Syed University of Engineering and Technology


Step no 2:
If player 1 is starting the game. It is necessary that player 1 must enter number
between 1 to 9 and symbol ‘x’ and if player 2 starting the game. It is necessary that
player 2 must enter number between 1 to 9 and symbol ‘o’.
Step no 3:

if Player 1 starts the game and he enter any number between 1 to 9 with symbol ‘x’
the program starts working on this and change that number with symbol ‘x’ in
show function we have in our project (in the form of array) after this turn is shifted
towards player 2. And player 2 have to enter number between 1 to 9 accept the
number which is enter once by opponent player before his turn with symbol
‘o’. The program starts working on this and change that number with symbol ‘o’ in
show function we have in our project (in the form of array) after this turn is shifted
towards player 1 and the process done that player 1 done in his 1st turn.

Step no 4:

if Player 2 starts the game and he enter any number between 1 to 9 with
symbol ‘o’ the program starts working on this and change that number with symbol
‘o’ in show function we have in our project (in the form of 2 array) after this turn is
shifted towards player 1. And player 1 have to enter number between 1 to 9 accept
the number which is enter once by opponent player before his turn with symbol
‘x’. The program starts working on this and change that number with symbol ‘x’ in
show function we have in our project (in the form of 2 array) after this turn is
shifted towards player 2 and the same done that player 2 done in his 1st turn.

Step no 5:
Are same with symbol ‘x’ or ‘o’ and similarly until all number between
1 and 9 are filled with symbols ‘x’ or ‘o’.
step no 6:
If any column, any row and any diagonal is match or same with symbol ‘x’ then
the player with symbol ‘x’ that is player 1 won the game. And if any column, any
row and any diagonal is match or same with symbol ‘o’ then player with symbol
‘o’ that is player 2 won the game.

Department of Electrical Engineering Sir Syed University of Engineering and Technology


Step no 7:

If not any diagonal, any column and row match or same with symbol
‘x’ or ‘o’ means game is draw and no one is the winner. Step
no 8:
User have choice if they want to play the game again they need to press y and if
they have not to play the game again they need to press n then press step no 3 OR
step no 4 repeat itself until any column, any row and any diagonal.
Flow chart:

Department of Electrical Engineering Sir Syed University of Engineering and Technology


CONCLUSION AND RESULT:
While making this project, we noticed countless things that helped in nourishing and
polishing our skills and contributed a lot in our life. We learn about window base
programing. New tricks and tip to play this game. How to create a button and implicit
that button .How a label and text field work. How action listener performer can
actlike in this project. Overall there was to many thing to learn .

Department of Electrical Engineering Sir Syed University of Engineering and Technology


PROJECT CODE:

package tictactoegame;
import java.util.*;

public class TicTacToeGame {

static String[] board;


static String turn;

// CheckWinner method will


// decide the combination
// of three box given below.
static String checkWinner()
{

for (int a = 0; a < 8; a++) {


String line = null;

switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;

Department of Electrical Engineering Sir Syed University of Engineering and Technology


case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}

// For O winner
else if (line.equals("OOO")) {
return "O";
}
}

for (int a = 0; a < 9; a++) {


if (Arrays.asList(board).contains(
String.valueOf(a + 1))) {
break;
}
else if (a == 8) {
return "draw";
}
}

// To enter the X Or O at the exact


place on board.

System.out.println(

Department of Electrical Engineering Sir Syed University of Engineering and Technology


turn + "'s turn; enter a slot number to
place "
+ turn + " in:");
return null;
}

// To print out the board.


/* |---|---|---|
|1|2|3|
|-----------|
|4|5|6|
|-----------|
|7|8|9|
|---|---|---|*/

static void printBoard()


{
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " |
"
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " |
"
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[6] + " |
"
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}

public static void main(String[] args)


{

Department of Electrical Engineering Sir Syed University of Engineering and Technology


Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String winner = null;

for (int a = 0; a < 9; a++) {


board[a] = String.valueOf(a + 1);
}

System.out.println("Welcome to 3x3
Tic Tac Toe.");
printBoard();

System.out.println(
"X will play first. Enter a slot number
to place X in:");

while (winner == null) {


int numInput;

// Exception handling.
// numInput will take input from user
like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error
"Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <=
9)) {
System.out.println(
"Invalid input; re-enter slot
number:");
continue;
}
}
catch (InputMismatchException e) {

Department of Electrical Engineering Sir Syed University of Engineering and Technology


System.out.println(
"Invalid input; re-enter slot
number:");
continue;
}

// This game has two player x and O.


// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;

if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}

printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot
number:");
}
}

// If no one win or lose from both


player x and O.
// then here is the logic to print
"draw".
if (winner.equalsIgnoreCase("draw"))
{
System.out.println(
"It's a draw! Thanks for playing.");

Department of Electrical Engineering Sir Syed University of Engineering and Technology


}

// For winner -to display


Congratulations! message.
else {
System.out.println(
"Congratulations! " + winner
+ "'s have won! Thanks for playing.");
}
}
}

OUTPUT:

Department of Electrical Engineering Sir Syed University of Engineering and Technology


REFRENCES
.
Book: Single Author Jdk1.3.1_001

Jdk8

Department of Electrical Engineering Sir Syed University of Engineering and Technology

You might also like