5 Replies - 4005 Views - Last Post: 01 July 2010 - 05:52 PM Rate Topic: -----

#1 ecrinne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 10

Need help with Java mouse game assignment!

Posted 30 June 2010 - 04:54 PM

For my programming class I have an assignment called "Blind Mouse". You may have heard of this before, but here it is if you haven't. There's a cat, a mouse and a block of cheese. The program prompts the user to enter the "board" size and the starting position of the mouse (the cat always starts on the far left, and the cheese the far right). When the program runs, the cat will move randomly left or right on the 2D "board" once per iteration and the cat will always move to the right. The game ends when either A)The cat eats the mouse or B) the mouse eats the cheese. The problem I'm having is that the game does not end when either of those events occur. My code is posted below. Does anyone have any suggestions on why this is happening? Any help is appreciated, thanks!
import java.io.*;

public class BlindMouse {



  public static void displayBoard(char[] board) {
    System.out.println(board); 
    
  }

  public static int moveMouse(char[] board, int mouse_index) {
    board[mouse_index] = '.';
    double rand = Math.random();

    if (rand < .5) {
      mouse_index = (mouse_index + 1);
    }
    else mouse_index = (mouse_index - 1);

    board[mouse_index] = 'm';
    return mouse_index;
 
  }

  public static int moveCat(char[] board, int cat_index) {
    board[cat_index] = '.';

    cat_index = (cat_index + 1);
    board[cat_index] = 'c';

    return cat_index;

  }
 

  public static boolean mouseHasCheese(int mouse_index, int cheese_index) {
    return (mouse_index == cheese_index);

  }


  public static boolean catHasMouse(int mouse_index, int cat_index) {
    return (mouse_index == cat_index);

  }
  

  public static void displayMouseWins(char[] board) {
    System.out.println("The mouse has reached the cheese!");
    
  }

  public static void displayCatWins(char[] board) {
    System.out.println("The mouse was eaten by the cat!");

  }




  public static void initBoard(char[] board,
                               int mouse_index,
                               int cat_index,
                               int cheese_index) {
    
    int length = board.length;
    for (int i = 0; i < length; i++) {
      board[i] = '.';
    }
    
    board[mouse_index] = 'm';
    board[cat_index] = 'c';
    board[cheese_index] = '%';
 
   
 }




  public static void startBlindMouse(char[] board,
                                     int mouse_index,
                                     int cat_index,
                                     int cheese_index) {
    initBoard(board, mouse_index, cat_index, cheese_index);
    displayBoard(board);
    while (!mouseHasCheese(mouse_index, cheese_index) ||
           !catHasMouse(mouse_index, cat_index)) {
 
      mouse_index = moveMouse(board, mouse_index);
      cat_index = moveCat(board, cat_index);
      displayBoard(board);
    }
    if (mouseHasCheese(mouse_index, cheese_index)) {
      displayMouseWins(board);
    }
    else displayCatWins(board);
  

  }
       




  public static void main(String[] args) {
    
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader bufRead = new BufferedReader(istream);

    System.out.println("Welcome to the Blind Mouse Program");

    try {
      System.out.println("Enter the board length");
      System.out.printf("%s", ">");
      String board_length = bufRead.readLine();

      System.out.println("Enter the Mouse's starting positon");
      System.out.printf("%s", ">");
      String mouse_position = bufRead.readLine();

      int board_size = (1 + Integer.parseInt(board_length));
      int mouse_index = Integer.parseInt(mouse_position);

      int cat_index = 0;
      int cheese_index = board_size - 1;
      
      char[] board = new char[board_size];
      
      startBlindMouse(board, mouse_index, cat_index, cheese_index);
    

    }

    catch (IOException err) {
      System.out.println("Error reading line");
    }


  

      
  }


}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Java mouse game assignment!

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Need help with Java mouse game assignment!

Posted 30 June 2010 - 04:59 PM

These conditions are mutually exclusive. That means that when one happens, the other can't. So if mouseHasCheese == true, then catHasMouse == false. Because of this, the condition will always evaluate to true. However, if you use the && operator for the AND logical gate instead of the || operator, you should get the result you want.
while (!mouseHasCheese(mouse_index, cheese_index) ||  
       !catHasMouse(mouse_index, cat_index)) {  


Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Need help with Java mouse game assignment!

Posted 30 June 2010 - 05:46 PM

By the way you complicated your life for nothing

 System.out.printf("%s", ">");  

]
can be
 System.out.print(">");


Was This Post Helpful? 0
  • +
  • -

#4 ecrinne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 10

Re: Need help with Java mouse game assignment!

Posted 30 June 2010 - 09:48 PM

I tried changing my code to && before and to no avail. I keep getting this output(example):

c...m...%
.c.m....%
..c.m...%
...c....%
..m.c...%
...m.c..%
..m...c.%
.m.....c%
..m.....c


And it should look like (example):

c...m...%
.c.m....%
..c.m...%
...c....%
The cat at the mouse :(
Was This Post Helpful? 0
  • +
  • -

#5 creap  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 1
  • Joined: 01-July 10

Re: Need help with Java mouse game assignment!

Posted 01 July 2010 - 08:22 AM

You are moving the cat and the mouse without testing to see if they are on the same slot in between. In this manner they will never have the same index since one moves, then the other, THEN you test to see if they are in the same spot.
Was This Post Helpful? 4
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Need help with Java mouse game assignment!

Posted 01 July 2010 - 05:52 PM

View Postcreap, on 01 July 2010 - 09:22 AM, said:

You are moving the cat and the mouse without testing to see if they are on the same slot in between. In this manner they will never have the same index since one moves, then the other, THEN you test to see if they are in the same spot.

Congratulations creap you are the first one is this forum to have a number of thanks == the number of posts
For the long timers here a ratio of 1 to 10 is good but you beat us by 10
Good show :^: stay here :D
+1 to boost the stats at 200% may be there will be a Report Generator that will die on it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1