Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. Agram Program
  2.  
  3. /**
  4. *
  5. * Jared Bergan
  6. * Feb 13, 2017
  7. * Agram.java
  8. * This program will
  9. *
  10. */
  11. import java.util.*;
  12. public class Agram {
  13.  
  14. public static void main(String[] args)
  15. {
  16. Scanner keyboard=new Scanner(System.in);
  17. ArrayList<String> cards=new ArrayList<String>(5);
  18. ArrayList<Card> cardsDiv=new ArrayList<Card>();
  19. Card base=new Card();
  20. Card output=new Card();
  21. boolean repeat=false;
  22. int value=0;
  23. char suit=0;
  24. char test=97;
  25. for(int x=0;x<5;x++)
  26. cardsDiv.add(base);
  27. for(int x=0;x<5;x++) //entering all of the cards and checking if they are valid
  28. {
  29. do{
  30. System.out.println("Enter card "+(x+1)+": ");
  31. cards.add(keyboard.nextLine());
  32. cards.add(cards.get(x).toLowerCase());
  33. cards.remove(x);
  34. //checking if card value is valid
  35. if(!(cards.get(x).charAt(0)=='a'||cards.get(x).charAt(0)=='2'||cards.get(x).charAt(0)=='3'||cards.get(x).charAt(0)=='4'||cards.get(x).charAt(0)=='5'||cards.get(x).charAt(0)=='6'||cards.get(x).charAt(0)=='7'||cards.get(x).charAt(0)=='8'||cards.get(x).charAt(0)=='9'||cards.get(x).charAt(0)=='j'||cards.get(x).charAt(0)=='q'||cards.get(x).charAt(0)=='k'||(cards.get(x).charAt(0)=='1'&&cards.get(x).charAt(1)=='0')))
  36. repeat=true;
  37. else
  38. repeat=false;
  39. //checking if card suit is valid //checking if valid suit for card value 10
  40. if(!(cards.get(x).charAt(1)=='d'||cards.get(x).charAt(1)=='h'||cards.get(x).charAt(1)=='s'||cards.get(x).charAt(1)=='c'||((cards.get(x).charAt(0)=='1'&&cards.get(x).charAt(0)=='0')&&cards.get(x).charAt(2)=='d'||cards.get(x).charAt(2)=='h'||cards.get(x).charAt(2)=='s'||cards.get(x).charAt(2)=='c')))
  41. repeat=true;
  42. else
  43. repeat=false;
  44. }while(repeat); //repeats while the user did not enter a valid card
  45. }
  46. System.out.println(cards);
  47. for(int x=0;x<cards.size();x++) //dividing up the suit and value and putting in an array list of type card
  48. {
  49. if(cards.get(x).charAt(0)=='a') //a
  50. cardsDiv.get(x).setValue(1);
  51. else if(cards.get(x).charAt(0)=='1') //1
  52. cardsDiv.get(x).setValue(10);
  53. else if(cards.get(x).charAt(0)=='j') //j
  54. cardsDiv.get(x).setValue(11);
  55. else if(cards.get(x).charAt(0)=='q') //q
  56. cardsDiv.get(x).setValue(12);
  57. else if(cards.get(x).charAt(0)=='k') //k
  58. cardsDiv.get(x).setValue(13);
  59. else
  60. cardsDiv.get(x).setValue(Character.getNumericValue(cards.get(x).charAt(0)));
  61.  
  62. //putting the suits into the array list of cards
  63. if(cards.get(x).charAt(1)=='0') //if the card value is 10
  64. cardsDiv.get(x).setSuit(cards.get(x).charAt(2));
  65. else //card value is not 10, so the suit will be in spot 1
  66. cardsDiv.get(x).setSuit(cards.get(x).charAt(1));
  67. }
  68.  
  69. base.setValue(cardsDiv.get(0).getValue());
  70. base.setSuit(cardsDiv.get(0).getSuit());
  71. cardsDiv.remove(0);
  72. System.out.println(cardsDiv.toString());
  73. int lowest=100;
  74. for(int x=0;x<cardsDiv.size();x++) //finding the card to play
  75. {
  76. if(base.getSuit()==cardsDiv.get(x).getSuit()) //finds the lowest card of the same suit, higher than the original
  77. {
  78. if(cardsDiv.get(x).getValue()>base.getValue()&&cardsDiv.get(x).getValue()<lowest) //finds the lowest of the same suit
  79. {
  80. lowest=cardsDiv.get(x).getValue();
  81. output.setValue(cardsDiv.get(x).getValue());
  82. output.setSuit(cardsDiv.get(x).getSuit());
  83. }
  84. }
  85. }
  86.  
  87. if(lowest==100) //if there was no card in the same suit higher than base
  88. {
  89. for(int x=0;x<cardsDiv.size();x++) //finds card in the same suit with lowest number, regardless of base
  90. {
  91. if(base.getSuit()==cardsDiv.get(x).getSuit()) //checks if suit is the same
  92. {
  93. if(cardsDiv.get(x).getValue()<lowest)
  94. {
  95. lowest=cardsDiv.get(x).getValue();
  96. output.setValue(cardsDiv.get(x).getValue());
  97. output.setSuit(cardsDiv.get(x).getSuit());
  98. }
  99. }
  100. }
  101. }
  102. if(lowest==100) //there are no cards of the same suit
  103. {
  104. for(int x=0;x<cardsDiv.size();x++)
  105. {
  106. if(cardsDiv.get(x).getValue()<lowest)
  107. {
  108. lowest=cardsDiv.get(x).getValue();
  109. output.setValue(cardsDiv.get(x).getValue());
  110. output.setSuit(cardsDiv.get(x).getSuit());
  111. }
  112. }
  113. }
  114. System.out.println(output.toString());
  115. }
  116. }
  117.  
  118.  
  119. -----------------------------------------------------Cards Class
  120.  
  121.  
  122. /**
  123. *
  124. * Jared Bergan Feb 13, 2017 Card.java This program will
  125. *
  126. */
  127. public class Card {
  128. private int value;
  129. private char suit;
  130.  
  131. public Card() {
  132. value = 0;
  133. suit = '\0';
  134. }
  135.  
  136. public Card(int v, char s) {
  137. value = v;
  138. suit = s;
  139. }
  140.  
  141. public void setValue(int v)
  142. {
  143. value = v;
  144. }
  145.  
  146. public void setSuit(char s)
  147. {
  148. suit = s;
  149. }
  150.  
  151. public int getValue()
  152. {
  153. return value;
  154. }
  155.  
  156. public char getSuit()
  157. {
  158. return suit;
  159. }
  160.  
  161. public String toString()
  162. {
  163. if(this.getValue()==1)
  164. return ("a"+suit).toUpperCase();
  165. else if(this.getValue()==11)
  166. return ("j"+suit).toUpperCase();
  167. else if(this.getValue()==12)
  168. return ("q"+suit).toUpperCase();
  169. else if(this.getValue()==13)
  170. return ("k"+suit).toUpperCase();
  171. else
  172. return (""+this.getValue()+this.getSuit()).toUpperCase();
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement