tc4b
v1.1.0
Published
A basic Connect Four Bot API.
Downloads
148
Readme
TC4B.js Documentation
Welcome to the official documentation for TC4B.js, the ultimate API for integrating intelligent Connect Four gameplay into your applications. Whether you're developing a web-based game, a mobile app, or any other project that requires a smart Connect Four bot, TC4B.js provides a seamless and efficient solution.
How to Use
Integrate TC4B.js into your project by following the appropriate steps based on your development environment:
1. Install via npm
If you're using TC4B.js in a Node.js project or any environment that supports npm, you can install it using the following command:
npm install tc4b
2. Import TC4B.js into Your Project
After installation, import TC4B.js into your JavaScript files to start using its functionalities.
For CommonJS (Node.js):
const TC4B = require('tc4b');
const bot = new TC4B(); // Initialize the bot
// Player makes a move in column 3
bot.makeMove(3, 'player');
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 moved at column ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
For ES Modules:
import TC4B from 'tc4b';
const bot = new TC4B(); // Initialize the bot
// Player makes a move in column 4
bot.makeMove(4, 'player');
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 moved at column ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
Using TC4B.js in Browser with Module Bundlers
If you're using module bundlers like Webpack or Parcel, you can import TC4B.js as follows:
// Import TC4B.js
import TC4B from 'tc4b';
// Initialize the bot
const bot = new TC4B();
// Player makes a move in column 2
bot.makeMove(2, 'player');
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 moved at column ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
3. Using TC4B.js Directly in Web Projects
If you're developing a TurboChat application and prefer to include TC4B.js directly without a module bundler, you can reference the local tc4b.js
file.
<script src="tc4b.js"></script>
Once included, utilize the TC4B
class as shown below:
<script>
const bot = new TC4B(); // Initialize the bot
// Player makes a move in column 6
bot.makeMove(6, 'player');
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 moved at column ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
</script>
API Reference
- constructor(): Initializes a new instance of TC4B.js.
- resetGame(): Resets the game state to start a new game.
- makeMove(column, player): Makes a move for the specified player ('player' or 'bot') in the given column (0-6).
- getBotMove(): Calculates and makes the bot's move based on the current game state. Returns the column where the bot moved.
- checkWin(player): Checks if the specified player ('player' or 'bot') has won the game.
- getBoard(): Returns a copy of the current board state as a 2D array.
- isBoardFull(): Checks if the board is completely filled with discs.
- getStatus(): Retrieves the current status of the game (e.g., "Player wins!", "Bot wins!", "It's a draw!", "Game in progress").
Example Code
Here are some examples demonstrating how to use the TC4B
class:
1. Basic Game Flow
const TC4B = require('tc4b'); // For CommonJS
const bot = new TC4B(); // Initialize the bot
// Player makes a move in column 3
bot.makeMove(3, 'player');
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 moved at column ${botMove}`);
console.log(bot.getBoard());
console.log(bot.getStatus());
2. Resetting the Game
const bot = new TC4B();
// Player makes a move in column 4
bot.makeMove(4, 'player');
// Bot makes a move
bot.getBotMove();
// Reset the game
bot.resetGame();
console.log(bot.getBoard()); // [['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', '']]
console.log(bot.getStatus()); // 'Game in progress'
3. Checking for a Win
const bot = new TC4B();
// Simulate a series of moves leading to a player win
bot.makeMove(0, 'player');
bot.makeMove(1, 'player');
bot.makeMove(2, 'player');
bot.makeMove(3, 'player');
console.log(bot.getBoard());
console.log(bot.checkWin('player')); // true
console.log(bot.getStatus()); // 'Player wins!'
Frequently Created Bugs
Here are some common issues developers encounter when using TC4B.js and how to resolve them:
Bug: "TC4B is not defined."
- Cause: The
tc4b.js
file is not correctly included or imported in your project. - Solution: Ensure you've installed the package via npm and correctly imported it into your JavaScript file.
- Cause: The
Bug: "makeMove function is throwing an error."
- Cause: Attempting to make a move in a full column or after the game has ended.
- Solution: Check if the column is available and the game is active before making a move.
Bug: "Bot is not making a move."
- Cause: The bot is not initialized correctly or the game is already over.
- Solution: Ensure the bot is properly initialized and the game is active.
Bug: "Unexpected board state after moves."
- Cause: Concurrent modifications to the board without proper synchronization.
- Solution: Ensure that all moves are made sequentially and that the board state is not altered externally.
Additional Information
The TC4B
class provides a robust and intelligent Connect Four bot that can be easily integrated into various projects. Its efficient algorithms ensure challenging and fair gameplay, enhancing the user experience across different platforms.
For advanced customization or to extend the bot's functionalities, you can modify the tc4b.js
file or contribute to its development on the project's repository.
More Resources
Check out the API Overview for a deeper dive into available functions and customization options.