11using System . Collections ;
22using DefaultNamespace ;
3+ using DefaultNamespace . Problems . Common ;
34using DG . Tweening ;
45using Nineva ;
56using UnityEngine ;
67using UnityEngine . Assertions ;
78
8- public class SortStackUsingTwoStacksProblem : MonoBehaviour
9+ public class SortStackUsingTwoStacksProblem : MonoBehaviour , IProblem
910{
10- [ SerializeField ] [ Range ( 0.1f , 2f ) ] float _speed = 0.5f ;
11-
11+ [ SerializeField ] [ Range ( 0.1f , 2f ) ] float _speed = 0.5f ;
12+
1213 [ SerializeField ] GameObject tmpLocation ;
1314 [ SerializeField ] VisualStackInt s1 ;
1415 [ SerializeField ] VisualStackInt s2 ;
1516
16- void Start ( )
17- {
18- // StartCoroutine(Run(s1));
19- // StartCoroutine(Run(s2));
20-
21- // Sort(s1);
22- }
23-
24- public void OnRunButtonClick ( )
25- {
26- StartCoroutine ( Sort ( ) ) ;
27- }
28-
29- IEnumerator Run ( VisualStackInt stack )
30- {
31- yield return PushAnimated ( 1 , new WaitForSeconds ( 0.5f ) , stack ) ;
32- yield return PushAnimated ( 2 , new WaitForSeconds ( 0.5f ) , stack ) ;
33- yield return PushAnimated ( 3 , new WaitForSeconds ( 0.5f ) , stack ) ;
34- yield return PushAnimated ( 4 , new WaitForSeconds ( 0.5f ) , stack ) ;
35- yield return PushAnimated ( 5 , new WaitForSeconds ( 0.5f ) , stack ) ;
36-
37- yield return PopAnimated ( new WaitForSeconds ( 0.5f ) , stack ) ;
38- yield return PopAnimated ( new WaitForSeconds ( 0.5f ) , stack ) ;
39- yield return PopAnimated ( new WaitForSeconds ( 0.5f ) , stack ) ;
40- yield return PopAnimated ( new WaitForSeconds ( 0.5f ) , stack ) ;
41- yield return PopAnimated ( new WaitForSeconds ( 0.5f ) , stack ) ;
42- }
43-
44- IEnumerator PopAnimated ( WaitForSeconds waitForSeconds , VisualStackInt stack )
45- {
46- yield return waitForSeconds ;
47- var gameObj = stack . Pop ( ) . go ;
48- yield return SmoothLerp ( 2f , gameObj ) ;
49- Destroy ( gameObj ) ;
50- }
51-
52- // just to demo animation
53- static IEnumerator SmoothLerp ( float time , GameObject target )
54- {
55- var position = target . transform . position ;
56- var startingPos = position ;
57- var finalPos = position + target . transform . right * 5 ;
58- float elapsedTime = 0 ;
59-
60- while ( elapsedTime < time )
61- {
62- target . transform . position = Vector3 . Lerp ( startingPos , finalPos , elapsedTime / time ) ;
63- elapsedTime += Time . deltaTime ;
64- yield return null ;
65- }
66- }
67-
68- IEnumerator PushAnimated ( int item , WaitForSeconds waitFor , VisualStackInt stack )
69- {
70- stack . Push ( item ) ;
71- yield return waitFor ;
72- }
73-
7417 IEnumerator Sort ( )
7518 {
7619 Assert . IsTrue ( s2 . IsEmpty , "Second stack must be empty" ) ;
@@ -92,4 +35,14 @@ IEnumerator Sort()
9235 yield return s2 . PopAndPushTo ( s1 , _speed ) ;
9336 }
9437 }
38+
39+ public void Reset ( )
40+ {
41+ throw new System . NotImplementedException ( ) ;
42+ }
43+
44+ public void RunSimulation ( )
45+ {
46+ StartCoroutine ( Sort ( ) ) ;
47+ }
9548}
0 commit comments