Need help with a Java Assignment regarding enums and arrays
I have an assignment to make a game of breakthrough by creating a class that links provided classes together and makes the game playable.
The enum class is provided below.
I have just started, but I'm having trouble with some basics. First off, there is an enum class which I need to relate to but don't know how,
as I need to be able to make a method "public Piece getTurn()" in my class which relates to this enum class. The "getTurn()" method needs to be able
to give the player their turn (return Piece.WHITE or Piece.Black, white player starts or black player starts respectively). The rules are white
starts, and then you take turn from there.
I also need to write another method "startGame()" which generates the board via a 2d array with either WHITE, BLACK, or NONE spaces (as shown in the code pasted).
Its a 4x6 board like this : (B = Black piece, W = White piece, N = empty space)
W W W W
W W W W
N N N N
N N N N
B B B B
B B B B
How do I do this and generate the array so that other methods may do checks on it too? I managed to make an array that does this board but its created completely
within the method so other methods can't access the same array again.
Please help:(
Code :
package com1003.breakthrough;
public enum Piece {
NONE,
WHITE,
BLACK;
// This method returns the direction of travel of the piece in terms of increasing or
// decreasing row numbers.
// A white piece moves 'down' the board, e.g. from (0, 1) to (0, 2), thus its direction is 1,
// whereas a black piece moves 'up' the board, e.g. from (0, 4) to (0, 3), thus its direction is -1.
public int getDirection() {
if (this == WHITE) {
return 1;
} else if (this == BLACK) {
return -1;
} else {
return 0;
}
}
public Piece getOpponent() {
if (this == WHITE) {
return BLACK;
} else if (this == BLACK) {
return WHITE;
} else {
return NONE;
}
}
}
Re: Need help with a Java Assignment regarding enums and arrays
How come nobody can help, I thought this would be easy for some of the advanced programmers, as this is only an assignment mid-way through a first year course..
Re: Need help with a Java Assignment regarding enums and arrays
Slice your original question up and only leave a short, fruitful and direct question (not plural) and any errors you get.
I need this and that to happen isn't the best way to structure a question.
Something to the point attracts more helpers.
Re: Need help with a Java Assignment regarding enums and arrays
Quote:
Originally Posted by
Kranti1992
How come nobody can help, I thought this would be easy for some of the advanced programmers, as this is only an assignment mid-way through a first year course..
Please keep in mind that a couple hours is not a very long wait for free help, especially on a weekend. We're doing this in our spare time, for free, so some patience would be appreciated. Also, making multiple posts and bumping your own threads will actually decrease your chances of getting help. The best way to get help is to follow the directions in the link in my signature on asking questions the smart way, provide an SSCCE, and ask a specific technical question. It's pretty hard to answer "how do I do this" type questions other than to point you to the basic tutorials and google.
The OP did create another post, although it still contains "how do I do this" type questions: http://www.javaprogrammingforums.com...-question.html
I'm locking this thread now to avoid duplicating answers, so please continue discussion in the other thread.