0% found this document useful (0 votes)
15 views22 pages

Sample 12

tic tac toe

Uploaded by

kedarprj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

Sample 12

tic tac toe

Uploaded by

kedarprj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Grade 11 Computer Science Project

Surya V.
NPS INTERNATIONAL, CHENNAI
Perumbakkam, Chennai – 600 100

ROLL NO: 11
CERTIFICATE

Certified that this is a bonafide record of


Hitesh
project work done by

………………………………………….in the
subject Computer Science during the
academic year 2024 to 2025.

Date: 28.11.2024 Teacher-in-Charge

Submitted to the Practical Examination in

NPS International,

Chennai held on ………………………..


Principal Examiner

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.

JAVA PROGRAMING LANGUAGE


What is Java?
Java was first developed by James Gosling at Sun Microsystems,
which is now a part of Oracle Corporation. It was released in 1995
as a part of Sun Microsystems' Java platform. The language has
developed much of its syntax from C and C++. Java is a general-
purpose language, and works on the principle of WORA: Write
Once, Run Anywhere. Java utilises a certain type of code; bytecode,
to allow it to do so, along with a Java Virtual Machine (JVM). Java
is also a class-based, object-oriented language.

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.

Step 3: Array is displayed as user interface in a typical 3*3 matrix


fashion.

Step 4: User inputs a number corresponding to a particular position


in the array in step 2 in for loop containing turn number.

Step 5: Checks if position corresponding to value inputted by user is


already taken by another “X” or “O”

 Step 5.1: If so, game terminates, other user wins, display


message sent.

Step 6: : Inputted value is checked for even or odd using modulus,

 Step 6.1: If even, position containing value inputted by user is


replaced by “X”.
 Step 6.2: If odd, the same position in above step, is replaced by
an “O”.

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: Check for 3 characters in a row in array:

 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 9: Array displayed in 3*3 matrix form.

Step 10: If position corresponding to most recent user input is neither


X nor O:

 Step 10.1: Display player X winning if turn number is even.


 Step 10.2: Display player X winning if turn number is odd.

Step 11: Else if position is still “X” or “O”, repeat from step 4 till
users win or turn number no longer below 9.

Step 12: End

8
PROGRAM

import java.util.*;

public class tictactoe

Scanner sc=new Scanner(System.in);

String ar[][]=new String[3][3]; //declaring array serving as 3*3


grid for tic-tac-toe

int c=1,input=0;

void main()

System.out.println("Input Interface: ");

for(int i=0;i<3;i++){ //first for loop for initializing values 1 to


9 to array using counter variable c

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.print(ar[i][j]+" "); //array is printed as well to


act as an input interface for users where each of its numbers indicates
that position in the array, and consequently the grid

System.out.println("");

for(int k=0;k<9;k++)//second, main for loop containing number


of turns in variable k so that game ends in 9 turns

input=sc.nextInt(); //accepting number from user

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("Invalid Input, Space already taken.");

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 i=0;i<3;i++){ //for loop to print modified array in the


form of a grid

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

System.out.println((i<2)?"__|__|__":""); //symbols used to


make grid pattern

for(int i=0,j=0;(i<3 && j<3);i++,j++){ //checking if either


any of the rows or columns have all 3 values equal, thus ending game

11
if((ar[0][j]==ar[1][j]) && (ar[0][j]==ar[2][j]))

ar[0][j]=ar[1][j]=ar[2][j]="|"; symbol to indicate


crossed out vertical line akin to crossed out lines in real life tic tac
toe after a player wins

if((ar[i][0]==ar[i][1]) && (ar[i][0]==ar[i][2]))

ar[i][0]=ar[i][1]=ar[i][2]="-";

if(ar[1][1]==ar[2][2] && ar[1][1]==ar[0][0]) //checking


diagonal for equal terms

ar[0][0]=ar[1][1]=ar[2][2]="\\";

if(ar[1][1]==ar[2][0] && ar[1][1]==ar[0][2])

ar[1][1]=ar[2][0]=ar[0][2]="/";

for(int i=0;i<3;i++){ //printing input interface again

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

System.out.print(ar[i][j]+" ");

System.out.println("");

if(ar[((input-1)/3)][((input-1)%3)]!="X" && ar[((input-1)/3)]


[((input-1)%3)]!="O"){ //checking if any player won for game to end

12
System.out.println((k%2==0)?"Player X wins.":"Player O
wins."); //displays player win message

System.exit(0); //exits code if player wins

13
OUTPUT

Input interface of the game.

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

Game ending by virtue of player “O” making an invalid move and


giving an input for a location already taken up by player “X”.

17
BIBLIOGRAPHY

“Computer Science with java”-Sumita Arora

https://bluej.org/faq.html

https://cs.stackexchange.com/

https://www.geeksforgeeks.org/

https://en.wikipedia.org/wiki/Tic-tac-toe

18

You might also like