汉诺塔的规则:
- 有ABC三个柱子,A柱子上从小到大排列圆盘
- 要将A柱子上所有圆盘移动到C柱子上,每次只能移一个
- 圆盘放置必须从小到大,不能存在此盘子上面有比它大的存在。
比如三个汉诺塔玩法:
理理思路,大体算法就是这个样:
那么算法就很清晰了。
不知道哪一天我又想把这个游戏扩展,我决定用四个类,让游戏的设计更条理一点:
Temp类//临时存储圆盘对象,就是正在移动的圆盘
Torus类//圆盘类,每个圆盘都有
Cylinder类//圆柱类,每个圆柱都用,鼠标点击触发游戏
GameManage类//游戏管理类,储存的游戏对象可以方便管理
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 /// <summary>
6 /// 版本Unity2017.1.0f3
7 /// </summary>
8
9 public class Cylinder : MonoBehaviour
10 {
11 [SerializeField]
12 private int _index;//本柱序号
13
14 public List<GameObject> Torus_List = new List<GameObject>();//存储本柱圆环
15
16 [SerializeField]
17 private GameObject _Temp;
18 private bool _isTrans;//可以最上面可以移动
19
20 [SerializeField]
21 private GameManage GameManager;
22
23 public int Index
24 {
25 get { return _index; }
26 }
27
28 void OnMouseDown()
29 {
30 _isTrans = _Temp.GetComponent<Temp>().isNull;
31 if (_isTrans == true)//可以移动
32 {
33 if (Torus_List.Count != 0)//判断柱子上是否有圆环
34 {
35 TakeTorus();
36 }
37 else if (Torus_List.Count == 0)//判断柱子上没有东西
38 {
39 Debug.Log("你点击的这个柱子没有东西!");
40 }
41 }
42 if (_isTrans == false)
43 {
44 if (Torus_List.Count == 0)//判断要放置的柱子是否有物体
45 {
46 TranslateFunc();
47 }
48 if (Torus_List.Count != 0)//判断要放置的柱子有圆环
49 {
50 if (_Temp.GetComponent<Temp>().Torus_Obj != null)
51 {
52 int a_length = _Temp.GetComponent<Temp>().Torus_Obj.GetComponent<Torus>().TLength;//暂存的圆环长度
53 int b_length = Torus_List[Torus_List.Count - 1].GetComponent<Torus>().TLength;
54 if (a_length < b_length)
55 {
56 TranslateFunc();
57 if (Torus_List.Count == GameManager.mytorus.Length && this._index == 3)
58 {
59 Debug.LogWarning("胜利!!!");
60 }
61 }
62 else
63 {
64 Debug.Log("放置错误,请重新放置!!!");
65 }
66 }
67 }
68 }
69
70 }
71
72 void TranslateFunc()
73 {
74 Torus_List.Add(_Temp.GetComponent<Temp>().Torus_Obj);//为泛型列表添加_Temp暂存得东西
75 Torus_List[Torus_List.Count - 1].transform.position = new Vector3(transform.position.x, transform.position.y-6 + (Torus_List.Count - 1), transform.position.z);//让移动的圆环移动过去
76 _Temp.GetComponent<Temp>().Torus_Obj = null;//清空暂存
77 _Temp.GetComponent<Temp>().isNull = true;//可以再次移动,_Temp是空的
78 Debug.Log("已经移动到" + gameObject.name);
79 GameManager.AddScore();//步数增加
80 }
81
82 void TakeTorus()
83 {
84 //Debug.Log("圆柱被点击!");
85 //Debug.Log(Torus_List[Torus_List.Count - 1] + "为最上面的!");
86 Torus_List[Torus_List.Count - 1].transform.position = _Temp.transform.position;//移动位置
87 _Temp.GetComponent<Temp>().Torus_Obj = Torus_List[Torus_List.Count - 1];//Temp暂存圆环
88 _Temp.GetComponent<Temp>().isNull = false;//Temp处已经有东西了
89 Torus_List.RemoveAt(Torus_List.Count - 1);//移除在在最上面的圆环
90 //Debug.Log(_isTrans);
91 }
92 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 /// <summary>
6 /// 版本Unity2017.1.0f3
7 /// </summary>
8
9 public class Torus : MonoBehaviour
10 {
11 [SerializeField]
12 private int t_Length;//圆环的大小
13
14
15 public int TLength
16 {
17 get { return t_Length; }
18 }
19
20 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 /// <summary>
6 /// 版本Unity2017.1.0f3
7 /// </summary>
8
9 public class Temp : MonoBehaviour
10 {
11
12 public bool isNull = true;//是否为空
13 public GameObject Torus_Obj;//临时存储对象
14
15
16 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 /// <summary>
7 /// 版本Unity2017.1.0f3
8 /// </summary>
9
10 public class GameManage : MonoBehaviour
11 {
12
13 public GameObject[] mycylinders;//所有圆柱
14
15 public GameObject[] mytorus;//所有圆环
16 public GameObject Temp;//临时存储
17
18 public Text scoreText;
19 private int step;
20 void Start ()
21 {
22 //Debug.Log(mycylinders[0]);
23 for (int i = 0; i < mytorus.Length; i++)//让所有圆环先加入第一个圆柱中
24 {
25 Debug.LogWarning("第" + i + "个圆环被插入圆柱A");
26 mycylinders[0].GetComponent<Cylinder>().Torus_List.Add(mytorus[i]);
27 }
28 }
29
30 public void AddScore()
31 {
32 step++;
33 scoreText.text = "移动步数:" + step;
34 }
35 }