I’m currently developing a browser-based game, but the issue lies in the fact that the same repetitive actions occur in each

Creating a web-based game has been a challenge for me as the same sequence of events occurs in every round, leading to predictability and monotony. The issue lies in the lack of randomization, which is crucial for maintaining excitement and unpredictability in gameplay.

The concept behind the game (inspired by Russian roulette) involves a 1 in 6 chance of dying when pressing the button. Each player takes their turn, starting from the leftmost position and progressing towards the bottom left ball. If the shot value matches the loaded value when the trigger is pulled, the player dies. The objective is to survive until the end.

If you have a solution to address this problem, I would greatly appreciate it. As a beginner in game development, any guidance or explanation will be invaluable. Thank you for your assistance.

Answer №1

The issue lies in the initial assignment within your first if statement. You are continuously reassigning 'players' to 'balls.length', resulting in a consistent outcome.

var shot = (Math.floor(Math.random() * 6) + 1);
var loaded = (Math.floor(Math.random() * 6) + 1);
var players = 0;
var shootbtn = document.getElementById("shootbtn");
var tabletext = document.getElementById("tableP");
var lucktext = document.getElementById("tableluck");
var balls = document.getElementsByClassName('ball');
var won = document.getElementById("won");

shootbtn.addEventListener("click", shoot);

function shoot() {

  if (players === balls.length) { // There was a mistake here with a single '=' symbol, causing it to reassign instead of checking
    players = 0;
  }
  if (loaded == shot) {
    balls[players].remove();
    tabletext.currentTime = 0;
    tabletext.classList.add("ani");
    setTimeout(function() {
      tabletext.classList.remove("ani");
    }, 1001);
    shot = (Math.floor(Math.random() * 6) + 1);
    loaded = (Math.floor(Math.random() * 6) + 1);
    if (loaded > 6) {
      loaded = 1;
    }
    console.log('Game over');
  } else {
    lucktext.currentTime = 0;
    lucktext.classList.add("ani");
    setTimeout(function() {
      lucktext.classList.remove("ani");
    }, 1001);
    loaded = loaded + 1;
    if (loaded > 6) {
      loaded = 1;
    }
    console.log("You survived this round");
    players = players + 1;

  }
}
... (CSS and HTML code remains the same)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on using jCrop to resize images to maintain aspect ratio

Utilizing Jcrop to resize an image with a 1:1 aspect ratio has been mostly successful, but I've encountered issues when the image is wider. In these cases, I'm unable to select the entire image. How can I ensure that I am able to select the whole ...

Experiencing an error while trying to implement a node.js server-side service through AngularJS. Encountering the error message: "Error: [$injector:nomod] Module ‘angularjsNode

An error has occurred: Error: [$injector:modulerr] Failed to instantiate module angularjsNodejsTutorial due to: Error: [$injector:nomod] Module ‘angularjsNodejsTutorial’ is not available! You either misspelled the module name or forgot to load it. If r ...

Create a PHP script that directly sets a cookie based on user input from a

In the code snippet provided below, I have attempted to create a simple name input form that will be stored as a cookie in the browser. However, it seems that the functionality is not working as expected. Can anyone help me identify the issues in this co ...

I'm having trouble setting up Stripe Elements in PHP. It seems like there's a communication issue between my PHP code and my JS

New to setting up Stripe Elements, I've followed the documentation closely. Installed the necessary JS modules, included the Stripe API, and connected it to the Stripe JS. In my index.php file, PHP script is at the top with HTML and JavaScript below i ...

Revamping JSON structure by identifying id references

I recently attempted to update the city name within the JSON object provided below. "City":[ { "Name":"Delhi", "id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd", "Towns":[ ...

JavaScript loop to target a specific item

My goal is to animate the correct div under each navigation item, rather than all of them with the "navItemUnder" class. You can see exactly what I mean by hovering over a navigation item in this codePen. I am looking for a solution to target only one lin ...

Experiencing difficulties transmitting the ® symbol via AJAX request

I am currently working on a simple asp.net web application that utilizes YUI for Ajax requests. The application reads text from a text box and sends an AJAX request to the server. Below is the code snippet: <body> <form id="form1" runat="serv ...

Enabling Autoplay for HTML Videos on Safari iOS 14.2

Recently, I've observed that my autoplay videos are not functioning on iPhone iOS 14.2. Surprisingly, everything was working fine prior to this version. Is there anyone familiar with a solution or workaround for this issue? Perhaps there's a met ...

"Encountering a Heroku error due to an oversized cookie while using Express.js and

Being unsure of the exact cause, I am encountering an issue with Heroku that gives me the error message t=error code=H25 desc="HTTP restriction: oversized cookie" whenever I attempt to authenticate myself with Discord OAuth. Interestingly, this problem onl ...

How can a string be transformed into a JavaScript Object without using JSON?

I have a good grasp on parsing a valid JSON string using JSON.parse('{"key" : "value"}'). But what happens when dealing with a valid JS object, but invalid JSON, such as: JSON.parse("{ key : 'value'}")? The outcome of this example is a ...

Display a pop-up video player when a button is clicked to watch multiple videos on a webpage utilizing jQuery/Javascript

I am in need of assistance with integrating multiple embedded YouTube videos (using iframes) that appear on the screen after a user clicks a custom button. While there are solutions available for single videos, I am struggling to make it work for multiple ...

Inquiring about monitoring a date object in AngularJS?

Is it possible to monitor a date in angularJS? I have an ng-model that has a date and I would like to monitor it so that I receive a notification when it changes month, year, or day. scope.$watch('ngModel',function(newVal){ console.log(newVa ...

Transferring an array between Javascript and Django

I am working with an array of objects in JavaScript, like this: Arr = [0: {k;v}, 1: {k,v}] and so on, each containing numerous fields. The challenge I'm facing is in sending these objects to Django. I have attempted using JSON.stringify to send the ...

Can you explain the distinction between using `yarn link` versus `npm link`?

There have been instances where npm link seemed to work better than yarn link which resulted in strange errors. This discrepancy caught my attention a few times. I was under the impression that both commands were essentially creating symbolic links, but ...

clearing all input fields upon submission in React Native

I need help resolving an error that occurs when I try to clear text input fields on a button press. Instead of clearing the fields, it throws an undefined error because I am trying to set the value as {this.state.inputTextValue} and then clear it using set ...

What are the best ways to personalize the Ant Design table and pagination component?

Is there a way to customize and design table and pagination components? Specifically, I am looking to set the header color of the table as green. How can this be achieved? Similarly, for the pagination component, I want to have a background color on page n ...

What is the best way to understand and decipher this CSS code?

As I delve into customizing the CSS of an Internet theme, I am faced with a syntax that is unfamiliar and puzzling to me. One example from the theme is as follows: @media screen and(max-width: 768 px) { .wy-tray-container { bottom: auto;top: 0; ...

What is the best way to generate a fresh JSON object within a react hook function?

I am currently facing two issues. Firstly, I am having trouble figuring out how to add/update the JSON items within a hook. Secondly, React seems to be restricting me from using the name that is stored in a previous JSON file. I am open to exploring alter ...

What is the best way to showcase images in a carousel using an image URL?

I am struggling to populate images in a carousel using image URLs from JSON data. Although I attempted to use the attr() method in jQuery to set attributes, I have not been able to achieve the desired outcome. Below is the code snippet in question: <di ...

Attempting to pass an asynchronous initial state to the useReducer method is resulting in an undefined return value

I am facing an issue where I am fetching asynchronous data in the parent component and trying to use it as initialState in VersionContainer. However, when I pass this initialState to the context provider, the values in ContentVersionProvider end up being ...