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

tictactoe-game-modules

v1.0.0

Published

A Tic Tac Toe Game Module, comprising of a Board, Minimax algorithm and a random choice algorithm

Downloads

2

Readme

tictactoe-game-module

A Tic Tac Toe Game Modules, comprising of a Board, Minimax algorithm, and a random choice algorithm with inbuilt methods

Node.js CI

Installation

npm install tictactoe-game-modules --save

Usage

import { Board } from tictactoe-game-modules

import { Minimax } from tictactoe-game-modules

import { RandomChoice } from tictactoe-game-modules

Modules and Methods

Board Module

import { Board } from tictactoe-game-modules

Initialize an instance object of the Board

const board = new Board(optional parameter);

You can pass in an optional custom Array of Strings as parameter

Example:
 
const board = new Board(['X', 'X', '', '', 'O', 'O', '', '', ''])

Methods:

board.grid

This returns an array of empty strings

['', '', '', '', '', '', '', '', '']

board.makeMove(index: number, symbol: string)

This makes a move on the board and returns a new array with the symbol.

Example:

board.makeMove(1, 'X) = ['X', '', '', '', '', '', '', '', '']

N.B: Index should be `1 -9`

board.currentMark()

This returns the current turn taking symbol(mark) of either X or O

If remaining position on the board is an odd number, the current mark is X, if its an even number, the current mark is O

Example:
 
grid = ['X', 'O', 'X', '', '', '', '', '', ''] = current symbol is 'O'

board.defaultPositionState(index: number)

This returns back the default board position that has already has a symbol.

Example:
 
grid = ['X', 'O', 'X', '', '', '', '', '', ''] 

board.defaultPositionState(1) = ''

board.isPositionTaken(index: number)

This returns a boolean if a position already ahs a symbol I.e If the value of the index in the array is not '', that means the index is already marked.

Example:
 
grid = ['X', '', '', '', '', '', '', '', ''] 

board.isPositionTaken(1) = true

board.availablePositions()

This returns an array with the available index's on the board

Example:
 
grid = ['X', '', 'O', '', '', '', '', '', ''] = [2, 4, 5, 6, 7, 8, 9 ]

board.availablePositionCount()

This returns the count total of the available index's on the board

Example:
 
grid = ['X', '', 'O', '', '', '', '', '', ''] = 7

board.isMoveValid(input: number)

This validates the available postions in the board array and returns a boolean

Example:
 
grid = ['', '', '', '', '', '', '', '', ''] 
board.isMoveValid(12) =  false

board.markedBoardPositionValue(input: number)

This returns a string value of the board array index

Example:
 
grid = ['', '', 'X', 'O', '', '', '', '', ''] 

board.markedBoardPositionValue(3) = `X`

board.row()

This transposes a flat board array and return a 2D array with the row values in each child array

Example: 

grid = ['X', 'X', 'X', 'O', 'O', 'O', 'X', 'O', 'X'] 

=  [['X', 'X', 'X'],
   `['O', 'O', 'O'],
    ['X', 'O', 'X']]

board.colums()

This transposes a flat board array and return a 2D array with the column values in each child array

Example:

grid = ['X', 'X', 'X', 'O', 'O', 'O', 'X', 'O', 'X'] 

=  [['X', 'O', 'X'],
   `['X', 'O', 'O'],`
    ['X', 'O', 'X']]

board.diagonals()

This transposes a flat board array and return a 2D array with the diagonal values in each child array

Example: 

grid = ['X', 'X', 'X', 'O', 'O', 'O', 'X', 'O', 'X'] 

=  [['X', 'O', 'X'],`
   `['X', 'O', 'X']]

board.hasWinner()

This method loop every line(child array) of the 2D arrays (rows, columns or diagonally) and returns a boolean if there is a win in any of the lines.

Eg: 

grid = ['X', 'X', 'X', 'O', '', 'O', '', 'O', 'X'] = true, for a row win

board.winningPlayer()

This the winning mark on the baord array

Example; 

grid = ['X', 'X', 'X', 'O', '', 'O', '', 'O', 'X'] = 'X' the winning symbol

board.isGameDraw()

This returns a boolean if there is a tie on the board

Example: 

grid = ['X', 'O', 'X', 'O', 'X', 'O', 'O', 'X', 'O'] = true

board.isGameOver()

This returns a boolean if there is a winner or a tie on the board

Example; 

grid = 'X', 'X', 'X', 'O', '', 'O', '', 'O', 'X'] = true, there a win on first row

grid = ['X', 'O', 'X', 'O', 'X', 'O', 'O', 'X', 'O'] = true, there is tie

Minimax Module

import { Minimax } from tictactoe-game-modules

Intialize an instance object of the Minimax and Board

const board = new Board();

const minimax = new Minimax(currentPlayer: string, opponent: string);
Example:

const minimax = new Minimax('X, 'O);

You can pass in an optional custom Array of Strings as parameter to the Board

Example:
 
board = new Board(['X', 'X', '', '', 'O', 'O', '', '', ''])

now board.grid = ['X', 'X', '', '', 'O', 'O', '', '', '']

instead of default grid = ['', '', '', '', '', '', '', '', '']

Method:

minimax.findBestMove(board)

This returns the best winning index of the given board

Example sceenerio:

const baord = new Board();

minimax.findBestMove(board) = ['X', '', '', '', '', '', '', '', ''] = 1

That means position one is the best optimal position for the opponent to pick

RandomChoice Module

import { RandomChoice } from tictactoe-game-modules

Intialize an instance object of the RandomChoice and Board

const board = new Board();

const random = new Random(board);

You can pass in an optional custom Array of Strings as parameter to the Board

Example:
 
board = new Board(['X', 'X', '', '', 'O', 'O', '', '', ''])

now board.grid = ['X', 'X', '', '', 'O', 'O', '', '', '']

instead of default grid = ['', '', '', '', '', '', '', '', '']

Method:

random.findRandomMove(board)

This returns the best winning index of the given board

Example sceenerio:

const baord = new Board();

random.findRandomMove(board) = random index

This means position on the board is a random index from the available board index's