SHARE
TWEET

Untitled

a guest Jan 24th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner
  2.  
  3. public class HelloWorld
  4. {
  5.     public static String[] values = { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
  6.     public static String[] suits = { "H", "S", "C", "D" };
  7.        
  8.     public static void main(String []args)
  9.     {
  10.         Scanner scan = new Scanner(System.in);
  11.         String[] hand = new String [7];
  12.    
  13.         String cardString;
  14.         String userGuess;
  15.        
  16.         // populate hand string with non-repeating random cards
  17.         for (int i = 0; i < hand.length; i++)
  18.         {
  19.             hand[i] = GetNewCard(hand);
  20.             System.out.println (hand[i]);
  21.         }
  22.        
  23.         System.out.println ("I have 7 cards... Can you guess what I have?");
  24.         System.out.println ("(Valid guesses are formatted as value, followed by suit, eg: 3H, JC, 10S)");
  25.         System.out.print (">");
  26.        
  27.         // Prompt the user to guess a card (only allow valid guesses)
  28.         do{
  29.             userGuess = scan.nextLine();
  30.         }while (ValidUserGuess(userGuess) == false);
  31.        
  32.         // Process the user's guess, now that we know it's valid
  33.         if (ArrayContains(hand, userGuess))
  34.         {
  35.             System.out.println ("Wow you're the smartest!");
  36.         }
  37.         else
  38.         {
  39.             System.out.println ("You're a loser!");
  40.         }
  41.     }
  42.    
  43.     // gets a new card as a string (eg: 3H, JC, 10S). Will not duplicate values in hand
  44.     public static String GetNewCard(String[] hand)
  45.     {
  46.         String returnString = "";
  47.         int randNum = 0;
  48.        
  49.         do{
  50.             returnString = "";
  51.            
  52.             randNum = (int) Math.floor(Math.random() * values.length);
  53.             returnString += values[randNum];
  54.            
  55.             randNum = (int) Math.floor(Math.random() * suits.length);
  56.             returnString += suits[randNum];
  57.            
  58.         }while (ArrayContains(hand, returnString));
  59.        
  60.         return returnString;
  61.     }
  62.    
  63.     // Looks through a string array, and returns true if it contains the value / false if it does not
  64.     public static boolean ArrayContains (String[] array, String value)
  65.     {
  66.         for (int i = 0; i < array.length; i++)
  67.         {
  68.             if (value.equals(array[i]))
  69.                 return true;
  70.         }
  71.        
  72.         return false;
  73.     }
  74.    
  75.     // Validates that the user guess has a valid suit and value
  76.     public static boolean ValidUserGuess (String guess)
  77.     {
  78.         String valueGuess = guess.substring (0, guess.length() - 1);
  79.         String suitGuess = guess.substring (guess.length() - 1); // tries to get just the suit
  80.      
  81.         if (ArrayContains(values, valueGuess) == false)
  82.             return false;
  83.            
  84.         if (ArrayContains(suits, suitGuess) == false)
  85.             return false;
  86.            
  87.         return true;
  88.     }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top