Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:02:55+00:00 2026-05-30T02:02:55+00:00

So basically I have a GridWorld Project that I’m doing right now in my

  • 0

So basically I have a GridWorld Project that I’m doing right now in my AP Comp Sci class. I’m doing Pacman. Here is my code for the act method (for those unfamiliar with GridWorld, the act method is called every time an actor is expected to make a new move) :

public void act()
{
    Location loc = getLocation();

    if(direction==null) {
    }

    else if(direction.equals("NORTH")) {
        Location next = loc.getAdjacentLocation(loc.NORTH);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(next);
            direction = "NORTH";
        }
    }

    else if(direction.equals("SOUTH")) {
        Location next = loc.getAdjacentLocation(loc.SOUTH);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(getLocation().getAdjacentLocation(getLocation().SOUTH));
            direction = "SOUTH";
        }
    }

    else if(direction.equals("EAST")) {
        Location next = loc.getAdjacentLocation(loc.EAST);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(getLocation().getAdjacentLocation(getLocation().EAST));
            direction = "EAST";
        }

        else if(getLocation().getCol()==20 && getLocation().getRow()==9) {
            moveTo(new Location(9,0));
            direction = "EAST";
        }
    }

    else if(direction.equals("WEST")) {
        Location next = loc.getAdjacentLocation(loc.WEST);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            moveTo(getLocation().getAdjacentLocation(getLocation().WEST));
            direction = "WEST";
        }

        else if(getLocation().getCol()==0 && getLocation().getRow()==9) {
            moveTo(new Location(9,20));
            direction = "WEST";
        }
    }
}

The reason for the weird wording in the last two if statements is bc I want the Pacman to be able to teleport in the real game. Now when I run the game, about 90% of the time it works but in the other 10% I get an IllegalArgumentException bc it says I am trying to move to a place that is not on the board (eg. (9,-1) and (9,21)). I want to know how I can catch or throw or whatever I need to do to stop this from happening. I have never used catch or throw so also please try to explain your reasoning thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:02:56+00:00Added an answer on May 30, 2026 at 2:02 am

    To throw an exception, you use keyword throw. To catch, you use the try / catch construct. See this for more details:

    • http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html

    For your case, you’d do something like this – this is a test case:

    try {
        throw new IllegalArgumentException("Threw an IllegalArgumentException");
    } catch(IllegalArgumentException e) {
        System.out.println("Caught an IllegalArgumentException..." + e.getMessage());
    }
    

    You should, however, look into your code to see why IllegalArgumentException is being thrown anyway and fix that part. Using exceptions and try / catch is for unexpected events, not events that you expect to happen and that you can handle in a better way.

    For example, FileNotFoundException gets thrown when a file could not be found. You generally try / catch that, so that you do something if the file was not found. However, if it’s expected in a reasonable number of cases that the file might not be there, it would be better to first check if the file exists and then if it does actually do something with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have a lot of poorly designed code to do something that, I'm
well right now I basically have the jquery load function like this $('#results').load(sources.php?source=1&page=<?=$_GET['page']?>&search=<?=$_GET['search']?>); so
I basically have this problem: right now, we have a system where it gets
I basically have a unix process running and it is doing some heavy processing
I basically have 7 select statements that I need to have the results output
basically have two questions. 1. Is there a c++ library that would do full
I basically have the exact same problem as here: http://stackoverflow.com/questions/2416155/issue-in-executing-spring-web-project-in-eclipse-on-tomcat-server but the fix for
I basically have this piece of code. char (* text)[1][80]; text = calloc(2821522,80); The
I basically have a div on my site that always has the same stuff.
I basically have a table with headers that I read in from a DB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.