parent
5b0b75045a
commit
c8e92e38d5
@ -0,0 +1,43 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Players extends ArrayList<Player> {
|
||||
private final Pile pot = new Pile();
|
||||
private final Pile draws = new Pile();
|
||||
|
||||
public void play() {
|
||||
while (playersLeft() != 1) {
|
||||
draws.clear();
|
||||
for (int i = 0; i < this.size(); i++) {
|
||||
String output = String.format("Player %d (%d cards)", i, this.get(i).cardsCount());
|
||||
Card card = this.get(i).draw();
|
||||
draws.add(card);
|
||||
Out.println(String.format("%s: %d - %s", output, card.number, card.suit));
|
||||
}
|
||||
pot.addAll(draws);
|
||||
givePotToWinner();
|
||||
}
|
||||
Out.println(String.format("Player %s wins the game!", get(0).name));
|
||||
}
|
||||
|
||||
private int playersLeft() {
|
||||
for (int i = size() - 1; i >= 0; i--) {
|
||||
if (get(i).cardsCount() == 0) {
|
||||
remove(i);
|
||||
}
|
||||
}
|
||||
return size();
|
||||
}
|
||||
|
||||
private void givePotToWinner() {
|
||||
Card max = draws.getMax();
|
||||
if (draws.occurrences(max.number) == 1) {
|
||||
int maxIndex = draws.indexOf(max);
|
||||
Out.println(String.format("Player %d wins this round", maxIndex));
|
||||
this.get(maxIndex).discardPile.addAll(pot);
|
||||
pot.clear();
|
||||
} else {
|
||||
Out.println("No winner in this round");
|
||||
}
|
||||
Out.println();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue