0

Game:
There is a box divided into five sections. Inside the box sits mouse. Cat sitting near the box.
Each turn, cat puts his paw on the section.
1) if the cat put his paw on the section with the mouse, the game is over
2) else, otherwise, the mouse moves to the neighboring section, including the section under the cat's paw
I'm trying to find a strategy cat which will win in a minimum number of moves (in average).
Chain - cyclically repeating sequence of moves of the cat.
The following function returns the average number of moves to win for a given chain:

public static double computePerformanceForChain(String chain)
{
    final int iterationsCount = 10000;
    int catPos, mousePos,steps=0;
    Random random = new Random(System.currentTimeMillis());
    for(int i=0; i<iterationsCount; i++)
    {
        mousePos=random.nextInt(5);
        for(int j=0;;j++)
        {
            catPos=Integer.parseInt(String.valueOf(chain.charAt(j%chain.length())));
            steps++;
            if(catPos==mousePos)  break;
            if(mousePos==0) mousePos=1;
            else if(mousePos==4) mousePos=3;
            else mousePos+=random.nextInt(2)*2-1;
        }
    }
    return (double)steps/iterationsCount;
}

For example, computePerformanceForChain("1133") returns approximately 3.
But for chain "23" function loops.
Why is this happening? Thanks.

3
  • what do you mean by "function loops" ? Commented Feb 27, 2013 at 6:53
  • If your value is 0 to start with, then it could end up unluckily bouncing between 0 and 1. There is no guarantee that you will land on 2 or 3 when catPos does, ever. Commented Feb 27, 2013 at 6:54
  • "function loops" means infinite loop. Commented Feb 27, 2013 at 6:59

2 Answers 2

2

Simple answer: there is no guarantee that execution will get out of inner loop
Check out inner loop:

    for(int j=0;;j++) {
        catPos=Integer.parseInt(String.valueOf(chain.charAt(j%chain.length())));
        steps++;
        if(catPos==mousePos)  break;
        if(mousePos==0) mousePos=1;
        else if(mousePos==4) mousePos=3;
        else mousePos+=random.nextInt(2)*2-1;
    }

So, on each iteration mousePos's parity is changed. So if:

  • mousePos get initially assigned to odd number
  • chain is even-odd sequence such as "23"

then catPos will never be equal to mousePos and loop never finishes.

Simply put: if mouse is initially in odd-number section (section 3 for example), then cat is not able to catch it with 2-3 chain and will infinitely repeat this sequence.

Sign up to request clarification or add additional context in comments.

Comments

1

Your sequence for the cat has the cat moving from an even square to an odd square, and doing this repeatedly, always even, odd, even, odd. The mouse, because it always moves to an adjacent square also always moves from even to odd to even to odd. Thus there is a 50/50 chance that the mouse will start on the right square. If the mouse is on an odd square at the time that the cat moves to the even square, then the mouse moves to an even square when the cat tries the odd square.

In this situation, the cat will never catch the mouse.

This will be true of any even-numbered solution where the cat always goes from odd to even to odd to even.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.