我如何检查我的价值是否与其他人匹配,例如配对游戏
how do i check if my value match with other like a pair matching game
所以我有一个游戏有9个盒子,有3个不同的数字,每个数字在随机位置分布3个不同的盒子,每次我转动轮子,都会产生1个随机数,这将打开它对应的。例如,如果我掷出 3 号,盒子 3 将打开......到目前为止我已经做到了,但现在我被困在如何检测它们是否与我之前打开的盒子匹配(意味着如果我打开 2 个盒子相同内乘数)
//here is my box class
public class Safe
{
public int multiplier;
public GameObject reward;
}
//and here is my gamemanager class
public class GameManager : MonoBehaviour
{
public Text[] mText;
public GameObject[] openBox;
private int a, b, c;
public Safe[] safe;
// Start is called before the first frame update
void Start()
{
GenerateRandom()
}
// Update is called once per frame
void Update()
{
// this is where i check, e.g if i roll number 5, box 5 will open..
switch (WheelController.finalAngle)
{
case 0:
openBox[0].SetActive(true);
mText[1].gameObject.SetActive(true);
safe[1].reward.gameObject.SetActive(true);
break;
case 40:
openBox[1].SetActive(true);
mText[2].gameObject.SetActive(true);
safe[2].reward.gameObject.SetActive(true);
break;
case 80:
openBox[2].SetActive(true);
mText[3].gameObject.SetActive(true);
safe[3].reward.gameObject.SetActive(true);
break;
case 120:
openBox[3].SetActive(true);
mText[4].gameObject.SetActive(true);
safe[4].reward.gameObject.SetActive(true);
break;
case 160:
openBox[4].SetActive(true);
mText[5].gameObject.SetActive(true);
safe[5].reward.gameObject.SetActive(true);
break;
case 200:
openBox[5].SetActive(true);
mText[6].gameObject.SetActive(true);
safe[6].reward.gameObject.SetActive(true);
break;
case 240:
openBox[6].SetActive(true);
mText[7].gameObject.SetActive(true);
safe[7].reward.gameObject.SetActive(true);
break;
case 280:
openBox[7].SetActive(true);
mText[8].gameObject.SetActive(true);
safe[8].reward.gameObject.SetActive(true);
break;
case 320:
openBox[8].SetActive(true);
mText[0].gameObject.SetActive(true);
safe[0].reward.gameObject.SetActive(true);
break;
}
Matching();
}
void GenerateRandom() //this is where i generate random Array
{
a = Random.Range(15, 21);
b = Random.Range(15, 21);
c = Random.Range(15, 21);
while ((a == b || b == c || a == c))
{
a = Random.Range(15, 21);
b = Random.Range(15, 21);
c = Random.Range(15, 21);
}
for (int i = 0; i < mText.Length; i++)
{
if (i % 3==0)
{
mText[i].text = "x"+ a.ToString();
safe[i].multiplier = a;
}
if (i % 3 == 1)
{
mText[i].text = "x" + b.ToString();
safe[i].multiplier = b;
}
if (i % 3 == 2)
{
mText[i].text = "x" + c.ToString();
safe[i].multiplier = c;
}
}
}
void Matching()
{
// I WANT TO CHECK IF THEY MATCHING AND WIN THE GAME.
}
}
您需要以某种方式缓存您打开的框,可以在它们自己的字段中用于 first/second/third 打开等,也可以在数组中。
然后在你的匹配方法中,你可以根据你已经缓存的值检查你刚刚打开的框。如果他们匹配,您就知道玩家连续匹配了两个(或三个或您的游戏需要的任何内容),此时您可以在玩家获胜时触发任何您想做的事情。
如果你检查你之前的号码没有匹配,你可以只缓存最近的号码作为最后一个选择的号码,让玩家重新开始选择号码。
所以我有一个游戏有9个盒子,有3个不同的数字,每个数字在随机位置分布3个不同的盒子,每次我转动轮子,都会产生1个随机数,这将打开它对应的。例如,如果我掷出 3 号,盒子 3 将打开......到目前为止我已经做到了,但现在我被困在如何检测它们是否与我之前打开的盒子匹配(意味着如果我打开 2 个盒子相同内乘数)
//here is my box class
public class Safe
{
public int multiplier;
public GameObject reward;
}
//and here is my gamemanager class
public class GameManager : MonoBehaviour
{
public Text[] mText;
public GameObject[] openBox;
private int a, b, c;
public Safe[] safe;
// Start is called before the first frame update
void Start()
{
GenerateRandom()
}
// Update is called once per frame
void Update()
{
// this is where i check, e.g if i roll number 5, box 5 will open..
switch (WheelController.finalAngle)
{
case 0:
openBox[0].SetActive(true);
mText[1].gameObject.SetActive(true);
safe[1].reward.gameObject.SetActive(true);
break;
case 40:
openBox[1].SetActive(true);
mText[2].gameObject.SetActive(true);
safe[2].reward.gameObject.SetActive(true);
break;
case 80:
openBox[2].SetActive(true);
mText[3].gameObject.SetActive(true);
safe[3].reward.gameObject.SetActive(true);
break;
case 120:
openBox[3].SetActive(true);
mText[4].gameObject.SetActive(true);
safe[4].reward.gameObject.SetActive(true);
break;
case 160:
openBox[4].SetActive(true);
mText[5].gameObject.SetActive(true);
safe[5].reward.gameObject.SetActive(true);
break;
case 200:
openBox[5].SetActive(true);
mText[6].gameObject.SetActive(true);
safe[6].reward.gameObject.SetActive(true);
break;
case 240:
openBox[6].SetActive(true);
mText[7].gameObject.SetActive(true);
safe[7].reward.gameObject.SetActive(true);
break;
case 280:
openBox[7].SetActive(true);
mText[8].gameObject.SetActive(true);
safe[8].reward.gameObject.SetActive(true);
break;
case 320:
openBox[8].SetActive(true);
mText[0].gameObject.SetActive(true);
safe[0].reward.gameObject.SetActive(true);
break;
}
Matching();
}
void GenerateRandom() //this is where i generate random Array
{
a = Random.Range(15, 21);
b = Random.Range(15, 21);
c = Random.Range(15, 21);
while ((a == b || b == c || a == c))
{
a = Random.Range(15, 21);
b = Random.Range(15, 21);
c = Random.Range(15, 21);
}
for (int i = 0; i < mText.Length; i++)
{
if (i % 3==0)
{
mText[i].text = "x"+ a.ToString();
safe[i].multiplier = a;
}
if (i % 3 == 1)
{
mText[i].text = "x" + b.ToString();
safe[i].multiplier = b;
}
if (i % 3 == 2)
{
mText[i].text = "x" + c.ToString();
safe[i].multiplier = c;
}
}
}
void Matching()
{
// I WANT TO CHECK IF THEY MATCHING AND WIN THE GAME.
}
}
您需要以某种方式缓存您打开的框,可以在它们自己的字段中用于 first/second/third 打开等,也可以在数组中。
然后在你的匹配方法中,你可以根据你已经缓存的值检查你刚刚打开的框。如果他们匹配,您就知道玩家连续匹配了两个(或三个或您的游戏需要的任何内容),此时您可以在玩家获胜时触发任何您想做的事情。
如果你检查你之前的号码没有匹配,你可以只缓存最近的号码作为最后一个选择的号码,让玩家重新开始选择号码。