reset password
Author Message
lakerfan94
Posts: 143
Posted 20:31 Feb 26, 2014 |

What are we supposed to print for the nim game in lab 8?

Eric Liao
Posts: 158
Posted 20:33 Feb 26, 2014 |

You print the winner (either player or computer) after the game ends.

Anonymous
Posts: 166
Posted 20:50 Feb 26, 2014 |

how can we make the game to keep going until somebody wins. like after who ever goes first how can we switch the turn to the other player and how do we determine who wins.

Eric Liao
Posts: 158
Posted 20:56 Feb 26, 2014 |

Game will end at a few cases:

1. When the piles runs out (n <= 0)

2. User input invalid input(such as less than 1 or greater than half of the piles). At this case, the other side wins

which means in your main loop, you might want to have something like this.

while ( n > 0 && ( input > 0 || input <= n / 2 ) ) {

     // game continues

}

// print out winner

lakerfan94
Posts: 143
Posted 21:55 Feb 26, 2014 |

I have a problem though if you're left with 1 marble. Considering that it's your turn and(as said in the question description) you can't take more than half the pile, considering that we're using integers, 1/2=0. Therefore, if I try to take away that 1 last marble, then 1 is greater than 0 and the only amount of marbles I can take is 0 and I get an error message when stupid mode tries to run. The reason is that the variable I have to determine how much stupid mode takes away from the pile ranges, as said in the description, from 1 to n/2. But if you're left with one marble left, then I end up getting an error message saying that "n has to be positive".

ytsai8
Posts: 47
Posted 22:10 Feb 26, 2014 |

Terminate the game and determine who is the winner when there is only 1 marbles left?

Like when there is 2 marbles left, and you takes 1 away, and just terminate the game and print

computer loses. well that's what I did..

lakerfan94
Posts: 143
Posted 22:45 Feb 26, 2014 |

Should we also just terminate the game and say that the human player won if the computer in smart mode tries to remove marbles when the size is already a power of two minus one?

ytsai8
Posts: 47
Posted 22:58 Feb 26, 2014 |

But I thought you can't take more than half of the remaining.

So for example, when there's 63 left, computer is gonna take 31 instead of 63?

or just do as the problem stated "expect when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move "

so just remove random.nextInt(pileSize/2)+1; 

if I did not interpret the problem wrongly

lakerfan94
Posts: 143
Posted 23:25 Feb 26, 2014 |

I wonder if he met "except" for "expect".

Eric Liao
Posts: 158
Posted 11:11 Feb 27, 2014 |

Sorry, I fell asleep yesterday early.

Yes, there was a typo on the except. It should be except not expect.

There are some exception in this while loop.

1. The game ends when there is no pile left by the definition

> What about I end the game when there is one pile left?

+ You can do that, that will terminate the game loop in a smart way. In fact, I will encourage you to do that way.

2. To avoid cheating moves (e.g. entering zero or invalid number of piles), you will need to do some validation on the input. In other word, you will need to check the number user input is between 1 to half of pile size (inclusive).

> If there is 63 marbles in the piles, user can input any number between 1 to 31.

3. For the stupid mode of computer, Java yells at me when the pile goes to 1! It says I cannot do random.nextInt(piles/2)

> Random.nextInt(int n) method cannot take zero as parameter. So at this case, you can either terminate the program before reaching to this point as indicated in first point. Or you do an if-else to check if number of piles is equal to 1.