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

标签 c# unity3d

该程序是一个简单的猜数字游戏,但是当我将猜测随机化时,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/

相关文章:

c# - 检测具有相似名称的最远物体

c# - 在 Unity C# WWW 中显示进度条

c# - 在 Unity 中使用 Getters 和 Setters

c# - 停止 Visual Studio 2019,让我在 Debug模式下编辑文件

时间:2019-03-27 标签:c#NewtonJsonJarraychecknull/empty错误

c# - 传递对 C# 数组中元素的引用

c# - 如何在 ReactiveX 中合并两个 bool 值

c# - 通用容器 : why do I need an interface?

c# - 连接四(Unity3D 和 C#)的 Minimax : Problems

c# - 与地面上的 "dirt"碰撞时放慢播放器