/* ShogiMove.java a shogi move 97-feb-26 ped created */ import java.util.* ; public class ShogiMove implements Enum_ShogiPieces { // allow package access to fields int fromfile ; // minus fromfile = piece to drop int fromrank ; int tofile ; int torank ; // torank >=10 = promotion private String comment ; private String moveString ; ShogiMove( int fromfile, int fromrank, int tofile, int torank ) { this.fromfile = fromfile ; this.fromrank = fromrank ; this.tofile = tofile ; this.torank = torank ; } ShogiMove( String str, ShogiBoard board ) { moveString = str ; boolean senteToMove = board.isSenteToMove( ) ; char pieceSym = str.charAt( 0 ) ; boolean promotedPiece = false ; if( pieceSym == '+' ) { pieceSym = str.charAt( 1 ) ; promotedPiece = true ; str = str.substring( 2 ) ; } else str = str.substring( 1 ) ; char pieceNames [ ] = { 'P', 'L', 'N', 'S', 'G', 'B', 'R', 'K' } ; int pieces [ ] = { SP, SL, SN, SS, SG, SB, SR, SK } ; int piece = EM ; for( int i = 0 ; i < 8 ; i ++ ) { if( pieceSym == pieceNames[ i ] ) { piece = pieces[ i ] ; if( ! board.isSenteToMove( ) ) piece += 16 ; if( promotedPiece ) piece += 8 ; } } // assert( piece != EM ) boolean promotion = str.endsWith( "+" ) ; if( promotion || str.endsWith( "=" ) ) str = str.substring( 0, str.length( ) - 1 ) ; fromfile = -1 ; fromrank = -1 ; boolean drop = false ; while( str.length( ) > 2 ) { char disambiguator = str.charAt( 0 ) ; if( disambiguator == '*' ) drop = true ; else if( disambiguator != 'x' || disambiguator != '-' ) { if( Character.isDigit( disambiguator ) ) fromfile = '9' - disambiguator ; else fromrank = 'i' - disambiguator ; } str = str.substring( 1 ) ; } torank = 'i' - str.charAt( 1 ) ; tofile = '9' - str.charAt( 0 ) ; if( ! drop ) { Vector moves = board.generateLegalMoves( piece ) ; for( int i = 0 ; i < moves.size( ) ; i ++ ) { ShogiMove move = ( ShogiMove ) moves.elementAt( i ) ; // debug // System.out.println( i + " " + move.fromfile + " " + move.fromrank // + " " + move.tofile + " " + move.torank ) ; // debug if( tofile == move.tofile && torank == move.torank && ( fromfile == -1 || fromfile == move.fromfile ) && ( fromrank == -1 || fromrank == move.fromrank ) ) { fromfile = move.fromfile ; fromrank = move.fromrank ; break ; } } } if( fromfile == -1 || fromrank == -1 ) // failed to find move on list drop = true ; if( drop ) fromfile = - piece ; else if( promotion ) torank = torank + 10 ; // debug // System.out.println( moveString + " " + fromfile + " " + fromrank + " " + tofile + " " + torank ) ; } public void putComment( String str ) { comment = str ; } public String getComment( ) { return comment ; } public String getString( ) { return moveString ; } }