Need bug-fix help related to bad input given to code that is the iterative, Stack-using equivalent of some other recursive code (in Java)?

2017-10-27 6:16 am
The recursiveVersion and iterativeVersion methods are supposed to state whether game can be won or not.

The instructions for how the game is played can be found here.:
https://www.docdroid.net/YOP9dkK/stuckonprogrammingquestion.pdf#page=3

My recursiveVersion method works well for both good and bad inputs.

My iterativeVersion method only works well for good inputs.

Here is the code with the iterativeVersion method (in the TheRowGame class).:
http://dpaste.com/1T7YXQX

Here is the code for the helper class, StackFrame.:
http://dpaste.com/2K0WTD8

Here is the error I am getting.:
http://dpaste.com/3A9512X

Why is the index not always in the interval [0,9]?

Could someone please help me fix this issue?

Any help in fixing this issue would be greatly appreciated!

P.S.
I'm aware that I can fix other minor things like making the methods static, but right now, I just want to focus on the more difficult stuff, which, for me, at least, is getting this bug fixed.

回答 (1)

2017-10-27 8:34 am
You return false when you pop a visited node. That's wrong, since the goal may be reachable from some stacked unvisited node. The correct action is a continue statement. Don't return false until all stacked alternatives have been checked.

The exception is an error in your isRightMoveValid logic, where you test for index + cell/2 <= length-1,

Suppose the length is 10, the index is 5 and the cell contains 9. Then 5 + 9/2 == 5+4 is less than or equal to 10-1, but the move will be to 5 + 9/2 + 1 == 10, and that's invalid.

Change the comparison to < instead of <=.


收錄日期: 2021-04-24 00:48:01
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20171026221643AAkuj9t

檢視 Wayback Machine 備份