finished 2025st
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
package j2025.ST;
|
package j2025.ST;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.List;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
private List<Player> players = new LinkedList<>();
|
private List<Player> players = new LinkedList<>();
|
||||||
private List<Player> playersRanking = new ArrayList<>();
|
private List<Player> playersRanking = new ArrayList<>();
|
||||||
private Set<Card> cards = new HashSet<>();
|
|
||||||
|
|
||||||
public void addPlayer(Player player) {
|
public void addPlayer(Player player) {
|
||||||
|
|
||||||
@@ -22,28 +24,55 @@ public class Game {
|
|||||||
|
|
||||||
public void dealCards(List<Card> cards) {
|
public void dealCards(List<Card> cards) {
|
||||||
|
|
||||||
}
|
if (players.isEmpty()) {
|
||||||
|
throw new IllegalStateException("No players to deal cards to");
|
||||||
private void createDeck() {
|
|
||||||
for (Animal animal : Animal.values()) {
|
|
||||||
cards.add(new AnimalCard(animal, Sex.Male));
|
|
||||||
cards.add(new AnimalCard(animal, Sex.Female));
|
|
||||||
}
|
}
|
||||||
cards.add(PeterCard.INSTANCE);
|
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < cards.size(); i++) {
|
||||||
|
players.get(i % players.size()).addCard(cards.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Card> createDeck() {
|
||||||
|
List<Card> deck = new ArrayList<>();
|
||||||
|
for (Animal animal : Animal.values()) {
|
||||||
|
deck.add(new AnimalCard(animal, Sex.Male));
|
||||||
|
deck.add(new AnimalCard(animal, Sex.Female));
|
||||||
|
}
|
||||||
|
deck.add(PeterCard.INSTANCE);
|
||||||
|
return deck;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Player getCurrentPlayer() {
|
public Player getCurrentPlayer() {
|
||||||
return players.getFirst();
|
return players.getFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(int index) {
|
public void draw(int index) {
|
||||||
|
|
||||||
|
if (players.size() < 2) {
|
||||||
|
throw new IllegalStateException("Not enough players to draw cards");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Player currentPlayer = getCurrentPlayer();
|
||||||
|
Player nextPlayer = players.get(1);
|
||||||
|
|
||||||
|
currentPlayer.addCard(nextPlayer.removeCard(index));
|
||||||
|
|
||||||
|
players.addLast(players.removeFirst());
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid card index");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getLoser() {
|
public Player getLoser() {
|
||||||
return playersRanking.isEmpty() ? null : playersRanking.getLast();
|
return playersRanking.isEmpty() ? null : playersRanking.getLast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void finishPlayer(Player player) {
|
||||||
|
playersRanking.add(player);
|
||||||
|
players.remove(player);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ public class Player {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private List<Card> cards = new ArrayList<>();
|
private List<Card> cards = new ArrayList<>();
|
||||||
|
private final Game game;
|
||||||
|
|
||||||
public Player(String name) {
|
public Player(String name, Game game) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.game = game;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Card> getCards() {
|
public List<Card> getCards() {
|
||||||
@@ -21,6 +23,9 @@ public class Player {
|
|||||||
for (Card c : cards) {
|
for (Card c : cards) {
|
||||||
if (c.formsPairWith(card)) {
|
if (c.formsPairWith(card)) {
|
||||||
cards.remove(c);
|
cards.remove(c);
|
||||||
|
if (cards.isEmpty()) {
|
||||||
|
game.finishPlayer(this);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,7 +36,7 @@ public class Player {
|
|||||||
public Card removeCard(int index) {
|
public Card removeCard(int index) {
|
||||||
|
|
||||||
if(index < 0 || index >= cards.size()) {
|
if(index < 0 || index >= cards.size()) {
|
||||||
throw new IllegalArgumentException("Index out of bounds");
|
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||||
}
|
}
|
||||||
|
|
||||||
return cards.remove(index);
|
return cards.remove(index);
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ public class SchwarzerPeterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test3() {
|
public void test3() {
|
||||||
final Player p = new Player("Alice");
|
final Game game = new Game();
|
||||||
|
final Player p = new Player("Alice", game);
|
||||||
p.addCard(PeterCard.INSTANCE);
|
p.addCard(PeterCard.INSTANCE);
|
||||||
p.addCard(new AnimalCard(Animal.Dog, Sex.Male));
|
p.addCard(new AnimalCard(Animal.Dog, Sex.Male));
|
||||||
p.addCard(new AnimalCard(Animal.Dog, Sex.Female));
|
p.addCard(new AnimalCard(Animal.Dog, Sex.Female));
|
||||||
|
|||||||
Reference in New Issue
Block a user