简体   繁体   中英

simple Javascript game, repeat function. (newbie question)

I am new to coding and stack overflow so go easy on me. I would very much appreciate some help trying to interpret my goals into a functional script for a game on a html5 website. The script functions to provide a race result simulating a horse race, that can then be used within the RPG later.

Pick your selection, hit race and a winner is selected with odds related to chance of winning.

I would like alter the script to provide a full set of places from 1st through to 8th. I figured the two ways to achieve this. Roll the dice again excluding the winner all the way to 8th or, have the race the result of +1 points first to 10 points. This way I can simulate winning by x lengths.

var gen_number = getRoll(serverSeed, clientSeed, $nonceValue);

var winnerNumber = 0;
if(gen_number < 836)
  winnerNumber = 1;
else if(gen_number < 2172)
  winnerNumber = 2;
else if(gen_number < 2840)
  winnerNumber = 3;
else if(gen_number < 4109)
  winnerNumber = 4;
else if(gen_number < 5766)
  winnerNumber = 5;
else if(gen_number < 7837)
  winnerNumber = 6;
else if(gen_number < 8589)
  winnerNumber = 7;
else
  winnerNumber = 8;

var status = '';
if($selectedRange.includes('' + winnerNumber)) {
  document.getElementById('winner_lose').innerHTML = winnerNumber + " Wins";
  status = 'Win';

Questions - How do I script automatic reroll for 2nd, 3rd place. Press race button once for roll get winner, roll again excluding winner, roll again excluding winner and 2nd. Output results?

or

How do I make winning conditions first to 10 points 1 point per roll?

Any help would be appreciated its a fun concept to learn with!

Thankyou in advance.

Here is a working example based on your code that demonstrates a simple way to award one point per roll with the first to 10 declared the winner:

const serverSeed = 0, clientSeed = 0, $nonceValue = 0;  // stubbed values
const getRoll = () => Math.random() * 10000;  // stubbed function

const getPointWinner = () => {
  const gen_number = getRoll(serverSeed, clientSeed, $nonceValue);

  if (gen_number < 836)
    return 0;
  if (gen_number < 2172)
    return 1;
  if (gen_number < 2840)
    return 2;
  if (gen_number < 4109)
    return 3;
  if (gen_number < 5766)
    return 4;
  if (gen_number < 7837)
    return 5;
  if (gen_number < 8589)
    return 6;
  return 7;
}

let raceWon = false;
const pointsWon = [0, 0, 0, 0, 0, 0, 0, 0];
while (!raceWon) {
  const pointWinner = getPointWinner();  // get the winner for this point
  pointsWon[pointWinner]++;  // award the point
  raceWon = pointsWon[pointWinner] >= 10;  // race won if point winner has 10 points
}

// pointsWon can be used to print out the winner, final order, won by x lengths, etc.
console.log(pointsWon);  // prints the array containing the points won

const winner = pointsWon.indexOf(10) + 1;  // + 1 since pointsWon uses a zero-based index
console.log('winner:', winner);  // prints the first to 10 points

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM