• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Manhattanisgr8

Beginner Programmer Help

10 posts in this topic

I am trying to make a simple program that has the user buy items, sell items, and list their items. Just trying to practice what I have learned thus far. I have made a class to buy items, and a class to list the items. The problem I am encountering is when the "buy" class is done running and has stored the items in a playersInventory array, that data in the heap gets erased before the "list" class can list what the player has bought. Any help with this would be much appreciated, as well as any and all critique on my code.

 

Program Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuySell_Gear
{
    class Program
    {
        static void Main(string[] args)
        {
            MainMenu mainMenu = new MainMenu();

            mainMenu.MenuScreen();
        }
    }
}

MainMenu struct

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuySell_Gear
{
    struct MainMenu
    {
        static int MainMenuMethod()
        {
            Console.Clear();
            Console.WriteLine("Select what you would like to do:");
            Console.WriteLine("1. Buy");
            Console.WriteLine("2. Sell");
            Console.WriteLine("3. List");
            Console.WriteLine("4. Exit");
            return Convert.ToInt32(Console.ReadLine());
        }

        public void MenuScreen()
        {
            int menuSelection = 0;
            while (menuSelection != 4)
            {
                menuSelection = MainMenuMethod();
                Buy buy = new Buy();
                List list = new List();

                if (menuSelection == 1)
                {
                    buy.BuyStuff();
                }
                else if (menuSelection == 2)
                {
                    break;
                }
                else if (menuSelection == 3)
                {
                    list.GenerateList();
                }
                else if (menuSelection == 4)
                {
                    break;
                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("\t\t***Invalid Entry***");
                    Console.ReadLine();
                }
            }
        }
    }
}

Buy class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuySell_Gear
{
    class Buy
    {
        private string[] itemsForSale = new string[] { "M-16", "Grenade", "Flak Jacket", "Kevlar", "K-Bar" };
        private int[] pricesForItems = new int[] { 300, 90, 100, 50, 20 };
        private string[] playersInventory = new string[5];  //Player can only carry 5 items
        private int[] playersInventoryIndex = new int[5];
        private int playersMoney = 500;
        private int playersSelection = 0;
        private int placeItemInIndexOfPlayersInventory = 0;

        //Begin Constructors

        public string[] PlayersInventory
        {
            get { return playersInventory; }
            set { playersInventory = value; }
        }

        public int[] PricesForItems
        {
            get { return pricesForItems; }
        }

        public int[] PlayersInventoryIndex
        {
            get { return playersInventoryIndex; }
            set { playersInventoryIndex = value; }
        }

        public int PlayersMoney
        {
            get { return playersMoney; }
            set { playersMoney = value; }
        }

        private int PlayersSelection
        {
            get { return playersSelection; }
            set { playersSelection = value; }
        }

        //Begin Methods:

        public void BuyStuff()
        {
            while (PlayersSelection != 21)
            {    
                Console.Clear();
                Console.WriteLine("What would you like to buy:(type 22 to exit)");
                int list = 1;
                    for (int index = 0; index < itemsForSale.Length; index++)
                    {
                        Console.WriteLine(list + ". " + itemsForSale[index] + " costs $" + pricesForItems[index]);
                        ++list;
                    }
                playersSelection = Convert.ToInt32(Console.ReadLine()) - 1;

                if (PlayersSelection == 21)
                    {
                        break;
                    }

                else if (PlayersInventory.Length == placeItemInIndexOfPlayersInventory)
                    {
                        Console.Clear();
                        Console.WriteLine("You cannot carry any more objects");
                        Console.ReadLine();
                        break;
                    }

                else if (PlayersMoney < PricesForItems[playersSelection]) 
                    {
                        Console.Clear();
                        Console.WriteLine("\nYou do not have enough money");
                        Console.ReadLine();
                        continue;
                    }
                else
                    {
                        PlayersInventory[placeItemInIndexOfPlayersInventory] = itemsForSale[playersSelection];
                        PlayersInventoryIndex[placeItemInIndexOfPlayersInventory] = PlayersSelection;
                        ++placeItemInIndexOfPlayersInventory;
                        PlayersMoney -= PricesForItems[playersSelection];

                        Console.WriteLine("You just bought a/n " + itemsForSale[playersSelection] + " for $" + pricesForItems[playersSelection]);
                        Console.WriteLine("You have $" + PlayersMoney + " left.");
                        Console.ReadLine();
                    }                
            }
        }
    }
}

List Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuySell_Gear
{
    class List
    {
        public void GenerateList()
        {
            Buy buy = new Buy();
            Console.Clear();
            int list = 1;
            for (int index = 0; index < buy.PlayersInventory.Length; index++)
            {
                if (buy.PlayersInventory[index] == null)
                {
                    Console.WriteLine(list + ". empty");
                    ++list;
                    continue;
                }
                else
                {
                    Console.WriteLine(list + ". " + buy.PlayersInventory[index]);
                    ++list;
                }
            }
            Console.ReadLine();
        }
    }
}

 

2

Share this post


Link to post
Share on other sites

Thank you both for the great advice. Would it be easier to store the players inventory in a multidimensional array in order to store the objects name and price?

1

Share this post


Link to post
Share on other sites

Thank you both for the great advice. Would it be easier to store the players inventory in a multidimensional array in order to store the objects name and price?

 

 

It depends on how you want to represent the items that would be in the inventory, but that sounds a little over-engineered to me. What you are describing would work, but I would prefer either a Dictionary with a string key and an int value or a distinct Item class with a string field for the name and an int field for the price.

I can flesh this out a bit more, but later; my phone limits my post quality :/

 

EDIT: Home now, let me expand a little.

 

I think that creating an Item class would be a good, intuitive, direct approach to take. That way all of the information relevant to the items in your program are grouped together to be conveniently used as you need.

 

If you had something like:

 

 

public class Item
{
string name;
 
int price;

}

 

then you could have a List<Item> to represent inventory and another List<Item> to represent the store. An array of items (Item[]) would also be fine, though the size would be fixed.

 

The player's inventory doesn't seem like it would benefit from having item prices, so unless the price is included as a field in an Item class I wouldn't include it in the inventory at all. If you don't use an Item class, I would have the player's inventory be a List<string> or an array of strings, similar to above. The Buy interface is the only place that really needs the prices, so you could use a Dictionary<string, int> where the string is the item name and the int is the price as a field in the Buy class definition.

Edited by Khaiy
1

Share this post


Link to post
Share on other sites

If I were to use a Dictionary to set the item and price, then store what the user selects into a List, would I be able to reference the item back to the Dictionary if the user wants to sell that item back for what he paid?

0

Share this post


Link to post
Share on other sites

Guesswork here, is it because you're creating a new object each time you generate the list?

 

 public void GenerateList()

{ Buy buy = new Buy(); //this 'buy' object is empty

Console.Clear();

0

Share this post


Link to post
Share on other sites

If I were to use a Dictionary to set the item and price, then store what the user selects into a List, would I be able to reference the item back to the Dictionary if the user wants to sell that item back for what he paid?


I'm imagining the dictionary as a persistent variable that is referenced as needed for buying and selling, and inventory is stored separately. So the Buy interface doesn't add or remove an item from the dictionary but only accesses it to get the price for that item. Inventories, like a store's current stock, would be something different (potentially another dictionary with the item as a key and the number in stock as the value, or any other arrangement you like).

If the item/price dictionary variable is named priceList, the Buy interface would get the price for itemX by accessing priceList[itemX].

When the player buys an item, you deduct int price = priceList[itemX] from their current money supply and add the item to their inventory (player.Inventory.Add(the item), if the inventory is a list). To sell the item back, the player's money supply is credited with priceList[itemX] dollars, and the item is removed from the player's inventory (player.Inventory.Remove(the item)).

Does that help? I'm back on my phone, so again my post quality is taking a hit.

 

EDIT: Home again. Here's a quick sample of what the code might look like if you have a dictionary for storing price information, a list for inventory, a dictionary for store stock, and items represented only as strings:

 

 
// A class defining a player:
 
public partial class Player
{
     // Fields
     public List<string> inventory;
 
     int money;
}
 
// A class defining a store
 
public partial class Store
{
     // Fields
     public Dictionary<string, int> priceList;
 
     public Dictionary<string, int> itemStock;
 
     // Methods
     public void DisplayStock()
     {
          // Code to display the items for sale
          foreach (string s in itemStock.Keys)
          {
               Console.WriteLine("{0} costs ${1}.", s, priceList[s]);
          }          
     }
 
     public void BuyItem(Player p, string itemPurchased)
     {
          if (p.money >= priceList[itemPurchased])
          {
               p.money -= priceList[itemPurchased];
               p.inventory.Add(itemPurchased);
               itemStock[itemPurchased]--;
          }
 
          else
          {
               Console.WriteLine("You don't have enough money.");
          }
     }
 
     public void SellItem(Player p, string itemSold)
     {
          p.inventory.Remove(itemSold);
          p.money += priceList[itemSold];
          itemStock[itemPurchased]++;
     }
}
 
Edited by Khaiy
1

Share this post


Link to post
Share on other sites

Thank you so much for the great and invaluable advice Khaiy. Would you mind if I PMed you about some other questions I have?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0