0% found this document useful (0 votes)
25 views16 pages

Tic Tac Toegame 1

This project report details the development of a Tic-Tac-Toe game using inheritance and GUI design in Java, aimed at fulfilling the requirements for a laboratory course in Object-Oriented Programming. The game utilizes the Java Swing library for its graphical interface and implements strategies to avoid losses using genetic algorithms. The report includes sections on the game's design, source code, and methodologies used, along with acknowledgments and references.

Uploaded by

238r1a6667
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)
25 views16 pages

Tic Tac Toegame 1

This project report details the development of a Tic-Tac-Toe game using inheritance and GUI design in Java, aimed at fulfilling the requirements for a laboratory course in Object-Oriented Programming. The game utilizes the Java Swing library for its graphical interface and implements strategies to avoid losses using genetic algorithms. The report includes sections on the game's design, source code, and methodologies used, along with acknowledgments and references.

Uploaded by

238r1a6667
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/ 16

A

PROJECT REPORT

ON

TIC-TAC-TOE GAME USING INHERITANCE AND GUI DESIGN


Submitted to the CMR Engineering College, Hyderabad in partial fulfilment of the
requirement for the award of the Laboratory of
OOP THROUGH JAVA
Of
II-B.Tech. I-Semester
in
Computer Science and Engineering (AI&ML)
Submitted by:
N.CHARITH BHARAGAV: 238R1A6699
M.ANUSHA: 238R1A6693
U.AVINASH: 238R1A66C7
SYED SHAZIA : 238R1A66B9

Under the Esteemed Guidance of

Mr.R.S.Panda

CMR ENGINEERING COLLEGE

(UGC AUTONOMUS)
1
(Approved by AICTE, Affiliated to JNTU, Kukatpally, Hyderabad)

Kandlakoya, Medchal Road, Hyderabad.

CMR INSTITUTE OF TECHNOLOGY


(UGC AUTONOMUS)
(Approved by AICTE, Affiliated to JNTU, Kukatpally, Hyderabad)
Kandlakoya, Medchal Road, Hyderabad

Department of Computer Science and Engineering (AI&ML)

CERTIFICATE
The results embodied in this project report have not been submitted to any other institute or
university for the award of any other degree or diploma. This is to certify that a Project
entitled with: “TIC-TAC-TOE GAME” is being submitted by:

N.CHARITH BHARAGAV: 238R1A6699


M.ANUSHA: 238R1A6693
U.AVINASH: 238R1A66C7
SYED SHAZIA : 238R1A66B9

2
In partial fulfillment of the requirement for award of the OOP through Java lab of II-B.Tech I-
Semester in CSE(AI&ML) towards a record of a bonafide work carried out under our guidance
and supervision.

Signature of Faculty Signature of HOD


Mr. Radhey Shyam Panda Dr. M.Kumar Swamy

3
ACKNOWLEDGEMENT

I take this opportunity to thank all the magnanimous persons who rendered their full
support to my work the pleasure, the achievement, the glory, the satisfaction, the appreciation
and the construction of this regular schedule spared their valuable time for me. This
acknowledgement is not just a position of words but also an account of indictment. They have
been a guiding and a source of inspiration towards the completion of this project.

I express my gratitude to Dr.SRINIVASULA REDDY (Principal), CME ENGINEERING COLLEGE for


giving me an opportunity to do this project work.

I am thankful to Dr.M.Kumar Swamy, Head of the department for his constant


encouragement and lab facilities provided to complete the project.

I would like to thank my internal guide Mr.Radhey Shyam Panda who with his continuous
efforts, unfailing interest, constant support and providing the right infrastructure helped me in
completing this project work.

I would like to thank to all the faculty members of EEE department for their valuable
suggestions and support which helped me in mounding this project into a comprehensive one.

N.CHARITH BHARAGAV: 238R1A6699


M.ANUSHA: 238R1A6693
U.AVINASH: 238R1A66C7
SYED SHAZIA : 238R1A66B9

4
DECLARATION

I hereby declare that this Seminar work titled “TIC-TAC-TOE GAME USING

INHERITANCE AND GUI DESIGN” is submitted by me to thE CMR ENGINEERING COLLEGE. This
project was done by our own efforts and is not submitted to any other university for the award of the
degree.

N.CHARITH BHARAGAV: 238R1A6699


M.ANUSHA: 238R1A6693
U.AVINASH: 238R1A66C7
SYED SHAZIA : 238R1A66B9

5
INDEX:

S.NO PARTICULARS PAGE NO


1 ABSTRACT 7
2 INTRODUCTION 8
3 EXPLANATION 9
4 DIFFERENT METHODS USED 10
5 SOURCE CODE 13
6 OUTPUT 14
7 CONLUSION 15

6
TIC-TAC-GAME

ABSTRACT

The game of Tic-tac-toe is one of the most commonly known games.


This game does not allow one to win all the time and a significant
proportion of games played results in a draw. Thus, the best a player
can hope is to not lose the game. This study is aimed at evolving a
number of no-loss strategies using genetic algorithms and comparing
them with existing methodologies. To efficiently evolve no-loss
strategies, we have developed innovative ways of representing and
evaluating a solution, initializing the GA population, developing GA
operators including an elite preserving scheme. Interestingly, our GA
implementation is able to find more than 72 thousand no-loss
strategies for playing the game. Moreover, an analysis of these
solutions has given us insights about how to play the game to not
lose it. Based on this experience, we have developed specialized
efficient strategies having a high win-to-draw ratio. The study and its
results are interesting and can be encouraging for the techniques to
be applied to other board games for finding efficient strategies.

7
Introduction :
In this article, we will demonstrate how to build Tic Tac Toe Game using Java
Swing library. This Tic Tac Toe Game allows you to Play a game easily using a
graphical user interface (GUI) application. This game is develop with the Java
Swing library which offers a set of components that can be use to build a
Graphical user interface(GUI). This game has an easy and simple design with
buttons to play the game. So grab a seat, pour a cup of coffee and start playing
Tic Tac Toe Game.

Explanation:
This Tic Tac Toe Game is a Graphical user interface(GUI) Game that allows user
to easily Play the game using buttons. Users may very quickly play the Game
using the system simple design. The Tic Tac Toe Game has simple design with
buttons. The player can click on the button one by one to play the game.
The users may play the game using buttons. This game can be played by 2
users. The 1st player can click on any button the value of the button will be ‘x’.
When the 2nd player clicks the button the value of button will be ‘o’. The
winner of the game is decided based on the rows, column or diagonal values
should be same. 8
The Java Swing library which provides flexible set of components is used to
build the Tic Tac Toe Game. The application makes use of Swing components
such as Jbutton, JPanel and JFrame among many other Swing components.
Button event is handled by the ActionListener interface and button events are
defined using ActionListener’s actionPerformed() method. The winner of the
game is checked using checkForWinner() method.
Methods used in the project:
actionPerformed():
The action or events of the buttons are handled using this method. Buttons in
the system to generate the value of button is handled using this method. This
method describes how the component should behave after the button is click.
checkForWinner():
This is a user defined method to check the winner of game. To win the game
the any one player needs the value of row, column or diagonal should be same.
If values of row, column or diagonal is not same it pop ups the dialog box with
‘tie’ message.
resetGame():
This is a user defined method to reset the game when the winner of game is
declared. The value of buttons are assigned to null when this method is
triggered.

Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class tic_tac_toe implements ActionListener {


private JFrame frame;
private JPanel panel;
private JButton[] buttons = new JButton[9];
9
private boolean xTurn = true;
public tic_tac_toe() {
frame = new JFrame("Tic-Tac-Toe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();


panel.setLayout(new GridLayout(3, 3));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

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


buttons[i] = new JButton();
buttons[i].setFont(new Font("Arial", Font.PLAIN, 40));
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}

frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {


JButton button = (JButton) e.getSource();
if (xTurn) {
button.setText("X");
10
} else {
button.setText("O");
}
button.setEnabled(false);
xTurn = !xTurn;

checkForWinner();
}

public void checkForWinner() {


// Check rows
for (int i = 0; i < 9; i += 3) {
if (buttons[i].getText().equals(buttons[i+1].getText()) &&
buttons[i].getText().equals(buttons[i+2].getText()) && !buttons[i].isEnabled()) {
JOptionPane.showMessageDialog(frame, buttons[i].getText() + "
wins!");
resetGame();
return;
}
}

// Check columns
for (int i = 0; i < 3; i++) {
if (buttons[i].getText().equals(buttons[i+3].getText()) &&
buttons[i].getText().equals(buttons[i+6].getText()) && !buttons[i].isEnabled()) {
JOptionPane.showMessageDialog(frame, buttons[i].getText() + "
wins!");
resetGame(); 11
return;
}
}

// Check diagonals
if (buttons[0].getText().equals(buttons[4].getText()) &&
buttons[0].getText().equals(buttons[8].getText()) && !buttons[0].isEnabled()) {
JOptionPane.showMessageDialog(frame, buttons[0].getText() + "
wins!");
resetGame();
return;
}
if (buttons[2].getText().equals(buttons[4].getText()) &&
buttons[2].getText().equals(buttons[6].getText()) && !buttons[2].isEnabled()) {
JOptionPane.showMessageDialog(frame, buttons[2].getText() + "
wins!");
resetGame();
return;
}

// Check for tie


boolean tie = true;
for (int i = 0; i < 9; i++) {
if (buttons[i].isEnabled()) {
tie = false;
break;
} 12
}
if (tie) {
JOptionPane.showMessageDialog(frame, "Tie game!");
resetGame();
}
}

public void resetGame() {


for (int i = 0; i < 9; i++) {
buttons[i].setText("");
buttons[i].setEnabled(true);
}
xTurn = true;
}

public static void main(String[] args) {


new tic_tac_toe();
}
}

13
Output:

14
CONCLUSION:
The player who succeeded in placing three respective marks in a
horizontal, vertical, or diagonal row wins the game. The Tic Tac Toe is
a great way to pass your free time whether you're standing in a line
or spending time with your kids.
REFERENCE:
1. Oracle JavaFX Documentation
Oracle. (2023). JavaFX Documentation. Retrieved from
https://openjfx.io/
 This was the primary resource for learning how to use JavaFX to
build graphical user interfaces (GUIs). It provided valuable
information on components such as RadioButton, TextArea,
Button, and event handling. 15
2. Bloch, J. (2008). Effective Java (2nd ed.). Addison-Wesley.
 This book by Joshua Bloch was instrumental in understanding
the core concepts of Java programming, including best practices
for writing clean and efficient code.
3. Sierra, K., & Bates, B. (2005). Head First Java (2nd ed.). O'Reilly
Media.
 This book helped in solidifying foundational Java concepts,
focusing on object-oriented programming and event-driven
development.
4. JavaFX: Introduction to GUI Programming. (2020). TutorialsPoint.
Retrieved from https://www.tutorialspoint.com/javafx/
 This online tutorial provided practical examples and code
snippets to help set up JavaFX projects, including event
handling and UI component management.
5. Oracle Java Documentation. (2023). The Java™ Programming
Language. Retrieved from https://docs.oracle.com/javase/8/docs/
 Oracle’s official Java documentation provided detailed
explanations of Java language features, standard libraries, and
the Java API, which were essential for implementing event-
driven programming in the survey system.

16

You might also like