baavgai's Profile User Rating: *****

Reputation: 6578 Overlord
Group:
Mentors
Active Posts:
13,938 (4.47 per day)
Joined:
16-October 07
Profile Views:
147,670
Last Active:
User is offline Today, 05:06 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
775
Expert In:
C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

Latest Visitors

Icon   baavgai has not set their status

Posts I've Made

  1. In Topic: How can I improve this code? Text Based Game

    Posted 1 May 2016

    Step back. Also, review Object Oriented concepts. Your Items should NOT look like:
    public class Items {
        // POTIONS
        private int healthPotions;
        private int healthPotionHealAmount;
        private int healthPotiondropChance;
        
        private int strengthPotions;
        private int strengthPotionAmount;
        private int strengthPotiondropChance;
        
        public Items(int healthpotions, int healthpotionhealamount, int healthpotiondropchance, int strengthpotions, int strengthpotionamount, int strengthpotiondropchance) {
    
    


    Think about what you have there. Two different potions and quantities. A potion seems to make a sensible object. Perhaps something like:
    class Potion {
        private final int amount, dropChance;
        public final String name;
        public Potion(String name, int amount, int dropChance) {
            this.name = name;
            this.amount = amount;
            this.dropChance = dropChance;
        }
        // avoid setters
        // we can because of the constructor
        
        public String getName() { return name; }
        public int getAmount() { return amount; }
        public int getDropChance() { return dropChance; }
    }
    
    


    Now, what might items look like? Perhaps:
    class Items {
        public final List<Potion> potions;
        public Items() {
            this.potions = new java.util.ArrayList<>();
            // if we like, we can put some init code in here
            this.add(BasicHealth, 3);
        }
        // helper methods
        public void add(Potion p) { this.potions.add(p); }
        public void add(Potion p, int count) { while(count-- > 0) { add(p); } }
        
        // a couple of known potions we may use a lot
        public static final Potion BasicHealth = new Potion("Healing", 40, 40);
        public static final Potion BasicStrength = new Potion("Strength", 5, 20);
    }
    
    


    Why three entries of the same thing in our list? Well, as we use them, we can delete them. That's not the only way to do it, you could use a counter. You might also abstract an Item class to include everything your player could carry and pick up. There is no one correct way. Your first goal is really not to confuse yourself.

    In your Character you've lumped in Enemy data with Player data. An Enemy should be a class as well. Perhaps something like:
    interface Enemy {
        String getName();
        int getHealth();
        int getDamage();
        void applyDamage(int damage);
        boolean isAlive();
    }
    
    interface EnemyFactory {
        Enemy getRandomEnemy(int level);
    }
    
    


    Your EnemyFactory could contain your list of all possible baddies.

    Remember there are objects and there are collections on objects. An object represents a distinct entity you can think about, like the player, a weapon, a potion, a monster, etc. A collection would be all the weapons available in your world or all the weapons the player has in their inventory.

    Think about the flow of a game. Your player encounters an enemy. You generate an enemy object. If you kill the enemy, it can have properties like loot or xp. Such properties may even me modified by a given level. Such an encounter would be a logical block of code, so would have its own method or several methods.

    However, your dialog isn't quite a thing, so it may be just a method in your game.

    Hope this helps.
  2. In Topic: How can I improve this code? Text Based Game

    Posted 30 Apr 2016

    Without getting into OO just yet:
    public class MyGame {
        private final Scanner in = new Scanner(System.in);
        private final Random rand = new Random();
    
        // note, this is horrid, but it's a start
        private int playerLevel, health, attackDamage, minAttackDamage;
        private int totalEXP, totalKills;
        private int numHealthPotions, healthPotionHealAmount, healthPotiondropChance;
        private int numStrengthPotions, strengthPotionAmount, strengthPotiondropChance;
        private final String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assasin", "Goblin", "Troll",};
        private int maxEnemyHealth, enemyAttackDamage, minEnemyHealth, minEnemyAttackDamage;
        private boolean gameisrunning = true;
        
        public static void main(String[] args) {
            new MyGame().run();
        }
        
        public void run() {
            beginGame();
            // never COMBAT:
            while (gameisrunning) {
                loadEnemy();
                firstMenu();
                if (gameisrunning) {
                    secondMenu();
                }
            }
        }
        private void loadEnemy() {
            // ENEMY GENERATE       
            System.out.println("--------------------------------------------------");
            int enemyHealth = rand.nextInt(maxEnemyHealth);
            if (enemyHealth < 40) {
                enemyHealth = minEnemyHealth;
            }
            String enemy = enemies[rand.nextInt(enemies.length)];
            System.out.println("\t# " + enemy + " has appeared!");
            System.out.println("--------------------------------------------------");
        }
        
        private void beginGame() {
            gameisrunning = true;
            // PLAYER STATS
            playerLevel = 1;
    
    


    Now, your goal is the figure out which of those vars you need in the root of your game, and which need only exist in certain methods. You then want to figure out code you can reuse, only with different values. You have that with your first menu. What if you had a monster class? What if you had a method that generated a random monster? You could pass a monster to your first menu...

    First, break up any logical block of code into a method. See how far you get.
  3. In Topic: Beginners coding

    Posted 29 Apr 2016

    I like the "refactor my code" game. I tried to put enough comments and stayed close to what you had:
    public static void Main(string[] args) {
        // not here
        // Console.WriteLine("Welcome to Pricer Pro v0.2!");
        // Console.WriteLine("Enter pricing code as \"quit\" when done");
        new Program().Run();
    }
    
    // C# methods are PascalCase, not camelCase
    // more here: https://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx
    private void Run() {
        Console.WriteLine("Welcome to Pricer Pro v0.2!");
        Console.WriteLine("Enter pricing code as \"quit\" when done");
        string pc = ReadString("Enter pricing code: ").ToUpper();
        // if you want user, call upper, don't wait
        // string pc = pcLower.ToUpper();
        float iPrice = ReadFloat("Enter item price: ");
        CalcPrice(iPrice, pc);
        Console.ReadLine();
    }
    
    private void CalcPrice(float iPrice, string pc) {
        if (pc == "H") { // always use curlies 
            // avoid writing the same thing over and over
            // make a function for this
            ShowPrice(iPrice, 0.5F);
        } else if (pc == "F") {
            ShowPrice(iPrice, 0.4F);
        } else if (pc == "T") {
            ShowPrice(iPrice, 0.33F);
        } else if (pc == "Q") {
            ShowPrice(iPrice, 0.25F);
        // never, ever, call Environment.Exit
        // also, this seems pointless 
        } else if (pc == "QUIT") {
            Console.WriteLine("Goodbye.");
            // Environment.Exit(0);
        } else {
            Console.WriteLine("error");
        }
    }
    
    private void ShowPrice(float iPrice, float modifier) {
        // also, note, avoid extra parens
        // and wrap it if you can see the whole thing on the screen
        // Console.WriteLine(("Original Price: $" + iPrice + "   Discount: $" + (iPrice * .5) + "   New price: $" + (iPrice - (iPrice * .5))));
    
        // burn a var if it makes sense
        var discount = iPrice * modifier;
        Console.WriteLine(
            "Original Price: $" + iPrice 
            + "   Discount: $" + discount 
            + "   New price: $" + (iPrice - discount));
    }
    
    
    private string ReadString(string v) {
        Console.Write(v);
        // avoid unneeded vars
        // string line = Console.ReadLine();
        // return (line);
        return Console.ReadLine();
    }
    
    private float ReadFloat(string v) {
        // leverage prior functions
        // Console.Write(v);
        // string line = Console.ReadLine();
        // return float.Parse(line);
        return float.Parse(ReadString(v));
    }
    
    


    Changes I would make: use a loop so your quit makes sense. Check for quit first, then check if pay code is valid. If paycode isn't valid, don't bother asking for price. This might be a little advanced, but I'd use a nullable float. e.g.
    bool done = false;
    while (!done) {
        string priceCode = ReadString("Enter pricing code: ").ToUpper();
        done = priceCode == "QUIT";
        if (!done) {
            float? modifier = GetDiscountPct(priceCode);
            if (modifier.HasValue) {
                // your code here
            } else {
                Console.WriteLine("Invalid price code, please try again");
            }
        }
    }
    // ...
    
    private float? GetDiscountPct(string pc) {
        if (pc == "H") { return 0.5F; }
        if (pc == "F") { return 0.4F; }
        if (pc == "T") { return 0.33F; }
        if (pc == "Q") { return 0.25F; }
        return null;
    }
    
    


    Hope this helps.
  4. In Topic: Recursion questions, passing values

    Posted 26 Apr 2016

    Heh. To be fair, that's a very C way of doing things. It's generally not seen as great practice to mutate your loop in your loop condition, but C programmers often like to live dangerously. It's also a super slight optimization, as you needn't allocate extra memory for another loop var. Again, not something you should have to worry about, but C programmers are strange like that.

    In a larger sense, you should consider the impact of coding as you go on. How many variables, checks, loops, lines. While always favoring clarity, think about the work the computer is doing if it can achieve the same result with less.
  5. In Topic: Recursion questions, passing values

    Posted 26 Apr 2016

    This is what I had in mind. Note, without setw which just seems extra.

    Spoiler

My Information

Member Title:
Dreaming Coder
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Jersey, be afraid.
Years Programming:
25
Programming Languages:
Most.

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://

Comments

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  1. Photo

    Poorstudent324 Icon

    10 Mar 2016 - 09:06
    Hello,
    I need help with a Ceaser Cipher code that I've been working on for a class. I have tried to get help from other older students but nobody really has the commitment to help me. I saw a post where you helped someone with their code and also noticed how well known and credible you are here so I'm hoping that you can help me.
    I'm new to this website; should I start a f...
  2. Photo

    cpickard2790 Icon

    23 Jan 2016 - 15:04
    I can't say thank you enough for the help you have given me so far! It is appreciated more than you know!
  3. Photo

    vivek_91 Icon

    14 Sep 2015 - 04:26
    Hi,
    First time, I am using white automation framework.
    Can you please let me know how to use ItemType property if Automation Id is not given using White?
    and how to draw a line in writing type application using white??
  4. Photo

    ImChicago Icon

    17 Mar 2015 - 17:34
    Hey, im new to coding, how do you recommend starting? What should I master first? Should I use a website to help me?
  5. Photo

    8byte Icon

    10 Jan 2014 - 11:08
    Awesome, thanks for all the help!
  6. Photo

    codiddle Icon

    07 Mar 2013 - 18:59
    GENIUS GUY HERE MAN. Write a book!
  7. Photo

    bilbil0629 Icon

    24 Oct 2012 - 05:39
    hey! you mongolian?
  8. Photo

    Ticon Icon

    20 Aug 2012 - 13:23
    I just noticed your an expert in cheese. I lol'd
  9. Photo

    mostyfriedman Icon

    18 Aug 2012 - 23:13
    Hello there Brett
  10. Photo

    MehrdadSecret Icon

    26 Jul 2012 - 05:51
    hello, can i have a contact email or Yahoo or something? Need ur help badly
  11. Photo

    Jeet.in Icon

    18 Apr 2012 - 23:17
    Thanks ! Solved My problem !
    http://www.dreamincode.net/forums/topic/275578-simple-whitespace-remover/page__gopid__1603556&#entry1603556
  12. Photo

    RetardedGenius Icon

    25 Oct 2011 - 14:29
    I agree so much with your opinion of incompetent CS teachers. So true!
    http://www.dreamincode.net/forums/topic/252848-venting-a-frustration/page__view__findpost__p__1469659
  13. Photo

    WabiSabi Icon

    21 Oct 2011 - 11:00
    I agree with assert(C). However, I'd rather not specify what you are "the best" at.
  14. Photo

    assert(C) Icon

    20 Oct 2011 - 08:38
    You are the best
  15. Photo

    macosxnerd101 Icon

    30 Sep 2011 - 09:33
    Congrats on hitting the big 3k!
  • (3 Pages)
  • +
  • 1
  • 2
  • 3