Skip to content

About

Mastering Java fundamentals

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Console Tic-Tac-Toe (Java)

A console-based implementation of the classic Tic-Tac-Toe game, built as part of the JetBrains Academy (Hyperskill) Java track. This project demonstrates core programming fundamentals, input sanitization, and state-tracking mechanics in standard Java.

While a fundamental project, it serves as a showcase of clean control flow, comprehensive edge-case handling, and algorithmic grid scanning without dependencies.

Key Learning Objectives

  • Multidimensional Arrays: Manipulating and updating elements dynamically across rows, columns, and diagonals within a char[][] grid structure.
  • Input Validation & Sanitization: Using Regular Expressions (java.util.regex) to parse console commands, filter non-numeric tokens, and bounds-check structural inputs (1-3).
  • Game-Loop Control Flow: Implementing precise state machines for turn tracking (X vs O), index offsets (converting human-readable 1-3 inputs to 0-indexed positions), and determining structural termination conditions (Win/Draw).

Features & Input Validation

The engine is robust against improper console inputs, guiding the user until a valid move is recorded:

  • Rejects non-numeric characters gracefully ("You should enter numbers!").
  • Flags inputs out of the standard grid range ("Coordinates should be from 1 to 3!").
  • Blocks attempts to overwrite existing placements ("This cell is occupied! Choose another one!").

Sample Gameplay Output

---------
| _ _ _ | 
| _ _ _ | 
| _ _ _ | 
---------
2 2
---------
| _ _ _ |
| _ X _ |
| _ _ _ |
---------
2 2
This cell is occupied! Choose another one!
two two
You should enter numbers!
1 4
Coordinates should be from 1 to 3!
1 1
---------
| O _ _ |
| _ X _ |
| _ _ _ |
---------
3 3
---------
| O _ _ |
| _ X _ |
| _ _ X |
---------
2 1
---------
| O _ _ |
| 0 X _ |
| _ _ X |
---------
3 1
---------
| O _ _ |
| 0 X _ |
| X _ X |
---------
2 3
---------
| O _ _ |
| 0 X 0 |
| X _ X |
---------
3 2
---------
| O _ _ |
| 0 X 0 |
| X X X |
---------
X wins

How to Run Locally

  1. Clone this repository:
    git clone https://github.com/your-username/repository-name.git
  2. Navigate to the project's task source directory:
    cd "Simple Tic-Tac-Toe (Java)/task/src"
  3. Compile the game:
    javac tictactoe/Main.java
  4. Execute the binary:
    java tictactoe.Main

About

Mastering Java fundamentals

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages