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 6 of 6

Thread: Problems with objects and enum

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Problems with objects and enum

    Hello

    After some advice from a few more experienced members on this board I decided to scrap my poor attempt at a chess engine and start over , this time using OOP.
    I am currently having an issue with using enum variables and objects.
    Please excuse my ignorance before hand , I come from a background of embedded programming.

    Basically I have a class ChessGame for the game itself , a class ChessPiece and a class for each of the types of Pieces (king , queen , bishop etc) which extends ChessPiece.
    First of all , why do the individual pieces classes extend ChessPiece? Because I use an enum type for the color and rank of the piece and I doesn't allow me to declare it in each class separately.

    Now , when I start the interface , I create a new instance of the ChessGame. In that instance of the ChessGame I want to create an instances of all the pieces on the board meaning 2 kings , 2 queens , 16 pawns etc .

    Can someone tell me why this is giving me an error? The line where I make an instance of a king.
    I've tried making an instance of the king from the class , constructor , main and anywhere else I could think of but it's giving me an error saying that the enum types ( WHITE , RANK )
    - Cannot find symbol : variable WHITE .
    Why is it not working? The King class extends the ChessPiece class where the enum types are declared.

    public class ChessGame  {
     
        King kingWhite = new King(WHITE , KING , 0 , 4  );
     
         private int chosenSquareXCoordinate; 
         private int chosenSquareYCoordinate;
     
         //...........................................


    The constructor in the King class:

     public King(Shade color, Rank rank, int piecePositionX , int PiecePositionY) 
        {
     
           super();
           isAlive = true; // Whether the piece is still on the board
           this.color = color; // Color of the piece
           this.rank = rank;  // Type of piece
           this.piecePositionX = piecePositionX;
           this.piecePositionY = piecePositionY;
     
        }

    and the Chesspiece Class where the enum is declared:



     
    enum Shade
    {
        WHITE , BLACK
    }
     
    enum Rank
    {
       PAWN , ROOK , KNIGHT , BISHOP , QUEEN , KING , BLANK
    }
     
    public class ChessPiece {
     
        Shade color; // color of the piece
        Rank rank;  // Type of piece 
     
     
     
        public ChessPiece()
     
         {
     
     
         }
     
    // ....................

  2. The Following User Says Thank You to Att1li For This Useful Post:

    ChristopherLowe (December 7th, 2013)


  3. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problems with objects and enum

    Is it Shade.WHITE? But I'm not sure what that gets you yet. Keep us posted on your progress. Sounds like a more interesting approach than your last one.

  4. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problems with objects and enum

    Excellent question OP!

    You need to fully qualify the 'Shade' and 'Rank'.

    // It cannot find WHITE because it isn't local to ChessGame
    King kingWhite = new King(WHITE , KING , 0 , 4  );
     
    // Fix
    King kingWhite = new King(ChessPiece.Color.WHITE , ChessPiece.Rank.KING , 0 , 4  );

  5. #4
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problems with objects and enum

    Greg Thanks alot for the Fix!

    kingWhite = new King( Shade.WHITE , Rank.KING , 0 , 4  );

    Seems to be correct .
    The reason I am assigning a parameter Rank and Color to each piece is so the game knows what rules to apply to the moves.
    But i'm thinking of changing those parameters to Strings because I don't know how to work with enum so well.




    Christopherlowe:

    King kingWhite = new King(ChessPiece.Color.WHITE , ChessPiece.Rank.KING , 0 , 4 );

    Gives me a red point : Cannot find symbol : color .
    Could you explain in abit more detail what it means to "fully qualify" the KING?

    - Considering that the individual piece classes ( King , Queen etc..) extend ChessPiece , I don't need to create an object of ChessPiece to use the individual classes do I?
    I'm guessing not .

    Thanks for your answers so far .

    --- Update ---

    I have another question :

    The way my engine works is that I have a String array

         private String[][] ChessBoard = {                    // Piece array
     
     
            {"r","k","b","q","a","b","k","r"},
            {"p","p","p","p","p","p","p","p"},
            {" "," "," "," "," "," "," "," "},
            {" "," "," "," "," "," "," "," "},
            {" "," "," "," "," "," "," "," "},
            {" "," "," "," "," "," "," "," "},
            {"P","P","P","P","P","P","P","P"},
            {"R","K","B","Q","A","B","K","R"}
     
        };

    Which holds the initial and current position of the pieces and everytime I make a move then the values in the array are changed accordingly .
    My question is , instead of the array being a String array and having functions which decipher what piece by what letter is currently in the space , could
    I make an array of Instances of each piece, and use null where there are no pieces?

    If this would be possible then I would have to have an array of a certain type right ? I assume I can't make an array of type ChessPiece and insert instances of
    the individual piece classes into it..
    Or perhaps if I declared my piece instances something like :

       ChessPiece KING = new King([parameters here]);
     
    or maybe
     
         King KING = new ChessPiece([parameters here]);

    I am aware that this is called polymorphism but I'm not exactly sure what it is or what It will do in this case

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problems with objects and enum

    You're welcome. I don't use enums much myself and was a bit uncertain in my answer, but I verified later - especially after seeing Christopher's answer.

    As for using Strings rather than the path you're currently on, I don't know which would be better. Stick with what you're doing, because I don't think your design is firm yet. You're in the early stages of defining the design, and there will be plenty of times to make adjustments. Think it through a few times (many), write a little code, see how what you're thinking all fits together, then adjust.

    I'm wondering what will be the purpose for the ChessBoard array. Isn't this going to be a graphical program? I think the chessboard (an object) should have 8 x 8 or 64 locations (cells, whatever you call them) referenced by row/column which themselves indicate which piece is on them, if any.

  7. #6
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problems with objects and enum

    Yes it's a graphical project.
    I already have the graphics done and they seem to be working fine.
    The clicks on the squares adress the array in ChessGame class to see what is currently on the position of the square clicked , and then the mouse release also adresses the same array to see where the piece
    is supposed to be moved. After those two functions are performed, a repaint() is executed and the graphical board updates.

    The purpose of the Chessboard array is for the graphic mouse click coordination and calculating the allowed moves of each piece.
    It seems reasonable to me ... so far.
    Thanks for your input , I only have 2 weeks to finish this so I'll be asking more questions pretty soon

Similar Threads

  1. Problems With Image Loading and Extended Objects
    By Gravity Games in forum Object Oriented Programming
    Replies: 13
    Last Post: April 22nd, 2013, 01:31 PM
  2. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  3. enum
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 23rd, 2012, 03:19 PM
  4. [SOLVED] Alternative to Enum
    By Doctor X in forum Java Theory & Questions
    Replies: 2
    Last Post: October 6th, 2012, 03:49 PM
  5. String to Enum problems...
    By alex13809 in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 2nd, 2011, 10:59 AM