Skip to content

AnjaliGupta3911/Tick-Tac-Toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tick-Tac-Toe

Created a simple Tick Tac Toe GUI game in Java using Swing.

The Game Board:

The board is logically divided into 9 partitions/boxes as shown below:

1 2 3
4 5 6
7 8 9

Classes:

1. GameMain.java: contains main method
2. gameFrame.java: defines the containing frame
3. gamePanel.java: defines the panel of the frame and paints the board & symbols
4. getLocation.java: implements MouseAdapter to fetch coordinates of mouse click
5. fillBoard.java: contains the main logic of populating board and making next move
6. populateBoard.java: updates the 8 sum variables and checks if we have a winner
7. computerTurn.java: contains logic for making computer's move
8. chooseBox.java: contains logic for choosing box for computer's next move
9. checkAvailaility.java: checks if the given box is available or not

Main Data structures and variables:

1. LinkedList zerox1, zeroy1, crossx1, crossy1: contains coordinates of all the cross and noughts drawn on board
2. LinkedList boxesLeft: list of box numbers that are still unfilled
3. int turn: indicates if it is user's turn or computer's with values 0 and 1 respectively
4. int user_pt, comp_pt: 1 and -1 respectively
5. int sum_rowX, sum_colX, sum_digX: contains the total sum of the points accumulated for Xth row, column or diagonal
6. int arrayOfPoints[]: array consisting of values of sum_rowX, sum_colX and sum_digX
7. HashSet availablePos: set representaion of boxesLeft
8. HashSet possiblePos: set conatining box numbers that can be filled up in the current turn
9. HashSet pointPos: contains indexes of arrayOfPoints where we get these values: -2/2/1

Logical workflow:

Step 1: User makes first move. The selected box is painted with a cross
Step 2: Sum variables and boxesLeft are updated
Step 3: possiblePos is updated with boxes that either results in computer's win (priority 1) or blocks user's win (priority 2) or block user's future move (priority 3)
Step 4: Computer chooses one random box from possiblePos
Step 5: Repeat through 1-4 until we have a winner (value of any sum variable is 3/-3) or there's a tie (no more boxes left)

Screenshot of the game:

screenshot of game

About

Single user Tick Tac Toe GUI game in Java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages