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

qm-tictactoe-minimax

v1.0.8

Published

bestMove function for tictactoe game based on minimax algorithm

Downloads

14

Readme

tictactoe minimax

Updated Badge

Remote repository for minimax algorithm npm package

Description

A basic best move function for a cpu player of tic tac toe, based on a minimax algorithm - given a board as an array of strings ("", "X" or "O"), evaluates all possible board states and returns the move that gives the highest score.

A score of 10 is given for a winning terminal state, -10 for a losing state, and 0 for a draw. If a winning state is found, the algorithm will subtract the depth of this winning state (i.e. how many moves are required to reach it), and favour winning states that arise in the least moves possible.

Installation / Usage

npm install qm-tictactoe-minimax

const bestMove = require("qm-tictactoe-minimax").bestMove

const cpuMove = bestMove(board, symbol)

Dependencies

License

MIT

Contact

[email protected]

Usage and rules

Currently the function relies on the CPU playing as X and the human playing as O - if these are switched, the cpu ai will deliberately aim to lose bestMove function takes an array of strings (board) plus the current symbol (X/O) as parameters, and returns the index of the square that it considers to be the best move.

Other functions

bestMove function relies on a number of other private functions, listed below:

  • hasWon: returns true if board is in winning terminal state
  • hasDrawn: returns true if there are no more free square on the board
  • evaluateBoard (importable): returns +10 if the board is in a winning terminal state for THE CPU, -10 for the human player, and 0 in all other cases
  • returnEmptyIndexes: returns all indexes that are currently empty
  • minimax (importable): recursive function that scans all possible board states. If it finds a terminal state (winning, losing or drawing) it will return an appropriate score to bestMove - bestMove keeps track of the highest score and returns the matching index.