Esempio n. 1
0
 private static void useCard(FreeCellPlayer player, int selectedCard, Deck deck)
     throws NullPointerException {
   NamePrinter np = new NamePrinter();
   player.setThrownCard(player.getHand().get(selectedCard - 1));
   player.getHand().remove(selectedCard - 1);
   np.print(player.getName() + " uses " + player.getThrownCard().toString());
   deck.getCardPile().add(player.getThrownCard());
   delay(1000);
 }
Esempio n. 2
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String input = null;
    boolean valid = false;
    NamePrinter np = new NamePrinter();

    // players
    Deck deck = new Deck();
    System.out.println("Welcome to FreeCell! Let's play!");
    ArrayList<FreeCellPlayer> players = new ArrayList<FreeCellPlayer>();

    int numOfPlayers = 0;
    int numOfCpus;
    while (!valid) {
      System.out.print("Enter number of players (4 MAX): ");
      try {
        input = in.nextLine();
        numOfPlayers = Integer.parseInt(input);
        if (numOfPlayers < 0 || numOfPlayers > 4) {
          throw new NumberFormatException();
        }
        break;
      } catch (NoSuchElementException e) {
        System.out.println("Null, try again.");
        continue;
      } catch (NumberFormatException e) {
        System.out.println("Please enter a number from 1 to 4");
        continue;
      }
    }

    if (numOfPlayers == 0) {
      // an all-AI match
      np.print("All-AI Match!");
      delay(1500);
      for (int i = 0; i < 4; i++) {
        players.add(new FreeCellPlayer("CPU " + (i + 1)));
      }
    } else {
      for (int i = 0; i < numOfPlayers; i++) {
        System.out.println("Player " + (i + 1));
        FreeCellPlayer player = new FreeCellPlayer();
        players.add(player);
      }

      if (numOfPlayers < 4) {
        while (!valid) {
          System.out.print("Do you want to fill CPUs to make 4 players? [Y/n] ");
          try {
            input = in.next();
            if (input == null) {
              throw new NoSuchElementException();
            } else if (!input.equalsIgnoreCase("y") && !input.equalsIgnoreCase("n")) {
              throw new NoSuchElementException();
            } else if (input.equalsIgnoreCase("y")) {
              numOfCpus = 4 - numOfPlayers;
              for (int i = 0; i < numOfCpus; i++) {
                FreeCellPlayer cpu = new FreeCellPlayer("CPU " + (i + 1));
                players.add(cpu);
              }

              break;
            } else if (input.equalsIgnoreCase("n") && numOfPlayers == 1) {
              System.out.println(
                  "The game will automatically add a CPU since you cannot play alone");
              players.add(new FreeCellPlayer("CPU"));
              input = null;

              break;
            }
          } catch (NoSuchElementException e) {
            System.out.println("Error, try again.");
            continue;
          }
        }
      }
    }

    distributeToPlayers(deck, players, 5);
    System.out.println("--START--");

    // get the base card
    Card baseCard = deck.draw();

    while (stillPlaying(players)) {
      System.out.println("\nBase Card: " + baseCard.toString());
      delay(1000);

      for (FreeCellPlayer player : players) {
        if (!player.isOriginOfBaseCard()) {
          player.setThrownCard(null);
        }
      }

      for (FreeCellPlayer player : players) {
        if (!player.isWin()) {
          if (!player.isOriginOfBaseCard()) {
            System.out.println("\n--");
            System.out.println(player.getName() + "'s Turn");
            if (!player.isAi()) {
              if (countValidPlayers(players) > 1) {
                System.out.println("Are you " + player.getName() + "?");
                pressAnyKeyToContinue();
              }

              int selectedCard;
              sortHand(player);

              if (checkIfHandsHasSameSuit(player, baseCard)) {
                while (!valid) {
                  System.out.println(player.showHand());
                  System.out.print("Pick a card with the suit of " + baseCard.getSuit() + ": ");

                  try {
                    input = in.nextLine();
                    if (input == null || input.equalsIgnoreCase("")) {
                      throw new NullPointerException();
                    }

                    selectedCard = Integer.parseInt(input);
                    if (selectedCard < 1 || selectedCard > player.getHand().size()) {
                      throw new NumberFormatException();
                    } else {
                      if (!hasSameSuit(player.getHand().get(selectedCard - 1), baseCard)) {
                        throw new NumberFormatException();
                      } else {
                        useCard(player, selectedCard, deck);
                        checkIfWinner(player);
                      }
                    }

                    break;
                  } catch (NullPointerException | NumberFormatException e) {
                    System.out.println("Try again");
                    clearScreen();
                    continue;
                  }
                }
              } else {
                if (deck.isEmpty()) {
                  addCardPile(player, players, deck);
                  break;
                } else {
                  drawUntilYouGotIt(players, deck, player, baseCard);
                  if (deck.isEmpty()) {
                    break;
                  }
                }
              }

              // Press Any Key to Continue...
              if (countValidPlayers(players) > 1) {
                System.out.println("--END TURN--");
                pressAnyKeyToContinue();
                clearScreen();
              }
            } else {
              if (checkIfHandsHasSameSuit(player, baseCard)) {
                aiDropCard(player, baseCard, deck);
                checkIfWinner(player);
              } else {
                if (deck.isEmpty()) {
                  addCardPile(player, players, deck);
                  break;
                } else {
                  drawUntilYouGotIt(players, deck, player, baseCard);
                  if (deck.isEmpty()) {
                    break;
                  }
                }
              }
            }
          }
        }
      }

      if (!stillPlaying(players)) {
        break;
      }

      winnerForThisRound(players);
      deck.getCardPile().clear();

      for (FreeCellPlayer player : players) {
        if (player.isOriginOfBaseCard() && !player.isWin()) {
          System.out.println("\n" + player.getName() + " wins this round!");
          delay(1000);

          if (!player.isAi()) {
            System.out.println("Are you " + player.getName() + "?");
            pressAnyKeyToContinue();

            sortHand(player);
            System.out.println(player.showHand());
            int selectedCard = 0;

            while (!valid) {
              System.out.println("Pick a new base card:");

              try {
                input = in.nextLine();

                if (input.equalsIgnoreCase("")) {
                  throw new NullPointerException();
                }

                selectedCard = Integer.parseInt(input);

                if (selectedCard < 1 || selectedCard > player.getHand().size()) {
                  throw new NumberFormatException();
                }

                break;
              } catch (NullPointerException e) {
                System.out.println("Try again.");
                continue;
              } catch (NumberFormatException e) {
                System.out.println("Try again.");
                continue;
              }
            }

            useCard(player, selectedCard, deck);
            baseCard = player.getThrownCard();
            checkIfWinner(player);
          } else {
            int selectedCard = -1;
            Random random = new Random();

            while (selectedCard < 0) {
              selectedCard = random.nextInt(player.getHand().size());
            }

            if (!player.isWin()) {
              useCard(player, selectedCard + 1, deck);
              baseCard = player.getThrownCard();
              checkIfWinner(player);
            }
          }
          break;
        }
      }

      // sort players
      sortPlayers(players);
    }

    for (Player player : players) {
      if (player.getHand().size() > 0) {
        System.out.println("\n--END--\n" + player.getName() + " lost...");
      }
    }

    in.close();
  }