gpt4 book ai didi

c# - 如何使统一不两次选择相同的数字?

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:35 25 4
gpt4 key购买 nike

该程序是一个简单的猜数字游戏,但是当我将猜测随机化时,unity 往往会多次选择相同的数字,有没有办法让它知道它已经选择了哪些数字?如果它不能选择另一个号码,还有没有办法让它自动转到丢失的场景?任何帮助表示赞赏^_^

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class NumberGuesser : MonoBehaviour {

int min;
int max;
int guess;
int maxGuessesAllowed = 15;
public Text text;
//guess = (max + min) / 2;

// Use this for initialization
void Start () {
min = 1;
max = 1000;
NextGuess();
max = max + 1;
}

// Update is called once per frame
public void guessHigher () {
min = guess;
NextGuess ();
}
public void guessLower() {
max = guess;
NextGuess();
}
void NextGuess(){
guess = (max + min) / 2;
//guess = Random.Range (min,max);//Randomizes Guesses
//print (guess);
text.text = guess.ToString ();
maxGuessesAllowed = maxGuessesAllowed - 1;
if (maxGuessesAllowed <= 0) {
Application.LoadLevel ("Win");
}
}
}//main

最佳答案

试试这个:

List<int> alreadyGuessed = new List<int>();
...

int NextGuess()
{
int theGuess = Random.Range(min, max);
while(alreadyGuessed.Contains(theGuess))
theGuess = Random.Range(min, max);

alreadyGuessed.Add(theGuess);
return theGuess;
}

它会跟踪已经猜到的内容并继续猜测,直到之前没有猜到。

关于c# - 如何使统一不两次选择相同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30707686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com