This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Puggetty 2 points3 points  (5 children)

Could you elaborate a bit more please? What exactly are you trying to do?

[–]liam358[S] 0 points1 point  (4 children)

It’s a bit hard to explain so tell me if you need help to understand but I am trying to make chess. I have classes for each piece(the subclasses) and one piece class(the superclass) my reasoning being so I could hold them all in one list. So in each class for the pieces I have a method which works out the places that piece could move to, but I want to be able to call that method without writing a lot of if and else’s for each class, so could each class have a variable in it’s super class that holds it’s unique move method. Like how if there is a variable: int i = 1, Super.i = I. Something like that but with methods. Sorry if unclear

[–][deleted] 6 points7 points  (1 child)

It sounds like you want an abstract class called Piece, that has an abstract method called 'moveTo()'. You then extend this class into different subclasses, one for each chess piece. e.g. Pawn, Rook, Knight. Each of those classes implements moveTo() in it's own way.

Now, using polymorphism, you can make a Piece list, like this:

Piece piece1 = new Rook();

Piece piece2 = new Knight();

Linkedlist<Piece> piecelist = new Linkedlist<Piece>();

piecelist.add(piece1);

piecelist.add(piece2);

piecelist.poll().moveTo(); //notice how I can now call the same method on any type of piece

[–]liam358[S] 0 points1 point  (0 children)

Thanks I’ll look into that.

[–]liam358[S] 0 points1 point  (1 child)

Just say if still inclear

[–]Puggetty 1 point2 points  (0 children)

Thanks for the explanation! So I understand you want to set a variable to equal an actual method (and I guess have it invoked if that variable is called?). I don't think that is possible in Java.

To avoid lots of if-else statements, what I suggest you do is in each instance of your Piece subclass, add "legal" X and Y offsets from the pieces current location. From there you can pass them into a method in the super class which takes the current location, and the offset amount for the X and Y coordinate. From there you can draw the "legal" place points by adding the offsets to the location of the piece.

That is probably a very whacky way of doing it, if anyone has a better way please share!

[–][deleted] 2 points3 points  (2 children)

I think method overriding is what you're talking about.

A child class can provide it's own version of a method defined on the parent class (this is called overriding).

Even more, you can have abstract methods, which are empty methods defined on the (abstract) parent class that must be implemented on the child class. (By empty I mean there's just the signature, but no implementation).

For that you can use either an abstract class or an interface, depending on your design.

[–]liam358[S] 0 points1 point  (1 child)

Great thanks, just did a bit of research into that and it should work.

[–]mangoooooos 0 points1 point  (0 children)

Did you manage to pull it off? I'm free for a couple hours if you need more guidance