- c4i.js is a standalone browser library for computing the next best move in a game of Connect 4.
- It contains a function for analyzing a board position for a sequence of moves and returning the next best move as a column (0-6).
- A move sequence is represented as a number string (
'4251'for example), where each digit represents a column where a move was made. - In c4i.js you can input a sequence of moves using columns numbered 0-6 and get a best column to move to as a result numbered 0-6 too.
-
Include the Library
Add c4i.js to your HTML file:<script src="c4i.js"></script>
-
Call the getBestMove Function
Use thegetBestMove(sequence)function to compute the next best move. Give it a move sequence using columns 0-6. For example:let sequence = "4251"; // Moves so far (0-6): first move in col 4, then col 2, etc. let bestMove = getBestMove(sequence); console.log("Best move (column):", bestMove);
The function returns an integer from 0 to 6 indicating the best column to make a move to.
Below is an HTML example that demonstrates how to use the library:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Connect 4 Best Move</title>
<script src="c4i.js"></script>
</head>
<body>
<input id="moveInput" type="text" placeholder="Enter move sequence (e.g. 4251)" />
<button id="calculateMove">Get Best Move</button>
<div id="result"></div>
<script>
document.getElementById("calculateMove").addEventListener("click", function() {
let sequence = document.getElementById("moveInput").value;
try {
let bestMove = getBestMove(sequence);
document.getElementById("result").innerText = "Best move (column): " + bestMove;
} catch (error) {
document.getElementById("result").innerText = "Error: " + error.message;
}
});
</script>
</body>
</html>In this example the user inputs a move sequence '4251' into the text field. When the button is clicked, the getBestMove function is called and the best move (as a column numbered from 0 to 6) is displayed.
-
Node.js and npm:
InstallNode.js (22.14.0)andnpm (10.9.2)with:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh|bash;nvm install 22
-
Brotli:
The build process requires the Brotli command-line tool.sudo apt update;sudo apt install brotli
-
Install Dependencies:
In your project directory, run:npm install uglify-js simple-git node-fetch
- Note that dependencies get cleaned after the build.
-
Run the Build Script:
node build.js
- Note that building this takes roughly 1.5 to 2 minutes on average hardware.
-
File b128.js is my library for decoding a custom Base128 string.
-
File c4.js is my rewrite of
connect4 solverbyPascalPonsfor web-browser JavaScript. -
File demo.html is a graphical example of this library. It can be ran only when c4i.js is built.
-
File build.js is for building c4i.js. Read more about it below.
-
Fetching Resources:
It clones thebrotlijsrepository and downloads thebookfile. -
Compressing Book:
It compresses the book file using Brotli. -
Encoding Compressed Book:
The compressed file is then encoded using my custom Base128 algorithm. -
Processing brotli:
The script processesbrotli.jsby removing specified line ranges and patching it with an encoded dictionary. -
Generating c4i:
It combines multiple source files, replaces a placeholder with the encoded book data, minifies the final output usingUglifyJSto producec4i.js, and then removes all temporary files and directories created during the build.
Made with ♡ by Neo X.