Java Cat and Mouse Game


 
Thread Tools Search this Thread
Top Forums Programming Java Cat and Mouse Game
# 1  
Old 11-11-2014
Java Cat and Mouse Game

I am having problems with another exercise from a website to learn Java.
I am supposed to create classes for a game called cat and mouse.

Basically, there is a grid 5x5 (representing the island) surrounded by water and has 6 bridges off the grid. The cat is hungry and wants the mouse, but the mouse can escape if he finds a bridge or dies if he goes into the water. The cat and mice are placed randomly on the grid at the beginning of the game. Bridges are evenly-spaced, 2 on each side.

There are a lot of parts for this exercise, but I'll just start with the first one right now. I want to create an Animal abstract class with a protected attribute of type Point (the location on the map) and a private String attribute (the animal name). A few more attributes would be: String getName(), Point getLocation(), void setStartLocation(). It will also have one constructor that takes a String (the animal's name) and a Random object. It'll store these values as instance variables (attributes).

Animal( String name, Random rng )

It'll also declare an abstract method:

void move()

I understand what it wants me to do, but it doesn't ask me to create the grid so am I not supposed to? I am not sure how to start this. There are also other classes to be created. Any help and guidance would be appreciated.
# 2  
Old 11-12-2014
The grid is context, but you should make a 2d arrray or collection of classes that rep the squares and the ref and nature of 4 (assuming no diagonal movement) adjacent squares (die, square, bridge).

2 per side is 8 bridges.

Of course, displaying it all is a second puzzle.
# 3  
Old 11-12-2014
Quote:
Originally Posted by DGPickett
The grid is context, but you should make a 2d arrray or collection of classes that rep the squares and the ref and nature of 4 (assuming no diagonal movement) adjacent squares (die, square, bridge).

2 per side is 8 bridges.

Of course, displaying it all is a second puzzle.
Okay, I'll just create the classes. What does it mean by: create an Animal abstract class with a protected attribute of type Point (the location on the map) and a private String attribute (the animal name)? Can you give me a layout on how to do this in general?
# 4  
Old 11-14-2014
This is what I have so far:

Code:
public abstract class Animal
{
   public class Point
   {
      public int x,y;
   }  
   public class Animal
   {
      protected Point loc;
      protected String name;
      protected Random rng;
      String getName()
      {return name;}
 
      Point getLocation()
      {return loc;}
 
      void setStartLocation(Point p)
      {
      }  
      Animal( String name, Random rng )
      {
         this.name = name;
         this.rng;
      }  

      void move()
      {
      }  
   }


Last edited by totoro125; 11-16-2014 at 06:45 PM..
# 5  
Old 11-21-2014
The Animal needs to be a child/friend of the Grid object, so movement can be determined from the nature of the current 4 walls.
Code:
char Grid[5][5][4] = {{ // North, East, South, West is Grid, Water, Bridge
{ 'G', 'G', 'W', 'W' }, // Bottom left
{ 'G', 'G', 'B', 'G' },   // moving right ( x increments before y )
 .
 .
 .
 .
} };

Of course, there are other ways to encode the grid, but the more compact, the more work to figure out what happens with a direction choice.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

CAt utility in java

Hello friends, could you please advice me of how to traslate this program written in C to java? #include <cstdio> main( ){ char c; c = getchar( ); while (c != EOF) { putchar(c); c = getchar( ); } } I am supposed to test the difference in time between compiling the C... (2 Replies)
Discussion started by: bentaboha87
2 Replies

2. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

3. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

4. UNIX for Dummies Questions & Answers

Changing middle mouse button for pasting to right mouse button in cygwin rxvt

Hi, I'm using rxvt in Cygwin and I'm wondering how to change my mouse bindings from the middle button for pasting to the right button. The main reason why I want to do this is because my laptop doesn't have a middle mouse button. Thanks for any help! (2 Replies)
Discussion started by: sayeo
2 Replies

5. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies
Login or Register to Ask a Question