예제 #1
0
 private void getChildren(boolean setStringMoves) {
   dbh.getCharArray(oldPosition);
   int counter = 0;
   // looks at every spot on the board
   for (int row = 0; row < height; row++) {
     for (int col = 0; col < width; col++) {
       // only a valid child if there is an empty space there
       if (board[row][col].getPiece() == ' ') {
         Cell place = board[row][col];
         Cell[] neighbors = place.getNeighbors();
         // looks at each spot around the given spot
         for (int index = 0; index < 8; index++) {
           // makes sure the spot is within bounds, then checks if
           // there is an opposing piece next to it
           Cell neighbor = neighbors[index];
           if (neighbor != null
               && neighbor.getPiece() == pieces[opposite(turn)]
               && isFlippable(place.boardNum, index, false, null)) {
             if (setStringMoves) {
               stringMoves[counter] = (char) (col + 'A') + Integer.toString(row + 1);
             }
             System.arraycopy(oldPosition, 0, tempPosition, 0, boardSize);
             isFlippable(place.boardNum, index, true, tempPosition);
             int newWhitePieces = count(tempPosition);
             int nextTier = getTier() + 1;
             children[counter].tier = nextTier;
             children[counter].hash =
                 offsetTable[nextTier][opposite(turn)][newWhitePieces]
                     + dbh.setNumsAndHash(tempPosition);
             counter++;
             dbh.setNumsAndHash(oldPosition);
             break;
           }
         }
       }
     }
   }
   if (setStringMoves && counter == 0) {
     turn = opposite(turn);
     getChildren(false);
     if (numChildren > 0) {
       stringMoves[0] = "pass";
       children[0].tier = getTier();
       children[0].hash = getHash();
       counter = 1;
     }
     turn = opposite(turn);
   }
   numChildren = counter;
   isChildrenValid = true;
 }
예제 #2
0
 private boolean isFlippable(int boardNumber, int direction, boolean flip, char[] tempPosition) {
   int addRow = 0;
   int addCol = 0;
   switch (direction) {
     case 0:
       addRow = -1;
       addCol = -1;
       break;
     case 1:
       addRow = -1;
       addCol = 0;
       break;
     case 2:
       addRow = -1;
       addCol = 1;
       break;
     case 3:
       addRow = 0;
       addCol = -1;
       break;
     case 4:
       addRow = 0;
       addCol = 1;
       break;
     case 5:
       addRow = 1;
       addCol = -1;
       break;
     case 6:
       addRow = 1;
       addCol = 0;
       break;
     case 7:
       addRow = 1;
       addCol = 1;
       break;
     default:
       throw new Error("Bad direction");
   }
   int row = boardNumber / width;
   int col = boardNumber % height;
   if (flip) {
     tempPosition[row * width + col] = (turn == BLACK ? 'X' : 'O');
   }
   char piece = 'P';
   row += addRow;
   col += addCol;
   int pieceCounter = 0;
   while (col >= 0 && col < width && row >= 0 && row < height) {
     piece = (flip ? tempPosition[row * width + col] : board[row][col].getPiece());
     if (piece == ' ' || (pieceCounter == 0 && piece == (turn == BLACK ? 'X' : 'O'))) break;
     else {
       if (flip) tempPosition[row * width + col] = pieces[turn];
       if (pieceCounter > 0 && piece == (turn == BLACK ? 'X' : 'O')) {
         if (flip) {
           for (int direct = 0; direct < 8; direct++) {
             if (direct != direction && isFlippable(boardNumber, direct, false, null))
               isFlippable(boardNumber, direct, true, tempPosition);
           }
         }
         return true;
       }
       row += addRow;
       col += addCol;
       if (piece == (turn == BLACK ? 'O' : 'X')) pieceCounter++;
     }
   }
   return false;
 }