简体   繁体   English

简单的Javascript游戏,重复功能。 (新手问题)

[英]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. 我非常感谢您在尝试将我的目标解释为html5网站上的游戏的功能脚本时所提供的帮助。 The script functions to provide a race result simulating a horse race, that can then be used within the RPG later. 该脚本的功能是提供模拟赛马的比赛结果,随后可在RPG中使用。

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. 我想更改脚本以提供从1号到8号的完整位置。 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. 再次将骰子掷出,一直排除在获胜者之外,一直上升到第8位,或者使比赛的结果先是+1分到10分。 This way I can simulate winning by x lengths. 这样,我可以模拟x长度的获胜。

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? 我如何使获胜条件首先达到10点/卷1点?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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