-1

Here's the question explicitly: I want to get the coordinate of the mouse and put that in a variable that I can plug into the equation shown in the code below [method movePosition()]. Is this possible or do I have to figure out a different approach?

The question is won't make sense without context, so here's a description: A mouse has 8 possible moves to make in a maze, which work wherever it is. A random number is picked to represent the move the mouse has to take. In my head it looks like this, the "-" being the mouse itself:

1 2 3
4 - 5
6 7 8

If the mouse is at move #F, for example, how would I get it to move to position 8 (move #G from the example below)

A B 0 F 0 0 0 0
0 0 C E G J K L
0 0 D H I 0 M N

I was thinking to make a switch statement with each "coordinate" matching to a number from 1-8 which would look like so:

public int movePosition()
{
    int moveToPosition = movePicker();

    switch (moveToPosition)
    {
        case 1:
            this.getMaze()[row - 1][column - 1] = mouse;
            break;
        case 2:
            this.getMaze()[row - 1][column] = mouse;
            break;
        case 3:
            this.getMaze()[row - 1][column + 1] = mouse;
            break;
        //and so on...
    }

But I realized that wouldn't work for obvious reasons. (row and column are the user defined row and column) Is there a way I could follow this same logic wherever the mouse is? I want to get the coordinate of the mouse and put that in a variable that I can plug into the equation above. Or do I have to figure out a different approach?

This is what I found similar to what I want (click here) and other questions are sort of like that too. However they all are asking how to find the coordinates from the size of the array. I need to know how to find the coordinate from the position of the mouse which could be anywhere.

Some Background:

This is homework. I have looked at other questions and although some are related to what I'm looking for, they don't have what I need.

I have a mouse that's thrown in a user defined maze. This means that I, the programmer, don't know what the size of the maze is. After the user defines the size of the maze, it gets set up with 0's. For example's sake, let's do a 3 x 8 maze.

The maze then looks like this (but user can't see this part):

array[3][8]:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

By the end of the program, the mouse would have crossed to the end leaving behind a number indicated to the move it made. A for the first move, B for the second, C for the third, etc. this is just an example the actual code has numbers not letters. I just need to differentiate for clarity The output would look as so (user sees this):

A B 0 F 0 0 0 0
0 0 C E G J K L
0 0 D H I 0 M N

Thanks!

angelfire
  • 9
  • 1
  • 6
  • Is the mouse allowed to go over the same places it has already went? Also is the mouse allowed to move to the left? – luckydog32 Oct 30 '17 at 21:31
  • definitely not a programming problem, and also the question is not clear. – ram Oct 30 '17 at 21:35
  • @luckydog32 - It can't go over the same places it went nor over the "edge" – angelfire Oct 30 '17 at 21:37
  • @ram - Thank you, I'll update the question and put it at the top. But I'm not sure where else to put it as I need help with programming it. Unless this falls in another category – angelfire Oct 30 '17 at 21:38
  • @ram It is 100% a programming question, just because it is entry level doesn't make it any less of a legitimate question. – luckydog32 Oct 30 '17 at 21:40
  • @angelfire I would break it down into two parts. Think about when the mouse cant move to a point in the maze. After the mouse moves to an index in the maze it changes that indexes value from 0 to a letter. The second part is to stop the mouse from going out of bounds. Whats the values of the mouse's index when the mouse cant go left? What about up, down and right? Try to come up with an algorithm that will block those moves from happening. If you are still a bit lost just give me a heads up. – luckydog32 Oct 30 '17 at 21:47
  • It's also worth noting that none of the examples has the mouse moving left, this could just be from it not being shown, but having the mouse be able to move left leads to the possibility of the mouse trapping itself within the maze, so I would double check to make sure that you have to include that. – luckydog32 Oct 30 '17 at 21:52
  • @luckydog32 - Yes the mouse can trap itself by moving left, at which point the maze would restart. The only illegal moves are if it falls off the edge or it takes a move that it has already taken (I've changed the example, thank you). However that's not the part I'm stuck on. It's the index of the mouse that I'm looking for. If the random number generator tells the mouse to move down, that would mean the index of the mouse would be a[R+1][C] and then the next move is left, then the index would be a[R][C-1] The index of the mouse is always changing, so how would I get its "R" and "C"? – angelfire Oct 30 '17 at 22:17
  • If you just are looking for the index of the mouse why not have a global variables `int mouseRow` `int mouseCol`? Unless I am misunderstanding. – luckydog32 Oct 30 '17 at 22:26
  • @luckydog32 - Now I just feel stupid. I am so sorry for wasting your time. But thank you so much, that really helped. If you want to put that as an answer, I can mark it. – angelfire Oct 30 '17 at 22:50
  • @angelfire No problem. Sometimes you get stuck overthinking things, its happened to me plenty of times. I was actually making a little code structure as an example to help you out, no need to mark it though, the answer was in the comments lol. – luckydog32 Oct 30 '17 at 22:56

1 Answers1

0

This is how I would structure the methods, although there's plenty of other ways to do it:

private int mouseCol;
private int mouseRow;

int[][] maze;

private final String LEFT       = "left";
private final String LEFT_UP    = "left_up";
private final String LEFT_DOWN  = "left_down";
//Etc

public String getRandomMove(){
    List<String> moves = getValidMoves();
    //pick a random one
}

public List<String> getValidMoves(){
    List<String> validMoves = new ArrayList<>();
    //Check left
    if(mouseCol != 0 && maze[mouseRow][mouseCol-1] == 0){
        validMoves.add(LEFT);
    }
    //Check left_up
    if(mouseRow != 0 && mouseCol != 0 && maze[mouseRow-1][mouseCol-1] == 0){
        validMoves.add(LEFT_UP);
    }
    //Etc
}
luckydog32
  • 909
  • 6
  • 13