如何创建一个随机的计算机选项,除了你已经选择的选项

How to create a random computer choice, except the choice you already choose?

本文关键字:选项 计算机 选择 除了你 一个 何创建 创建 随机      更新时间:2023-09-26

我正在做一个拳击游戏。用户可以在三个拳击手之间进行选择。计算机可以在剩下的两个(你没有选择的)之间进行选择。https://jsfiddle.net/1ehpxto6/我的HTML是

<p id="printNames0"></p>
<div id="jouwbokser">
<h1>Kies Jouw Bokser!</h1>
<img id="bokser1" onClick="gevecht1()"  src="img/bokser1.png"     alt="bokserlinks" />
<img id="bokser2" onClick="gevecht2()"  src="img/bokser2.png" alt="boksermidden" />
<img id="bokser3" onClick="gevecht3()"  src="img/bokser3.png" alt="bokserrechts" />
</div>

我的javascript是:

var bokser = ['bokser1', 'bokser2', 'boker3' ];
var userChoice;
var comChoice;

//functie die laat zien welke bokser je hebt gekozen.
function greet(){
return ("Je hebt" + userChoice.toLowerCase() + "gekozen");
}
//functie die tekst showt in html
function printToPage(test){
var content = document.getElementsByTagName('body')[0];
content.innerHTML = ("<p>" + test + "</p>" + content.innerHTML);
}

//bokser object
var bokser1 = {
hitpoints: 100,
attack1: 2,
attack2:5,
attack3:8,
attack4:12
};
var bokser2 = {
hitpoints:100,
attack1:2,
attack2:5,
attack3:8,
attack4:12
};
var bokser3 = {
hitpoints:100,
attack1:2,
attack2:5,
attack3:8,
attack4:12
    };

//kies bokser
//andere boksers worden display none als bokser gekozen is
//kies bokser1
function gevecht1(){
document.getElementById("bokser2").style.display = "none";
document.getElementById("bokser3").style.display = "none";
userChoice = bokser[0];
printToPage(greet());
}
//kies bokser2
function gevecht2(){
document.getElementById("bokser1").style.display = "none";
document.getElementById("bokser3").style.display = "none";
userChoice = bokser[1];
printToPage(greet());
}
//kies bokser3
function gevecht3(){
document.getElementById("bokser1").style.display = "none";
document.getElementById("bokser2").style.display = "none";
userChoice = bokser[2];
printToPage(greet());
}

有没有人知道如何创建随机选择的计算机,除了你以前选择的那个。

实际上,只需这样做,您就可以做您想做的事情:

user_choice = getUserChoice();
choices.splice(choices.indexOf(user_choice),1);
computer_choice = choices[ Math.round(Math.random()*(choices.length-1)) ];

编辑:看起来这可能更简单。请参见上文。

你要找的是排列盒子,除了已经选择的盒子。下面是一个函数(使用函数permute):

function permute_except(user_choice, remaining_choices) {
  // find the choice
  var index = remaining_choices.indexOf(user_choice);
  // remove the choice 
  remaining_choices.splice(index,1);
  // return the remaining ones, permuted
  return permute(remaining_choices);
}

像一样使用它

user_choice = getUserChoice(); // somehow
possible_choices = permute(user_choice, possible_choices);
var computer_choice_i_1 = possible_choices.pop();
user_choice = getUserChoice(); // somehow
possible_choices = permute_except(user_choice, possible_choices);
var computer_choice_i_2 = possible_choices.pop(); 
// ... etc

编辑:

我以为你要找的东西叫做排列。所以下面描述的是,实际上你想要的是不同的,所以见上面。

排列只是按照某种顺序排列一组事物的一种方式(可能与你发现它的方式不同)。你想对你的盒子进行随机排列。permute(希望足够清楚)返回数组的排列choices(或者如果不将参数传递给permute,则返回数组[0,1,2]的排列):

function permute(choices) { 
   choices = choices || [0,1,2];
    var random_choices = [];
    while(choices.length > 0) {
      var choice_index = Math.round(Math.random()*(choices.length-1));
      random_choices.push(choices[choice_index]); 
      choices.splice(choice_index,1);
    }
    return random_choices;
}

或者,您可以实现不同类型的排列函数,如RC4中的算法,它只需交换数组中随机选择的索引对的值。

如果你想获得更多的功能,你可以实现一个non-fixed的排列,也就是说,它没有固定点。这也被称为derangement

剩下的,由你决定,祝你好运!

您的问题基本上就像选择彩票球而不将球送回盒子一样。它被称为Fisher–Yates shuffle(F-Y-Knuth shuffle),是中描述的O(n)算法http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle