Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 93 94 [95] 96 97 ... 796

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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1410 on: February 10, 2012, 09:59:41 pm »

How could any version of Java not support implicit casting to a super type? I mean what the fuck? That is more a glitch than lack of functionality.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1411 on: February 10, 2012, 10:04:47 pm »

How could any version of Java not support implicit casting to a super type? I mean what the fuck? That is more a glitch than lack of functionality.

You're absolutely right.  If everything is defined properly, then it's a glitch in the IDE or JVM.  Unfortunately, fixing those isn't necessarily an option when it isn't your network.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1412 on: February 11, 2012, 08:27:39 pm »

How could any version of Java not support implicit casting to a super type? I mean what the fuck? That is more a glitch than lack of functionality.

You're absolutely right.  If everything is defined properly, then it's a glitch in the IDE or JVM.  Unfortunately, fixing those isn't necessarily an option when it isn't your network.

It is not a glitch. Annoying to deal with and kind of confusing perhaps-

but not a glitch!

Searching out the correct function call, does not work with subtypes!

obj a
 orglist(list){return list;}


arraylist x = new arraylist;
orglist(x); // this will not be able to compile. You need a cast!

orglist((list)x);//this will work.

To know which function is chosen, the type of object or variable returned must be exactly the same as is stated in the function.

This is so compilation can function properly. (yes, it is necessary, obtuse, obnoxious, but still necessary.)
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1413 on: February 11, 2012, 08:29:52 pm »

Erm, the whole point of polymorphism is that derived classes can substitute for base classes. You can't exactly consider any language that doesn't allow this to be object oriented.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1414 on: February 11, 2012, 09:41:00 pm »

I'm pretty sure eerr's incorrect.  Only "pretty sure", because I'm not entirely certain what he's saying...  Regardless, Java will accept any derived class in place of it's superclass.

This is valid, btw...
Code: [Select]
// B is a subclass of A
// This compiles fine
List<A> aList = new ArrayList<>();
aList.add(new A());
aList.add(new B());

Unless eerr's referring to a List of Lists...
Code: [Select]
List<List<A>> lists = new ArrayList<>();
lists.add(new ArrayList<A>());
lists.get(0).add(new A());
lists.get(0).add(new B());
In which case, yes, you do have to tell it the type for the internal list...but nothing else about this makes it crash...

As far as function calls, yes that works.  Java checks the actual class of the input object, checks it for functions and if it can't find it, supers up the chain until it finds the function.
Code: [Select]
// A has method func()
// B doesn't
A a = new A();
B b = new B();
// Calls A.func()
a.func();
// Also calls A.func();
b.func();

Or if he's referring to parameter types...
Code: [Select]
// C has functions with the signatures
// public static void func(A a);
// public static void func(B b);

C.func(a); // Outputs func(A)
C.func(b); // Outputs func(B)
C.func((A)b); // Outputs func(A)

eerr, you'll have to clarify what you're saying, because I can't of anything in Java that lines up with what you're saying...thought of one more thing...
Code: [Select]
// A is a class
// B is a subclass of A
// D is a subclass of B
// C has functions with the signatures
// public static void func(A a)
// public static void func(B b)
D d = new D();

C.func(d); // outputs func(B)
Code: [Select]
// A is a class
// B is a subclass of A
// D is a subclass of B
// C has functions with the signatures
// public static void func(A a)
D d = new D();

C.func(d); // outputs func(A)

As long as the object is a subclass of the thing in the signature by some chain, it will be considered valid...
Logged

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1415 on: February 12, 2012, 12:01:01 am »

The constraint system and objects and so on start making sense eventually, right?  I just read two weeks ahead or so in my textbook, and lordy am I confused.

I'm much happier when we're working with functions =/
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Fayrik

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1416 on: February 12, 2012, 12:05:24 am »

Goddammit, why does converting the output of math with Doubles into Integers produce totally insane results?  All I want to do is multiply a percentage (less-than-1 double) against another double, then turn the result into an int.  Somehow, this makes [ (4.6 * 70.0) * 0.3378 ] = 9.  What the fuck, C#?

Of course, after posting, I can see where some of the problem is my own mistake.  It's actually passing bad values, but the math is still wrong wrong wrong.  Namely, [ 10 * 0.42 ] = 8 for instance.  Weird.
Bit of an old problem, and I'm not sure this is going to help with what you've come across or not, but I'd always run a Math.Floor(), Math.Round() or Math.Ceiling() before moving a decimal-able number to an integer.

@Fayrik:
Depending on how small your game is, you may well end up needing to use such graphics libraries. Texture copying to a buffer is extremely slow. For one, you are running it on the CPU; two, the GPU hardware is specially optimized for rendering graphics to the screen. Pretty much all 2D games these days go through roughly the same graphics pipeline in hardware as a 3D game would, though the level of encapsulation of that functionality by libraries varies. It is not only more efficient, but makes some otherwise nearly impossible tasks possible. In a bitmap copy based system, you are pretty much screwed if you decide you want to rotate a texture; for a proper rendering system, it is calculated automatically with pretty much 0 additional cost. You may consider 40 fps to be good, but for a 2D game, especially if you don't have all your graphical content in yet, it's getting really low. Depending on how much more you need to add, you may be forced to use a graphics library to get an acceptable framerate.
As I said previously, I'm totally aware that I'm getting 40fps when my computer could achieve 200fps. The post also mentions that the cap of 4fps actually appears to work unnoticably.
Indeed, I'm running into constraining problems constantly. Like now I wish to colourize certain tiles, which is going to add a lot of CPU or RAM load depending how I do it.

The reason I feel this is favourable, however, is that using this method, I have so far been able to make the entire program without any external libraries.
And frankly, if any of these 3d libraries can pull off the same stuff within the same, or simpler methods, I'd love to see it.
Spoiler (click to show/hide)
Logged
So THIS is how migrations start.
"Hey, dude, there's this crazy bastard digging in the ground for stuff. Let's go watch."

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1417 on: February 12, 2012, 12:07:50 am »

Ha, I was productive today.  I wrote myself a complete schedule of what I was going to do today in hopes that it would prevent me from slacking off all day like I do everyday.  And it worked!

Made a ton of progress rewriting some infrastructure on my little ascii game project and set up a front menu screen. 
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1418 on: February 12, 2012, 12:26:38 am »

The constraint system and objects and so on start making sense eventually, right?  I just read two weeks ahead or so in my textbook, and lordy am I confused.

I'm much happier when we're working with functions =/
Yes, it will become a lot clearer in the future.
To the point where you start thinking in objects, and applying the same logic to a lot of the real world, it can get annoying sometimes, but the point is that it will become second nature given enough effort.

You might have been happier with functions to start with, but there will come a time when you ask yourself how you ever did anything without.

If you want, I could give a Python lesson on objects, what they are, how they work, and how to use them.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1419 on: February 12, 2012, 12:52:01 am »

To me, it just looks like ways of imposing some additional structure, so I don't really have a problem with that.

It's just that they do a terrible job of explaining how it works, I can't speak computer scientist quite the same way I can speak mathematician, and I pretty much don't learn anything by reading around in the text... I wish they'd do in-line exercises in the textbook, the way my department does.  It's a lot easier to "learn along" that way.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1420 on: February 12, 2012, 01:03:09 am »

Some can be a little bit of an understatement. When you start really using objects, your program can be seen as less of a process that does something, and more of a model that can be used to do things. Although that comes a bit later, after you get the basics down.

Hold on, I'll get an example up in a second, I'm half decent at translating computer scientist into English.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1421 on: February 12, 2012, 01:32:49 am »

Urgh, okay... let me ask something really quick.

If I map some function over a string and I want the altered string back, is there any easy way to do this?  I don't really feel like reading through any more docs tonight =/  I've been basically going with str(map(f, "string a ling")), but that only returns the string of the pointer to the map function, not the result I want.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1422 on: February 12, 2012, 01:37:35 am »

Ok! Object oriented programming, the art of building black boxes.
Let's say we wanted to make a game, something small and simple. Let's say the goal of this game is to guess a random number, and each player has their own number to try and guess. We also want to keep track of the name of each player so we know who is guessing, so let's list some of the requirements.
- We have players
- Each player has a name
- Each player has a hidden number
- Each player can make a guess at their number.

Now the second thing on that list means we are going to need strings, and the third means integers, somewhere in the program. The fourth thing looks like a function, but what about the first? It is more like a thing to contain the other three items in out list. It is going to be an object!
We can use objects to group different values together, and define methods to act on these values. How about we test this out with a small prototype object.

Spoiler (click to show/hide)

Well... Let's take a step back and look at what we did. Firstly we started with
Code: [Select]
class Player:Just like you define functions with def (name), we define a class with class (name). We could have used any name we want, but it is important to always keep class names descriptive.
Then we move onto
Code: [Select]
def  __init__ (self):Well you should be able to tell from the 'def' keyword that we are defining a function, but '__init__' (Those are double _ on either side, by the way) means we are making a very special function. This function will be called when ever we make a new object, and it is called the constructor. Think of it as the function that is used to build the new object. The really weird bit here is use of another keyword, 'self'. Don't worry too much about this, just accept for now that it is black magic, and all methods defined in a class need this. This isn't exactly true, but I can explain where you do and don't need it later on, and for now you will always need it.

Next up,
Code: [Select]
self.name = raw_input('Please enter player name: ')Hopefully you will have seen the second half of that before, it just gets some input from who ever is using the computer, but the first half is interesting. There is that 'self' keyword again. In this case, because we are building an object, we are saying that this objects that we are building in this method (A.K.A self) has a value called 'name', and giving it a value. We will be able to use name later.

And that is it for making our class, but not for using objects. I should explain one from the other...
A class is a blueprint, or the abstract ideal of something. An object is a real thing that you can see and touch and hold. For example, in real life, 'building' is a class, but the building you are sitting in right now is an object. Building is sort of this abstract term that can be used to describe a lot of real things, to help us understand those things, while your house is a real object that can be interacted with. It is very much the same in programming. Player is just a concept, but we don't have any players yet, nor do we have any integers or strings or anything else, so let's make one!

Code: [Select]
a =  Player()When I called Player(), it made a new object and called the __init__ method. 'a' is now an object, that we can see and touch and use! So let's use it!
Code: [Select]
print a.nameRemember how we told it what it's name was? Well it still knows, and we can use that as any other variable. Here we are just having it print it's name.

Continues in part 2.

PREPOST EDIT:
Why yes! I do believe there is such a way using return types. For example

Code: [Select]
def ModString(s):
    s += " Dawg"
    return s

myString = "Hello"
print myString
myString = ModString(myString)
print myString

Need further help with this? If you have never used return types before I might need to give a better explanation.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1423 on: February 12, 2012, 01:43:26 am »

I will read all of that in a minute, because right now I am waiting for the caffeine to kick in.

I understand that last bit of code perfectly fine, but I have a specific problem with the map function where I want it to modify a string.  What it returns seems to be a function or something, which would be fine but I need another string to come back, and that's just not happening.

Worst comes to worst I'll just convert it into a list and screw around there, but I'd really rather not do that.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1424 on: February 12, 2012, 01:46:20 am »

Strange...
Would you care to post the code sample for what you are trying to do? You might be missing a pair of ()
Pages: 1 ... 93 94 [95] 96 97 ... 796