xotb
v2.3.0
Published
A customizable timer API for TurboChat applications.
Downloads
268
Readme
XOTB.js Documentation
Welcome to the official documentation for XOTB.js, the ultimate API for integrating intelligent Tic Tac Toe gameplay into your applications. Whether you're developing a web-based game, a mobile app, or any other project that requires a smart Tic Tac Toe bot, XOTB.js provides a seamless and efficient solution.
Disclaimer
⚠️ Disclaimer
XOTB.js is designed for educational and recreational purposes. While it employs sophisticated algorithms to provide challenging gameplay, it should not be used for critical applications where security and fairness are paramount.
How to Use
Integrate XOTB.js into your project by following the appropriate steps based on your development environment:
1. Install via npm
If you're using XOTB.js in a Node.js project or any environment that supports npm, you can install it using the following command:
npm install xotb
2. Import XOTB.js into Your Project
After installation, import XOTB.js into your JavaScript files to start using its functionalities.
For CommonJS (Node.js):
const XOTB = require('xotb');
const bot = new XOTB('medium'); // Initialize with 'easy', 'medium', or 'hard'
// Player makes a move at position 0 (top-left corner)
bot.makeMove(0, 'X');
console.log(bot.getBoard()); // Display current board state
console.log(bot.getStatus()); // Display current game status
// Bot makes its move
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
For ES Modules:
import XOTB from 'xotb';
const bot = new XOTB('hard'); // Initialize with 'easy', 'medium', or 'hard'
// Player makes a move at position 4 (center)
bot.makeMove(4, 'X');
console.log(bot.getBoard()); // Display current board state
console.log(bot.getStatus()); // Display current game status
// Bot makes its move
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
Using XOTB.js in Browser with Module Bundlers
If you're using module bundlers like Webpack or Parcel, you can import XOTB.js as follows:
// Import XOTB.js
import XOTB from 'xotb';
// Initialize the bot with desired difficulty
const bot = new XOTB('easy');
// Player makes a move at position 2 (top-right corner)
bot.makeMove(2, 'X');
console.log(bot.getBoard()); // Display current board state
console.log(bot.getStatus()); // Display current game status
// Bot makes its move
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
3. Using XOTB.js Directly in Web Projects
If you're developing a TurboChat application and prefer to include XOTB.js directly without a module bundler, you can reference the local xotb.js
file.
<script src="xotb.js"></script>
Once included, utilize the XOTB
class as shown below:
<script>
const bot = new XOTB('medium'); // Initialize with 'easy', 'medium', or 'hard'
// Player makes a move at position 6 (bottom-left corner)
bot.makeMove(6, 'X');
console.log(bot.getBoard()); // Display current board state
console.log(bot.getStatus()); // Display current game status
// Bot makes its move
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
</script>
API Reference
constructor(difficulty)
: Initializes a new instance of XOTB.js with the specified difficulty.resetGame()
: Resets the game state to start a new game.setDifficulty(level)
: Sets the difficulty level of the bot. Acceptable values are'easy'
,'medium'
, and'hard'
.makeMove(index, player)
: Makes a move for the specified player ('X'
or'O'
) at the given board index (0-8
).getBotMove()
: Calculates and makes the bot's move based on the current difficulty level. Returns the index where the bot moved.getStatus()
: Retrieves the current status of the game (e.g., "Player X wins!", "Bot O wins!", "It's a draw!", "Current turn: Player X").getBoard()
: Returns a copy of the current board state as an array.
Parameters:
difficulty
:string
- The difficulty level for the bot. Acceptable values are'easy'
,'medium'
, and'hard'
.index
:number
- The board position index (0-8
) where the player wants to make a move.player
:string
- The player symbol, either'X'
for the human player or'O'
for the bot.
Returns:
getBotMove
:number
- The index where the bot made its move.getStatus
:string
- The current status of the game.getBoard
:array
- A copy of the current board state.
Example Code
1. Basic Game Flow
const XOTB = require('xotb'); // For CommonJS
const bot = new XOTB('medium'); // Initialize with 'easy', 'medium', or 'hard'
// Player X makes a move at position 0
bot.makeMove(0, 'X');
console.log(bot.getBoard()); // ['X', '', '', '', '', '', '', '', '']
console.log(bot.getStatus()); // 'Current turn: Player O'
// Bot O makes its move
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);
console.log(bot.getBoard()); // Updated board with bot's move
console.log(bot.getStatus()); // 'Current turn: Player X'
2. Resetting the Game
const bot = new XOTB('hard');
// Player X makes a move
bot.makeMove(4, 'X');
// Bot O makes a move
bot.getBotMove();
// Reset the game
bot.resetGame();
console.log(bot.getBoard()); // ['','','','','','','','','']
console.log(bot.getStatus()); // 'Current turn: Player X'
3. Changing Difficulty Mid-Game
const bot = new XOTB('easy');
// Player X makes a move
bot.makeMove(2, 'X');
// Change difficulty to 'hard'
bot.setDifficulty('hard');
// Bot O makes its move based on new difficulty
const botMove = bot.getBotMove();
console.log(`Bot (O) moved at position ${botMove}`);