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

Thread: Need help with Gridworld

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

    Default Need help with Gridworld

    I'm writing a Chess program in Gridworld, and I'm running into a bit of a problem coming up with a line of code that checks if a location is occupied or not. Right now, I am using
    if((getGrid().isValid(l1) == true && getGrid().get(l1) != null))
    , but this isn't working. Any help would be greatly appreciated. Thank you!


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Need help with Gridworld

    Please elaborate on what you mean by "isn't working." If there are error messages, please post here the messages in their entirety. Also, the single line of code you posted is not sufficient for anyone to be able to tell if there's a problem with it.

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

    Default Re: Need help with Gridworld

        public ArrayList<Location> getAttackLocations()
        {
            ArrayList<Location> moves = new ArrayList<Location>();
            ArrayList<Location> possibleMoves = new ArrayList<Location>();
     
            for(Location l : moves)
            {
                if(getGrid().get(l) != null)
                {
                    moves.add(l);
                }
            }
     
            return moves;
        }
    This is the method, and this is the method it is referencing
    public ArrayList<Location> getMoveLocations()
        {
            ArrayList<Location> moves = new ArrayList<Location>();
     
            boolean canContinue = true;
            int numSpacesMoved = 1;
            Location l = getLocation();
            while(canContinue)
            {
                Location l1 = new Location(l.getRow() - numSpacesMoved, l.getCol() - numSpacesMoved);
                if(getGrid().isValid(l1) == true)
                {
                    moves.add(l1);
                    numSpacesMoved++;
                }
                else
                {
                    canContinue = false;
                }
            }
            canContinue = true;
            numSpacesMoved = 1;
            while(canContinue)
            {
                Location l1 = new Location(l.getRow() + numSpacesMoved, l.getCol() + numSpacesMoved);
                if(getGrid().isValid(l1) == true)
                {
                    moves.add(l1);
                    numSpacesMoved++;
                }
                else
                {
                    canContinue = false;
                }
            }
            canContinue = true;
            numSpacesMoved = 1;
            while(canContinue)
            {
                Location l1 = new Location(l.getRow() + numSpacesMoved, l.getCol() - numSpacesMoved);
                if(getGrid().isValid(l1) == true)
                {
                    moves.add(l1);
                    numSpacesMoved++;
                }
                else
                {
                    canContinue = false;
                }
            }
            canContinue = true;
            numSpacesMoved = 1;
            while(canContinue)
            {
                Location l1 = new Location(l.getRow() - numSpacesMoved, l.getCol() + numSpacesMoved);
                if(getGrid().isValid(l1) == true)
                {
                    moves.add(l1);
                    numSpacesMoved++;
                }
                else
                {
                    canContinue = false;
                }
            }
            return moves;
        }

    The getMoveLocations() method is working fine, but whenever I try the getAttackLocations() method, it just pulls up an empty ArrayList

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with Gridworld

    Your loop in the getAttackLocations() never runs because the moves ArrayList is empty when you attempt to loop over it, so it just returns the empty list.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. gridworld error: " ';' expected"
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 16th, 2014, 07:20 PM
  2. [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
  3. GridWorld
    By keepStriving in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2013, 01:27 PM
  4. GridWorld Code - Need Help
    By prodigytoast in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 09:02 PM