Help with assignment about a board game (enum and array question)
I posted another question about this a couple hours ago, and though it had 30 views it had no replies so I'm attempting to explain it better this time as my english isn't good and might have rushed the previous question.
My Assignment: To make a single class that extends another already written and provided class of which links to other provided classes. This class must make the program work. The program is a boardgame called Breakthrough, but is a 4x6 squares version. Its very simple to play, 8 pieces aside, one side is whites one side is blacks, and you can move forward or diagonally one step at a time. You can take out and opponent diagonally but no consequetive take outs. If you reach the other end of the board, you win. It is due 3pm tomorrow so I'm quite worried about it :S
My Problem 1: One of the methods we have to right is a "getTurn()" method, which needs to say who's turn it is (the whites or the blacks). To do this we need to return
either Piece.WHITE or Piece.BLACK (its related to the enum code provided below). Whites always starts. How do I do this? I know its a if else statement
but I'm pretty new to java so I'm not sure how it works. I need a toggle type if else statement so that players take turns.
My Problem 2: 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
enum 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 make a 2d array that "generates" this board if called upon the method.. I know how to print out the array of these whites and blacks, but it just needs a void method that if called upon generates the board and starts the game. What do I write for it to be such a method?
Thanks, please help, I'm really struggling and don't have much time.
Code :
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: Help with assignment about a board game (enum and array question)
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.
Re: Help with assignment about a board game (enum and array question)
Quote:
Originally Posted by
Kranti1992
My Problem 1: One of the methods we have to right is a "getTurn()" method, which needs to say who's turn it is (the whites or the blacks). To do this we need to return
either Piece.WHITE or Piece.BLACK (its related to the enum code provided below). Whites always starts. How do I do this? I know its a if else statement
but I'm pretty new to java so I'm not sure how it works. I need a toggle type if else statement so that players take turns.
How do you know whose turn it is? What do you mean by a "toggle type if else statement"? Again, I suggest you put together an SSCCE demonstrating just this problem.
Quote:
Originally Posted by
Kranti1992
My Problem 2: 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
enum 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 make a 2d array that "generates" this board if called upon the method.. I know how to print out the array of these whites and blacks, but it just needs a void method that if called upon generates the board and starts the game. What do I write for it to be such a method?
Well, how is an array declared and initialized? Again, an SSCCE of just this problem would be easier to address than the code you posted, which doesn't really help with these problems.
Recommended reading:
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)