1.0.0 • Published 2 years ago

simple-tictactoe v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

simple-tictactoe

Easy to use package to make tictactoe games

Note: This only supports 3x3 tictactoe

Install

npm i simple-tictactoe

Usage

A example of using this package, this is not a fully working example, and just a example of how to use it.

import { TicTacToe, Player} from 'simple-tictactoe';

// use the second parameter to set a custom item that will be used to represent the player
const game = new TicTacToe(new Player('X', { userID: '1111111',   }), new Player('O', { userID: '2222222', someOtherField: true   }));

// use some way to get a input

// this is player 1 which is (x)
game.turn(0, 0)
// next turn is player 2 which is (o)
game.turn(0, 1)
// go on from here on until one wins

// a easy way to get the board on a string
const board = game.boardToString()

console.log(board)

Finding the winner

Every turn returns the following object

{ 
player: currentPlayer, 
board: the ongoing board, 
players: ['Array', 'of', 'the 2 players'], 
nextPlayer: the player who is supposed to have the next turn, 
status: {
	if won: 
		won: true
		player: the player who won
	else if draw:
	    draw: true
		players: ['Array', 'of', 'the 2 players since its a draw']
	else nothing:
		{} - empty object
},
ended: true/false
}
  • We can use the status object to see if the current user who's turn we proceeded with won
const data = game.turn(0, 1);

if(data.status.won) {
	console.log(`${data.status.player.id} won!`)
}

// same for draws

Using a ai

  • the package also includes a simple ai that selects turn randomly from available spots (is this really even a ai??)

following is a fully automated ai game without any use inputs

import { TicTacToe, Player, aiPlayer } from 'simple-tictactoe';

// use the second parameter to set a custom item that will be used to represent the player
const game = new TicTacToe(new Player('X', { userID: '1111111',   }), new Player('O', { userID: '2222222', someOtherField: true   }));

const ai = new aiPlayer('O', game)
const ai2 = new aiPlayer('X', game)

while(game.getEmptySpaces().length > 0 && !game.ended) {
	if(game.currentTurn === 'X') {
		const d = ai2.takeTurn()
		if(d.status.won) {
			console.log('X won!')
		}
		else if(d.status.draw) {
			console.log('Draw!')
		}
	}
	else {
		const d = ai.takeTurn()
		if(d.status.won) {
			console.log('O won!')
		}
		else if(d.status.draw) {
			console.log('Draw!')
		}
	}
}

// print the board to see how it did

const board = game.boardToString()
console.log(board);

Api

new Game(player1, player2)

  • player1 - the first player
  • player2 - the second player

\<Game>.turn(x, y)

used to pick a turn in the game

Parameters

  • x: the x position of board (where to place)
  • y: the y position of the board (where to place)

After every call to this function, the game will check if the game is over (if it is not over, the currentTurn will be changed to the next player)

\<Game>.boardToString()

returns the board as a prettified string

\<Game>.getEmptySpaces()

returns an array of all the available spots on the board

ex

[
	{ x: 0, y: 0, item: '-' },
	{ x: 1, y: 2, item: '-' },
]

Support

Click for discord support