-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural-network-simple-game.js
56 lines (48 loc) · 1.81 KB
/
neural-network-simple-game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const brain = require('brain.js');
const readline = require('readline');
// Initialize the neural network
const net = new brain.NeuralNetwork();
// Define training data
const trainingData = [];
for (let i = 1; i <= 100; i++) {
trainingData.push({ input: { guess: i / 100 }, output: { target: i / 100 } });
}
// Train the neural network
net.train(trainingData, {
iterations: 2000, // Increase the number of training iterations
errorThresh: 0.01, // Set a lower error threshold
log: true,
logPeriod: 100
});
// Initialize readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function to play the game
function playGame() {
console.log("Welcome to the Number Guessing Game!");
console.log("Guess a number between 1 and 100.");
// Generate a random number for the player to guess
const targetNumber = Math.floor(Math.random() * 100) + 1;
// Function to handle user input
rl.question("Your guess: ", (answer) => {
const playerGuess = parseInt(answer);
if (playerGuess === targetNumber) {
console.log("Congratulations! You guessed the correct number.");
rl.close();
return;
} else {
// Normalize the player's guess between 0 and 1 for the neural network
const normalizedGuess = playerGuess / 100;
// Run the neural network to predict the target number
const output = net.run({ guess: normalizedGuess });
const predictedNumber = Math.round(output.target * 100);
console.log(`Sorry, that's not correct. The neural network predicts the number is approximately ${predictedNumber}.`);
// Ask for another guess
playGame();
}
});
}
// Start the game
playGame();