Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Agram Program
- /**
- *
- * Jared Bergan
- * Feb 13, 2017
- * Agram.java
- * This program will
- *
- */
- import java.util.*;
- public class Agram {
- public static void main(String[] args)
- {
- Scanner keyboard=new Scanner(System.in);
- ArrayList<String> cards=new ArrayList<String>(5);
- ArrayList<Card> cardsDiv=new ArrayList<Card>();
- Card base=new Card();
- Card output=new Card();
- boolean repeat=false;
- int value=0;
- char suit=0;
- char test=97;
- for(int x=0;x<5;x++)
- cardsDiv.add(base);
- for(int x=0;x<5;x++) //entering all of the cards and checking if they are valid
- {
- do{
- System.out.println("Enter card "+(x+1)+": ");
- cards.add(keyboard.nextLine());
- cards.add(cards.get(x).toLowerCase());
- cards.remove(x);
- //checking if card value is valid
- 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')))
- repeat=true;
- else
- repeat=false;
- //checking if card suit is valid //checking if valid suit for card value 10
- 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')))
- repeat=true;
- else
- repeat=false;
- }while(repeat); //repeats while the user did not enter a valid card
- }
- System.out.println(cards);
- for(int x=0;x<cards.size();x++) //dividing up the suit and value and putting in an array list of type card
- {
- if(cards.get(x).charAt(0)=='a') //a
- cardsDiv.get(x).setValue(1);
- else if(cards.get(x).charAt(0)=='1') //1
- cardsDiv.get(x).setValue(10);
- else if(cards.get(x).charAt(0)=='j') //j
- cardsDiv.get(x).setValue(11);
- else if(cards.get(x).charAt(0)=='q') //q
- cardsDiv.get(x).setValue(12);
- else if(cards.get(x).charAt(0)=='k') //k
- cardsDiv.get(x).setValue(13);
- else
- cardsDiv.get(x).setValue(Character.getNumericValue(cards.get(x).charAt(0)));
- //putting the suits into the array list of cards
- if(cards.get(x).charAt(1)=='0') //if the card value is 10
- cardsDiv.get(x).setSuit(cards.get(x).charAt(2));
- else //card value is not 10, so the suit will be in spot 1
- cardsDiv.get(x).setSuit(cards.get(x).charAt(1));
- }
- base.setValue(cardsDiv.get(0).getValue());
- base.setSuit(cardsDiv.get(0).getSuit());
- cardsDiv.remove(0);
- System.out.println(cardsDiv.toString());
- int lowest=100;
- for(int x=0;x<cardsDiv.size();x++) //finding the card to play
- {
- if(base.getSuit()==cardsDiv.get(x).getSuit()) //finds the lowest card of the same suit, higher than the original
- {
- if(cardsDiv.get(x).getValue()>base.getValue()&&cardsDiv.get(x).getValue()<lowest) //finds the lowest of the same suit
- {
- lowest=cardsDiv.get(x).getValue();
- output.setValue(cardsDiv.get(x).getValue());
- output.setSuit(cardsDiv.get(x).getSuit());
- }
- }
- }
- if(lowest==100) //if there was no card in the same suit higher than base
- {
- for(int x=0;x<cardsDiv.size();x++) //finds card in the same suit with lowest number, regardless of base
- {
- if(base.getSuit()==cardsDiv.get(x).getSuit()) //checks if suit is the same
- {
- if(cardsDiv.get(x).getValue()<lowest)
- {
- lowest=cardsDiv.get(x).getValue();
- output.setValue(cardsDiv.get(x).getValue());
- output.setSuit(cardsDiv.get(x).getSuit());
- }
- }
- }
- }
- if(lowest==100) //there are no cards of the same suit
- {
- for(int x=0;x<cardsDiv.size();x++)
- {
- if(cardsDiv.get(x).getValue()<lowest)
- {
- lowest=cardsDiv.get(x).getValue();
- output.setValue(cardsDiv.get(x).getValue());
- output.setSuit(cardsDiv.get(x).getSuit());
- }
- }
- }
- System.out.println(output.toString());
- }
- }
- -----------------------------------------------------Cards Class
- /**
- *
- * Jared Bergan Feb 13, 2017 Card.java This program will
- *
- */
- public class Card {
- private int value;
- private char suit;
- public Card() {
- value = 0;
- suit = '\0';
- }
- public Card(int v, char s) {
- value = v;
- suit = s;
- }
- public void setValue(int v)
- {
- value = v;
- }
- public void setSuit(char s)
- {
- suit = s;
- }
- public int getValue()
- {
- return value;
- }
- public char getSuit()
- {
- return suit;
- }
- public String toString()
- {
- if(this.getValue()==1)
- return ("a"+suit).toUpperCase();
- else if(this.getValue()==11)
- return ("j"+suit).toUpperCase();
- else if(this.getValue()==12)
- return ("q"+suit).toUpperCase();
- else if(this.getValue()==13)
- return ("k"+suit).toUpperCase();
- else
- return (""+this.getValue()+this.getSuit()).toUpperCase();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement