Sample 12
Sample 12
Surya V.
NPS INTERNATIONAL, CHENNAI
Perumbakkam, Chennai – 600 100
ROLL NO: 11
CERTIFICATE
………………………………………….in the
subject Computer Science during the
academic year 2024 to 2025.
NPS International,
ACKNOWLEDGEMENT
This project would have not been possible without the guidance and the help of
several individuals who in one way or another contributed and extended their
valuable assistance in the preparation and completion of this study.
Firstly, I express utmost gratitude to our computer teacher, Mrs. Jeba Andrew
whose inputs and encouragement has been my inspiration as I hurdle over the
obstacle in the completion of this project work.
I specially thank my principal Mrs. Sudha Balan for her selfless interest in my
project.
I thank all the members of the family who always had a kind concern and
consideration regarding all my project and academic requirement.
Last but not the least, I thank all my class mates for all the cooperation and
resources they extended to me
S.no. Contents Page no.
1. SYNOPSIS 1
Objective
Introduction
Why I chose this project
2. JAVA PROGRAMING LANGUAGE 2
What is java?
Why did I use java?
3. FEATURES USED: 4
For loop
If-else statements
Ternary operators
Wrapper class
Hardware and software requirements
Packages used
4. ALGORITHM 7
5. PROGRAM 9
6. OUTPUT 14
7. BIBLIOGRAPHY 17
INDEX
SYNOPSIS
Objective:
The objective of my program is to create a double player Tic-Tac-
Toe game, where 2 users are able to play a game of tic tac toe with
each other, with a turn based input interface and output display. In
their turn, the users type in a number corresponding to the location
in the tic-tac-toe grid where they want to mark their “X” or “O”.
The input interface relies on a simple grid of 9 numbers
corresponding to the 9 squared grid of tic-tac-toe, that is displayed
on the screen after every turn to allow for an intuitive game
controls, while also displaying the preoccupied squares This classic
game is really brought to life by the small visual cues and details
that many users attach to tic-tac-toe in real life, and thus this allows
for a more enriching game experience. This game entirely relies on
the input from both the users which can be seen in the program
itself.
Introduction:
Tic-tac-toe or Xs and Os is a paper-and-pencil game for two players
who take turns marking the spaces in a three-by-three grid
with X or O. The player who succeeds in placing three of their marks
in a horizontal, vertical, or diagonal row is the winner.
1
Why I chose this Project:
Tic-Tac-Toe is a timeless classic with extremely simple rules.
However its beauty lies in its simplicity as it has a lot of strategy
behind it where every move makes a difference. I enjoy playing tic-
tac-toe as it is a very elegant game to play that encourages and
heavily relies on foresight, understanding of probability of different
outcomes and general awareness. reasoning skills while
simultaneously providing enjoyment. I felt it would be a great game
to try and recreate in Java since capturing the heart and soul of the
game, both graphical and algorithmically through simple characters
and symbols felt like a fun and challenging task.
2
Why did I use Java ?
JAVA is a very user-friendly programming language whose
functions and operations are very easy to learn and use.
JAVA is an object-oriented programming which makes the whole
program modular, flexible and extensible.
JAVA is platform independent, meaning that the user can run my
program in whatever system that he prefers. He/she is not restricted
to one particular operating system.
Using JAVA, I could use several operations and features in a
modular and systematic manner.
3
FEATURES USED
For Loop:
I used a number of for loops in my program, from initializing values
into the array, for terminating code if game exceeds 9 move limit, for
displaying the input interface and grid output, as well as for
checking if any player achieved the 3 in a row target of tic-tac-toe.
Example Instance 1:
Below is the for loop for initializing my array with values 1-9:
for(int i=0;i<3;i++){
for(int j=0;j<3;j++,c++){
ar[i][j]=ar[i][j].valueOf(c);
System.out.print(ar[i][j]+" ");
}
System.out.println("");
}
Example Instance 2:
Another example is where I used a for loop with 2 variables to check
all 3 rows and columns each, simultaneously to see if any of them
4
have 3 concurrent values. This saves a lot of lines of code compared
to checking all 6 rows and columns totally. This for loop is seen
below:
for(int i=0,j=0;(i<3 && j<3);i++,j++){
if((ar[0][j]==ar[1][j]) && (ar[0][j]==ar[2][j]))
ar[0][j]=ar[1][j]=ar[2][j]="|";
if((ar[i][0]==ar[i][1]) && (ar[i][0]==ar[i][2]))
ar[i][0]=ar[i][1]=ar[i][2]="-";
}
If - Else Statements:
A lot of if-else statements were used all around the code for any
form of checking.
Example Instance 3: To check which player’s turn it is to decide
who won:
if(ar[((input-1)/3)][((input-1)%3)]!="X" && ar[((input-1)/3)]
[((input-1)%3)]!="O"){
System.out.println((k%2==0)?"Player X wins.":"Player O
wins.");
System.exit(0);
}
Ternary Operators:
They helped save a lot of lines of code compared to if else
statements, thus I used them wherever I could.
Example Instance 4:
5
System.out.println((k%2==0)?"Player O wins.":"Player X wins.");
Wrapper class:
Example Instance 4:
I used the valueOf method to convert values of integer data type to
String data type:
ar[i][j]=ar[i][j].valueOf(c);
Hardware requirements:
Minimum: 64Mb main memory, Pentium II processor or equivalent
Recommended: 128Mb main memory, 400MHz Pentium III processor
or above
Software requirements:
J2SE 1.4.2 (Java 2 SDK version 1.4.2) or newer must be installed.
BlueJ versions up to version 1.3.0 also run on JDK 1.3.x. BlueJ
versions up to version 1.1.3 also run on JDK 1.2.2.
Packages used:
For my project I used java.util package. I used “util” package in
order to utilize its features, especially it’s input features. My program
comprises mainly of users input, hence making the “util” package
very necessary. I made use of the Scanner class of the utility package
to take inputs from the user.
6
ALGORITHM
Step 1: Start
Step 2: 3*3 grid array filled with numbers 1-9 using loop.
7
Step 7: New array with only the replaced characters is printed using
for loop in a 3*3 visual grid made by printing dashes and vertical
lines in the intervals, labelled as output display.
Step 8.1: All rows checked if they contain same character using
incrementing row counter. If so, all values inside said row
replaced with “-“.
Step 8.2: All columns checked if they contain same character
using incrementing column counter. If so, all values inside said
row replaced with “|“.
Diagonals checked for the same. If bottom right diagonal all
values replaced by “\” else replaced by “/”.
Step 11: Else if position is still “X” or “O”, repeat from step 4 till
users win or turn number no longer below 9.
8
PROGRAM
import java.util.*;
int c=1,input=0;
void main()
for(int j=0;j<3;j++,c++){
9
ar[i][j]=ar[i][j].valueOf(c); //since ar is a string array,
valueof function used to convert integer values of counter variable c
to string values
System.out.println("");
if(ar[((input-1)/3)][((input-1)%3)]=="X" || ar[((input-1)/3)]
[((input-1)%3)]=="O"){ //checking if position corresponding to
number inputted by user, is taken or not
System.out.println((k%2==0)?"Player O wins.":"Player X
wins."); //if position already taken, other player wins by checking
move turn to see which player is current player, where odd turn
means O and even means X
System.exit(0);
10
}
ar[((input-1)/3)][((input-1)%3)]=(k%2==0)?"X":"O";
//original array modified by replacing one of its numbers which the
user entered, with X or O depending on turn
System.out.println((k==0)?"Output display:":"");
for(int j=0;j<3;j++){
if(j<2)
System.out.print((ar[i][j]=="X" || ar[i][j]=="O")?ar[i]
[j]+" |":" |"); //displaying modified terms only
else
System.out.print((ar[i][j]=="X" || ar[i][j]=="O")?ar[i]
[j]+" ":" ");
System.out.println("");
11
if((ar[0][j]==ar[1][j]) && (ar[0][j]==ar[2][j]))
ar[i][0]=ar[i][1]=ar[i][2]="-";
ar[0][0]=ar[1][1]=ar[2][2]="\\";
ar[1][1]=ar[2][0]=ar[0][2]="/";
for(int j=0;j<3;j++){
System.out.print(ar[i][j]+" ");
System.out.println("");
12
System.out.println((k%2==0)?"Player X wins.":"Player O
wins."); //displays player win message
13
OUTPUT
14
Output grid pattern.
15
How the game progresses.
16
How the game can end by one player getting 3 in a row, along with the
visual cue in the subsequent input box displayed the 3 similar figures
getting striked out, as is seen in tic-tac-toe
17
BIBLIOGRAPHY
https://bluej.org/faq.html
https://cs.stackexchange.com/
https://www.geeksforgeeks.org/
https://en.wikipedia.org/wiki/Tic-tac-toe
18