Fixed some bugs and added a AI Test player class.

This commit is contained in:
LevelX2 2015-06-08 18:31:54 +02:00
parent 9da44a547d
commit 3b61a10237
25 changed files with 1732 additions and 388 deletions

View file

@ -0,0 +1,54 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package org.mage.test.AI.basic;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBaseAI;
/**
*
* @author LevelX2
*/
public class CastCreaturesTest extends CardTestPlayerBaseAI {
/**
* Tests that the creature is cast if enough mana is available
*/
@Test
public void testSimpleCast() {
addCard(Zone.HAND, playerA, "Silvercoat Lion");
addCard(Zone.BATTLEFIELD, playerA, "Plains", 2);
setStopAt(1, PhaseStep.BEGIN_COMBAT);
execute();
assertPermanentCount(playerA, "Silvercoat Lion", 1);
}
}

View file

@ -91,7 +91,7 @@ public class FadingTest extends CardTestPlayerBase {
public void testFadesAway() {
addCard(Zone.BATTLEFIELD, playerA, "Forest", 4);
addCard(Zone.HAND, playerA, "Blastoderm");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Blastoderm");
setStopAt(9, PhaseStep.BEGIN_COMBAT);

View file

@ -41,12 +41,16 @@ public class SewerNemesisTest extends CardTestPlayerBase {
/**
* Sewer Nemesis count's all cards in each player's graveyard to determine it's * / *, not just the cosen player's graveyard.
* BUG: Sewer Nemesis count's all cards in each player's graveyard to determine it's * / *, not just the chosen player's graveyard.
*
*/
@Test
public void test1() {
addCard(Zone.BATTLEFIELD, playerA, "Swamp", 4);
// As Sewer Nemesis enters the battlefield, choose a player.
// Sewer Nemesis's power and toughness are each equal to the number of cards in the chosen player's graveyard.
// Whenever the chosen player casts a spell, that player puts the top card of his or her library into his or her graveyard.
addCard(Zone.HAND, playerA, "Sewer Nemesis");
addCard(Zone.GRAVEYARD, playerA, "Raging Goblin",4);

View file

@ -18,18 +18,25 @@ public class SigardaHostOfHeronsTest extends CardTestPlayerBase {
*/
@Test
public void testCard() {
// Spells and abilities your opponents control can't cause you to sacrifice permanents.
addCard(Zone.BATTLEFIELD, playerA, "Sigarda, Host of Herons");
// {T}, Tap two untapped Humans you control: Exile target artifact or enchantment.
addCard(Zone.BATTLEFIELD, playerA, "Devout Chaplain");
// {2}{B}, Sacrifice a creature: Target opponent reveals his or her hand. You choose a card from it. That player discards that card. Activate this ability only any time you could cast a sorcery.
addCard(Zone.BATTLEFIELD, playerA, "Corpse Traders");
// Target player sacrifices a creature.
addCard(Zone.HAND, playerA, "Diabolic Edict");
addCard(Zone.BATTLEFIELD, playerA, "Swamp", 2);
addCard(Zone.HAND, playerB, "Diabolic Edict");
addCard(Zone.BATTLEFIELD, playerB, "Swamp", 2);
// At the beginning of your upkeep, return target creature card from your graveyard to the battlefield.
// At the beginning of each opponent's upkeep, that player sacrifices a creature.
addCard(Zone.BATTLEFIELD, playerB, "Sheoldred, Whispering One");
castSpell(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Diabolic Edict", playerA);
castSpell(3, PhaseStep.PRECOMBAT_MAIN, playerA, "Diabolic Edict", playerB);
castSpell(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Diabolic Edict", playerA); // sacrificing for player A prevented by Sigarda
castSpell(3, PhaseStep.PRECOMBAT_MAIN, playerA, "Diabolic Edict", playerB); // playerB has to sacrifice Sheldred
setStopAt(3, PhaseStep.END_TURN);
execute();
@ -40,9 +47,13 @@ public class SigardaHostOfHeronsTest extends CardTestPlayerBase {
assertPermanentCount(playerA, "Sigarda, Host of Herons", 1);
assertPermanentCount(playerA, "Devout Chaplain", 1);
assertPermanentCount(playerA, "Corpse Traders", 1);
assertGraveyardCount(playerA, "Diabolic Edict", 1);
assertGraveyardCount(playerA, 1);
assertPermanentCount(playerB, "Sheoldred, Whispering One", 0);
assertHandCount(playerB, "Diabolic Edict", 0);
assertGraveyardCount(playerB, "Sheoldred, Whispering One", 1);
assertGraveyardCount(playerB, "Diabolic Edict", 1);
assertGraveyardCount(playerB, 2);
}

View file

@ -102,16 +102,18 @@ public class RandomPlayer extends ComputerPlayer {
List<Ability> playables = getPlayableAbilities(game);
Ability ability;
while (true) {
if (playables.size() == 1)
if (playables.size() == 1) {
ability = playables.get(0);
else
} else {
ability = playables.get(rnd.nextInt(playables.size()));
}
List<Ability> options = getPlayableOptions(ability, game);
if (!options.isEmpty()) {
if (options.size() == 1)
if (options.size() == 1) {
ability = options.get(0);
else
} else {
ability = options.get(rnd.nextInt(options.size()));
}
}
if (ability.getManaCosts().getVariableCosts().size() > 0) {
int amount = getAvailableManaProducers(game).size() - ability.getManaCosts().convertedManaCost();
@ -154,10 +156,11 @@ public class RandomPlayer extends ComputerPlayer {
ability = source;
}
else {
if (options.size() == 1)
if (options.size() == 1) {
ability = options.get(0);
else
} else {
ability = options.get(rnd.nextInt(options.size()));
}
}
if (ability.isUsesStack()) {
game.getStack().push(new StackAbility(ability, playerId));
@ -203,15 +206,18 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public void selectBlockers(Game game, UUID defendingPlayerId) {
int numGroups = game.getCombat().getGroups().size();
if (numGroups == 0) return;
if (numGroups == 0) {
return;
}
List<Permanent> blockers = getAvailableBlockers(game);
for (Permanent blocker: blockers) {
int check = rnd.nextInt(numGroups + 1);
if (check < numGroups) {
CombatGroup group = game.getCombat().getGroups().get(check);
if (group.getAttackers().size() > 0)
if (group.getAttackers().size() > 0) {
this.declareBlocker(this.getId(), blocker.getId(), group.getAttackers().get(0), game);
}
}
}
actionCount++;
@ -243,8 +249,9 @@ public class RandomPlayer extends ComputerPlayer {
protected boolean chooseRandomTarget(Target target, Ability source, Game game) {
Set<UUID> possibleTargets = target.possibleTargets(source==null?null:source.getSourceId(), playerId, game);
if (possibleTargets.isEmpty())
if (possibleTargets.isEmpty()) {
return false;
}
if (!target.isRequired(source)) {
if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
return false;
@ -300,8 +307,9 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public boolean chooseTarget(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) {
if (cards.isEmpty())
if (cards.isEmpty()) {
return !target.isRequired(source);
}
Card card = cards.getRandom(game);
target.addTarget(card.getId(), source, game);
return true;
@ -373,8 +381,9 @@ public class RandomPlayer extends ComputerPlayer {
public Mode chooseMode(Modes modes, Ability source, Game game) {
Iterator<Mode> it = modes.values().iterator();
Mode mode = it.next();
if (modes.size() == 1)
if (modes.size() == 1) {
return mode;
}
int modeNum = rnd.nextInt(modes.values().size());
for (int i = 0; i < modeNum; i++) {
mode = it.next();

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,14 @@
package org.mage.test.serverside.base;
import mage.constants.PhaseStep;
import java.util.List;
import mage.abilities.Ability;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import mage.filter.Filter;
import mage.players.Player;
import org.mage.test.player.TestPlayer;
import java.util.List;
/**
* Interface for all test initialization and assertion operations.
@ -81,11 +82,14 @@ public interface CardTestAPI {
/**
* Define turn number to stop the game on.
* @param turn
*/
void setStopOnTurn(int turn);
/**
* Define the turn number and step to stop the game on.
* @param turn
* @param step
*/
void setStopAt(int turn, PhaseStep step);

View file

@ -0,0 +1,69 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package org.mage.test.serverside.base;
import java.io.FileNotFoundException;
import mage.constants.MultiplayerAttackOption;
import mage.constants.RangeOfInfluence;
import mage.game.Game;
import mage.game.GameException;
import mage.game.TwoPlayerDuel;
import mage.player.ai.ComputerPlayer7;
import org.mage.test.player.TestPlayer;
import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl;
/**
*
* @author LevelX2
*/
public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl {
int skill = 9;
@Override
protected Game createNewGameAndPlayers() throws GameException, FileNotFoundException {
Game game = new TwoPlayerDuel(MultiplayerAttackOption.LEFT, RangeOfInfluence.ONE, 0, 20);
playerA = createPlayer(game, playerA, "PlayerA");
playerB = createPlayer(game, playerB, "PlayerB");
return game;
}
@Override
protected TestPlayer createPlayer(String name) {
if (name.equals("PlayerA")) {
return new TestPlayer(new ComputerPlayer7("PlayerA", RangeOfInfluence.ONE, skill));
}
return super.createPlayer(name);
}
public void setAISkill(int skill) {
this.skill = skill;
}
}

View file

@ -31,7 +31,6 @@ import java.io.FilenameFilter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.mage.test.serverside.base.MageTestPlayerBase.currentGame;
/**
* Base class for all tests.

View file

@ -28,6 +28,7 @@ import java.io.FilenameFilter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mage.player.ai.ComputerPlayer;
/**
* Base class for all tests.
@ -260,14 +261,15 @@ public abstract class MageTestPlayerBase {
}
private TestPlayer getPlayer(String name) {
if (name.equals("ComputerA")) {
return playerA;
} else if (name.equals("ComputerB")) {
return playerB;
} else if (name.equals("ComputerC")) {
return playerC;
} else if (name.equals("ComputerD")) {
return playerD;
switch (name) {
case "ComputerA":
return playerA;
case "ComputerB":
return playerB;
case "ComputerC":
return playerC;
case "ComputerD":
return playerD;
}
throw new IllegalArgumentException("Couldn't find player for name=" + name);
}
@ -339,6 +341,7 @@ public abstract class MageTestPlayerBase {
}
protected TestPlayer createPlayer(String name) {
return new TestPlayer(name, RangeOfInfluence.ONE);
return new TestPlayer(new ComputerPlayer(name, RangeOfInfluence.ONE));
}
}

View file

@ -28,6 +28,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
/**
* Default game initialization params for red player (that plays with Mountains)
*/
@Override
public void useRedDefault() {
// *** ComputerA ***
// battlefield:ComputerA:Mountain:5
@ -88,6 +89,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param player {@link Player} to add cards for. Use either playerA or playerB.
* @param cardName Card name in string format.
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName) {
addCard(gameZone, player, cardName, 1, false);
}
@ -100,6 +102,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param cardName Card name in string format.
* @param count Amount of cards to be added.
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName, int count) {
addCard(gameZone, player, cardName, count, false);
}
@ -114,6 +117,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param tapped In case gameZone is Battlefield, determines whether permanent should be tapped.
* In case gameZone is other than Battlefield, {@link IllegalArgumentException} is thrown
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName, int count, boolean tapped) {
@ -179,6 +183,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param player {@link Player} to set life count for.
* @param life Life count to set.
*/
@Override
public void setLife(TestPlayer player, int life) {
if (player.equals(playerA)) {
commandsA.put(Zone.OUTSIDE, "life:" + String.valueOf(life));
@ -190,16 +195,18 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
/**
* Define turn number to stop the game on.
*/
@Override
public void setStopOnTurn(int turn) {
stopOnTurn = turn == -1 ? null : Integer.valueOf(turn);
stopOnTurn = turn == -1 ? null : turn;
stopAtStep = PhaseStep.UNTAP;
}
/**
* Define turn number and step to stop the game on.
*/
@Override
public void setStopAt(int turn, PhaseStep step) {
stopOnTurn = turn == -1 ? null : Integer.valueOf(turn);
stopOnTurn = turn == -1 ? null : turn;
stopAtStep = step;
}
@ -208,6 +215,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
*
* @param turn Expected turn number to compare with. 1-based.
*/
@Override
public void assertTurn(int turn) throws AssertionError {
Assert.assertEquals("Turn numbers are not equal", turn, currentGame.getTurnNum());
}
@ -217,21 +225,28 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
*
* @param result Expected {@link GameResult} to compare with.
*/
@Override
public void assertResult(Player player, GameResult result) throws AssertionError {
if (player.equals(playerA)) {
GameResult actual = CardTestAPI.GameResult.DRAW;
if (currentGame.getWinner().equals("Player PlayerA is the winner")) {
actual = CardTestAPI.GameResult.WON;
} else if (currentGame.getWinner().equals("Player PlayerB is the winner")) {
actual = CardTestAPI.GameResult.LOST;
switch (currentGame.getWinner()) {
case "Player PlayerA is the winner":
actual = CardTestAPI.GameResult.WON;
break;
case "Player PlayerB is the winner":
actual = CardTestAPI.GameResult.LOST;
break;
}
Assert.assertEquals("Game results are not equal", result, actual);
} else if (player.equals(playerB)) {
GameResult actual = CardTestAPI.GameResult.DRAW;
if (currentGame.getWinner().equals("Player PlayerB is the winner")) {
actual = CardTestAPI.GameResult.WON;
} else if (currentGame.getWinner().equals("Player PlayerA is the winner")) {
actual = CardTestAPI.GameResult.LOST;
switch (currentGame.getWinner()) {
case "Player PlayerB is the winner":
actual = CardTestAPI.GameResult.WON;
break;
case "Player PlayerA is the winner":
actual = CardTestAPI.GameResult.LOST;
break;
}
Assert.assertEquals("Game results are not equal", result, actual);
}
@ -243,6 +258,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param player {@link Player} to get life for comparison.
* @param life Expected player's life to compare with.
*/
@Override
public void assertLife(Player player, int life) throws AssertionError {
int actual = currentGame.getPlayer(player.getId()).getLife();
Assert.assertEquals("Life amounts are not equal", life, actual);
@ -265,6 +281,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param scope {@link mage.filter.Filter.ComparisonScope} Use ANY, if you want "at least one creature with given name should have specified p\t"
* Use ALL, if you want "all creature with gived name should have specified p\t"
*/
@Override
public void assertPowerToughness(Player player, String cardName, int power, int toughness, Filter.ComparisonScope scope)
throws AssertionError {
int count = 0;
@ -298,6 +315,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
/**
* {@inheritDoc}
*/
@Override
public void assertAbilities(Player player, String cardName, List<Ability> abilities)
throws AssertionError {
int count = 0;
@ -326,6 +344,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param player {@link Player} which permanents should be counted.
* @param count Expected count.
*/
@Override
public void assertPermanentCount(Player player, int count) throws AssertionError {
int actualCount = 0;
for (Permanent permanent : currentGame.getBattlefield().getAllPermanents()) {
@ -343,6 +362,7 @@ public abstract class CardTestAPIImpl extends MageTestBase implements CardTestAP
* @param cardName Name of the cards that should be counted.
* @param count Expected count.
*/
@Override
public void assertPermanentCount(Player player, String cardName, int count) throws AssertionError {
int actualCount = 0;
for (Permanent permanent : currentGame.getBattlefield().getAllPermanents()) {

View file

@ -1,6 +1,8 @@
package org.mage.test.serverside.base.impl;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.UUID;
import mage.abilities.Ability;
import mage.cards.Card;
import mage.cards.decks.Deck;
@ -18,20 +20,18 @@ import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.ExileZone;
import mage.game.Game;
import mage.game.GameException;
import mage.game.GameOptions;
import mage.game.command.CommandObject;
import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard;
import mage.players.Player;
import org.junit.Assert;
import org.junit.Before;
import org.mage.test.player.TestPlayer;
import org.mage.test.serverside.base.CardTestAPI;
import org.mage.test.serverside.base.CardTestAPI.GameResult;
import org.mage.test.serverside.base.MageTestPlayerBase;
import java.util.List;
import java.util.UUID;
import mage.game.GameOptions;
import org.junit.Before;
/**
* API for test initialization and asserting the test results.
*
@ -213,7 +213,6 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
*
* @param player {@link Player} to remove all library cards from.
*/
@Override
public void removeAllCardsFromLibrary(TestPlayer player) {
getCommands(player).put(Zone.LIBRARY, "clear");
}
@ -235,7 +234,6 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
* @param player {@link Player} to add cards for. Use either playerA or playerB.
* @param cardName Card name in string format.
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName) {
addCard(gameZone, player, cardName, 1, false);
}
@ -248,7 +246,6 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
* @param cardName Card name in string format.
* @param count Amount of cards to be added.
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName, int count) {
addCard(gameZone, player, cardName, count, false);
}
@ -263,7 +260,6 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
* @param tapped In case gameZone is Battlefield, determines whether permanent should be tapped.
* In case gameZone is other than Battlefield, {@link IllegalArgumentException} is thrown
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName, int count, boolean tapped) {
if (gameZone.equals(Zone.BATTLEFIELD)) {
@ -318,7 +314,6 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
* @param player {@link Player} to set life count for.
* @param life Life count to set.
*/
@Override
public void setLife(TestPlayer player, int life) {
getCommands(player).put(Zone.OUTSIDE, "life:" + String.valueOf(life));
}