Exemplo n.º 1
0
 public void addBlankStep(Step s) {
   if (theSteps.size() > 0) {
     Step lastStep = theSteps.get(theSteps.size() - 1);
     s.setStartLocation(lastStep.getEndLocation());
     theSteps.add(s);
   } else {
     theSteps.add(s);
     startingLocation = s.getStartLocation(); // first step
     endingLocation = s.getEndLocation();
   }
 }
Exemplo n.º 2
0
  public Move(StringBuffer s, Board bd) {
    // strings are of the form Move:<color>:<row-col>:<row-col>^+
    // <row-col> is of the form (<row>:<col>)
    // System.err.println("Move::constructorFromString: with "+s);
    // do not init, get start position, and copy rank this.rankAtStart = Piece.Rank.PAWN;
    // do not init, get start position, and copy rankthis.rankAtEnd = Piece.Rank.PAWN;
    theSteps = new java.util.ArrayList<Step>();
    int nchars = s.length();
    char[] theChars = s.toString().toCharArray();
    char theChar;
    int howManyColons = 0;
    int theRow = -1;
    int theColumn = -1;
    whoseTurn = Move.Side.BLACK;
    StringBuffer assembledNumber_sb = new StringBuffer("");
    int stepIndex = -1;
    for (int i = 3; i < nchars; i++) {
      theChar = theChars[i];
      switch (theChar) {
        case 'M':
        case 'o':
        case 'v':
        case 'e':
          break;
        case ':':
          howManyColons++;
          if (howManyColons == 2) {
            Step step = new Step();
            stepIndex++;
            addStep(step);
          } else if ((howManyColons == 3)) { // have a row, and it is only a starting point
            int assembledNumber_int = -1;

            try {
              Integer assembledNumber_Int = Integer.valueOf(assembledNumber_sb.toString());
              assembledNumber_int = assembledNumber_Int.shortValue();
            } catch (Exception ex) {
              System.err.println(
                  "Move::constructor(StringBuffer)excptn: number assembly " + assembledNumber_sb);
            }
            theRow = assembledNumber_int;
            int terminalRow = 0;
            switch (whoseTurn) {
              case WHITE:
                terminalRow = 7;
                break;
              case BLACK:
                terminalRow = 0;
            }
            if (theRow == terminalRow) { // promote, ok for incoming moves
              becomeKing = true;
              rankAtEnd = Piece.Rank.KING;
              rankAtStart = Piece.Rank.KING;
            }
            // need to clear out assembledNumber_sb
            assembledNumber_sb.replace(0, assembledNumber_sb.length(), "");
            // stepStart = false;
            // System.err.println("Move::the assembled"+assembledNumber_sb);
          } // end of  colon==3 handling
          else if (howManyColons > 4
              && howManyColons % 2 == 1) { // between row, column, so have a row, and it is an end,
            // but could be a beginning if a later colon
            int assembledNumber_int = -1;

            try {
              Integer assembledNumber_Int = Integer.valueOf(assembledNumber_sb.toString());
              assembledNumber_int = assembledNumber_Int.shortValue();
            } catch (Exception ex) {
              System.err.println(
                  "Move::constructor(String)excptn: number assembly " + assembledNumber_sb);
            }
            theRow = assembledNumber_int;
            // need to clear out assembledNumber_sb
            assembledNumber_sb.replace(0, assembledNumber_sb.length(), "");

            // stepStart = false;
          } // end of odd colon handling, state is: finished collecting a row
          else if (howManyColons > 5
              && howManyColons % 2
                  == 0) { // a new step, start is previous (r:c), ends is coming(r:c)
            // stepStart = true;
            Step step = new Step();
            stepIndex++;
            // System.err.println("Move::constructor with StringBuffer stepindex = "+stepIndex);
            // need the correct starting values or addStep will refuse
            addBlankStep(step);
            step.setStartLocation(sam_loc); // start is previous (r:c)
          } // end of even colon handling, state is: collecting a new step
          break;
        case 'W':
          whoseTurn = Move.Side.WHITE;
          break;
        case 'h':
        case 'i':
        case 't': /*case'e':*/
        case 'B':
        case 'l':
        case 'a':
        case 'c':
        case 'k':
          break;
        case '(':
          // state is, beginning to collect a row
          if (assembledNumber_sb.length() == 0) {;
          } else {
            assembledNumber_sb.replace(0, assembledNumber_sb.length() - 1, "");
          } // clear out the assembled number
          break;
        case ')':
          // state is: end of (row:column), first one goes in start location of move, and first step
          int assembledNumber_int = -1;
          try {
            Integer assembledNumber_Int = Integer.valueOf(assembledNumber_sb.toString());
            assembledNumber_int = assembledNumber_Int.shortValue();
            // System.out.println("Move::constructor(String): number assembly
            // "+assembledNumber_int);
          } catch (Exception ex) {
            System.err.println("Move::constructor(String): number assembly " + assembledNumber_sb);
          }
          // now having row and column
          theColumn = assembledNumber_int;
          assembledNumber_sb.replace(0, assembledNumber_sb.length(), "");
          // System.err.println("Move::constructor(String): with row " +theRow+" and col
          // "+theColumn);
          sam_loc = samloc[theRow][theColumn];
          // do the shifts so that can look in existing kings, and set rank accordingly
          if (howManyColons == 3) {
            long where = 1L << sam_loc;
            int x = 0;
            if (bd.moveCounter > 59) {
              x++;
            }
            int y = x;
            long myBAB = bd.getBAb();
            long myFAW = bd.getFAw();
            long amIKing = where & (myBAB | myFAW);
            if (amIKing != 0) {
              rankAtStart = Piece.Rank.KING;
              rankAtEnd = Piece.Rank.KING;
            } // ok for incoming moves
            else {
              rankAtStart = Piece.Rank.PAWN;
              rankAtEnd = Piece.Rank.PAWN; /*System.err.println("Move::setting to pawn");*/
            }
          }
          // convert the row and column into sam notation
          if (stepIndex == 0
              && startNotWritten) { // start of move only gets written once, start of step once
            // here, later in some :'s
            getStep(0).setStartLocation(sam_loc);
            startingLocation = sam_loc;
            startNotWritten = false;
          }
          getStep(stepIndex).setEndLocation(sam_loc); // the end of step 0
          // if all characters are used up, then we have a move.
          if (i == (nchars - 1)) {
            // System.err.println("Move::constructorFromString: with move "+this.toString());
            this.endingLocation = sam_loc;
          }
          // state is, we are expecting a :, because we have not had last character
          break;
        default:
          // it's a digit, so assemble digits into numbers
          assembledNumber_sb.append(theChar);
      }
    } // loop on characters
  }