📄 gameturn.java
字号:
super( playerId ); this.mask = classMask; } /** * Determine if the given entity is a valid one to use for this turn. * * @param entity the <code>Entity</code> being tested for the move. * @param game the <code>IGame</code> the entity belongs to * @return <code>true</code> if the entity can be moved. */ public boolean isValidEntity( Entity entity, IGame game ) { // The entity must be in the mask, and pass // the requirements of the parent class. return (GameTurn.getClassCode(entity) & this.mask) != 0 && super.isValidEntity(entity, game); } /** * Determine if entities of the given class get to move. * * @param classCode the <code>int</code> class code being tested * @return <code>true</code> if entities of that class can move. */ public boolean isValidClass( int classCode ) { return (classCode & this.mask) != 0; } } /** * A type of game turn that indicates that one or more players should * be given the opportunity to unload entities that are stranded on * immobile transports. Each player declares which stranded units they * will unload at the beginning of the movement phase, without being * told what stranded units their opponent(s) are unloading. * <p/> * According to <a href="http://www.classicbattletech.com/w3t/showflat.php?Cat=&Board=ask&Number=555466&page=2&view=collapsed&sb=5&o=0&fpart="> * Randall Bills</a>, the "minimum move" rule allow stranded units to * dismount at the start of the turn. */ public static class UnloadStrandedTurn extends GameTurn { private int[] entityIds = null; /** * Any player that owns an entity whose ID is in the passed array * should be given a chance to unload it. * * @param ids the array of <code>int</code> IDs of stranded entities. * This value must not be <code>null</code> or empty. * @exception <code>IllegalArgumentException</code> if a * <code>null</code> or empty value is passed for ids. */ public UnloadStrandedTurn( int[] ids ) { super( Player.PLAYER_NONE ); // Validate input. if ( null == ids ) { throw new IllegalArgumentException ( "the passed array of ids is null" ); } if ( 0 == ids.length ) { throw new IllegalArgumentException ( "the passed array of ids is empty" ); } // Create a copy of the array to prevent any post-call shenanigans. this.entityIds = new int[ids.length]; System.arraycopy( ids, 0, this.entityIds, 0, ids.length ); } /** * Any player that owns an entity in the passed enumeration * should be given a chance to unload it. * * @param entities the <code>Enumeration</code> of stranded entities. * This value must not be <code>null</code> or empty. * @exception <code>IllegalArgumentException</code> if a * <code>null</code> or empty value is passed for entities. */ public UnloadStrandedTurn( Enumeration entities ) { super( Player.PLAYER_NONE ); // Validate input. if ( null == entities ) { throw new IllegalArgumentException ( "the passed enumeration of entities is null" ); } if ( !entities.hasMoreElements() ) { throw new IllegalArgumentException ( "the passed enumeration of entities is empty" ); } // Get the first entity. Entity entity = (Entity) entities.nextElement(); // Do we need to get more entities? if ( entities.hasMoreElements() ) { // It's a bit of a hack, but get the Game from the first // entity, and create a temporary array that can hold the // IDs of every entity in the game. int[] ids = new int[ entity.game.getNoOfEntities() ]; int length = 0; // Store the first entity's ID. ids[length++] = entity.getId(); // Walk the list of remaining stranded entities. while ( entities.hasMoreElements() ) { ids[length++] = ( (Entity) entities.nextElement() ).getId(); } // Create an array that just holds the stranded entity ids. this.entityIds = new int[length]; System.arraycopy( ids, 0, this.entityIds, 0, length ); } // End have-more-stranded-entities else { // There was only one stranded entity. this.entityIds = new int[1]; this.entityIds[0] = entity.getId(); } } /** * Determine if the given entity is a valid one to use for this turn. * * @param entity the <code>Entity</code> being tested for the move. * @param game the <code>IGame</code> the entity belongs to * @return <code>true</code> if the entity can be moved. */ public boolean isValidEntity( Entity entity, IGame game ) { boolean retVal = false; // Null entities don't need to be checked. if ( null != entity ) { // Any entity in the array is valid. // N.B. Stop looking after we've found the match. final int entityId = entity.getId(); for ( int index = 0; index < this.entityIds.length && !retVal; index++ ) { if ( entityId == this.entityIds[index] ) { retVal = true; } } } // End entity-isn't-null return retVal; } /** * Returns true if the player and entity are both valid. */ public boolean isValid(int playerId, Entity entity, IGame game) { return ( null != entity && entity.getOwnerId() == playerId && isValidEntity(entity, game) ); } /** * Returns true if the player is valid. */ public boolean isValid(int playerId, IGame game) { boolean retVal = false; for ( int index = 0; index < this.entityIds.length && !retVal; index++ ) { if ( game.getEntity( this.entityIds[index] ) != null && playerId == game.getEntity( this.entityIds[index] ).getOwnerId() ) { retVal = true; } } return retVal; } public String toString() { return getClass().getName() + ", entity IDs: [" + entityIds + "]"; } public int[] getEntityIds() { return this.entityIds; } } /** * A type of game turn that allows only entities * belonging to certain units to move. */ public static class UnitNumberTurn extends GameTurn { private final char unitNumber; /** * Only allow entities for the given player which have types in * the class mask to move. * * @param playerId the <code>int</code> ID of the player * @param unit the <code>int</code> unit number of the entities * allowed to move. */ public UnitNumberTurn( int playerId, char unit ) { super( playerId ); this.unitNumber = unit; } /** * Returns true if the specified entity is a valid one to use for * this turn. */ public boolean isValidEntity(Entity entity, IGame game) { return ( super.isValidEntity( entity, game ) && this.unitNumber == entity.getUnitNumber() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -