Players as ArrayList<Player>

master
Saša Kocić 6 years ago
parent 5b0b75045a
commit c8e92e38d5

@ -1,10 +1,5 @@
import java.util.ArrayList;
public class Game { public class Game {
Player[] players; final Players players = new Players();
final Pile pot = new Pile();
final Pile draws = new Pile();
final int numberOfPlayers;
/** /**
* @param numberOfCards Total number of cards * @param numberOfCards Total number of cards
@ -12,62 +7,13 @@ public class Game {
*/ */
public Game(int numberOfCards, int numberOfPlayers) { public Game(int numberOfCards, int numberOfPlayers) {
Pile deck = Pile.createDeck(numberOfCards); Pile deck = Pile.createDeck(numberOfCards);
createPlayersWithCards(numberOfPlayers, deck);
this.numberOfPlayers = numberOfPlayers;
}
private void createPlayersWithCards(int numberOfPlayers, Pile deck) {
this.players = new Player[numberOfPlayers];
int cardsPerPlayer = deck.size() / numberOfPlayers; int cardsPerPlayer = deck.size() / numberOfPlayers;
for (int i = 0; i < numberOfPlayers; i++) { for (int i = 0; i < numberOfPlayers; i++) {
players[i] = new Player(String.valueOf(i), deck.take(cardsPerPlayer)); Player player = new Player(String.valueOf(i), deck.take(cardsPerPlayer));
} players.add(player);
}
public void play() {
while (noWinner()) {
draws.clear();
for (int i = 0; i < numberOfPlayers; i++) {
String output = String.format("Player %d (%d cards)", i, players[i].cardsCount());
Card card = players[i].draw();
draws.add(card);
Out.println(String.format("%s: %d - %s", output, card.number, card.suit));
}
processWinner();
} }
players.play();
} }
private void processWinner() {
int maxIndex = 0;
Card max = draws.get(maxIndex);
for (int i = 1; i < numberOfPlayers; i++) {
if (max.number < draws.get(i).number) {
maxIndex = i;
max = draws.get(maxIndex);
}
}
pot.addAll(draws);
if (draws.uniqueMaximum(max, maxIndex)) {
Out.println(String.format("Player %d wins this round", maxIndex));
players[maxIndex].discardPile.addAll(pot);
pot.clear();
} else {
Out.println("No winner in this round");
}
Out.println();
}
private boolean noWinner() {
ArrayList<Player> activePlayers = new ArrayList<>();
for (Player player : players) {
if (player.cardsCount() > 0) {
activePlayers.add(player);
}
}
if (activePlayers.size() == 1) {
Out.println(String.format("Player %s wins the game!", activePlayers.get(0).name));
return false;
}
return true;
}
} }

@ -15,14 +15,14 @@ class GameTest {
@Test @Test
void createDeck() { void createDeck() {
assertEquals(2, game.players.length); assertEquals(2, game.players.size());
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
assertEquals(20, game.players[i].drawPile.size()); assertEquals(20, game.players.get(i).drawPile.size());
} }
} }
@Test @Test
void play() { void play() {
game.play(); game.players.play();
} }
} }

@ -4,8 +4,7 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Game game = new Game(10, 2); new Game(10, 2);
game.play();
System.out.print(Out.get()); System.out.print(Out.get());
} }
} }

@ -1,6 +1,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Random; import java.util.Random;
import java.util.stream.IntStream;
public class Pile extends ArrayList<Card> { public class Pile extends ArrayList<Card> {
public static Pile createDeck(int numberOfCards) { public static Pile createDeck(int numberOfCards) {
@ -38,12 +39,17 @@ public class Pile extends ArrayList<Card> {
shuffle(random); shuffle(random);
} }
public boolean uniqueMaximum(Card max, int maxIndex) { public long occurrences(int max) {
for (int i = 0; i < this.size(); i++) { return IntStream.range(0, size()).filter(i -> get(i).number == max).count();
if (maxIndex != i && max.number == this.get(i).number) { }
return false;
public Card getMax() {
Card max = get(0);
for (int i = 1; i < this.size(); i++) {
if (max.number < get(i).number) {
max = get(i);
} }
} }
return true; return max;
} }
} }

@ -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…
Cancel
Save