Beispiel #1
1
 private Move solveMove(Move move) throws NoFreeCellException, AgainstTheRulesException {
   MoveMacro newMove = new MoveMacro();
   Iterator stackIterator = stacks[move.getFrom()].createIterator();
   FreeCellAllocator allocator = new FreeCellAllocator();
   {
     // Find out if any move would be possible before we go to the trouble of creating a movemacro.
     boolean found = false;
     while (stackIterator.hasNext()) {
       Card theCard = (Card) stackIterator.next();
       if (stacks[move.getTo()].canPush(theCard)) {
         found = true;
         break;
       }
       /*
        * TODO: A side effect of this is if the fromstack includes any
        * card that could be moved, the move will allocate free cells
        * until it runs out, even if the card is buried deep?
        * //int freeCells = allocator.getNumFreeCells();
        * //for (int i=0; i < freeCells; i++) {
        */
     }
     if (!found) {
       throw new AgainstTheRulesException("There are no valid moves possible.");
     }
   }
   // Go back to the beginning.
   stackIterator.reset();
   while (stackIterator.hasNext()) {
     Card theCard = (Card) stackIterator.next();
     // if this card can be moved, then create the move and end
     if (stacks[move.getTo()].canPush(theCard)) {
       newMove.addMove(move);
       break;
     } else {
       // if it can't be moved directly, then get a free cell and create a move to the free cell.
       int cell = allocator.getFreeCell();
       newMove.addMove(new Move(move.getFrom(), cell, this));
     } // If we get a NoFreeCellException here, it's passed back and newMove gets gc'ed.
   }
   // Now, go back through the moves and move everything to the destination stack in reverse order.
   Iterator moveIterator = newMove.createIterator();
   moveIterator.previous(); // Skip the very last move, we don't need to repeat that.
   while (moveIterator.hasPrevious()) {
     Move theMove = (Move) moveIterator.previous();
     newMove.addMove(new Move(theMove.getTo(), move.getTo(), this));
   }
   return newMove;
 }
Beispiel #2
0
 public void unmove(Move move) {
   try {
     Card theCard = stacks[move.getTo()].getTopCard();
     stacks[move.getFrom()].deal(theCard);
     stacks[move.getTo()].pop();
   } catch (Exception e) {
     controller.alert("Sorry, I guess you're outta luck.");
   }
   this.repaint();
 }
Beispiel #3
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());
           }
         }
       }
     }
   }
 }
Beispiel #4
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());
    }
  }
Beispiel #5
-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();
 }