如何阻止Javascript执行

时间:2015-05-25 17:31:12

标签: javascript

所以我用JavaScript编写了一个基本的“Rock Paper Scissors”游戏。当玩家(或计算机)获胜时,游戏会询问玩家是否想要再玩一次。当玩家说是,游戏就像往常一样开始。但是,当玩家说“不”时,整个脚本会重新开始。我想设置它,以便当玩家拒绝时,脚本停止,玩家可以正常浏览网页。我试过搜索stackoverflow,但是当我尝试任何建议时,脚本根本不会启动。有什么帮助吗?

编辑:要明确的是,我只是在Google Chrome上进行了测试。

CODE: ---开始代码---

var mywins = 0
var compwins = 0
var x = 'y'

function intro() {
    var name = prompt('What is your name?');
    alert('Hello, ' + name + '!');
    alert('I want to play rock, paper, scissors with you, ' + name + '.');
    alert("Let's begin. NOTE: You will not be able to quit out until you click 'Prevent this website from creating additional dialogues.' in Google Chrome. Other browsers, I don't know.");
    alert("type 's' for scissors, 'r' for rock, and 'p' for paper! The letters need to be lowercase. Typing in any other character will show you the current score. Your score is first, then mine.");
}

function MainLoop() {
    var mywins = 0
    var compwins = 0
    while (compwins < 3 || mywins < 3) {
        var choice = prompt('Your turn!');
        var compchoice = Math.floor(Math.random() * 3);
        if (choice == 'r' && compchoice == 0) {
            alert("That's a tie!");
        } else if (choice == 'r' && compchoice == 1) {
            alert('I won!');
            compwins = compwins + 1
        } else if (choice == 'r' && compchoice == 2) {
            alert('You won!');
            mywins = mywins + 1
        } else if (choice == 'p' && compchoice == 0) {
            alert('You won!');
            mywins = mywins + 1
        } else if (choice == 'p' && compchoice == 1) {
            alert('Darn! A tie!');
        } else if (choice == 'p' && compchoice == 2) {
            alert('Haha! I picked scissors!');
            compwins = compwins + 1
        } else if (choice == 's' && compchoice == 0) {
            alert('Oh! So sad! I won that round!')
            compwins = compwins + 1
        } else if (choice == 's' && compchoice == 1) {
            alert('Ugh. I guess you won that round...');
            mywins = mywins + 1
        } else if (choice == 's' && compchoice == 2) {
            alert('A tie!')
        }
        alert(mywins + '-' + compwins);
        if (mywins == 3 || compwins == 3) {
            break;
        }
    }
}

function DisplayWin() {
    if (compwins == 3) {
        alert("That's it! Three wins! I won the game! HAHA!");
    } else if (mywins == 3) {
        alert("Crap! you won! Bet you can't beat me TWICE in a row!");
    }
}

function playagain() {
    var x = prompt("Play again? (y) or (n)")
}
if (x === n) return;

---代码结束---

if(x === n) return;部分是我尝试从stackoverflow。我是否正确使用它?

1 个答案:

答案 0 :(得分:0)

如果用户不想继续这样做,您只需在MainLoop()函数的末尾添加提示,并在break循环中添加刚刚while

    if (!confirm("Continue to play?")) {
        break;
    }

或者,在整个函数的上下文中:

function MainLoop() {
    var mywins = 0
    var compwins = 0
    while (compwins < 3 || mywins < 3) {
        var choice = prompt('Your turn!');
        var compchoice = Math.floor(Math.random() * 3);
        if (choice == 'r' && compchoice == 0) {
            alert("That's a tie!");
        } else if (choice == 'r' && compchoice == 1) {
            alert('I won!');
            compwins = compwins + 1
        } else if (choice == 'r' && compchoice == 2) {
            alert('You won!');
            mywins = mywins + 1
        } else if (choice == 'p' && compchoice == 0) {
            alert('You won!');
            mywins = mywins + 1
        } else if (choice == 'p' && compchoice == 1) {
            alert('Darn! A tie!');
        } else if (choice == 'p' && compchoice == 2) {
            alert('Haha! I picked scissors!');
            compwins = compwins + 1
        } else if (choice == 's' && compchoice == 0) {
            alert('Oh! So sad! I won that round!')
            compwins = compwins + 1
        } else if (choice == 's' && compchoice == 1) {
            alert('Ugh. I guess you won that round...');
            mywins = mywins + 1
        } else if (choice == 's' && compchoice == 2) {
            alert('A tie!')
        }
        alert(mywins + '-' + compwins);
        if (mywins == 3 || compwins == 3) {
            break;
        }
        if (!confirm("Continue to play?")) {
            break;
        }
    }
}