Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with assignment about a board game (enum and array question)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


    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;
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Help with assignment about a board game (enum and array question)

    Quote Originally Posted by Kranti1992 View Post
    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 View Post
    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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  2. guessing game assignment
    By scottey313 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 22nd, 2011, 07:52 PM
  3. help with board game assignment
    By pjay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2011, 01:58 PM
  4. help with array assignment ! urgent please :(
    By dre2327 in forum Collections and Generics
    Replies: 5
    Last Post: October 6th, 2011, 02:30 PM
  5. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 03:53 PM

Tags for this Thread