ivana_andreevska

AV3 PlayingCards

May 9th, 2022
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package AV3;
  2.  
  3. import java.util.Objects;
  4.  
  5. public class PlayingCard {
  6.     private int rank;
  7.     private PlayingCardType type;
  8.  
  9.     public PlayingCard(int rank, PlayingCardType type)
  10.     {
  11.         this.rank=rank;
  12.         this.type=type;
  13.     }
  14.  
  15.     public void setType(PlayingCardType type) {
  16.         this.type = type;
  17.     }
  18.  
  19.     public void setRank(int rank) {
  20.         this.rank = rank;
  21.     }
  22.  
  23.     public int getRank() {
  24.         return rank;
  25.     }
  26.  
  27.     public PlayingCardType getType() {
  28.         return type;
  29.     }
  30.  
  31.     @Override
  32.     public String toString() {
  33.         return String.format("%d %s",rank,type.name());
  34.     }
  35.  
  36.     @Override
  37.     public boolean equals(Object o) {
  38.         if (this == o) return true;
  39.         if (o == null || getClass() != o.getClass()) return false;
  40.         PlayingCard that = (PlayingCard) o;
  41.         return rank == that.rank && type == that.type;
  42.     }
  43.  
  44.     @Override
  45.     public int hashCode() {
  46.         return Objects.hash(rank, type);
  47.     }
  48. }
  49. package AV3;
  50.  
  51. import java.util.Arrays;
  52. import java.util.Collections;
  53. import java.util.List;
  54.  
  55. public class Deck {
  56.     private PlayingCard[] cards;
  57.     private boolean[] isDealt;
  58.     private int dealtTotal;
  59.  
  60.     public Deck() {
  61.         cards = new PlayingCard[52];
  62.         isDealt=new boolean[52];
  63.         dealtTotal=0;
  64.  
  65.         for (int i = 0; i < PlayingCardType.values().length; i++) {
  66.             for (int j = 0; j < 13; j++) {
  67.                 cards[i * 13 + j] = new PlayingCard(j + 2, PlayingCardType.values()[i]);
  68.             }
  69.         }
  70.     }
  71.  
  72.     public PlayingCard[] getCards() {
  73.         return cards;
  74.     }
  75.  
  76.     public void setCards(PlayingCard[] cards) {
  77.         this.cards = cards;
  78.     }
  79.  
  80.     @Override
  81.     public boolean equals(Object o) {
  82.         if (this == o) return true;
  83.         if (o == null || getClass() != o.getClass()) return false;
  84.         Deck deck = (Deck) o;
  85.         return Arrays.equals(cards, deck.cards);
  86.     }
  87.  
  88.     @Override
  89.     public int hashCode() {
  90.         return Arrays.hashCode(cards);
  91.     }
  92.  
  93.     @Override
  94.     public String toString() {
  95.         StringBuilder stringBuilder = new StringBuilder();
  96.  
  97.         for (PlayingCard card : cards) {
  98.             stringBuilder.append(card.toString());
  99.             stringBuilder.append("\n");
  100.         }
  101.         return stringBuilder.toString();
  102.     }
  103.  
  104.     public boolean hasCardsLeft() {
  105.         return (cards.length - dealtTotal) > 0;
  106.     }
  107.  
  108.     public PlayingCard dealCard() {
  109.         if (!hasCardsLeft()) return null;
  110.  
  111.         int card = (int) (Math.random() * 52);
  112.  
  113.         if (!isDealt[card]) {
  114.             isDealt[card] = true;
  115.             dealtTotal++;
  116.             return cards[card];
  117.         }
  118.         return dealCard();
  119.     }
  120.  
  121.     public PlayingCard[] shuffle() {
  122.         List<PlayingCard> playingCardList = Arrays.asList(cards);
  123.         Collections.shuffle(playingCardList);
  124.         return cards;
  125.     }
  126.  
  127.     public static void main(String[] args) {
  128.         Deck deck = new Deck();
  129.         System.out.println(deck);
  130.         deck.shuffle();
  131.         System.out.println(deck);
  132.         PlayingCard card;
  133.         while ((card = deck.dealCard()) != null) {
  134.             System.out.println(card);
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. //PlayingCardType.values() vraka niza od enumeracijata ke iterira niz heart diamond..
  141.  
  142. package AV3;
  143.  
  144. import java.util.Arrays;
  145.  
  146. public class MultipleDecks {
  147.     private Deck[]decks;
  148.     public MultipleDecks(int numOfDecks)
  149.     {
  150.         decks=new Deck[numOfDecks];
  151.         for(int i=0;i<numOfDecks;i++)
  152.         {
  153.             decks[i]=new Deck();
  154.         }
  155.     }
  156.  
  157.     @Override
  158.     public String toString() {
  159.         StringBuilder stringBuilder=new StringBuilder();
  160.         for(Deck deck : decks)
  161.         {
  162.             stringBuilder.append(deck.toString());
  163.             stringBuilder.append("\n");
  164.         }
  165.         return stringBuilder.toString();
  166.     }
  167.  
  168.     public static void main(String[] args) {
  169.         MultipleDecks multipleDecks=new MultipleDecks(3);
  170.         System.out.println(multipleDecks);
  171.     }
  172. }
  173.  
  174. package AV3;
  175.  
  176. public enum PlayingCardType {
  177.     HEARTS,
  178.     DIAMONDS,
  179.     SPADES,
  180.     CLUBS
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment