Example #1
0
 public ShopNode InsertHelper(ShopNode s, ShopNode newItem, int numStock)
 {
     //if shop is empty
     if (s == null)
     {
         s = newItem;
         s.setNumStock(s.getNumStock() + numStock);
         return(s);
     }
     //if player enters an item into the shop that is already there, increases stock
     if (s.shopitem.weaponName == newItem.shopitem.weaponName)
     {
         s.setNumStock(s.getNumStock() + numStock);
         return(s);
     }
     //if item goes right in tree
     if (string.Compare(newItem.shopitem.weaponName, s.shopitem.weaponName) > 0)
     {
         s.right = InsertHelper(s.right, newItem, numStock);
     }
     //if item goes left in tree
     else if (string.Compare(newItem.shopitem.weaponName, s.shopitem.weaponName) < 0)
     {
         s.left = InsertHelper(s.left, newItem, numStock);
     }
     return(s);
 }
Example #2
0
        static void buyItem(Player p, BinaryTreeShop bt)
        {
            Console.Clear();
            string input = "";

            while (input != "end")
            {
                Console.Clear();
                Console.WriteLine("-----Buy Item from Shop-----");
                bt.printShop();
                p.printBackpack();
                Console.WriteLine("Please enter the name of a weapon to buy (enter 'end' to quit):");
                input = Console.ReadLine();
                //all items stored in lowercase so searches done in lowercase
                input = input.ToLower();
                if (input == "end")
                {
                    break;
                }
                if (bt.Search(input) == null)
                {
                    Console.WriteLine("This item is not in the shop. Press any key to continue...");
                    Console.ReadKey();
                }
                else
                {
                    ShopNode purchasedItem = bt.Search(input);
                    int      numpurchased;
                    Console.WriteLine("Please enter the number of " + input + " you wish to purchase:");
                    input = Console.ReadLine();
                    //check for correct input
                    while (!int.TryParse(input, out numpurchased) || numpurchased < 1)
                    {
                        Console.WriteLine("Please enter a valid number for number of items:");
                        input = Console.ReadLine();
                    }
                    if (purchasedItem.getNumStock() < numpurchased)
                    {
                        Console.WriteLine("There isn't that many of this item in the shop...");
                    }
                    else
                    {
                        if (p.buy(purchasedItem.shopitem, numpurchased))
                        {
                            purchasedItem.setNumStock(purchasedItem.getNumStock() - numpurchased);
                            if (purchasedItem.getNumStock() == 0)
                            {
                                bt.delete(purchasedItem.shopitem.weaponName);
                            }
                        }
                    }
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
            }
        }
Example #3
0
        public ShopNode deleteHelper(string w, ShopNode current)
        {
            if (current == null)
            {
                return(current);
            }
            //if item to the right
            if (string.Compare(w, current.shopitem.weaponName) > 0)
            {
                current.right = deleteHelper(w, current.right);
            }
            //if item to the left
            else if (string.Compare(w, current.shopitem.weaponName) < 0)
            {
                current.left = deleteHelper(w, current.left);
            }
            else if (w == current.shopitem.weaponName)
            {
                if (current.left == null)
                {
                    return(current.right);
                }
                if (current.right == null)
                {
                    return(current.left);
                }

                ShopNode successor = current.right;
                while (successor.left != null)
                {
                    successor = successor.left;
                }
                current.setNumStock(successor.getNumStock());
                current.shopitem = successor.shopitem;
                current.right    = deleteHelper(current.shopitem.weaponName, current.right);
            }
            return(current);
        }