Я создал игру, в которой пользователь вводит свое имя перед тем, как начать игру, когда он бросает две кости в игре и получает две совпадающие кости, он получает шанс сыграть еще один раунд, но когда Я нажимаю кнопку «Ролл», и снова запрашивается имя пользователя, когда начинается следующий раунд.
Как мне попросить один раз запросить имя, когда пользователь получает второй раунд для игры?
Я думал об использовании Boolean, но я не уверен, как это реализовать.
var diceImage = ["images/1.jpg","images/2.jpg","images/3.jpg","images/4.jpg","images/5.jpg","images/6.jpg"];
var practiceDate;
var name;
var sum = 0;
document.getElementById("Dice1st").style.display = "none";
document.getElementById("Dice2nd").style.display = "none";
var rounding =1;
var db = openDatabase('RollDice','1.0','Game DB', 2* 1024 * 1024);
var msg;
function roll() {
start();
var dice1 = document.getElementById("Dice1st");
var dice2 = document.getElementById("Dice2nd");
var math1 = Math.floor(Math.random() * 6);
var math2 = Math.floor(Math.random() * 6);
var button = document.getElementById("rollButton");
document.getElementById("Dice1st").style.display = "";
document.getElementById("Dice2nd").style.display = "";
var firstValue = diceImage[math1];
var secondValue = diceImage[math2];
dice1.src = firstValue;
dice2.src = secondValue;
if(math1 == math2)
{
sum = sum + math1 +1 + math2 + 1;
document.getElementById("rollButton").style.display ="";
document.getElementById("rounding").innerHTML = "Number of Round(s): " + rounding;
document.getElementById("totalSum").innerHTML ="Sum of the Dice: " + sum;
rounding++;
}
else
{
document.getElementById("rollButton").style.display ="none";
sum = sum + math1 +1 + math2 + 1;
document.getElementById("rounding").innerHTML = "Number of Round(s): " + rounding;
document.getElementById("totalSum").innerHTML ="Sum of the Dice: " + sum;
}
db.transaction(function (transaction) {
transaction.executeSql('INSERT INTO game (name, noOfAttempts, timeOfAttempt, totalResult) VALUES (?, ?, ?, ?);',[name, rounding, practiceDate.getTime(), sum])
})
}
function start() {
name = prompt("Enter your name. ");
if (name == null) return;
practiceDate = new Date();
document.getElementById("timeDiv").innerHTML = "";
gameCount = 0;
}
function createLocalDB() {
db.transaction(
function (transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS game ( gID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, noOfAttempts INTEGER NOT NULL, timeOfAttempt INTEGER NOT NULL, totalResult NUMERIC NOT NULL);',[],
function dataHandler (transaction, results) {},
function errorHandler (transaction, error) {
console.error("Error creating table: "+ error.code + ": " + error.message);
});
});
}
function checkAnswer() {
//save the results into database.
db.transaction(function (transaction) {
transaction.executeSql('INSERT INTO game (name, noOfAttempts, timeOfAttempt, totalResult) VALUES (?, ?, ?, ?);',[practiceDate.getTime(), name, noOfAttempts, timeOfAttempt, totalResult])
})
}
function processResult(transaction, results) {
displayResult(results);
}
</script>
Когда пользователь нажимает кнопку «Roll it», он запускает игру, а если он получает соответствующие кости, он позволяет пользователю сыграть еще один раунд. Я храню свои данные, такие как имя, количество раундов, время попытки и общие результаты в WebSQL, но когда пользователь получает второй шанс, Web-SQL показывает дублированные данные.