Object programming for beginner

Hi everyone,

I’am quite new to this website, and, well, to object programming too !
I’ve decided to learn OOP with Java, and to practice Java here.
However, I do not really understand if it is possible to do OOP here with Java, and how to do it.

For example, in the current challenge Ghost in the cell, I have this:

class Player{

public void main{
blablabla

game loop while()

}

So, if I want to build objects, I need a new class defining my objects right ? But it occurs to me i can not do that in the same file, and I don’t know how to do it otherwise ? (maybe define a package and import ?? is it even possible ?)

Or, I must define my objects in the same class Player ? Meaning that my objects must be called Player ? Can I rename the class ? Anyway, if this work, I should then define my class methods et other methods before the main method and then use them in the game loop ?

Final question: given the relative simplicity of the data structure involved in the puzzles here, is it even useful to use objects ? Or should I just stick to classical variables and subroutines ?

Thanks a lot for your answers !

In Java you can create a class in a class (so in the same file), it is called an inner class.

class Player {

    public static class MyClass() { ... }

    public static void main(String... args) {
        MyClass test = new MyClass();
    }

}
4 Likes

Ok great ! I followed a tutorial to learn Java and it did not mention that.

Thanks a lot !

Magus already answered for the possiblity to do it, so I’ll just answer for this part: yes it’s very useful.
Objects such as Factory, Troop, GameState will allow you to be much more productive when designing your IA, your game simulation (if needed), etc…
You can take a look at the past contests reports, sometimes there are some class diagrams included.

Note: it would be way better for code organization and tracking to be allowed to have multiple files to upload (typically one per class), but we have to stick to one file for the moment. You can still write a “merger” if you want, but it also has some drawbacks.

1 Like

Thanks for your answer ! Very useful !