Professional Documents
Culture Documents
Lecturer: Dr.Houria
Course : ECE
Name : Maha Nour
VPa ge
Problem statement 3
Implementation part 1
TicTacToe Class 11
GameTest Class 16
Implementation part 2
TicTacToe Class 19
JTic class design 23
JTic class source 23
Conclusion 50
uPa ge
The general goal of this project is to join all the experience from ECE 102
and ECE 202 to make a complete program.
Tic-Tac-Toe game needs a big box divided in 3 rows and in 3 columns to create 9
equal empty cells. Also it requires 2 players one of them is X-player and the other
one will be O-player. In each player turn he/she should fill only one empty cell
with X if it was X-player turn , and with O if it was O-player turn. The winner in
this game who can fill a 3 cells in a straight line with the same character. Either
vertically or horizontally or even in a slope form.
To design this game we use to integrated design environments (IDE) which they
are Eclips and NetBeans.
Eclips was un professional, time consuming and tiresome in short it was not
friendly to use. But in the other hand Net Beans was friendly to use it has buttons
and zero requirements from the user.
ÿPa ge
Ë
!
//Data fields:
// Methods:
public TicTacToe()
// constructor method toCreates and Initializes the board to empty.
public void play(int i, int j)
//initialize the row to the row entered by the user
// initialize the column to the column entered by the user .
public char possition ()
// in to fill the game board position (row, column) with X or O character.
public boolean checkWin()
// returns true if there is a winner and false I f not.
public boolean noWinner()
// test if there is no winner and the game should continue.
//it returns true if there is no winner.
public boolean checkEquals()
// test if all the game board positions are filled with out a winner.
public void displayBoard()
// to display the game board .
public void newGame()
// to Clear the board for a new game.
public char getPlayer()
mPa ge
// Methods:
Ë V
!
//Data fields:
// Methods:
public TicTacToe()
// Creates and Initializes the board to empty.
public void play(int i, int j)
//initialize the row to the row entered by the user
// initialize the column to the column entered by the user .
public char possition ()
// in to fill the game board position (row, column) with X or O character.
public boolean checkWin()
// returns true if there is a winner and false If not.
public boolean noWinner()
// test if there is no winner and the game should continue.
//it returns true if there is no winner.
public boolean checkEquals()
// test if all the game board positions are filled with out a winner.
public void newGame()
// to Clear the board for a new game.
public char getPlayer()
// Returns the character corresponding to the current player (X or O)
public void switchPlayer()
// to switch the player by changing the current player from X to O or from O
to X.
Pa ge
// variables :
JButton board [][]; // board is array of type JButton
// methods:
all the buttons from 2 to 9 have the same Pseudo code but the difference
will be in initializing the board position. The other difference will be in display
the player character in button(number) instead of button1.
" #
import javax.swing.JOptionPane;
//Data fields:
// Methods:
if((gameBoard[row][0]==gameBoard [row][1])&&(gameBoard[row][1]==gameBoar
d[row][2]))
return true;
else
// check if there is a winner in a slope
form :
if(gameBoard [1][1]=='X' || gameBoard [1][1]=='O'
)
{
if
((gameBoard[0][0]==gameBoard [1][1])&&( gameBoard[1][1]==gameBoard [2][2]))
return true;
else
if
((gameBoard[0][2]==gameBoard [1][1])&&( gameBoard[1][1]==gameBoard [2][0]))
return true;
else
return false;
if
((gameBoard[0][2]=='X'||gameBoard[0][2]=='O')&&(gameBoard[0][2]=='X'||gameBoa
rd[0][2]=='O')&&(gameBoard [0][2]=='X'||gameBoard [0][2]=='O'))
return true;
// this method of type void to Clear the board for a new game.
public void newGame()
{
player='X';// initialize the first player in the new game to X
gameBoard= new char [3][3]; // make new game board with the same
size 3 x 3.
ÿ P a g e
// this method to switch the player by changing the current player from
X to O or from O to X
public void switchPlayer()
{
if (player =='X')
player = 'O';
else
player = 'X';
// this method of type integer to increment and get the number of users
tries.
public int incrementNO()
{
return noOfTries ++;
}
//the method of type integer to save and get all the rows entered by
the user.
public int getRowArray()
{
return r[noOfTries ]=row; // assign and get number of users tries
row array to the current row.
}
//the method of type integer to save and ge t all the column entered by
the user.
public int getColumnArray()
{
return c[noOfTries ]=column;// assign and get number of users
tries column array to the current column.
}
}
m P a g e
import javax.swing.JOptionPane;
// declare variables:
String answer;
String str;
int numOfLoob=0;
int r;
int c;
do
{
TicTacToe game= new TicTacToe(); // create a new object game
from TicTacToe class.
game.displayBoard(); // display the game board.
do
{
// get the row & make sure it is 0 or 1 or 2.
do{
//get the row from the user
str =
JOptionPane. ×
(game.getPlayer()+ "-player, Enter the row you want
to play at." );
r = Integer. ×
(str);// convert it from
string to integer.
do{
//get a new row from the user
str =
JOptionPane. ×
(game.getPlayer()+ "-player, Enter the row you want
to play at." );
r = Integer. ×
(str);
game.play(r,c);
game.getRowArray();
- P a g e
game.getColumnArray();
}
}
game.incrementNO(); // call incrementNO method from
TicTacToe class by using game object
game.possition (); // call possition method.
game.displayBoard(); // display the game board again
after the entered position.
do
{
// ask the user if he / she want to play an new game.
answer = JOptionPane. ×
("DO you want to
play a new game ? " );
}while
((!answer.equalsIgnoreCase( "yes"))&&(!answer.equalsIgnoreCase( "no")));// ask
the user again if he/she typed something wrong.
}
P a g e
Ë V !
//Data fields:
// Methods:
public void play(int i, int j) // The current players plays at position (i, j)
{
row=i; //initialize the row to row entered by the user .
column=j; // initialize the column to column entered by the user.
}
{
// test if there is a winner in vertical form :
if
((gameBoard[0][column]==gameBoard[1][column])&&(gameBoard[1][column]==game
Board[2][column]))
return true;
else
// test if there is a winner in horizontal form :
if((gameBoard[row][0]==gameBoard[row][1])&&(gameBoard[row][1]==gameBo
ard[row][2]))
return true;
else
// check if there is a winner in a slope form
:
if(gameBoard[1][1]=='X' || gameBoard[1][1]=='O' )
{
if
((gameBoard[0][0]==gameBoard[1][1])&&(gameBoard[1][1]==gameBoard[2][2]))
return true;
else
if
((gameBoard[0][2]==gameBoard[1][1])&&(gameBoard[1][1]==gameBoard[2][0]))
return true;
else
return false;
// this is method of type boolean to test if there is no winner and the game
should continue.
//it returns true if there is no winner in the game and false if there is.
public boolean noWinner()
{
if ((!checkWin()))//
return true;
else
return false;
// this is method of type boolean to test if all the game board positions are filled
with out a winner.
V P a g e
if
((gameBoard[0][1]=='X'||gameBoard[0][1]=='O')&&(gameBoard[0][1]=='X'||gameBoar
d[0][1]=='O')&&(gameBoard[0][1]=='X'||gameBoard[0][1]=='O'))
if
((gameBoard[0][2]=='X'||gameBoard[0][2]=='O')&&(gameBoard[0][2]=='X'||gameBoar
d[0][2]=='O')&&(gameBoard[0][2]=='X'||gameBoard[0][2]=='O'))
return true;
// this method of type void to Clear the board for a new game.
public void newGame()
{
player='X';// initialize the first player in the new game to X
gameBoard= new char [3][3]; // make new game board with the same
size 3 x 3.
}
// this method to switch the player by changing the current player from X to O
or from O to X
public void switchPlayer()
{
if (player =='X')
player = 'O';
else
player = 'X';
}
Vu P a g e
(
import javax.swing.JOptionPane;
import javax.swing.JButton;
// Data fields :
board[0][1]=jButton2;
board[0][2]=jButton3;
board[1][0]=jButton4;
board[1][1]=jButton5;
board[1][2]=jButton6;
board[2][0]=jButton7;
board[2][1]=jButton8;
board[2][2]=jButton9;
}
@uppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOE);
jPanel1.setFocusTraversalPolicyProvider(true);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
V P a g e
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
Vm P a g e
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_IZE,
262, hort.MAX_VALUE)
.addGroup(jPanel1Layout.createequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.TRAILING)
.addComponent(jButton7,
javax.swing.GroupLayout.DEFAULT_IZE, 85, hort.MAX_VALUE)
.addComponent(jButton4,
javax.swing.GroupLayout.DEFAULT_IZE, 85, hort.MAX_VALUE)
.addComponent(jButton1,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_IZE, 85, hort.MAX_VALUE))
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)
V P a g e
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.TRAILING)
.addComponent(jButton8,
javax.swing.GroupLayout.DEFAULT_IZE, 83, hort.MAX_VALUE)
.addComponent(jButton2,
javax.swing.GroupLayout.DEFAULT_IZE, 83, hort.MAX_VALUE)
.addComponent(jButton5,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_IZE, 83, hort.MAX_VALUE))
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.LEADING)
.addComponent(jButton3,
javax.swing.GroupLayout.DEFAULT_IZE, 82, hort.MAX_VALUE)
.addComponent(jButton6,
javax.swing.GroupLayout.DEFAULT_IZE, 82, hort.MAX_VALUE)
.addComponent(jButton9,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_IZE, 82, hort.MAX_VALUE))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_IZE,
javax.swing.GroupLayout.DEFAULT_IZE,
javax.swing.GroupLayout.PREFERRED_IZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_IZE,
47, hort.MAX_VALUE)
V- P a g e
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.LEADING)
.addGroup(jPanel1Layout.createequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLa yout.Alignme
nt.BAELINE)
.addComponent(jButton4,
javax.swing.GroupLayout.PREFERRED_IZE, 49,
javax.swing.GroupLayout.PREFERRED_IZE)
.addComponent(jButton5,
javax.swing.GroupLayout.PREFERRED_IZE, 49,
javax.swing.GroupLayout.PREFERRED_IZE))
.addGap(6, 6, 6))
.addGroup(jPanel1Layout.createequentialGroup()
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_IZE,
49, hort.MAX_VALUE)
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignme
nt.TRAILING)
.addComponent(jButton9,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_IZE, 44, hort.MAX_VALUE)
.addComponent(jButton8,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_IZE, 44, hort.MAX_VALUE)
.addComponent(jButton7,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_IZE, 44, hort.MAX_VALUE))
.addContainerGap())
);
V P a g e
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD
ING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_IZE,
javax.swing.GroupLayout.DEFAULT_IZE,
javax.swing.GroupLayout.PREFERRED_IZE)
.addGroup(layout.createequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEAD
ING, false)
u$ P a g e
.addComponent(jTextField2,
javax.swing.GroupLayout.PREFERRED_IZE, 261,
javax.swing.GroupLayout.PREFERRED_IZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createequentialGroup()
.addComponent(jButton10)
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_IZE, hort.MAX_VALUE)
.addComponent(jButton11,
javax.swing.GroupLayout.PREFERRED_IZE, 105,
javax.swing.GroupLayout.PREFERRED_IZE)))))
.addGap(35, 35, 35))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_IZE,
javax.swing.GroupLayout.DEFAULT_IZE,
javax.swing.GroupLayout.PREFERRED_IZE)
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_IZE,
javax.swing.GroupLayout.DEFAULT_IZE,
javax.swing.GroupLayout.PREFERRED_IZE)
.addPreferredGap(javax.swing.Layouttyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAE
LINE)
.addComponent(jButton10,
javax.swing.GroupLayout.PREFERRED_IZE, 27,
javax.swing.GroupLayout.PREFERRED_IZE)
.addComponent(jButton11,
javax.swing.GroupLayout.PREFERRED_IZE, 25,
javax.swing.GroupLayout.PREFERRED_IZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_IZE,
hort.MAX_VALUE))
);
u P a g e
pack();
}// </editor-fold>
// test if the poition is not used before and no winner to diplay the player
charachter.
if (jButton1.getText().equals("")&&
(!jTextField2.getText().equals(" player " +game.getPlayer()+ " is the
winner")))
{
jButton1.setText(game.getPlayer()+"");// Dissplay in button 1 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
}
}
// mean the place is used before or there is a winner.
else
JOptionPane.showMessageDialog(null, "Error ");
uV P a g e
{
jButton2.setText(game.getPlayer()+"");// Dissplay in button 2 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
{
jButton3.setText(game.getPlayer()+"");// Dissplay in button 3 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
}
// mean the place is used before or there is a winner.
else
JOptionPane.showMessageDialog(null, "Error ");
{
jButton4.setText(game.getPlayer()+"");// Dissplay in button 4 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
{
jButton5.setText(game.getPlayer()+"");// Dissplay in button 5 the current
player. // test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
{
jButton6.setText(game.getPlayer()+"");// Dissplay in button 6 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
if (game.checkEquals())
jTextField2.setText(" TIE GAME ");
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner ");
}
}
// mean the place is used before or there is a winner.
else
JOptionPane.showMessageDialog(null, "Error ");
{
jButton7.setText(game.getPlayer()+"");// Dissplay in button 7 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
if (game.checkEquals())
jTextField2.setText(" TIE GAME ");
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
}
}
// mean the place is used before or there is a winner.
else
JOptionPane.showMessageDialog(null, "Error ");
{
jButton8.setText(game.getPlayer()+"");// Dissplay in button 8 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
if (game.checkEquals())
jTextField2.setText(" TIE GAME ");
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
}
}
// mean the place is used before or there is a winner.
else
JOptionPane.showMessageDialog(null, "Error ");
{
jButton9.setText(game.getPlayer()+"");// Dissplay in button 9 the current
player.
// test for the winner
if (game.checkWin())
jTextField2.setText(" player " +game.getPlayer()+ " is the winner");
setVisible(false);
// TODO add your handling code here:
}
});
ÿ P a g e
}
ÿV P a g e
1. I want to test if the program displays the state of the board correctly after
each player and switch the player's turn. o I run the program and chose a
position first and then I enter the row and column number. Then make
sure the program out put the character (X or O) in the position desired.
2. I want to make sure that the game switch the player's turn. After each
player try. Again I play the game and enter deferent positions and in each
time the player characters were sapping between X and O.
ÿu P a g e
3. I want to test if the program show's up an error message and ask the user
to chose other position if the player chooses a filled position, and don't
when the player choose valid one. o, I run the program and entered a
position. in the second player turn I entered the same position and the
error message shows up and ask me to enter a new row and a new
column. o, I choose a empty cell and its valid without error message.
5. I want to check if the game is end without a winner then the program will
display a message to show that game is end with Tie. thus I play the game
again but in this time I try to fill all the cells without a winner. After filling
the last position the message appears with Tie game.
ÿÿ P a g e
6. I want to test after the message for tie game or a winner the program will
ask the user if he /she want to play a new game. o I play the game twice.
in the first time I win and in the second time I end the game with tie and in
both case the program ask me if I want to play a new game.
7. After asking the user if he/she want to play a new game. I want to
examination the program if the answer is yes it works correctly and if the
answer was no thanks message display and if any other answer and error
message shows. Therefore, after I win in the game the program ask me if I
want to play a new game so my answer was '' I don͛t know " error message
appears then it ask the same question again hence I play the game again.
In this time when I win and the question appears again my answer is no,
the thanks message appears and the game closed.
ÿ P a g e
ÿm P a g e
V
1. I want to test if the program display user interface game board with 9
empty buttons , one long white box to show the players turns and the
game states , New game button and Exit button. o I run the file and it
shows me 9 empty cells, the while box and it shows X-player turn and the
2 buttons.
2. I want to test if the X-player click in a button then X appears in that button
and when O-player click in a button then O appears in that button .
Therefore, I look at the player turn in the white box then I click in any
button and check if it display the same character.
ÿ P a g e
3. I want to check after each click on a button the player turn change. o I run
the file and I notice the player turn in a white box then after each time I
click in a button the player turn change directly.
4. I want to examination if there is a winner in all case the n the program will
output the winner player in the white box. accordingly I play the game
many times and in each time I win in different way. In each time I win the
winner player in the white box appears.
ÿ- P a g e
5. I want to check if the game is end without a winner then the program will
display Tie game in white box. so I play the game again but in this time I
try to click on all buttons without a winner. After clicking on all buttons Tie
game display in the white box.
6. I want to test if the program gives error message if the user clicked on a
used button. o I clicked the same button twice then error messa ge
display.
7. I want to test after tie game or a winner the program gives error message
if the user clicked on a button. Therefore , after a winner condition, I
clicked on a empty button and an error message displayed .
ÿ P a g e
8. I want to exanimate the new game button. accordingly I play a game then I
clicked on a new game and it clears all the buttons to start a new game.
9. Finally I want to test the exit button so I clicked on it and the game board
disappeared .
$ P a g e
In the end all the objectives of this program are verified. This program can be
improved in many different ways. This program let me to think in creative way
and like to learn more about programming and know that course ECE202 is never
enough. I wish if I could add some effects like sounds or motions when the
winner message or the tie message appears.