• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to prevent a user from going out of bounds in simple array game.  RSS feed

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
]Alright, I'm resorting to the forums since I've tried everything within my knowledge. Here's what's going on:

I am making a very simple 2D array game where the player is asked what size they would like the game board to be. After entering, it displays the board and the player 'P' starts at index [0][0]. After that, they are asked for an action, which can be "up", "down", "left", "right", or "exit". I will be including some extra later (like a treasure at the end, or random obstacles), but for now this is what the game consists of.

I was instructed to "do nothing" when/if the player attempts to go out of bounds. I am trying to simply print an error, such as "Out of bounds! Try again.", then prompt the player again for an action. I even tried to make a boolean method to catch it, but to no avail.

I don't want the exception to occur at all. I just simply want the error message to print to the player and ask for another action. I would prefer not to use try/catch, or try/catch/finally since I already tried that and it still gave the exception error.

This program consists of two classes. I will show the class containing the main first, then the client-server type class second.



HERE IS THE SECOND CLASS:



PLEASE HELP. This is driving me nuts. Thanks in advance. :-)



 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I gave in and added a try/catch method and it worked since Java is cranky. Now I suppose I have one final question. Is there a way to implement code to make the user not have to type the opposite direction twice to get back "in bounds"?

For example, if I reach the end of the array going "down", it prints the message and asks to "try again". However, I have to type "up" twice (once to get back in bounds, and again to actually move up).

Here is the new code:

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem is with your nextLine() scanner method in Driver class Line 30. You have used it extra.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did that on purpose. Try it yourself. It won't work correctly without it.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working fine at my end after removing it.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, what do you mean by "works fine"? What exactly is the program doing now on your end? It's supposed to run like this from the beginning:

Enter World height: 5
Enter World width: 5

P - - - -
- - - - -
- - - - -
- - - - -
- - - - -

ACTION > down

- - - - -
P - - - -
- - - - -
- - - - -
- - - - -

ACTION > etc, etc etc
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remove the input.nextLine(); on Line 33, the while loop does not know what the action variable means in all of the if statements.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it is working like that only. Actually nextLine() should be before calling displayWordl(..). Here is my output after making changes.

Enter World width: 3
Enter World height: 3
P - -
- - -
- - -
ACTION > down
- - -
P - -
- - -
ACTION > right
- - -
- P -
- - -
ACTION > up
- P -
- - -
- - -
ACTION > exit
Thanks for playing. Goodbye!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out why you may have to call nextLine twice here. In my opinon the best way to handle keyboard input with Scanner is to put it into a utility class.

Please look in your book and tell us what it says about nextLine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!