由于某些原因未定义骰子掷骰游戏变量

时间:2019-03-29 02:41:08

标签: javascript

我创建了一个掷骰子游戏。数组图像用于骰子。页面上的两个骰子分别代表游戏1和游戏2。骰子值最高的游戏是获胜者。页面上显示的骰子应以相同的骰子值开始,直到用户刷新页面为止。当用户刷新页面时,骰子将显示新的随机骰子值。骰子上方将显示一条消息,说明一号或二号玩家是赢家。但是,在玩游戏之前,会出现一条消息,提示游戏是抽奖。为什么我的maximumDiceRoll和获奖者变量未定义?

#include <vector>

constexpr long long SIEVE_MAX = 10000;

template <int ADDER_INCREASE>
class GenericSieve
{
    static std::vector<bool> Sieve;

    static std::vector<bool> populated_sieve()
    {
        int number = 0;
        int adder = 1;
        std::vector<bool> sieve(SIEVE_MAX);

        for (int i = 0; i < SIEVE_MAX; i++)
        {
            if (i == number)
            {
                sieve[i] = true;
                number += adder;
                adder += ADDER_INCREASE;
            }
            else
            {
                sieve[i] = false;
            }
        }

        return sieve;
    }
public:
    static bool belongs(long long n)
    {
        if (Sieve.size() == 0)
        {
            Sieve = populated_sieve();
        }
        return Sieve.at(n);
    }
};
template<int inc>
std::vector<bool> GenericSieve<inc>::Sieve;

// define a sieve for every number you like
using TriangularSieve = GenericSieve<1>;
using SquareSieve = GenericSieve<2>;
using PentagonalSieve = GenericSieve<3>;

// define functions if you will
bool isTriangleNumber(long long int n)
{
    return TriangularSieve::belongs(n);
}
bool isSquareNumber(long long int n)
{
    return SquareSieve::belongs(n);
}
bool isPentagonalNumber(long long int n)
{
    return PentagonalSieve::belongs(n);
}

1 个答案:

答案 0 :(得分:1)

问题在于您正在比较两个数组player1player2。您应该比较随机值randPlayer1randPlayer2

var highestDiceRoll = 0;
var winner;
 function rollDice () {
 let player1 = ['<img src="images/dice1.png" alt="Italian Trulli">',
 '<img src="images/dice2.png" alt="Italian Trulli">',
 '<img src="images/dice3.png" alt="Italian Trulli">',
 '<img src="images/dice4.png" alt="Italian Trulli">',
 '<img src="images/dice5.png" alt="Italian Trulli">',
 '<img src="images/dice6.png" alt="Italian Trulli">'];

 let player2 = ['<img src="images/dice1.png" alt="Italian Trulli">',
 '<img src="images/dice2.png" alt="Italian Trulli">',
 '<img src="images/dice3.png" alt="Italian Trulli">',
 '<img src="images/dice4.png" alt="Italian Trulli">',
 '<img src="images/dice5.png" alt="Italian Trulli">',
 '<img src="images/dice6.png" alt="Italian Trulli">'];

var randPlayer1 = Math.floor(Math.random() * player1.length);
var randPlayer2 = Math.floor(Math.random() * player2.length);

 var diceRolls = player1[randPlayer1] + " " + player2[randPlayer2];

 if (randPlayer1 > randPlayer2) {

    highestDiceRoll = player1;
    winner = "Player 1";
  } else if (randPlayer2 > randPlayer1) {
    highestDiceRoll = player2;
    winner = "Player 2";
  } else {
    winner = "GAME IS A DRAW";
  }

document.write("The winner is "  + winner + "<br />");
   return diceRolls; //return to display dice on page
}

document.write(rollDice());