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

Thread: Gridworld Chess Pawn not working

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Gridworld Chess Pawn not working

    I am making a chess game in Gridworld. My pawn method is not working. For example, when I use getMoveLocations() on a pawn the first time, it brings up both valid moves. Once I move it two spaces and use getMoveLocations() again, it brings up the space behind the pawn. When I move it there, and then I use getMoveLocations(), it just brings up an empty ArrayList. Any help would be appreciated.

    Pawn class
    import info.gridworld.actor.Actor;
    import info.gridworld.grid.*;
    import java.util.ArrayList;
    import java.awt.Color;
    /**
     * @author 
     * A class that extends Piece to represent a Pawn
     */
    public class WhitePawn extends Piece
    {
        /**
         * A Piece that represents a Pawn
         * @param Location l- the Location of the Pawn
         * @param Color c- the Color of the Pawn
         */
        public WhitePawn(Location l,Color c)
        {
            super(l,c);
        }
        /**
         * A method that returns an ArrayList of possible move Locations
         * @return ArrayList moves- an ArrayList of moves
         */
        public ArrayList<Location> getMoveLocations()
        {
            ArrayList<Location> moves = new ArrayList<Location>();
            int locX = currentLocation.getRow();
            int locY = currentLocation.getCol();
            Location l1 = new Location(locX - 1,locY);
            Location l2 = new Location(locX - 2,locY);
            if(getGrid().isValid(l1))
            {
                moves.add(l1);
                if(getGrid().get(l1) instanceof Piece)
                {
                    moves.remove(l1);
                }
                if(getGrid().isValid(l2) && numTimesMoved == 0)
                {
                    moves.add(l2);
                    if(getGrid().get(l2) instanceof Piece)
                    {
                        moves.remove(l2);
                    }
                }
            }
     
            return moves;
        }
        /**
         * A method that returns an ArrayList of locations that can be attacked
         * @return ArrayList moves- an ArrayList of moves
         */
        public ArrayList<Location> getAttackLocations()
        {
            ArrayList moves = new ArrayList<Location>();
            ArrayList occupiedSpaces = getGrid().getOccupiedLocations();
            int locX = currentLocation.getRow();
            int locY = currentLocation.getCol();
            Location l1 = new Location(locX - 1, locY - 1);
            Location l2 = new Location(locX - 1, locY + 1);
     
            if(occupiedSpaces.contains(l1))
            {
                moves.add(l1);
            }
            if(occupiedSpaces.contains(l2))
            {
                moves.add(l2);
            }
            return moves;
        }
    }

    And this is the Piece class that Pawn is extending
    import java.util.ArrayList;
    import info.gridworld.grid.Location;
    import java.awt.Color;
    import info.gridworld.actor.Actor;
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    /**
     * @author
     * Piece
     * An Abstract class that represents a piece in a game of chess
     */
     
    public abstract class Piece extends Actor
    {
        Color color;
        Location currentLocation;
        int numTimesMoved;
        public Piece(Location l,Color c)
        {
            super();
            color = c;
            currentLocation = l;
        }
        public abstract ArrayList<Location> getMoveLocations();
        public abstract ArrayList<Location> getAttackLocations();
        public void moveTo()
        {
            String location = JOptionPane.showInputDialog("Where would you like to move? (use chess notation)");
            String row = location.substring(1);
            String column = location.substring(0,1);
            int r;
            int c;
            if(column.equalsIgnoreCase("A"))
                c = 0;
            else if(column.equalsIgnoreCase("B"))
                c = 1;
            else if(column.equalsIgnoreCase("C"))
                c = 2;
            else if(column.equalsIgnoreCase("D"))
                c = 3;    
            else if(column.equalsIgnoreCase("E"))
                c = 4;
            else if(column.equalsIgnoreCase("F"))
                c = 5;
            else if(column.equalsIgnoreCase("G"))
                c = 6;
            else
                c = 7;
     
            r = 8 - (Integer.parseInt(row));
            Location newLocation = new Location(r,c);
            ArrayList<Location> possibleMoves = getMoveLocations();
            ArrayList<Location> attackLocations = getAttackLocations();
            if(attackLocations.contains(newLocation))
            {
                getGrid().get(newLocation).removeSelfFromGrid();
                super.moveTo(newLocation);
                numTimesMoved++;
            }
            else if(possibleMoves.contains(newLocation))
            {
                super.moveTo(newLocation);
                numTimesMoved++;
            }
            else
            {
                JFrame frame = new JFrame("That is not a valid move");
                JOptionPane.showMessageDialog(frame, "That is not a valid move");
            }
        }
        public boolean isOppositeColor(Actor p)
        {
            boolean b = false;
            if(p.getColor().equals(Color.WHITE) && color.equals(color.black))
                b = true;
            if(p.getColor().equals(Color.black) && color.equals(color.WHITE))
                b = true;
            return b;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    Have you tried debugging the code to see what it is doing? I use println() statements that print out the values of variables as they are changed and used. The print out will show you what the code is doing when it executes.

    The code has lots of hard coded numbers: 11 and 12 with no explanation of what they are. It's better to use a variable with a name that describes what the value is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    l1 and l2 are variables for two different locations

    --- Update ---

    And yes I tried debugging it, but the code keeps pulling up logic errors I have no idea how to deal with

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    l1 and 11 are hard to distinguish between in the code font.

    Copy the print out that shows what is happening and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #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: Gridworld Chess Pawn not working

    Show us the errors you want help with in their entirety, copied from exactly as they appear at your end and pasted into a post.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    This is the boardScreen Shot 2014-05-26 at 3.15.21 PM.jpg
    I have moved the pawn to E4 (4,4)Screen Shot 2014-05-26 at 3.15.32 PM.jpg
    Now, the only move option it is giving me is one space back (5,4)Screen Shot 2014-05-26 at 3.15.43 PM.jpg
    I move it to that spotScreen Shot 2014-05-26 at 3.15.50 PM.jpg
    Now it just brings up an empty ArrayList for possible movesScreen Shot 2014-05-26 at 3.15.56 PM.jpg

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    Can you post the print out of the values of the variables that are used in the method? That will show what the code is doing.
    For example: l1 and l1 and the value returned by getGrid().get()
    and getGrid().isValid(l1)
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)

    That is what happened when I went through the sequence that I posted above

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    There are 4 sets of the same values. Why is that?

    What was returned by the methods: getGrid().get() and getGrid().isValid()
    What was in the ArrayList: moves?

    --- Update ---

    What is the orientation and range of values for the rows and columns?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    First call of getMoveLocations(), nothing moved
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    getGrid().getValid(l1) returns true
    Moves = [(5, 4)]
    getGrid().isValid(l2) returns true
    After moves.add(l2), moves = [(5, 4), (4, 4)]
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    getGrid().getValid(l1) returns true
    Moves = [(5, 4)]
    getGrid().isValid(l2) returns true
    After moves.add(l2), moves = [(5, 4), (4, 4)]

    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    getGrid().getValid(l1) returns true
    Moves = [(5, 4)]
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    getGrid().getValid(l1) returns true
    Moves = [(5, 4)]
    locX = 6
    locY = 4
    l1 = (5, 4)
    l2 = (4, 4)
    getGrid().getValid(l1) returns true
    Moves = [(5, 4)]
    getGrid().get(l1) returns WhitePawn[location=(5, 4),direction=0,color=java.awt.Color[r=0,g=0,b=255]]
    After move.remove(l1), moves = []

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    What is the valid move that should be returned?

    What is the orientation and range of values for the rows and columns?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    Once the Pawn moves to (4,4), the next valid move should be (3,4). It goes (rows, columns), so it's (y,x), and it starts from (0,0) in the top left corner

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    The debug print out does not show that the pawn is at 4,4. All the print outs show the pawn is at 6,4
    I assume that locX and locY are the pawn's location.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    Oh right, so the location isn't updating. I'm not sure how to get the location to update though, any ideas?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Gridworld Chess Pawn not working

    When a piece is moved, set its new location.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    May 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gridworld Chess Pawn not working

    Okay it worked! Thank you!

Similar Threads

  1. [SOLVED] Chess
    By fireredlink5000 in forum Object Oriented Programming
    Replies: 2
    Last Post: May 22nd, 2014, 10:28 PM
  2. Need help with Gridworld
    By corong1997 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 19th, 2014, 08:09 AM
  3. [SOLVED] how to set a new color in gridworld
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2014, 10:19 PM
  4. GridWorld
    By keepStriving in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2013, 01:27 PM
  5. GridWorld Code - Need Help
    By prodigytoast in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 09:02 PM