Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.89 KB | None | 0 0
  1. public class Card {
  2.  
  3.    int value;
  4.    String suit;
  5.  
  6.    public Card(int value, String suit) {
  7.        super();
  8.        this.value = value;
  9.        this.suit = suit;
  10.    }
  11.    public int getValue() {
  12.        return value;
  13.    }
  14.    public String getSuit() {
  15.        return suit;
  16.    }
  17.  
  18.    public String getPrettyValue() {
  19.        String val = null;
  20.        if(value <= 10) {
  21.            val = String.valueOf(value);
  22.        } else if(value == 11) {
  23.            val = "Jack";
  24.        } else if(value == 12) {
  25.            val = "Queen";
  26.        } else if(value == 13) {
  27.            val = "King";
  28.        } else if(value == 14) {
  29.            val = "Ace";
  30.        }
  31.        return val;
  32.    }
  33.  
  34.    @Override
  35.    public String toString() {
  36.        //Pretty print character for Jack/Queen/King and Ace
  37.        String val = null;
  38.        if(value <= 10) {
  39.            val = String.valueOf(value);
  40.        } else if(value == 11) {
  41.            val = "J";
  42.        } else if(value == 12) {
  43.            val = "Q";
  44.        } else if(value == 13) {
  45.            val = "K";
  46.        } else if(value == 14) {
  47.            val = "A";
  48.        }
  49.        return "Card [value=" + val + ", suit=" + suit + "]";
  50.    }
  51.  
  52. }
  53.  
  54. import java.util.Arrays;
  55. import java.util.LinkedList;
  56. import java.util.List;
  57.  
  58. public class Deck {
  59.  
  60.    List<Card> list;
  61.  
  62.    public Deck() {
  63.        list = new LinkedList<Card>();
  64.    }
  65.  
  66.    public void addCard(Card newCard) {
  67.        //insert at end of list
  68.        list.add(newCard);
  69.    }
  70.  
  71.    public Card getCard() {
  72.        //remove and return first element from list
  73.        if(list.size()>0) {
  74.            return list.remove(0);
  75.        }
  76.        return null;
  77.    }
  78.  
  79.    public void addDeck(Deck other) {
  80.        //keep removing card from other and add to current deck till other deck is empty
  81.        while(other!=null && !other.isEmpty()) {
  82.            addCard(other.getCard());
  83.        }
  84.    }
  85.  
  86.    public boolean isEmpty() {
  87.        if(list == null || list.size() == 0) {
  88.            return true;
  89.        }
  90.        return false;
  91.    }
  92.  
  93.    public Card getRandom() {
  94.        if(isEmpty()) {
  95.            return null;
  96.        }
  97.        //generate random integer index between 0 and queueSize
  98.        int index = (int)(Math.random()*list.size());
  99.        return list.remove(index);
  100.    }
  101.  
  102.    public int count() {
  103.        //pass a copy of original list so that original list is not modified
  104.        return countRecursive(new LinkedList<>(list));
  105.    }
  106.  
  107.    private int countRecursive(List<Card> l) {
  108.        //if empty list then count is 0
  109.        if(l == null || l.isEmpty()) {
  110.            return 0;
  111.        }
  112.  
  113.        //remove 1 element from list and add 1 to count
  114.        l.remove(0);
  115.        return 1 + countRecursive(l);
  116.    }
  117.  
  118.    public int sum() {
  119.        //pass a copy of original list so that original list is not modified
  120.        return sumRecursive(new LinkedList<>(list));
  121.    }
  122.  
  123.    private int sumRecursive(List<Card> l) {
  124.        //if empty list then sum is 0
  125.        if(l == null || l.isEmpty()) {
  126.            return 0;
  127.        }
  128.  
  129.        //remove one element from list and add it's value to sum
  130.        return (l.remove(0).getValue()) + sumRecursive(l);
  131.    }
  132.  
  133.    public void orderedInsert(Card in) {
  134.        if(list.isEmpty()) {
  135.            //if list of empty, simply insert new card
  136.            list.add(in);
  137.        } else {
  138.            int i=0;
  139.            for(i=0;i<list.size();i++) {
  140.                if(list.get(i).getValue() > in.getValue()) {
  141.                    //if card value greater than incoming value then break the loop
  142.                    break;
  143.                }
  144.            }
  145.            //insert new card at calculated index
  146.            list.add(i, in);
  147.        }
  148.  
  149.    }
  150.  
  151.    public String toString() {
  152.        return "\n" + printRecursive(list) + "]";
  153.    }
  154.  
  155.    private String printRecursive(List<Card> l) {
  156.  
  157.        if(l == null || l.isEmpty()) {
  158.            return "";
  159.        }
  160.  
  161.        //remove one element from list and add it's value to sum
  162.        return l.remove(0).toString() + (l.size()>0 ? "," : "") + printRecursive(l);
  163.    }
  164.  
  165.    public void printdeck() {
  166.        for(int i=0;i<list.size();i++) {
  167.            System.out.println(list.get(i));
  168.        }
  169.    }
  170.  
  171.    public static void main(String[] args) {
  172.      
  173.        /*System.out.println("--------Testing functions----------");
  174.        Deck deck = new Deck();
  175.        deck.orderedInsert(new Card(10, "Spades"));
  176.        deck.orderedInsert(new Card(3, "Hearts"));
  177.        deck.orderedInsert(new Card(13, "Hearts"));
  178.        deck.orderedInsert(new Card(14, "Hearts"));
  179.        deck.orderedInsert(new Card(5, "Hearts"));
  180.        deck.orderedInsert(new Card(1, "Hearts"));
  181.        deck.printdeck();
  182.  
  183.        System.out.println("Count:- " + deck.count());
  184.        System.out.println("Sum:- " +deck.sum());
  185.        System.out.println("toString():- "+deck.toString());
  186.        System.out.println("-----Simulation------");*/
  187.      
  188.  
  189.        Deck fulldeck = new Deck();
  190.        //create full deck of cards based on suit and value
  191.        for(String suit : Arrays.asList("spades","diamonds","hearts","clubs") ) {
  192.            for(int j = 1;j<=14;j++) {
  193.                fulldeck.addCard(new Card(j, suit));
  194.            }
  195.        }
  196.        //get random cards from fulldeck and assign to left/right
  197.        Deck leftdeck = new Deck();
  198.        Deck rightdeck = new Deck();
  199.  
  200.        while(!fulldeck.isEmpty()) {
  201.            leftdeck.addCard(fulldeck.getRandom());
  202.            rightdeck.addCard(fulldeck.getRandom());
  203.        }
  204.  
  205.        //print deck for left and right
  206.        /*System.out.println("\n Left's deck");
  207.        leftdeck.printdeck();
  208.        System.out.println("\n Right's deck");
  209.        rightdeck.printdeck();*/
  210.  
  211.        Card leftcard, rightcard;
  212.        int stakesPerPlayer = 0;
  213.        Deck leftStakes = new Deck();
  214.        Deck rightStakes = new Deck();
  215.        Card temp;
  216.  
  217.        while(!leftdeck.isEmpty() && !rightdeck.isEmpty())
  218.        {
  219.            for(int i=0;i<stakesPerPlayer;i++) {
  220.                temp = leftdeck.getCard();
  221.                if(temp == null) {
  222.                    break;
  223.                }
  224.                leftStakes.addCard(temp);
  225.                System.out.println("Left adds "+temp.getPrettyValue()+" "+temp.getSuit());
  226.                temp = rightdeck.getCard();
  227.                if(temp == null) {
  228.                    break;
  229.                }
  230.                rightStakes.addCard(temp);
  231.                System.out.println("Right adds "+temp.getPrettyValue()+" "+temp.getSuit());
  232.            }
  233.          
  234.            leftcard = leftdeck.getCard();
  235.            if(leftcard == null) {
  236.                break;
  237.            }
  238.            rightcard = rightdeck.getCard();
  239.            if(rightcard == null) {
  240.                break;
  241.            }
  242.            System.out.println("Left plays "+leftcard.getPrettyValue()+" "+leftcard.getSuit()+
  243.                    ", Right plays "+rightcard.getPrettyValue()+" " +rightcard.getSuit());
  244.          
  245.            if(leftcard.getValue()>rightcard.getValue()) {
  246.                System.out.println("Left wins the hand");
  247.                leftdeck.addCard(leftcard);
  248.                leftdeck.addCard(rightcard);
  249.                leftdeck.addDeck(leftStakes);
  250.                leftdeck.addDeck(rightStakes);
  251.                stakesPerPlayer = 0;
  252.            } else if(leftcard.getValue() < rightcard.getValue()) {
  253.                System.out.println("Right wins the hand");
  254.                rightdeck.addCard(leftcard);
  255.                rightdeck.addCard(rightcard);
  256.                rightdeck.addDeck(leftStakes);
  257.                rightdeck.addDeck(rightStakes);
  258.                stakesPerPlayer = 0;
  259.            } else {
  260.                System.out.println("It's war");
  261.                stakesPerPlayer++;
  262.            }
  263.        }
  264.        if(!leftdeck.isEmpty() && rightdeck.isEmpty()) {
  265.            System.out.println("Left wins");
  266.        }
  267.        if(leftdeck.isEmpty() && !rightdeck.isEmpty()) {
  268.            System.out.println("Right wins");
  269.        }
  270.  
  271.    }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement