Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 630 631 [632] 633 634 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887442 times)

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9465 on: May 05, 2016, 05:36:15 pm »

So I have a bug in this program for my college assignment. Program takes input/output from a binary file of struct data, and uses a menu interface to allow the user to add to the file, make changes to the file, or display either one specific struct or all of the structs in the file.
Not sure if this will fix the exact issue that you are facing, but you should change the displayRecords loop to be just a plain while() loop instead of a do/while() one; as is it will freak out if you try to just display an empty file as it tries to read data that doesn't actually exist.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9466 on: May 05, 2016, 06:15:38 pm »

Not sure if this will fix the exact issue that you are facing, but you should change the displayRecords loop to be just a plain while() loop instead of a do/while() one; as is it will freak out if you try to just display an empty file as it tries to read data that doesn't actually exist.
That was initially a while loop, actually. I don't remember why I changed it. Doesn't seem to make any difference in regards to the bug I'm trying to fix, so I'll change it back. Thanks.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9467 on: May 05, 2016, 08:03:24 pm »

Distributing a Python program means (unless your audience can safely be assumed to have Python, ie Linux) distributing Python, and there aren't a lot of tools that make that very easy unless you were embedding Python in C or Java or something. And at that point, why not just write your game/app in those languages instead?

I most commonly see Python used as an embedded scripting language, kind of like Lua, so that people can add their own functionality to programs like Blender.
I use pyinstaller. It plonks everything into an executable than can be run wherever. There's also py2exe and cxfreeze.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

monkey

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9468 on: May 05, 2016, 08:04:28 pm »

Make sure the read()s were successful. stream::gcount() tells you how many bytes were read.
Logged

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9469 on: May 05, 2016, 08:08:25 pm »

I would point out Unity (using C#), or maybe Haxe (which I haven't learned but it looks like it might be good).
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9470 on: May 06, 2016, 06:54:05 am »

<snip>

- in checkSize, rather than increasing accumulator by sizeof(dummy), then dividing by sizeof(dummy), just write accumulator++, and return accumulator. Same effect, less code. But you'll need to apply a fix to checkSize, see below for the explanation.

- the first thing I checked was printing from an empty file. It produced 1 record of garbage. You should always check assumptions first, before adding any data. This made it clear that "prints a junk record" was not actually related to storing a new record. Fixing the "empty file prints garbage" bug might fix the other garbage-printing incidents.

- the cause is partly the use of do ... while. "do" always executes once, even if it should fail. That is because the check is always applied at the end of the loop. So it's a bad choice for things which could happen zero times. e.g. only use do while for things which must happen at least once. Use normal while for things which might happen no times depending on their condition.

- The main cause is because eof only gets set to true once a read fails. This is no problem for text files if you readline, then the last line triggers eof. But for binary files, you need to read the end of file markers for your read function to alert the system that the file is depleted. So if you have an exact number of records in a binary file, then the last record can be read without triggering the "end of file" detection.

- the fix is that file.read returns a bool depending on whether it could successfully read any data or not. Knowing that, you can just make a while loop where the condition is your file.read function itself, instead of checking for eof at all. Get this working in void displayRecords(const char filename[]) and you can roll the same fix out in all the other functions which read records.

- btw, use do / while sparingly. It only makes sense for things where you have to run the loop once before checking what happened, e.g. for generating random x,y coordinates until they satisfy some condition.
« Last Edit: May 06, 2016, 07:25:17 am by Reelya »
Logged

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9471 on: May 06, 2016, 10:01:40 am »

-in depth answer snip-

Thaaaaaaank yoooooooooooou. This bug was really frustrating since I just couldn't see what I was doing wrong. Didn't think of using read() as a bool condition.

Also thanks to your explanation of eof and binary files, I managed to fix another troublesome bug in the changeRecords() function I added since my initial post last night.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9472 on: May 06, 2016, 10:15:22 pm »

Spoiler (click to show/hide)

Spoiler (click to show/hide)

Trying to get my code in main to call my other method in a different class to send it something. Concepts are their I just can't find the damn synthax. My main refuses to call the other function when it hits those if statements. I know this is synthax and i'm looking through my book to find the issue but haven't been able to.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9473 on: May 06, 2016, 10:24:05 pm »

Quick tip, you can simplify the main program by putting the read at the top of the loop. That means less code to write and just looks less amateur (plus it's faster to change the message when it only appears once). There's basically never any reason to have the instructions and input twice in the program:

Spoiler (click to show/hide)

Here is a list of the problems:

"DiceRollerTWO();" is part of the other class, but you're calling it as if it's a local function. You need to preface the call with info about which class it's in. you call functions in a class in the manner of "class.function()" because another class might have a function with the same name.

Also, if you call "DiceRoller.DiceRollerTWO()" then that will only work if DiceRollerTWO is a static function. You can put static in front of the function definition and then "DiceRoller.DiceRollerTWO()" would work.

If you'd can't or don't want to make the function static, then you need to declare a DiceRoller object. This will let you call its non-static functions. e.g.:

DiceRoller myDice;
int roll = myDice.DiceRollerTWO();"

will work without changing the DiceRoller class.
« Last Edit: May 06, 2016, 10:32:47 pm by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9474 on: May 06, 2016, 10:31:02 pm »

You know that because you're using do-while, there's no need to repeat the input read or the message? Do it like this, it's way more pro and easier to modify.

Spoiler (click to show/hide)


Good tip. Thank you!


And bro, this is the exact problem you had before with Java. More than one person explained the issue, didn't you read anything we wrote? Normally I try and be patient but when repeating the same explanation of the same issue to the same person, in print my patience wears thin.

You didn't make a DiceRoller object so you can't call a function on that object. You can only call a function on an object that doesn't exist, if the function is static. Which is the exact same conversation we had about the exact same problem with your virtually identical program from before.

No excuse I just didn't get it. It's one of those things where I just can't get in touch with again..

*sigh*

So steps thus far..

1. Make a DiceRoller Object
2. Call method
3. work well?

Apologies again I just can't for the life of me find this in my book right now.



EDIT:

Like this? I attempted to make the object within the if statements. Also I haven't put in Reelya, your fix on formatting. Not yet.

Spoiler (click to show/hide)

EDIT 2:

Okay I think I got it right. Objects seem to be made and are being called now i'm going to work on the actual dice roller methods. Also, it may be because I first worked on C++ but I almost always say 'function'. If that happens, sorry.

Spoiler (click to show/hide)
« Last Edit: May 06, 2016, 10:50:41 pm by 3man75 »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9475 on: May 06, 2016, 10:44:51 pm »

Well if the functions never change then you can make them static and just call them without creating an object. In that case you do "classname.function()" as the call. If the function always does the same thing for every possible dice object, then it might as well be static.

The real benefit of non-static functions is that they have access to the individual data of the object. So you need a stored value for the function to access, for there to be any point to creating an actual object.

Here is a simple dice class in C++. It allows any-sided dice and supports multiple-dice rolls. See if you can get how this works and port that into Java.

Code: [Select]
class Dice
{
    int sides; // this stores the number of sides of the dice we created

    Dice(int _sides) // this is the constructor - the function that makes a dice object
    {
        sides = _sides;

        if(sides < 1) sides = 1;
        // zero sides would cause a crash when the % operation happens (divide by zero)
        // also, dice with less than zero sides don't really make sense
    }

    int Roll() // this rolls the dice and returns a random number
    {
        return rand()%sides+1; // random number from 1 to "sides"
    }

    int Roll(int num) // this rolls multiple dice, then adds them together.
    {
        int total = 0;
        for(int i = 0; i < num; i++)
        {
             total += Roll();
        }

        return total;
    }
};

// make some dice, and roll them

Dice d6(6); // make a 6-sided dice called "d6" (this name is arbitrary)
Dice d10(10); // make a 10-sided dice called "d10" (this name is arbitrary)

int strength = d6.Roll(3);
int age = d10.Roll(8);

But you could do the whole thing as static too. A function called "RollDice" could take two inputs: number of sides and number of dice, then you could do the whole thing without any objects.
« Last Edit: May 06, 2016, 10:54:19 pm by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9476 on: May 06, 2016, 10:57:07 pm »

"The real benefit of non-static functions is that they have access to the individual data of the object. So you need a stored value for the function to access, for there to be any point to creating an actual object."

Can you explain this further?

 1. What does individual data mean?
 2. What stored value would I be using?

Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9477 on: May 06, 2016, 11:24:47 pm »

Say you have a "Person" class. This Person class has a LastName variable. What is meant by "individual data" is that each instance of a Person class (each object) can have a different LastName. For formatting purposes, you want to know how many letters are in the last name. If you have an individual with the name "doe" and a GetLettersInLastName() method, you don't want GetLettersInLastName() to be static because its result depends on the object's data.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9478 on: May 07, 2016, 09:45:17 am »

Another example, say your person object had a "height" value. Then you would have a non-static class function called "GetHeight". It would return the height of that particular person you called it on:

(this is just pseudocode)
Person Bob;
Bob.height = 1.8; // set Bob's height, which is non-static
Print Bob.GetHeight(); // this works
Print Person.GetHeight(); // this is an error

But if you had an "AverageHeight" field which was calculated periodically for all the people, it would make sense for all that to be static, since AverageHeight exists for a group of people, not an individual person:

Person.CalculateAverageHeight(); // a static function
Print Person.averageHeight; // accessing a static variable which holds the average height of all "Person"s

So, when the class appears on the left it means for "All the Persons", and when a specific person appears, it means for "Just one Person". That's the difference between static and non-static.

So, static and non-static is about the individual vs the group. e.g. a Car has a color. So "Color" is a non-static variable for your car. But "All the Cars" don't have a single "Color". Though you could calculate a static variable called "MostCommonColor" for the Cars.
« Last Edit: May 07, 2016, 09:49:34 am by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9479 on: May 07, 2016, 01:05:57 pm »

Say you have a "Person" class. This Person class has a LastName variable. What is meant by "individual data" is that each instance of a Person class (each object) can have a different LastName. For formatting purposes, you want to know how many letters are in the last name. If you have an individual with the name "doe" and a GetLettersInLastName() method, you don't want GetLettersInLastName() to be static because its result depends on the object's data.

Another example, say your person object had a "height" value. Then you would have a non-static class function called "GetHeight". It would return the height of that particular person you called it on:

(this is just pseudocode)
Person Bob;
Bob.height = 1.8; // set Bob's height, which is non-static
Print Bob.GetHeight(); // this works
Print Person.GetHeight(); // this is an error

But if you had an "AverageHeight" field which was calculated periodically for all the people, it would make sense for all that to be static, since AverageHeight exists for a group of people, not an individual person:

Person.CalculateAverageHeight(); // a static function
Print Person.averageHeight; // accessing a static variable which holds the average height of all "Person"s

So, when the class appears on the left it means for "All the Persons", and when a specific person appears, it means for "Just one Person". That's the difference between static and non-static.

So, static and non-static is about the individual vs the group. e.g. a Car has a color. So "Color" is a non-static variable for your car. But "All the Cars" don't have a single "Color". Though you could calculate a static variable called "MostCommonColor" for the Cars.

AHA!

So I made the class DiceRoller and now I have to add sides to it. So to write this out in Java (Again i'm off on synthax)

private DiceRoller sides {
//code here?
}

Then get the getters to access the above? I remember doing this in a different project (My bank project for class). I'm going to go look for that and my book right but now but first I am understanding this right?

Basically the 'Sides' that I have above are to create a trait for the blueprint of DiceRoller object and then create/instantiate that object? If so, do I want to put that code where I create actual object within the different class or on main? Gut feeling says in the different class but I wanted to ask.

Also wow, I'm able to understand so much better now that it's not 12 AM. Dully noted: Don't code at night if you don't have to.

EDIT: The above isn't me trying to be funny or anything. Just that I suck at writing code at night.
« Last Edit: May 07, 2016, 01:42:31 pm by 3man75 »
Logged
Pages: 1 ... 630 631 [632] 633 634 ... 796