reset password
Author Message
ladodgersfan
Posts: 13
Posted 16:00 Mar 08, 2014 |

I have a problem when player 2 wins. player 1 is supposed to win when the position of the scotch is 0 and when the scotch gets at zero player 1 wins but when the scotch gets at 10, player 2 doesn't win, I don't know what is wrong but when I change the position to be 11 for player 2 to win player two does win, any help is appreciated.

public boolean isGameEnded()
    {
        boolean is = true;
       
        while (firstMoney != 0 && secondMoney != 0)
        {
            if (getPos() == 0)
            {
                System.out.println("the game is over p1 wins");
                System.exit(0);
            }
            else if (getPos() == 10)
            {
                System.out.println("the game is over p2 wins");
                System.exit(0);
            }
           
        }
        return false;
    }

lakerfan94
Posts: 143
Posted 17:58 Mar 08, 2014 |

Considering your loop, there could be the situation that one player loses all his/her money and the bottle
is in the middle but the opponent still has money and can keep betting until the bottle gets at position 0 or 10.
Also, considering that you said "return false", this means that even if the loop terminates, the game is not ended because the
method returns false. The clue I'll give you is look at the variable "is" and change it accordingly.

ytsai8
Posts: 47
Posted 18:13 Mar 08, 2014 |

In addition, I think you don't need the call for System.exit(0). In the GameEngine class, we state the loop to be while ( !game.isGameEnded() ), in this case just return true when the game is suppose to end and return false when it's not suppose to end. 

ytsai8
Posts: 47
Posted 18:20 Mar 08, 2014 |

By the way, you might not want the   while (firstMoney != 0 && secondMoney != 0)  because in the situation where [ firstMoney ==1 and secondMoney==0 and position = 1] , and if P1 bids 1 gold (for the win) and P2 bids 0 (because he got nothing left), P1 couldn't win, which should be something you don't want.

Eric Liao
Posts: 158
Posted 21:01 Mar 08, 2014 |

The above two posts explains a few possibilities of what can happen in your code. All of those comments are correct!

Just a few more comments on what may happen in your code. Where you update the position can be important too. You might want to trace the position is updated right in the doMove() method.

Tsai brought up a good point that in the loop you probably want to return true when player1 or player2 wins so that the game loop will be terminated when this situation actually is true.