Ejemplo n.º 1
0
  public void testRandomMovements() {
    try {
      Random generator = new Random();
      int fromStack = 0;
      int toStack = 0;

      // Make sure we don't go from/to the same stack.
      while (fromStack == toStack) {
        fromStack = generator.nextInt(stacks.length);
        toStack = generator.nextInt(stacks.length);
      }

      stacks[toStack].push(stacks[fromStack].pop());
    } catch (Exception e) {
      controller.alert(e.getMessage());
    }
    this.repaint();
  }
Ejemplo n.º 2
0
  private void populatePlayArea(Deck deck) {
    // Retrieve cards from the deck and populate the play area.
    /*
    * 	Used for debugging playstack drawing.
    *
    * 		for (int i=0; i<13; i++) {
    		Card tempCard = deck.popRandom();
    		stacks[0].deal(tempCard);
    	}
    	for (int i=0; i<12; i++) {
    		Card tempCard = deck.popRandom();
    		stacks[1].deal(tempCard);
    	}
    	for (int i=0; i<11; i++) {
    		Card tempCard = deck.popRandom();
    		stacks[2].deal(tempCard);
    	}
    	for (int i=0; i<10; i++) {
    		Card tempCard = deck.popRandom();
    		stacks[3].deal(tempCard);
    	}
    	for (int i=0; i<6; i++) {
    		Card tempCard = deck.popRandom();
    		stacks[4].deal(tempCard);
    	}*/

    int stackNum = 0;
    int deckSize = deck.length();
    for (int i = 0; i < deckSize; i++) {
      try {
        Card tempCard = deck.popRandom();
        stacks[stackNum].deal(tempCard);
      } catch (Exception e) {
        controller.alert(e.getMessage());
      }
      if (stackNum == 7) {
        stackNum = 0;
      } else {
        stackNum++;
      }
    }
    // Now the deck is empty.
  }
Ejemplo n.º 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());
           }
         }
       }
     }
   }
 }
Ejemplo n.º 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());
    }
  }