Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner
- public class HelloWorld
- {
- public static String[] values = { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
- public static String[] suits = { "H", "S", "C", "D" };
- public static void main(String []args)
- {
- Scanner scan = new Scanner(System.in);
- String[] hand = new String [7];
- String cardString;
- String userGuess;
- // populate hand string with non-repeating random cards
- for (int i = 0; i < hand.length; i++)
- {
- hand[i] = GetNewCard(hand);
- System.out.println (hand[i]);
- }
- System.out.println ("I have 7 cards... Can you guess what I have?");
- System.out.println ("(Valid guesses are formatted as value, followed by suit, eg: 3H, JC, 10S)");
- System.out.print (">");
- // Prompt the user to guess a card (only allow valid guesses)
- do{
- userGuess = scan.nextLine();
- }while (ValidUserGuess(userGuess) == false);
- // Process the user's guess, now that we know it's valid
- if (ArrayContains(hand, userGuess))
- {
- System.out.println ("Wow you're the smartest!");
- }
- else
- {
- System.out.println ("You're a loser!");
- }
- }
- // gets a new card as a string (eg: 3H, JC, 10S). Will not duplicate values in hand
- public static String GetNewCard(String[] hand)
- {
- String returnString = "";
- int randNum = 0;
- do{
- returnString = "";
- randNum = (int) Math.floor(Math.random() * values.length);
- returnString += values[randNum];
- randNum = (int) Math.floor(Math.random() * suits.length);
- returnString += suits[randNum];
- }while (ArrayContains(hand, returnString));
- return returnString;
- }
- // Looks through a string array, and returns true if it contains the value / false if it does not
- public static boolean ArrayContains (String[] array, String value)
- {
- for (int i = 0; i < array.length; i++)
- {
- if (value.equals(array[i]))
- return true;
- }
- return false;
- }
- // Validates that the user guess has a valid suit and value
- public static boolean ValidUserGuess (String guess)
- {
- String valueGuess = guess.substring (0, guess.length() - 1);
- String suitGuess = guess.substring (guess.length() - 1); // tries to get just the suit
- if (ArrayContains(values, valueGuess) == false)
- return false;
- if (ArrayContains(suits, suitGuess) == false)
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement