0

So for one of my game models, there is an array of elements represented as a string "--X-X" so that this board has 5 boxes, and positions are 0-4. Each 'X' can only move left. There is an "X-Index" so that if I run getIXPosition(1) it will return the position of the first X which is 2. And getXPosition(2) will return second X's position which is 4. So the string board is "--X-X" but in my code it's represented as an array as "00102" so that I can keep track of xIndex.

Now my issue is that I need to make a move method that prevents the second x from skipping over the first X into position 1 or 0. That is not allowed in this game. I thought I wrote the method correctly but my tests aren't passing when I test to make sure second X can not hop over any X's before it.

public void move(int xIndex, int newPosition) 
{
    int oldPosition = getXPosition(xIndex);

    for(int i= oldPosition - 1; i >= 0;i--) 
    {
        while (board[i] == 0 ) 
        {
            board[oldPosition] = '0'; // so this spot is open '-'
            board[newPosition] = xIndex;
        }
        throw new IllegalArgumentException("Error cannot move X to new position");  
    }
}

What am I doing wrong?

4
  • 3
    You are always throwing an exception...?! Commented Apr 19, 2015 at 18:31
  • you have appeared to use an inner loop which you never break out of. BTW 0 != '0' of course. Commented Apr 19, 2015 at 18:31
  • You should set a flag in the loop if your condition is violated, then test that condition at the end of the loop to either update your board or raise the exception. (And you don't need the inner loop, by the way, just an if statement.) Commented Apr 19, 2015 at 18:32
  • Thank you to everyone. I finally realized the only thing wrong with my original code (not the weird garbage I posted here) was that I called the wrong type of game model in my testcases. (lax game model where skipping is allowed vs. strict game model -this one- where skipping is not allowed.) I was so frustrated I started trying anything to make it work not realizing my dumb mistake in the test cases! -__- Commented Apr 19, 2015 at 18:49

2 Answers 2

1

If you know the position you want to move to, you don't have to search for it, just move there.

if (board[newPosition] == '0') {
   board[newPosition[ = xIndex;
   board[oldPosition] = '0';
} else {
   throw new IllegalArgumentException("Error cannot move X to new position"); 
}

Note: The character '0' is not the value 0 (Actually it is 48 in ASCII)

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

Comments

0

I think the code is a bit flawed. First, I don't think you need to iterate all the way to 0. You should only iterate until you hit newPosition.

Then the while loop doesn't make much sense. I think you were after an if.

Lastly, personally I wouldn't throw an IllegalArgumentException in this case (actually, you're throwing after the first iteration regardless, so that's another flaw). It's the state of the board that's problematic, not the arguments. I would maybe throw IllegalArgumentException if one of the arguments was negative etc.

public void move(int xIndex, int newPosition) {
    int oldPosition = getXPosition(xIndex);

    for(int i= oldPosition - 1; i >= newPosition; i--) {
        if(board[i] == '0') {
            board[oldPosition] = '0';
            board[i] = xIndex;
            oldPosition = i;
        } else {
            //throw some custom exception; we found the other X here.
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.