npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jacobcohn/ultimate-tic-tac-toe-ai

v1.0.2

Published

finds the best move in any ultimate tic tac toe game

Downloads

3

Readme

Ultimate Tic Tac Toe AI

Ultimate Tic Tac Toe AI is a NodeJS Module written in TypeScript that gives the best move for any ultimate tic tac toe position.

Why did I create this project?

I am currently a senior in high school and enrolled in a dual credit Data Structures and Algorithms class. My teacher, Mr. Tiveron, assigned a fourth quarter project where we could create any project that inspired us. I was very intrigued by a YouTube video by Sebastian Lague on creating a Chess AI. And, I have always enjoyed playing Ultimate Tic Tac Toe with my dad. So, I put my abilities towards creating an AI for my old favorite game, Ultimate Tic Tac Toe.

How did I create this project?

I created this project in TypeScript using the minimax algorithm with alpha beta pruning.

I created my heuristic function by giving defining features and assigning them weights. Each small board has features for both X and O which counts how many possible combinations there are in the big board and how close they are to being complete combinations. Additionally, they have feautures for how many possible combinations there are inside each small board and how close they are to completion for both X and O. The weights are arbitrary numbers that give more value to how close the combinations are to completion.

All of the positions need to be searched at a certain depth for the best move to be valid. And, the bigger the depth, the better the move would be. But, there is no way to know how long a certain depth will take. My solution to this problem was to find the best move at depth 1, and then depth 2, and so on. Once a certain time has passed, then the algorithm would return the best move for the depth it was currently searching through. This allows programmers to have the algorithm return a best move in a relatively defined amount of time.

Where can you play against it?

About a year ago, I created a an Ultimate Tic Tac Toe app with React for two people to play against each other on the same device. I revamped this code so that you can play my AI. I did not want to spend a lot of time implementing a new GUI, so do not be surprised if you come across a bug. You can only play as X as I did not want to spend a lot of time on the GUI. Sadly, you will lose unless you are using your own bot against it. It is set to 500 milliseconds.

Ultimate Tic Tac Toe AI Implementation

Installation

Use a package manager of your choice to install this package. For example, you can use npm.

npm install @jacobcohn/ultimate-tic-tac-toe-ai

Usage

The position string contains 3 components. The first number is the current player. 1 corresponds to X and 2 corresponds to O. The second number is the current big board that the player can play in. 0-8 are the all the spots on the tic tac toe board from left to right, then up to down. 9 corresponds to any open board. Finally, the long sequence of digits and slashes is the board. 0 corresponds to an empty spot. 1 corresponds to X and 2 corresponds to O. Each slash is to separate each small board from each other.

Then, you give the algorithm a number of milliseconds to search for. I recommend 500 milliseconds as it is quick, yet is able to search deep enough to defeat pretty much any human.

import { getBestMove } from "@jacobcohn/ultimate-tic-tac-toe-ai";

const position = "1 9 000000000/000000000/000000000/000000000/000000000/000000000/000000000/000000000/000000000";
const numMilliseconds = 500;

// returns the best move (big and small) with its evaluation and depth
const [ { big, small }, evaluation, depth ] = getBestMove(position, numMilliseconds);

The move is returned in the form of a number for the big board position and a number for small board position. These numbers are both 0-8, which correspond to spots on the tic tac toe board from left to right, then up to down.

Contributing

Anyone can look through the code and see how it was written. If you do want to add to it, I only request that you send me an email on what you are adding as I am interested. My personal email is in my GitHub Profile.

License

MIT