Improve challenge selection system

- Game will no longer end unexpectedly, when all challenges are played.
- Challenges are now marked correctly as played.
This commit is contained in:
Thanos Paravantis 2016-06-28 21:57:17 +03:00
parent 5e9f16424c
commit 973666a42f
2 changed files with 24 additions and 21 deletions

View File

@ -352,33 +352,25 @@ public class BawkBawkBattles extends TeamGame implements IThrown
{
int limit = _list.size();
int attemps = 0;
boolean available = true;
Challenge instance = _list.select();
Challenge instance = _list.random();
while (!isSuitable(instance))
{
if (attemps < limit)
{
instance = _list.select();
instance = _list.random();
attemps++;
}
else
{
available = false;
break;
_list.resetPlayed();
attemps = 0;
}
}
if (available)
{
System.out.println("Found matching challenge: " + instance.getName());
return instance;
}
else
{
return null;
}
System.out.println("Found matching challenge: " + instance.getName());
return instance;
}
private boolean isSuitable(Challenge instance)
@ -889,6 +881,8 @@ public class BawkBawkBattles extends TeamGame implements IThrown
_settings.markMessagesAsSending(false);
_settings.markMessagesAsSent(false);
_list.addPlayed(_challenge);
Damage = false;
_challenge = null;
EndCheck();

View File

@ -11,13 +11,12 @@ public class ChallengeList
private List<Challenge> _played = new ArrayList<>();
private Challenge _restricted;
public Challenge select()
public Challenge random()
{
if (_restricted == null)
{
if (_played.size() == _challenges.size())
{
System.out.println("All challenges played, resetting and selecting new one.");
_played.clear();
return UtilMath.randomElement(_challenges);
}
@ -25,21 +24,16 @@ public class ChallengeList
{
Challenge challenge = UtilMath.randomElement(_challenges);
System.out.println("Attempt: " + challenge.getName());
while (_played.contains(challenge))
{
System.out.println("Attempt (Loop): " + challenge.getName());
challenge = UtilMath.randomElement(_challenges);
}
System.out.println("Selected: " + challenge.getName());
return challenge;
}
}
else
{
System.out.println("Restricted: " + _restricted.getName());
return _restricted;
}
}
@ -52,11 +46,21 @@ public class ChallengeList
}
}
public void addPlayed(Challenge challenge)
{
_played.add(challenge);
}
public void restrict(Challenge challenge)
{
_restricted = challenge;
}
public void resetPlayed()
{
_played.clear();
}
public void unrestrict()
{
_restricted = null;
@ -66,4 +70,9 @@ public class ChallengeList
{
return _challenges.size();
}
public int played()
{
return _played.size();
}
}