started 2025st

This commit is contained in:
2026-05-12 10:17:22 +02:00
parent 00cba2d45f
commit d45552f476
9 changed files with 218 additions and 5 deletions

View File

@@ -1,5 +0,0 @@
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}

5
src/j2025/ST/Animal.java Normal file
View File

@@ -0,0 +1,5 @@
package j2025.ST;
public enum Animal {
Dog, Hare, Hedgehog, Stork, Chicken, Goose, Duck, Pigeon, Starling, Finch, Sparrow, Hamster, Mole, Mouse, Frog
}

View File

@@ -0,0 +1,32 @@
package j2025.ST;
public class AnimalCard implements Card {
private final Animal animal;
private final Sex sex;
public AnimalCard(Animal animal, Sex sex) {
if(animal == null || sex == null) {
throw new IllegalArgumentException("Animal or sex cannot be null");
}
this.animal = animal;
this.sex = sex;
}
public Animal getAnimal() {
return animal;
}
public Sex getSex() {
return sex;
}
@Override
public boolean formsPairWith(Card other) {
return other.formsPairWith(this);
}
@Override
public boolean formsPairWith(AnimalCard other) {
return animal == other.getAnimal() && sex != other.getSex();
}
}

6
src/j2025/ST/Card.java Normal file
View File

@@ -0,0 +1,6 @@
package j2025.ST;
public interface Card {
boolean formsPairWith(Card other);
boolean formsPairWith(AnimalCard other);
}

49
src/j2025/ST/Game.java Normal file
View File

@@ -0,0 +1,49 @@
package j2025.ST;
import java.util.*;
public class Game {
private List<Player> players = new LinkedList<>();
private List<Player> playersRanking = new ArrayList<>();
private Set<Card> cards = new HashSet<>();
public void addPlayer(Player player) {
for (Player p : players) {
if (p.toString().equals(player.toString())) {
throw new IllegalArgumentException("Player with the same name already exists");
}
}
players.add(player);
}
public void dealCards(List<Card> cards) {
}
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);
}
public Player getCurrentPlayer() {
return players.getFirst();
}
public void draw(int index) {
}
public Player getLoser() {
return playersRanking.isEmpty() ? null : playersRanking.getLast();
}
}

View File

@@ -0,0 +1,20 @@
package j2025.ST;
public class PeterCard implements Card{
public static final PeterCard INSTANCE = new PeterCard();
private PeterCard() {
}
@Override
public boolean formsPairWith(Card other) {
return false;
}
@Override
public boolean formsPairWith(AnimalCard other) {
return false;
}
}

44
src/j2025/ST/Player.java Normal file
View File

@@ -0,0 +1,44 @@
package j2025.ST;
import java.util.ArrayList;
import java.util.List;
public class Player {
private final String name;
private List<Card> cards = new ArrayList<>();
public Player(String name) {
this.name = name;
}
public List<Card> getCards() {
return cards;
}
public void addCard(Card card) {
for (Card c : cards) {
if (c.formsPairWith(card)) {
cards.remove(c);
return;
}
}
cards.add(card);
}
public Card removeCard(int index) {
if(index < 0 || index >= cards.size()) {
throw new IllegalArgumentException("Index out of bounds");
}
return cards.remove(index);
}
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,49 @@
package j2025.ST;
import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
public class SchwarzerPeterTest {
@Test
public void test() {
final Card c1 = new AnimalCard(Animal.Dog, Sex.Male);
final AnimalCard c2 = new AnimalCard(Animal.Dog, Sex.Female);
final PeterCard c3 = PeterCard.INSTANCE;
assertTrue(c1.formsPairWith(c2));
assertFalse(c1.formsPairWith(c3));
assertTrue(c2.formsPairWith(c1));
assertFalse(c2.formsPairWith(c3));
assertFalse(c3.formsPairWith(c1));
assertFalse(c3.formsPairWith(c2));
}
@Test
public void test2() {
final Set<Card> set = new HashSet<>();
set.add(new AnimalCard(Animal.Dog, Sex.Male));
set.add(new AnimalCard(Animal.Dog, Sex.Male));
set.add(new AnimalCard(Animal.Dog, Sex.Female));
set.add(new AnimalCard(Animal.Dog, Sex.Female));
set.add(PeterCard.INSTANCE);
set.add(PeterCard.INSTANCE);
System.out.println(set);
}
@Test
public void test3() {
final Player p = new Player("Alice");
p.addCard(PeterCard.INSTANCE);
p.addCard(new AnimalCard(Animal.Dog, Sex.Male));
p.addCard(new AnimalCard(Animal.Dog, Sex.Female));
assertEquals(1, p.getCards().size());
assertEquals(PeterCard.INSTANCE, p.getCards().get(0));
}
}

13
src/j2025/ST/Sex.java Normal file
View File

@@ -0,0 +1,13 @@
package j2025.ST;
public enum Sex {
Male, Female;
@Override
public String toString() {
return switch (this) {
case Male -> "";
case Female -> "";
};
}
}