I'm working on a school project with leap motion. I have created a simple rock, paper, scissors javascript game where you can use your hand gestures to make your choice. You play against the computer. But unfortunately, I can't test my code because I always get the same error. It's the one below:
leap-1.1.1.js:381 WebSocket connection to 'ws://127.0.0.1:6437/v6.json' failed:
BrowserConnection.setupSocket @ leap-1.1.1.js:381
BaseConnection.connect @ leap-1.1.1.js:334
Controller.connect @ leap-1.1.1.js:613
Controller.loop @ leap-1.1.1.js:670
loop @ leap-1.1.1.js:2504
(anonymous) @ game.js:8
I personally have no understanding of what exactly this means and what I need to do to fix this. so I hope you guys can help me. Thanks!
I looked in the file where the error occurs. but this is the file I used to install leap.js. i do not expect to have to change anything in it. I also asked my question to chatGPT but I did not get a useful answer.
I tried this but I'm not sure I looked at it correctly because it honestly doesn't work. This is the code if you want to take a look at it:
// Set up the game
const choices = ['rock', 'paper', 'scissors'];
let playerChoice = null;
let computerChoice = null;
let result = null;
// Listen for the frame event
Leap.loop(frame => {
// Get the hands in the frame
const hands = frame.hands;
// If there are no hands in the frame, return
if (hands.length === 0) return;
// Get the first hand in the frame
const hand = hands[0];
// Get the fingers in the hand
const fingers = hand.fingers;
// Count the extended fingers
const extendedFingers = fingers.filter(finger => finger.extended);
// If the hand is in a fist, set the player's choice to null
if (extendedFingers.length === 0) {
playerChoice = null;
return;
}
// If one finger is extended, set the player's choice based on the finger
if (extendedFingers.length === 1) {
const finger = extendedFingers[0];
if (finger.type === 0) playerChoice = 'rock';
if (finger.type === 1) playerChoice = 'scissors';
if (finger.type === 2) playerChoice = 'paper';
}
});
// Listen for clicks on the choice buttons
$('#choices button').on('click', function() {
// Set the player's choice based on the button
playerChoice = $(this).attr('id');
// Generate a random computer choice
computerChoice = choices[Math.floor(Math.random() * 3)];
// Determine the result of the game
if (playerChoice === computerChoice) result = 'tie';
else if (playerChoice === 'rock' && computerChoice === 'scissors') result = 'win';
else if (playerChoice === 'paper' && computerChoice === 'rock') result = 'win';
else if (playerChoice === 'scissors' && computerChoice === 'paper') result = 'win';
else result = 'lose';
// Display the result of the game
$('#result').text(`You chose ${playerChoice}, computer chose ${computerChoice}. You ${result}!`);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Leap Motion Rock Paper Scissors Game</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="node_modules/leapjs/leap-1.1.1.js"></script>
<script src="game.js"></script>
</head>
<body>
<div id="choices">
<button id="rock">Rock</button>
<button id="paper">Paper</button>
<button id="scissors">Scissors</button>
</div>
<div id="result"></div>
</body>
</html>