예제 #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);
 }
예제 #2
0
        public void Insert(Weapon w, int n)
        {
            //inserts item into shop
            ShopNode newItem = new ShopNode(w);

            root = InsertHelper(root, newItem, n);
        }
예제 #3
0
 public ShopNode(Weapon s)
 {
     shopitem = s;
     numStock = 0;
     right    = null;
     left     = null;
 }
예제 #4
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();
                }
            }
        }
예제 #5
0
 public void printHelper(ShopNode s)
 {
     //prints nothing if shop is empty
     if (s == null)
     {
         return;
     }
     printHelper(s.left);
     Console.WriteLine("Name: {0} | Damage: {1} | Cost: {2} | Number in Stock: {3}", s.shopitem.weaponName, s.shopitem.damage, s.shopitem.cost, s.getNumStock());
     printHelper(s.right);
 }
예제 #6
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);
        }
예제 #7
0
 public ShopNode SearchHelper(ShopNode s, string w)
 {
     //if you reached the end of the current branch, item is not there
     if (s == null)
     {
         return(null);
     }
     //if item found
     if (w == s.shopitem.weaponName)
     {
         return(s);
     }
     //if item to the right
     if (string.Compare(w, s.shopitem.weaponName) > 0)
     {
         return(SearchHelper(s.right, w));
     }
     //if item to the left
     else if (string.Compare(w, s.shopitem.weaponName) < 0)
     {
         return(SearchHelper(s.left, w));
     }
     return(null);
 }
예제 #8
0
 public void delete(string w)
 {
     root = deleteHelper(w, root);
 }
예제 #9
0
 public BinaryTreeShop()
 {
     root = null;
 }