Professional Documents
Culture Documents
INTRODUCTION
This case study “Tic-tac-toe” it is a game was implemented using java awt, swings and java
applets.
Java programming language was originally developed by Sun Microsystems which was initiated
by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform
(Java 1.0 [J2SE]).
The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and
its widespread popularity, multiple configurations were built to suit various types of platforms.
For example: J2EE for Enterprise Applications, J2ME for Mobile Applications.
The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is
guaranteed to be Write Once, Run Anywhere.
Java is −
Object Oriented: In Java, everything is an Object. Java can be easily extended since it is
based on the Object model.
Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP
Java, it would be easy to master.
Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly
on compile time error checking and runtime checking.
Interpreted: Java byte code is translated on the fly to native machine instructions and is
not stored anywhere. The development process is more rapid and analytical since the
linking is an incremental and light-weight process.
High Performance: with the use of Just-In-Time compilers, Java enables high
performance.
1. 1 Java AWT
AWT stands for Abstract Window Toolkit. It is a platform dependent API for creating
Graphical User Interface (GUI) for java programs.
Java AWT calls native platform (Operating systems) subroutine for creating components such as
textbox, checkbox, button etc. For example an AWT GUI having a button would have a different
look and feel across platforms like windows, Mac OS & Unix, this is because these platforms
have different look and feel for their native buttons and AWT directly calls their native
subroutine that creates the button. In simple, an application build on AWT would look like a
windows application when it runs on Windows, but the same application would look like a Mac
application when runs on Mac OS.
AWT is rarely used now days because of its platform dependent and heavy-weight nature. AWT
components are considered heavy weight because they are being generated by underlying
operating system (OS). For example if you are instantiating a text box in AWT that means you
are actually asking OS to create a text box for you.
All the elements like buttons, text fields, scrollbars etc. are known as components. In AWT we
have classes for each component as shown in the above diagram. To have everything placed on a
screen to a particular position, we have to add them to a container. A container is like a screen
wherein we are placing components like buttons, text fields, checkbox etc. In short a container
contains and controls the layout of components. A container itself is a component (shown in the
above hierarchy diagram) thus we can add a container inside container.
Types of containers:
As explained above, a container is a place wherein we add components like text field, button,
checkbox etc. There are four types of containers available in AWT: Window, Frame, Dialog and
Panel. As shown in the hierarchy diagram above, Frame and Dialog are subclasses of Window
class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container for holding
components. An instance of the Panel class provides a container to which to add components.
Frame: A frame has title, border and menu bars. It can contain several components like buttons,
text fields, scrollbars etc. This is most widely used container while developing an application in
AWT.
Swing is a part of Java Foundation classes (JFC), the other parts of JFC are java2D and Abstract
window toolkit (AWT). AWT, Swing & Java 2D are used for building graphical user interfaces
(GUIs) in java. In this tutorial we will mainly discuss about Swing API which is used for
building GUIs on the top of AWT and are much more light-weight compared to AWT.
JFrame – A frame is an instance of JFrame. Frame is a window that can have title, border,
menu, buttons, text fields and several other components. A Swing application must have a frame
to have the components added to it.
JPanel – A panel is an instance of JPanel. A frame can have more than one panels and each
panel can have several components. You can also call them parts of Frame. Panels are useful for
grouping components and placing them to appropriate locations in a frame.
JLabel – A label is an instance of JLabel class. A label is an unselectable text and images. If you
want to display a string or an image on a frame, you can do so by using labels.
JTextField – Used for capturing user inputs, these are the text boxes where user enters the data.
JPasswordField – Similar to text fields but the entered data gets hidden and displayed as dots on
GUI.
Applets are small Java applications that can be accessed on an Internet server, transported over
Internet, and can be automatically installed and run as a part of a web document. After a user
receives an applet, the applet can produce a graphical user interface. It has limited access to
resources so that it can run complex computations without introducing the risk of viruses or
breaching data integrity. Any applet in Java is a class that extends the java.applet.Applet class.
An Applet class does not have any main () method. It is viewed using JVM. The JVM can use
either a plug-in of the Web browser or a separate runtime environment to run an applet
application. JVM creates an instance of the applet class and invokes init() method to initialize an
Applet.
Applet class
Applet class provides all necessary support for applet execution, such as initializing and
destroying of applet. It also provide methods that load and display images and methods that load
and play audio clips.
An Applet Skeleton
Most applets override these four methods. These four methods forms Applet lifecycle.
init() : init() is the first method to be called. This is where variable are initialized. This
method is called only once during the runtime of applet.
start() : start() method is called after init(). This method is called to restart an applet after
it has been stopped.
stop() : stop() method is called to suspend thread that does not need to run when applet is
not visible.
destroy() : destroy() method is called when your applet needs to be removed completely
from memory.
Tic-tac-toe is a simple, two-player game that, if played optimally by both players, will always
result in a tie. The game is also called noughts and crosses or Xs and Os.
Tic-tac-toe is a game that is traditionally played by being drawn on paper, and it can be played
on a computer or on a variety of media. Other games, such as Connect 4, are based on this
classic.
History of Tic-Tac-Toe
An early variation of the game was played in the Roman Empire, around the 1st century B.C. It
was called "Terni lapilli," which means "three pebbles at a time." The game's grid markings have
been found chalked all over Roman ruins. Evidence of the game was also found in ancient
Egyptian ruins. The first print reference to "noughts and crosses," the British name for the game,
appeared in 1864. The first print reference to a game called "tick-tack-toe" occurred in 1884 but
referred to a children's game played on a slate.
A relatively simple game usually played on a grid of 3-by-3 squares, tic-tac-toe is mainly
enjoyed by children. Tic-tac-toe can be made significantly more complex by increasing the size
of the board to 4-by-4, 5-by-5, or even up to a 20-by-20 grid.
Gameplay
The goal of tic-tac-toe is to be the first player to get three in a row on a 3-by-3 grid or four in a
row in a 4-by-4 grid.
To start, one player draws a board, creating a grid of squares, usually 3-by-3 or 4-by-4.In a 3-by-
3 grid game, the player who is playing "X" always goes first. Players alternate placing Xs and Os
on the board until either player has three in a row, horizontally, vertically, or diagonally or until
all squares on the grid are filled. If a player is able to draw three Xs or three Os in a row, then
that player wins. If all squares are filled and neither player has made a complete row of Xs or Os,
then the game is a draw.
One of the game's best strategies involves creating a "fork," which is placing your mark in such a
way that you have the opportunity to win two ways on your next turn. Your opponent can only
block one, thereby, you can win after that. The gameplay is the same if you are playing on a 4-
by-4 grid. The "X" player goes first. And, players alternate placing Xs and Os on the board until
a row is completed horizontally, vertically, or diagonally, or all 16 squares are filled. If all 16
squares are filled and neither player has four in a row, the game is a draw.
Other Variants
Tic-tac-toe can be also be played on a 5-by-5 grid with each player trying to get five in a row.
The game can also be played on larger grids, such as 10-by-10 or even 20-by-20. For any grid of
6-by-6 or greater, it might be best to make your goal to get five in a row. This turns the basic
game of tic-tac-toe into a much more complex game with similarities to the board game Pente,
meaning "five" in Greek. Similarly, the goal of Pente is for a player to score five marks in a row.
2. HARDWARE AND SOFTWARE SPECIFICATION
HARDWARE SPECIFICATIONS
The selection of hardware configuration is very important task related to the software
development. Insufficient RAM may affect adversely on the speed and efficiency of the entire
system. The processor should be powerful to handle the entire operations. The hard disk should
have sufficient capacity to store the file applications.
RAM : 2 GB
Mouse : 3 Buttons
SOFTWARE SPECIFICATIONS
The software is desired for JAVA version 7 or later. It can also work in Windows 7 or later
version.
OS : Windows 7
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
import java.applet.*;
// Initialize
topPanel.setLayout(new FlowLayout());
slider.setMajorTickSpacing(1);
slider.setPaintTicks(true);
slider.addChangeListener(this);
oButton.addActionListener(this);
xButton.addActionListener(this);
add(topPanel, BorderLayout.NORTH);
setVisible(true);
lineThickness = slider.getValue();
board.repaint();
// Change color of O or X
if (e.getSource()==oButton) {
if (newColor!=null)
oColor=newColor;
else if (e.getSource()==xButton) {
Color newColor = JColorChooser.showDialog(this, "Choose a new color for X", xColor);
if (newColor!=null)
xColor=newColor;
board.repaint();
public Board() {
addMouseListener(this);
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
g2d.setPaint(Color.WHITE);
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(lineThickness));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
double xpos=(i%3+0.5)*w/3.0;
double ypos=(i/3+0.5)*h/3.0;
double xr=w/8.0;
double yr=h/8.0;
if (position[i]==O) {
g2d.setPaint(oColor);
else if (position[i]==X) {
g2d.setPaint(xColor);
int ypos=e.getY()*3/getHeight();
int pos=xpos+3*ypos;
position[pos]=O;
repaint();
repaint();
// Computer plays X
void putX() {
if (won(O))
newGame(O);
else if (isDraw())
newGame(BLANK);
// Play X, possibly ending the game
else {
nextMove();
if (won(X))
newGame(X);
else if (isDraw())
newGame(BLANK);
return true;
return false;
&& position[(a+b)/2]==player;
void nextMove() {
do
r=random.nextInt(9);
while (position[r]!=BLANK);
position[r]=X;
if (result>=0)
return result;
return -1;
// are occupied by player and the third is blank, then return the
return b;
return a;
return -1;
boolean isDraw() {
if (position[i]==BLANK)
return false;
return true;
repaint();
String result;
if (winner==O) {
++wins;
else if (winner==X) {
++losses;
result = "I Win!";
else {
result = "Tie";
++draws;
if (JOptionPane.showConfirmDialog(null,
!=JOptionPane.YES_OPTION) {
wins=losses=draws=0;
position[j]=BLANK;
if ((wins+losses+draws)%2 == 1)
nextMove();
</applet>*/