Esempio n. 1
0
        private void CrushFiveCandiesHorizontal(Node currentNode, DLinked candiesLinkedList)
        {
            Node currentNodeRight      = currentNode.right;
            Node currentNodeRightRight = currentNodeRight.right;
            Node currentNodeLeft       = currentNode.left;
            Node currentNodeLeftLeft   = currentNodeLeft.left;

            currentNode.data.Name           = ".";
            currentNodeRight.data.Name      = ".";
            currentNodeRightRight.data.Name = ".";
            currentNodeLeft.data.Name       = ".";
            currentNodeLeftLeft.data.Name   = ".";

            Console.WriteLine("finish ");
            candiesLinkedList.Print();
            Console.WriteLine("Generating New Nodes ...: ");
            //generate Random
            GenerateNewRandom(currentNode);
            GenerateNewRandom(currentNodeRight);
            GenerateNewRandom(currentNodeRightRight);
            GenerateNewRandom(currentNodeLeft);
            GenerateNewRandom(currentNodeLeftLeft);

            CheckGrid(candiesLinkedList);
            candiesLinkedList.Print();
        }
Esempio n. 2
0
        private void CrushFourCandiesVertical(Node startNode, DLinked candiesLinkedList)
        {
            Node upNode                = startNode.GetUpNode(startNode);
            Node startNodeDown         = startNode.GetDownNode(startNode);
            Node startNodeDownDown     = startNodeDown.GetDownNode(startNodeDown);
            Node startNodeDownDownDown = startNodeDownDown.GetDownNode(startNodeDownDown);

            if (upNode == null)
            {
                startNode.data.Name             = ".";
                startNodeDown.data.Name         = ".";
                startNodeDownDown.data.Name     = ".";
                startNodeDownDownDown.data.Name = ".";
            }
            else
            {
                startNodeDownDownDown           = SwapNodes(startNodeDownDownDown, upNode);
                startNode.data.Name             = ".";
                startNodeDown.data.Name         = ".";
                startNodeDownDown.data.Name     = ".";
                startNodeDownDownDown.data.Name = ".";
            }

            Console.WriteLine("finish ");
            candiesLinkedList.Print();
            Console.WriteLine("Generating New Nodes ...: ");
            //generate Random
            GenerateNewRandom(startNode);
            GenerateNewRandom(startNodeDown);
            GenerateNewRandom(startNodeDownDown);
            GenerateNewRandom(startNodeDownDownDown);

            CheckGrid(candiesLinkedList);
            candiesLinkedList.Print();
        }
Esempio n. 3
0
        private void CrushFiveCandiesVertical(Node currentNode, DLinked candiesLinkedList)
        {
            Node currentNodeUp       = currentNode.GetUpNode(currentNode);
            Node currentNodeUpUp     = currentNodeUp.GetUpNode(currentNodeUp);
            Node currentNodeDown     = currentNode.GetDownNode(currentNode);
            Node currentNodeDownDown = currentNodeDown.GetDownNode(currentNodeDown);

            currentNode.data.Name         = ".";
            currentNodeUp.data.Name       = ".";
            currentNodeUpUp.data.Name     = ".";
            currentNodeDown.data.Name     = ".";
            currentNodeDownDown.data.Name = ".";

            Console.WriteLine("finish ");
            candiesLinkedList.Print();
            Console.WriteLine("Generating New Nodes ...: ");
            //generate Random
            GenerateNewRandom(currentNode);
            GenerateNewRandom(currentNodeUp);
            GenerateNewRandom(currentNodeUpUp);
            GenerateNewRandom(currentNodeDown);
            GenerateNewRandom(currentNodeDownDown);

            CheckGrid(candiesLinkedList);
            candiesLinkedList.Print();
        }
Esempio n. 4
0
        public void CandiesGrid()
        {
            DLinked candiesLinkedList = new DLinked();
            Random  rnd = new Random();

            for (int i = 0; i < size; i++)
            {
                Node newNode = new Node();
                newNode.data.Score = rnd.Next(1, 5);
                newNode            = SetNodeName(newNode);

                //int nod = rnd.Next(1, 5);
                //obj.add(nod);
                candiesLinkedList.add(newNode);
                if (i == 0)
                {
                    TopL = candiesLinkedList.head;
                }
                if (i == 24)
                {
                    RDown = candiesLinkedList.tail;
                }
            }

            CheckGrid(candiesLinkedList);
            candiesLinkedList.Print();
            Play(candiesLinkedList);
        }
Esempio n. 5
0
        private void CheckGrid(DLinked CandiesGrid)
        {
            Node currentNode = CandiesGrid.head;

            for (int i = 0; i < 25; i++)
            {
                if (MatchNodeWith2Right(currentNode) == true)
                {
                    GenerateNewRandom(currentNode.right.right);
                }

                if (MatchNodeWith2Left(currentNode) == true)
                {
                    GenerateNewRandom(currentNode);//change current node not the Up nodes to make sure all previous nodes are not the same
                }
                if (MatchNodeWith2Up(currentNode) == true)
                {
                    GenerateNewRandom(currentNode);//change current node not the Up nodes to make sure all previous nodes are not the same
                }
                if (MatchNodeWith2Down(currentNode) == true)
                {
                    var firstDown  = currentNode.GetDownNode(currentNode);
                    var secondDown = currentNode.GetDownNode(firstDown);
                    GenerateNewRandom(secondDown);// change last down node to make sure all previous nodes are not changed
                }

                currentNode = currentNode.right;
            }

            //TO DO: check if the grid has no available moves , then game over
        }
Esempio n. 6
0
        public Node GetNodeWithIndex(DLinked CandiesGrid, int index)
        {
            var currentNode = CandiesGrid.head;

            for (int i = 1; i <= index; i++)
            {
                currentNode = currentNode.right;
            }
            return(currentNode);
        }
Esempio n. 7
0
        public void Play(DLinked candiesLinkedList)
        {
            //1- Get Index From User with validation from 0 to 24 ,numbers only
            string indexInput = ReadInput("Enter the number of index ,you want to move ( from 0 t0 24)", ConsoleColor.Magenta);
            int    index      = ValidatIndex(indexInput);

            string directionInput = ReadInput("What is the Direction you want to move the candy to ?!", ConsoleColor.Magenta);
            string direction      = ValidateDirection(index, directionInput, candiesLinkedList);

            MoveCandy(index, direction, candiesLinkedList);
        }
Esempio n. 8
0
        private void CrushCandyWithTwoUpCandies(Node currentNodeAfterSwap, DLinked candiesLinkedList)
        {
            Node newCurrentUp   = new Node();
            Node newCurrentUpUp = new Node();
            Node TopNode        = new Node();

            while (TopNode != null)
            {
                //swap current Up
                newCurrentUp   = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                newCurrentUpUp = newCurrentUp.GetUpNode(newCurrentUp);

                TopNode = newCurrentUpUp.GetUpNode(newCurrentUpUp);

                if (TopNode == null)
                {
                    currentNodeAfterSwap.data.Name = ".";
                    newCurrentUp.data.Name         = ".";
                    newCurrentUpUp.data.Name       = ".";
                    break;
                }
                else
                {
                    currentNodeAfterSwap = SwapNodes(currentNodeAfterSwap, TopNode);
                    Node currentNodeAfterSwapUp = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                    if (currentNodeAfterSwapUp == null)
                    {
                        currentNodeAfterSwap.data.Name = ".";
                        newCurrentUp.data.Name         = ".";
                        newCurrentUpUp.data.Name       = ".";
                        break;
                    }
                    else
                    {
                        Node currentNodeAfterSwapDown     = currentNodeAfterSwap.GetDownNode(currentNodeAfterSwap);
                        Node currentNodeAfterSwapDownDown = currentNodeAfterSwapDown.GetDownNode(currentNodeAfterSwapDown);
                        currentNodeAfterSwapUp = SwapNodes(currentNodeAfterSwapUp, currentNodeAfterSwapDownDown);
                    }
                }
            }
            Console.WriteLine("finish ");
            candiesLinkedList.Print();
            Console.WriteLine("Generating New Nodes ...: ");
            //generate Random
            GenerateNewRandom(currentNodeAfterSwap);
            GenerateNewRandom(newCurrentUp);
            GenerateNewRandom(newCurrentUpUp);
            CheckGrid(candiesLinkedList);

            candiesLinkedList.Print();
        }
Esempio n. 9
0
        private void MoveCandyDown(Node currentNode, Node nodeToSwap, DLinked candiesLinkedList, int index, string direction)
        {
            Node currentNodeAfterSwap = new Node();

            if (MatchTwoRightTwoLeft(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 5 * currentNodeAfterSwap.data.Score;
                CrushFiveCandiesHorizontal(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoRightOneLeft(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
            }
            else if (MatchTwoLeftOneRight(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
            }
            else if (MatchTwoDown(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNode.data.Score;
                CrushCandyWithTwoDownCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoRight(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNode.data.Score;
                CrushCandyWithTwoRightCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoLeft(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNode.data.Score;
                CrushCandyWithTwoLeftCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchOneRightOneLeft(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
            }
            else
            {
                DisplayUnmatchedDirection(index, direction, candiesLinkedList);
            }
        }
Esempio n. 10
0
        private string ValidateDirection(int index, string direction, DLinked candiesLinkedList)
        {
            direction = direction.ToLower();
            while (!(direction == "up" || direction == "down" || direction == "right" || direction == "left"))
            {
                direction = ReadInput("Wrong Direction !! please enter Again :", ConsoleColor.Red);
                direction = direction.ToLower();
            }

            string validDirection = ValidateDirectionWithIndex(index, direction);

            validDirection = ValidateSameNode(candiesLinkedList, index, validDirection);

            return(validDirection);
        }
Esempio n. 11
0
        private void DisplayUnmatchedDirection(int index, string direction, DLinked candiesGrid)
        {
            DisplayMessage("Unmatched Candies, please select another direction, Or press N for New Index", ConsoleColor.Red);
            string directionInput = Console.ReadLine();

            if (directionInput.ToLower() == "n")
            {
                Play(candiesGrid);
            }
            else
            {
                direction = ValidateDirection(index, directionInput, candiesGrid);
            }

            MoveCandy(index, direction, candiesGrid);
        }
Esempio n. 12
0
        private string ValidateSameNode(DLinked candiesLinkedList, int index, string direction)
        {
            direction = direction.ToLower();
            Node currentNode = GetNodeWithIndex(candiesLinkedList, index);

            //string validDirection = direction;
            switch (direction)
            {
            case "up":
                Node upNode = currentNode.GetUpNode(currentNode);
                if (upNode != null && currentNode.data.Score == upNode.data.Score)
                {
                    string userInput = ReadInput("Wrong Movement up!!, Same Candies please select another Direction", ConsoleColor.Red);
                    direction = ValidateDirection(index, userInput, candiesLinkedList);
                }
                break;

            case "down":
                Node downNode = currentNode.GetDownNode(currentNode);
                if (downNode != null && currentNode.data.Score == downNode.data.Score)
                {
                    string userInput = ReadInput("Wrong Movement down!!, Same Candies please select another Direction", ConsoleColor.Red);
                    direction = ValidateDirection(index, userInput, candiesLinkedList);
                }
                break;

            case "right":
                if (currentNode.right != null && currentNode.data.Score == currentNode.right.data.Score)
                {
                    string userInput = ReadInput("Wrong Movement right!!, Same Candies please select another Direction", ConsoleColor.Red);
                    direction = ValidateDirection(index, userInput, candiesLinkedList);
                }
                break;

            case "left":
                if (currentNode.left != null && currentNode.data.Score == currentNode.left.data.Score)
                {
                    string userInput = ReadInput("Wrong Movement left!!, Same Candies please select another Direction", ConsoleColor.Red);
                    direction = ValidateDirection(index, userInput, candiesLinkedList);
                }
                break;

            default:
                break;
            }
            return(direction);
        }
Esempio n. 13
0
        private void MoveCandy(int index, string direction, DLinked candiesLinkedList)
        {
            //2- Get the node having this index
            Node currentNode = GetNodeWithIndex(candiesLinkedList, index);

            Node myUpNode    = currentNode.GetUpNode(currentNode);
            Node myDownNode  = currentNode.GetDownNode(currentNode);
            Node myRightNode = currentNode.right;
            Node myLeftNode  = currentNode.left;
            Node nodeToSwap  = new Node();

            Node currentNodeAfterSwap = new Node();

            switch (direction)
            {
            case "right":
                nodeToSwap = myRightNode;
                MoveCandyRight(currentNode, nodeToSwap, candiesLinkedList, index, direction);
                break;

            case "left":
                nodeToSwap = myLeftNode;
                MoveCandyLeft(currentNode, nodeToSwap, candiesLinkedList, index, direction);
                break;

            case "up":
                nodeToSwap = myUpNode;
                MoveCandyUp(currentNode, nodeToSwap, candiesLinkedList, index, direction);
                break;

            case "down":
                nodeToSwap = myDownNode;
                MoveCandyDown(currentNode, nodeToSwap, candiesLinkedList, index, direction);
                break;

            default:
                break;
            }


            //Console.WriteLine("node to swap after swap " + cu.data.Name);
            Console.WriteLine("Total Score = " + TotalScore);
            Play(candiesLinkedList);

            //Un Match
        }
Esempio n. 14
0
        private void CrushCandyWithTwoLeftCandies(Node currentNodeAfterSwap, DLinked candiesLinkedList)
        {
            Node newCurrentUp = new Node();

            while (newCurrentUp != null)
            {
                //swap current Up
                newCurrentUp = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                if (newCurrentUp == null)
                {
                    currentNodeAfterSwap.data.Name           = ".";
                    currentNodeAfterSwap.left.data.Name      = ".";
                    currentNodeAfterSwap.left.left.data.Name = ".";

                    break;
                }
                else
                {
                    currentNodeAfterSwap = SwapNodes(currentNodeAfterSwap, newCurrentUp);

                    //swap current.right up
                    Node currentNodeAfterSwapLeft = currentNodeAfterSwap.left;
                    Node newCurrentRightDown      = currentNodeAfterSwapLeft.GetDownNode(currentNodeAfterSwapLeft);
                    currentNodeAfterSwapLeft = SwapNodes(currentNodeAfterSwapLeft, newCurrentRightDown);


                    //swap current right right up
                    Node currentNodeAfterSwapLeftLeft = currentNodeAfterSwapLeft.left;
                    Node newCurrentRightRightUp       = currentNodeAfterSwapLeftLeft.GetUpNode(currentNodeAfterSwapLeftLeft);
                    currentNodeAfterSwapLeftLeft = SwapNodes(currentNodeAfterSwapLeftLeft, newCurrentRightRightUp);
                }
            }
            Console.WriteLine("finish ");
            candiesLinkedList.Print();
            Console.WriteLine("Generating New Nodes ...: ");
            //generate Random
            GenerateNewRandom(currentNodeAfterSwap);
            GenerateNewRandom(currentNodeAfterSwap.left);
            GenerateNewRandom(currentNodeAfterSwap.left.left);
            CheckGrid(candiesLinkedList);

            candiesLinkedList.Print();
        }
Esempio n. 15
0
        private void MoveCandyLeft(Node currentNode, Node nodeToSwap, DLinked candiesLinkedList, int index, string direction)
        {
            Node currentNodeAfterSwap = new Node();

            if (MatchTwoUpTwoDown(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 5 * currentNodeAfterSwap.data.Score;
                CrushFiveCandiesVertical(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoLeft(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNodeAfterSwap.data.Score;
                CrushCandyWithTwoLeftCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoUpOneDown(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 4 * currentNodeAfterSwap.data.Score;
                Node currentUp = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                Node startNode = currentUp.GetUpNode(currentUp);
                CrushFourCandiesVertical(startNode, candiesLinkedList);
            }
            else if (MatchTwoDownOneUp(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 4 * currentNodeAfterSwap.data.Score;
                Node startNode = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                CrushFourCandiesVertical(startNode, candiesLinkedList);
            }
            else if (MatchTwoUp(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNodeAfterSwap.data.Score;
                CrushCandyWithTwoUpCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchTwoDown(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                candiesLinkedList.Print();
                TotalScore += 3 * currentNodeAfterSwap.data.Score;
                CrushCandyWithTwoDownCandies(currentNodeAfterSwap, candiesLinkedList);
            }
            else if (MatchOneUpOneDown(currentNode, nodeToSwap))
            {
                currentNodeAfterSwap = SwapNodes(currentNode, nodeToSwap);
                TotalScore          += 3 * currentNodeAfterSwap.data.Score;
                Node currentUp = currentNodeAfterSwap.GetUpNode(currentNodeAfterSwap);
                CrushCandyWithTwoDownCandies(currentUp, candiesLinkedList);
            }

            else
            {
                DisplayUnmatchedDirection(index, direction, candiesLinkedList);
            }
        }