Skip to content
randomvladPublic

About

A password-protected web app to play tic-tac-toe against a simple opponent. Built with Java and Spring Boot. ❌❌⭕

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Repository files navigation

Tic-tac-toe App Logo
Tic-tac-toe App

CI Build CodeQL Codacy Badge codecov Snyk Security Monitoring OpenSSF Scorecard MIT license

A password-protected web app to play tic-tac-toe against a simple computer opponent.

Note

Back in 2015, I was given a take-home interview test to create a tic-tac-toe game. Over the years I kept tinkering and expanding the project to a more "production grade" level. It includes 90%+ test coverage, security scans, user login, a persistence layer, auto-generated docs, and clean, DRY code written with best practices and low coupling in mind. The interview loop has long been closed, but the project took on a life of its own as a happy accident.

Overview

  • Play on a 3x3 board, choosing whether to go first or second.
  • The computer opponent picks squares at random, except when it moves first, in which case it always takes the center square.
  • User game data is persisted to an in-memory database. As long as the server is not restarted, a player can leave and return to finish an in-progress game.
  • App is secured with a username and password login. The database is seeded with users rick and morty with password pickle.
  • UI is rendered server-side for simplicity, so each move triggers a full page refresh.
  • To learn more about the project and lessons learned, see Little Code Gems.
  • Unit tests are in src/test/java/tictactoe/*.

Tech Stack

Technology
Language Java 27
Framework Spring Boot 4
Data Layer H2 Database, JPA & Hibernate
UI Layer Thymeleaf, Bootstrap, HTML, CSS, JavaScript
Testing JUnit, Mockito, AssertJ
Build Tools & CI Gradle, GitHub Actions

Install & Run

  • Clone repo git clone https://github.com/randomvlad/TicTacToe.git and navigate cd TicTacToe
  • Pick one of the following installation options:
    • Option 1: Install Java 27. Tip: use SDKMAN! to easily manage different Java versions.
      • Run applicable Gradle Wrapper command:
        • macOS/Linux: ./gradlew bootRun
        • Windows: gradlew.bat bootRun
    • Option 2: Install Docker, e.g. Docker Desktop. Tip: on macOS, a lightweight free alternative is Colima.
      • Build the image: docker build -t tictactoe .
      • Run the container: docker run --rm -p 8080:8080 tictactoe
  • Once app is running, go to http://localhost:8080/tictactoe/.
  • Log in with username rick or morty and password pickle to play a game.
  • To stop the app, press Ctrl+C in the terminal.

HTTP Endpoints

Check the HTTP endpoint docs.

Note

The app renders HTML server-side, so its endpoints are browser routes (page loads and form posts) and not a JSON API. Docs are generated with Spring REST Docs. To build the docs locally, run ./gradlew asciidoctor and open build/docs/asciidoc/index.html.

Data Schema

The app’s data is stored in two relational tables, APP_USER and GAME.

Tic-tac-toe DB Schema Diagram

Table definitions:

create table PUBLIC.APP_USER
(
  ID       BIGINT not null primary key,
  PASSWORD CHARACTER VARYING(255),
  USERNAME CHARACTER VARYING(255)
);

create table PUBLIC.GAME
(
  NEXT_MOVE    TINYINT,
  PLAYER1_TYPE TINYINT,
  PLAYER2_TYPE TINYINT,
  STATE        TINYINT,
  APP_USER_ID  BIGINT,
  ID           BIGINT not null primary key,
  ROWS         JSON,
  constraint FKT1UCX1TG677JTR30CQDLR16O5 foreign key (APP_USER_ID) references PUBLIC.APP_USER,
  check ("NEXT_MOVE" BETWEEN 0 AND 1),
  check ("PLAYER1_TYPE" BETWEEN 0 AND 1),
  check ("PLAYER2_TYPE" BETWEEN 0 AND 1),
  check ("STATE" BETWEEN 0 AND 3)
);

Tic-tac-toe game state is persisted as JSON (an array of string arrays) to the GAME.ROWS column. Example:

[
  ["","o","x"],
  ["o","x",""],
  ["x","",""]
]

Screenshots

Login Page

Tic-tac-toe app login screenshot

Game Won

Tic-tac-toe app won game screenshot

Game Lost

Tic-tac-toe app lost game screenshot

Game Drawn

Tic-tac-toe app draw game screenshot

Custom Error Page

Tic-tac-toe app error page screenshot

About

A password-protected web app to play tic-tac-toe against a simple opponent. Built with Java and Spring Boot. ❌❌⭕

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages