Skip to content

A simple Java program to demonstrate Tic-Tac-Toe game.

License

Notifications You must be signed in to change notification settings

ymanshur/tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe

A simple console-based Tic-Tac-Toe game implemented in Java. Players take turns placing X and O on a 3x3 grid until someone wins or the game ends in a draw.

Features

  • Interactive Console Interface: Clear visual representation of the game board
  • Input Validation: Prevents invalid moves and provides user feedback
  • Win Detection: Automatically detects wins across rows, columns, and diagonals
  • Draw Detection: Recognizes when the board is full with no winner

How to Play

  1. The game displays a 3x3 grid with numbered positions (1-9)
  2. Player X goes first
  3. Enter a number (1-9) to place your mark in that position
  4. Players alternate turns until someone wins or the board is full
  5. The game announces the winner or declares a draw

Getting Started

To run this game, you'll need Java installed on your system.

Compilation and Execution

  1. Compile the program:

    javac src/App.java -d bin
  2. Run the game:

    java -cp bin App

Alternative (if using VS Code)

  • Open the project in VS Code with Java extensions installed
  • Press F5 or use the "Run" button to compile and execute

Simulation

O's have won

Welcome to 3x3 Tic-Tac-Toe.

|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

X will play first. Enter a slot number to place X in: 1

|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

O's turn; enter a slot number to place O in: 4

|---|---|---|
| X | 2 | 3 |
|-----------|
| O | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

X's turn; enter a slot number to place X in: 10
Invalid input; re-enter another slot number: 9

|---|---|---|
| X | 2 | 3 |
|-----------|
| O | 5 | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|

O's turn; enter a slot number to place O in: 5

|---|---|---|
| X | 2 | 3 |
|-----------|
| O | O | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|

X's turn; enter a slot number to place X in: 5
Invalid input; re-enter another slot number: 3

|---|---|---|
| X | 2 | X |
|-----------|
| O | O | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|

O's turn; enter a slot number to place O in: 6

|---|---|---|
| X | 2 | X |
|-----------|
| O | O | O |
|-----------|
| 7 | 8 | X |
|---|---|---|

Congratulations! O's have won! Thanks for playing

draw

Welcome to 3x3 Tic-Tac-Toe.

|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

X will play first. Enter a slot number to place X in: 1

|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

O's turn; enter a slot number to place O in: 5

|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

X's turn; enter a slot number to place X in: 2

|---|---|---|
| X | X | 3 |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

O's turn; enter a slot number to place O in: 3

|---|---|---|
| X | X | O |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

X's turn; enter a slot number to place X in: 7

|---|---|---|
| X | X | O |
|-----------|
| 4 | O | 6 |
|-----------|
| X | 8 | 9 |
|---|---|---|

O's turn; enter a slot number to place O in: 4

|---|---|---|
| X | X | O |
|-----------|
| O | O | 6 |
|-----------|
| X | 8 | 9 |
|---|---|---|

X's turn; enter a slot number to place X in: 6

|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | 8 | 9 |
|---|---|---|

O's turn; enter a slot number to place O in: 9

|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | 8 | O |
|---|---|---|

X's turn; enter a slot number to place X in: 8

|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | X | O |
|---|---|---|

It's a draw! Thanks for playing.

About

A simple Java program to demonstrate Tic-Tac-Toe game.

Resources

License

Stars

Watchers

Forks

Languages