Skip to content

Commit

Permalink
finish plusmounts TDD and ATDD
Browse files Browse the repository at this point in the history
  • Loading branch information
scolleyHuang committed Apr 6, 2024
1 parent c25b4a9 commit bd4af84
Show file tree
Hide file tree
Showing 26 changed files with 992 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.behavior.PlayCardBehaviorHandler;
import com.gaas.threeKingdoms.behavior.handler.DyingAskPeachBehaviorHandler;
import com.gaas.threeKingdoms.behavior.handler.NormalActiveKillBehaviorHandler;
import com.gaas.threeKingdoms.behavior.handler.PeachBehaviorHandler;
import com.gaas.threeKingdoms.behavior.handler.RedRabbitHouseBehaviorHandler;
import com.gaas.threeKingdoms.behavior.handler.*;
import com.gaas.threeKingdoms.events.*;
import com.gaas.threeKingdoms.gamephase.*;
import com.gaas.threeKingdoms.generalcard.GeneralCard;
Expand Down Expand Up @@ -43,14 +40,14 @@ public class Game {
private Stack<Behavior> topBehavior = new Stack<>();

public Game(String gameId, List<Player> players) {
playCardHandler = new DyingAskPeachBehaviorHandler(new PeachBehaviorHandler(new NormalActiveKillBehaviorHandler(new RedRabbitHouseBehaviorHandler(null, this), this), this), this);
playCardHandler = new DyingAskPeachBehaviorHandler(new PeachBehaviorHandler(new NormalActiveKillBehaviorHandler(new MinusMountsBehaviorHandler(new PlusMountsBehaviorHandler(null, this), this), this), this), this);
setGameId(gameId);
setPlayers(players);
enterPhase(new Initial(this));
}

public Game() {
playCardHandler = new DyingAskPeachBehaviorHandler(new PeachBehaviorHandler(new NormalActiveKillBehaviorHandler(new RedRabbitHouseBehaviorHandler(null, this), this), this), this);
playCardHandler = new DyingAskPeachBehaviorHandler(new PeachBehaviorHandler(new NormalActiveKillBehaviorHandler(new MinusMountsBehaviorHandler(new PlusMountsBehaviorHandler(null,this), this), this), this), this);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.gaas.threeKingdoms;

import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MountsCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MinusMountsCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.PlusMountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class SeatingChart {

Expand All @@ -24,22 +23,37 @@ public int calculateDistance(Player player, Player targetPlayer) {
int seat2 = players.indexOf(targetPlayer);

int totalSeats = players.size();
int minusOneMountCount = getMountsCount(player);
int equipmentDistance = getEquipmentDistance(player,targetPlayer);
int distanceClockwise = (seat2 - seat1 + totalSeats) % totalSeats;
int distanceCounterClockwise = (seat1 - seat2 + totalSeats) % totalSeats;
// Return the shorter distance of clockwise and counterclockwise
return Math.min(distanceClockwise, distanceCounterClockwise) + minusOneMountCount;
return Math.min(distanceClockwise, distanceCounterClockwise) + equipmentDistance;
}

private int getMountsCount(Player player) {
private int getEquipmentDistance(Player player,Player targetPlayer) {
int minusCount = getMinusOneDistance(player);
int plusCount = getPlusDistance(targetPlayer);
return minusCount + plusCount;
}

private int getMinusOneDistance(Player player) {
if (player.getEquipment() == null) return 0;
MountsCard minusOne = player.getEquipment().getMinusOne();
if (minusOne == null){
MinusMountsCard minusOne = player.getEquipment().getMinusOne();
if (minusOne == null) {
return 0;
} else {
return -1;
}
}
private int getPlusDistance(Player player) {
if (player.getEquipment() == null) return 0;
PlusMountsCard plusOne = player.getEquipment().getPlusOne();
if (plusOne == null) {
return 0;
} else {
return 1;
}
}

public Player getNextPlayer(Player player) {
int seat = (players.indexOf(player) + 1) % players.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.events.*;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.PlayCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MountsCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MinusMountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.List;
import java.util.Optional;

public class RedRabbitHouseBehavior extends Behavior {
public class MinusMountsBehavior extends Behavior {

public RedRabbitHouseBehavior(Game game, Player behaviorPlayer, List<String> reactionPlayers, Player currentReactionPlayer, String cardId, String playType, HandCard card) {
public MinusMountsBehavior(Game game, Player behaviorPlayer, List<String> reactionPlayers, Player currentReactionPlayer, String cardId, String playType, HandCard card) {
super(game, behaviorPlayer, reactionPlayers, currentReactionPlayer, cardId, playType, card, false, true);
}

@Override
public List<DomainEvent> askTargetPlayerPlayCard() {
playerPlayCard(behaviorPlayer, behaviorPlayer, cardId);
MountsCard mountsCard = behaviorPlayer.getEquipment().getMinusOne();
MinusMountsCard mountsCard = behaviorPlayer.getEquipment().getMinusOne();
String originEquipmentId = "";
if (mountsCard != null) {
originEquipmentId = mountsCard.getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.gaas.threeKingdoms.behavior.behavior;

import com.gaas.threeKingdoms.Game;
import com.gaas.threeKingdoms.Round;
import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.events.*;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.PlusMountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.List;

public class PlusMountsBehavior extends Behavior {
public PlusMountsBehavior(Game game, Player behaviorPlayer, List<String> reactionPlayers, Player currentReactionPlayer, String cardId, String playType, HandCard card) {
super(game, behaviorPlayer, reactionPlayers, currentReactionPlayer, cardId, playType, card, false, true);
}

@Override
public List<DomainEvent> askTargetPlayerPlayCard() {
playerPlayCard(behaviorPlayer, behaviorPlayer, cardId);
PlusMountsCard mountsCard = behaviorPlayer.getEquipment().getPlusOne();
String originEquipmentId = "";
if (mountsCard != null) {
originEquipmentId = mountsCard.getId();
}
card.effect(behaviorPlayer);

Round currentRound = game.getCurrentRound();
RoundEvent roundEvent = new RoundEvent(currentRound);
List<PlayerEvent> playerEvents = game.getPlayers().stream().map(PlayerEvent::new).toList();
return List.of(new PlayCardEvent(
"出牌",
behaviorPlayer.getId(),
behaviorPlayer.getId(),
cardId,
playType,
game.getGameId(),
playerEvents,
roundEvent,
game.getGamePhase().getPhaseName()),
new PlayEquipmentCardEvent(behaviorPlayer.getId(), cardId, originEquipmentId));
}

@Override
protected List<DomainEvent> doAcceptedTargetPlayerPlayCard(String playerId, String targetPlayerId, String cardId, String playType) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
import com.gaas.threeKingdoms.Game;
import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.behavior.PlayCardBehaviorHandler;
import com.gaas.threeKingdoms.behavior.behavior.PeachBehavior;
import com.gaas.threeKingdoms.behavior.behavior.RedRabbitHouseBehavior;
import com.gaas.threeKingdoms.behavior.behavior.MinusMountsBehavior;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.RedRabbitHorse;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MinusMountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

public class RedRabbitHouseBehaviorHandler extends PlayCardBehaviorHandler {
public RedRabbitHouseBehaviorHandler(PlayCardBehaviorHandler next, Game game) {
public class MinusMountsBehaviorHandler extends PlayCardBehaviorHandler {
public MinusMountsBehaviorHandler(PlayCardBehaviorHandler next, Game game) {
super(next, game);
}

@Override
protected boolean match(String playerId, String cardId, List<String> targetPlayerId, String playType) {
Player player = getPlayer(playerId);
Optional<HandCard> card = getCard(cardId, player);
return card.filter(handCard -> handCard instanceof RedRabbitHorse).isPresent();
return card.filter(handCard -> handCard instanceof MinusMountsCard).isPresent();
}

@Override
Expand All @@ -36,6 +35,6 @@ protected Behavior doHandle(String playerId, String cardId, List<String> reactio

HandCard card = player.getHand().getCard(cardId).orElseThrow(NoSuchElementException::new);

return new RedRabbitHouseBehavior(game, player, players, player, cardId, playType, card);
return new MinusMountsBehavior(game, player, players, player, cardId, playType, card);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.behavior.PlayCardBehaviorHandler;
import com.gaas.threeKingdoms.behavior.behavior.NormalActiveKillBehavior;
import com.gaas.threeKingdoms.exception.DistanceErrorException;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.basiccard.Kill;
import com.gaas.threeKingdoms.player.Player;
Expand Down Expand Up @@ -39,7 +40,7 @@ private boolean isPlayedValidCard(String cardId) {

private boolean isDistanceTooLong(Player player, Player targetPlayer) {
if (!game.isInAttackRange(player, targetPlayer)) {
throw new IllegalStateException("Players are not within range.");
throw new DistanceErrorException("Players are not within range.");
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.gaas.threeKingdoms.behavior.handler;

import com.gaas.threeKingdoms.Game;
import com.gaas.threeKingdoms.behavior.Behavior;
import com.gaas.threeKingdoms.behavior.PlayCardBehaviorHandler;
import com.gaas.threeKingdoms.behavior.behavior.PlusMountsBehavior;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.PlusMountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

public class PlusMountsBehaviorHandler extends PlayCardBehaviorHandler {
public PlusMountsBehaviorHandler(PlayCardBehaviorHandler next, Game game) {
super(next, game);
}

@Override
protected boolean match(String playerId, String cardId, List<String> targetPlayerId, String playType) {
Player player = getPlayer(playerId);
Optional<HandCard> card = getCard(cardId, player);
return card.filter(handCard -> handCard instanceof PlusMountsCard).isPresent();
}

@Override
protected Behavior doHandle(String playerId, String cardId, List<String> reactionPlayers, String playType) {
Player player = game.getPlayer(playerId);

List<String> players = game.getPlayers().stream()
.map(Player::getId)
.collect(Collectors.toList());

HandCard card = player.getHand().getCard(cardId).orElseThrow(NoSuchElementException::new);

return new PlusMountsBehavior(game, player, players, player, cardId, playType, card);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.gaas.threeKingdoms.events;

import com.gaas.threeKingdoms.handcard.equipmentcard.mountscard.MountsCard;
import com.gaas.threeKingdoms.player.Player;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gaas.threeKingdoms.exception;

public class DistanceErrorException extends RuntimeException {
public DistanceErrorException(String e){
super(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.gaas.threeKingdoms.events.PlayCardEvent;
import com.gaas.threeKingdoms.events.PlayerEvent;
import com.gaas.threeKingdoms.events.RoundEvent;
import com.gaas.threeKingdoms.exception.DistanceErrorException;
import com.gaas.threeKingdoms.handcard.HandCard;
import com.gaas.threeKingdoms.handcard.PlayType;
import com.gaas.threeKingdoms.player.Player;
Expand All @@ -25,7 +26,7 @@ public List<DomainEvent> playCard(String playerId, String cardId, String targetP
Player targetPlayer = game.getPlayer(targetPlayerId);

if (isDistanceTooLong(player, targetPlayer)) {
throw new IllegalStateException("Players are not within range.");
throw new DistanceErrorException("Players are not within range.");
}

// if (isSkip(playType)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gaas.threeKingdoms.handcard.equipmentcard.mountscard;

import com.gaas.threeKingdoms.handcard.PlayCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.EquipmentCard;
import com.gaas.threeKingdoms.player.Player;

public class MinusMountsCard extends EquipmentCard {
public MinusMountsCard(PlayCard playCard) {
super(playCard);
}

@Override
public void effect(Player player) {
player.getEquipment().setMinusOne(this);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gaas.threeKingdoms.handcard.equipmentcard.mountscard;

import com.gaas.threeKingdoms.handcard.equipmentcard.EquipmentCard;
import com.gaas.threeKingdoms.handcard.PlayCard;
import com.gaas.threeKingdoms.player.Player;

public class PlusMountsCard extends EquipmentCard {

public PlusMountsCard(PlayCard playCard) {
super(playCard);
}
@Override
public void effect(Player player) {
player.getEquipment().setPlusOne(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
/**
* 赤兔馬
*/
public class RedRabbitHorse extends MountsCard {
public class RedRabbitHorse extends MinusMountsCard {

public RedRabbitHorse(PlayCard playCard) {
super(playCard);
}

@Override
public void effect(Player player) {
player.getEquipment().setMinusOne(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gaas.threeKingdoms.handcard.equipmentcard.mountscard;

import com.gaas.threeKingdoms.handcard.PlayCard;

public class ShadowHorse extends PlusMountsCard {


public ShadowHorse(PlayCard playCard) {
super(playCard);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import com.gaas.threeKingdoms.handcard.PlayCard;
import com.gaas.threeKingdoms.handcard.equipmentcard.EquipmentCard;

import com.gaas.threeKingdoms.player.Player;
public abstract class WeaponCard extends EquipmentCard {

public WeaponCard(PlayCard playCard) {
super(playCard);
}

@Override
public void effect(Player player) {
player.getEquipment().setWeapon(this);
}

}
Loading

0 comments on commit bd4af84

Please sign in to comment.