Пример #1
0
 public void autoHome() {
   boolean found = true; // Set once for the beginning.
   while (found) { // As long as we found something on a previous pass, keep trying.
     found = false;
     // TODO: Search cellstacks as well. I got arrayIndexOutOfBounds before, because it overstepped
     // some boundary.
     for (int i = 0; i < PLAYSTACKLENGTH; i++) {
       if (stacks[i].hasCards()) {
         Card theCard = stacks[i].getTopCard();
         int homeStack = findHome(theCard);
         if (homeStack > -1) {
           found = true;
           Move move = new Move(i, homeStack, this);
           try {
             move.execute();
             addToHistory(move);
           } catch (Exception e) {
             controller.alert(e.getMessage());
           }
         }
       }
     }
   }
 }
Пример #2
0
  protected void keyPressed(int keyCode) {
    int selectionLocation = getSelectionLocation();

    // Selection buttons
    if ((keyCode == KEY_NUM4) || (keyCode == -3) || (keyCode == LEFT)) {
      // NUM4, left, bbtrackball left
      gotoPrevious();
    } else if ((keyCode == KEY_NUM6) || (keyCode == -4) || (keyCode == RIGHT)) {
      // NUM5, right, bbtrackball right
      gotoNext();

      // Action buttons
    } else if ((keyCode == -5) || (keyCode == FIRE) || (keyCode == -8) || (keyCode == 10)) {
      // Emulator OK, FIRE, bbtrackball click, keyboard return
      if (selectionLocation > -1) {
        if (selectionLocation == cursorLocation) {
          // Selecting the same card that's already selected
          try {
            // Toggle selection
            stacks[selectionLocation].unselect();
          } catch (Exception e) {
            controller.alert(e.getMessage());
          }
        } else {
          try {
            // Create the move and execute it.
            Move move = new Move(selectionLocation, cursorLocation, this);
            move.execute();
            addToHistory(move);
            if (stacks[selectionLocation].hasCards()) {
              stacks[selectionLocation].unselect();
            }
          } catch (AgainstTheRulesException e) {
            // If no solution is found, then complain.
            controller.alert(e.getMessage());
          } catch (NoFreeCellException e) {
            // If the solver couldn't find enough cells, then complain.
            controller.alert(e.getMessage());
          } catch (InvalidCardException e) {
            controller.alert(e.getMessage());
          } finally {
            try {
              // And make sure nothing is left selected
              selectionLocation = getSelectionLocation();
              if (selectionLocation > -1) {
                stacks[selectionLocation].unselect();
              }
            } catch (Exception e) {
              // And if that fails, complain
              controller.alert(e.getMessage());
            }
          }
        }
        this.repaint();
      } else {
        try {
          // Select the card under the cursor
          stacks[cursorLocation].select();
        } catch (Exception e) {
          // And if that fails, complain
          controller.alert(e.getMessage());
        }
        repaint();
      }
    } else if ((keyCode == UP) || (keyCode == -1)) {
      // UP, Emulator UP
      if (getSelectionLocation() == -1) {
        // Move the selection location to the top, for convenience.
        gotoLocation(PLAYSTACKLENGTH + CELLSTACKLENGTH - 1);
        this.repaint();
      } else {
        // Move the currently-selected card home if possible, or to a free cell, if available.
        try {
          Card theCard = stacks[selectionLocation].getTopCard();
          int homeslot = findHome(theCard);
          if (homeslot > -1) {
            Move move = new Move(selectionLocation, homeslot, this);
            move.execute();
            addToHistory(move);
          } else {
            // Find a free cell and if found, create a move and execute.
            int freeCell = new FreeCellAllocator().getFreeCell();
            Move move = new Move(selectionLocation, freeCell, this);
            move.execute();
            addToHistory(move);
          }
          if (stacks[selectionLocation].hasCards()) {
            stacks[selectionLocation].unselect();
          }
        } catch (AgainstTheRulesException e) {
          // If no solution is found, then complain.
          controller.alert(e.getMessage());
        } catch (NoFreeCellException e) {
          // If the solver couldn't find enough cells, then complain.
          controller.alert(e.getMessage());
        } catch (InvalidCardException e) {
          controller.alert(e.getMessage());
        } finally {
          try {
            // And make sure nothing is left selected
            selectionLocation = getSelectionLocation();
            if (selectionLocation > -1) {
              stacks[selectionLocation].unselect();
            }
          } catch (Exception e) {
            // And if that fails, complain
            controller.alert(e.getMessage());
          }
        }
      }

      // Other buttons
    } else {
      // DEBUG
      // controller.alert("KeyCode:"+keyCode+"\nCursor:"+cursor.toString());
    }
  }
Пример #3
-17
 public void doMove(Move move)
     throws AgainstTheRulesException, NoFreeCellException, InvalidCardException {
   Card theCard = null;
   try {
     theCard = stacks[move.getFrom()].pop();
     stacks[move.getTo()].push(theCard);
   } catch (AgainstTheRulesException e1) {
     if (!theCard.equals(null)) {
       // If we managed to pop the card, deal it back.
       stacks[move.getFrom()].deal(theCard);
     }
     // TODO: If this move is already part of a movemacro, then we don't want to keep trying to
     // solve it.
     Move newMove = solveMove(move);
     newMove.execute();
     addToHistory(move);
   }
   this.repaint();
 }