DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Mastering React App Configuration With Webpack
  • Overcoming React Development Hurdles: A Guide for Developers

Trending

  • How to Create a Successful API Ecosystem
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Code Reviews: Building an AI-Powered GitHub Integration
  1. DZone
  2. Coding
  3. JavaScript
  4. Building a Tic-Tac-Toe Game Using React

Building a Tic-Tac-Toe Game Using React

This tutorial explores the creation of a custom version of the classic Tic-Tac-Toe game using React and SCSS.

By 
Maulik Suchak user avatar
Maulik Suchak
·
Dec. 23, 24 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

Welcome to my version of the classic Tic-Tac-Toe game! I created a Tic-Tac-Toe game using React and SCSS, completing it in around 6 hours. The game features single-player mode against the computer, a winner announcement popup, and win counters for both players. While the grid is customizable to n x n, the current winning logic supports only 3 x 3. Future improvements include smarter computer moves and real-time multiplayer functionality.

Overview

  • Total development time needed: ~6 hours
  • Technologies used: JavaScript (React) + SCSS

Game Rules

1. Objective

The goal of the game is to align three of your symbols (X or O) in a horizontal, vertical, or diagonal row on a 3 x 3 grid.

2. Turn Order

  • Player A always starts and plays as X.
  • The computer opponent plays as O and makes its move immediately after Player A.

3. Gameplay

  • Players take turns marking empty cells on the grid.
  • The first player to align three symbols wins the game.

4. Winning Conditions

A player wins if they achieve:

  • Three symbols in a horizontal row
  • Three symbols in a vertical column, or
  • Three symbols in a diagonal line

5. Draw

If all cells are filled and no player meets the winning conditions, the game ends in a draw.

6. Score Tracking

Win counters keep track of the total number of victories for both Player A and the computer.

Features Implemented (MVP)

Here’s what’s included in this version of the game:

  1. Play against the computer: A fun single-player mode where you take on the computer
  2. Winner announcement popup: At the end of each game, a popup reveals the winner.
  3. Win counters: Track total wins for both the player and the computer.
  4. Customizable grid: Create an n x n grid (though the winning logic currently supports only 3 x 3).

Possible Enhancements

Looking ahead, here are a few areas for improvement:

  1. [UX] Local storage for scores: Save game scores locally so they persist after refreshing the browser.
  2. [UX/Tech] Smarter computer moves: Replace random moves with an algorithm for more competitive gameplay.
  3. [UX] Real-time multiplayer: Enable two players to compete in real-time using technologies like Socket.io.
  4. [Tech] Advanced SCSS organization: Refactor SCSS using mixins, variables, and better structuring techniques.
  5. [UX/Tech] Graceful degradation: Ensure compatibility with non-modern browsers for broader support.

Setup and Development

Prerequisites

Before diving in, ensure you have Node.js installed (recommended version: 6.4.1).

Getting Started

  1. Clone the repository from GitHub.

2. Install dependencies:

npm install


3. Start the development server:

npm start


  • Open http://localhost:3000 in your browser to view the app.

4. (Optional) Build the project for production:

npm run build


This creates an optimized build folder, ready for deployment.

Testing

Run tests in interactive watch mode with:

npm test


Technical Details

File Structure

The project follows the standard Create React App structure with a modular component-based approach.

 
src
├── index.js
├── App.css
├── App.js
├── Components
│   └── Board
│       ├── Board.scss
│       ├── Cross.jsx
│       ├── ScoreBoard.jsx
│       ├── index.jsx
│       ├── Round.jsx
│   └── Footer
│       ├── Footer.scss
│       ├── index.jsx
│   └── Header
│       ├── Header.scss
│       ├── index.jsx
├── utils
│   └── lib.js
public
├── index.html


Description and Approach

  • The app was bootstrapped with Create React App for a quick MVP setup.
  • The game initializes an n x n grid for gameplay.
  • Each cell has a click event listener, allowing Player A (X) to make the first move.
  • The computer’s turn is simulated with a random cell selection logic, which will need enhancement for custom grid sizes.
  • Used SCSS for styling with a focus on speed over complexity during development.

Testing Approach

Manual Tests

  • Ensure the computer makes a move after Player A finishes.
  • Verify the game ends with a winner announcement banner.
  • Test automated moves by the computer.

Potential Unit Tests

  • Check if the computer moves after Player A.
  • Validate that the game displays the correct winner.
  • Test edge cases for grid initialization and user interactions.

Conclusion

Thank you for exploring my project! 

You can find the complete source code and contribute to this project on GitHub. Let’s build cool stuff together!

JavaScript Minimum viable product Node.js React (JavaScript library) Testing

Opinions expressed by DZone contributors are their own.

Related

  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Mastering React App Configuration With Webpack
  • Overcoming React Development Hurdles: A Guide for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!