Reputation: 21875
Given the following :
// get the list of the players , in order to start the game
ArrayList<String> players = this.m_maze.getPlayers();
// human side
String humanPlayer = iterator.next();
String computerPlayer = null;
// define iterator for the players
Iterator<String> iterator = players.iterator();
boolean humanSide = true ,computerSide = false; // assume the human player is starting the game
// controller - start a game between the players , at least two players are playing
while (this.m_rounds > 0)
{
if (humanSide == false && computerSide == true) // then this is the turn of the human side
{
if (iterator.hasNext() == false)
{
// reinitialize the iterator
Iterator<String> iterator = players.iterator();
}
while (iterator.hasNext())
// more code
I try to reuse the iterator but I get a "Duplicate local variable iterator" compilation error. How can I reuse that iterator ? Thanks ,Ron
EDIT :
if (iterator.hasNext() == false)
{
// reinitialize the iterator
iterator = players.iterator();
}
while (iterator.hasNext())
{
computerPlayer = iterator.next();
// computer decides what would be his next move , between 1 - 3
Upvotes: 0
Views: 2592
Reputation: 7305
Don't use iterators like this, it can mess things up, just do it the old way, I mean using the famous Mr. Iterator "i". Moreover the code would look more sensible.
while(m_rounds > 0){
if(i == players.size()) {
i = 0;
}
currentPlayer = players.get(i);
//Do what you want to do with the current player...
...
//Next
i++;
}
A small suggestion, do you really need both the flags, i mean the humanSide and computerSide? Won't using just one suffice? Your if-else block would look a lot more simpler and clear:
if(humanSide) {
//Hope this move wouldn't crush your logic.
} else {
//Algorithm based awesome move.
}
Upvotes: 1
Reputation: 14837
I think google guava has pretty much what you want with Iterators#cycle
.
Use it like this:
Iterator<String> iterator = Iterators.cycle(players.iterator());
...and you will never run out of players.
Upvotes: 1
Reputation: 21875
Okay just remove the Iterator<String>
, meaning is , when reusing that iterator just write : iterator = players.iterator();
Thank you all !!
Upvotes: 0
Reputation: 34675
Don't re-declare the variable; just assign it.
if (iterator.hasNext() == false) {
iterator = players.iterator();
}
You should be careful about nested loop behavior. Is your real intent to have have the following block
while (iterator.hasNext()) { ... }
actually check for this condition?
while (iterator.hasNext() && (this.m_rounds > 0)) { ... }
Upvotes: 3
Reputation: 15333
You have put Iterator<String> iterator = players.iterator();
in your loop.
So each time it tries to create variable with name iterator
.
Just put it's declaration out of the loop... like
Iterator<String> iterator; //here ****
while (this.m_rounds > 0)
{
if (humanSide == false && computerSide == true) // then this is the turn of the human side
{
if (iterator.hasNext() == false)
{
// reinitialize the iterator
iterator = players.iterator();
}
while (iterator.hasNext())
Upvotes: 1