Advertisement
julianzhang

leave me alone

Mar 17th, 2023
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  * The ElevensBoard class represents the board in a game of Elevens.
  6.  */
  7. public class ElevensBoard extends Board{
  8.  
  9.     /**
  10.      * The size (number of cards) on the board.
  11.      */
  12.     private static final int BOARD_SIZE = 9;
  13.  
  14.     /**
  15.      * Flag used to control debugging print statements.
  16.      */
  17.     private static final boolean I_AM_DEBUGGING = false;
  18.  
  19.  
  20.     /**
  21.      * Creates a new <code>ElevensBoard</code> instance.
  22.      */
  23.     public ElevensBoard(){
  24.         super(BOARD_SIZE);
  25.     }
  26.    
  27.     /* Note:
  28.     * Before you write any new methods, recall that you can use all the non-abstract
  29.     public methods in the Board class.
  30.     * You cannot access the private instance variables from the Board class, but with
  31.     the available public methods, you shouldn't need to.  If you think you do, think
  32.     of a way to avoid it!
  33.     * It is possible to complete the project without implementing any methods
  34.     other than those here, but if you think that something should be added so the code
  35.     is clearer, feel free to do so.
  36.     */
  37.    
  38.     /**
  39.      * Determines if the selected cards form a valid group for removal.
  40.      * In Elevens, the legal groups are (1) a pair of non-face cards
  41.      * whose values add to 11, and (2) a group of three cards consisting of
  42.      * a jack, a queen, and a king in some order.
  43.      * @param selectedCards the list of the indices of the selected cards.
  44.      * @return true if the selected cards form a valid group for removal;
  45.      *         false otherwise.
  46.      */
  47.    
  48.     public boolean isLegal(List<Integer> selectedCards) {
  49.         // TODO: implement isLegal
  50.         if (selectedCards.size()==2){
  51.             if (containsPairSum11(selectedCards)){
  52.                 return true;
  53.             }
  54.         }
  55.         if (selectedCards.size()==3){
  56.             if (containsJQK(selectedCards)){
  57.                 return true;
  58.             }
  59.         }
  60.         return false;
  61.     }
  62.  
  63.     /**
  64.      * Determine if there are any legal plays left on the board.
  65.      * In Elevens, there is a legal play if the board contains
  66.      * (1) a pair of non-face cards whose values add to 11, or (2) a group
  67.      * of three cards consisting of a jack, a queen, and a k    ing in some order.
  68.      * @return true if there is a legal play left on the board;
  69.      *         false otherwise.
  70.      */
  71.     public boolean anotherPlayIsPossible() {
  72.         //TODO: implement anotherPlayIsPossible
  73.         if (containsPairSum11(cardIndexes()) || containsJQK(cardIndexes())){
  74.             return true;
  75.         }
  76.         return false;
  77.     }
  78.    
  79.     /**
  80.      * Check for an 11-pair in the selected cards.
  81.      * @param selectedCards selects a subset of this board.  It is list
  82.      *                      of indexes into this board that are searched
  83.      *                      to find an 11-pair.
  84.      * @return true if the board entries in selectedCards
  85.      *              contain an 11-pair; false otherwise.
  86.      */
  87.     private boolean containsPairSum11(List<Integer> selectedCards) {
  88.         //TODO: implement containsPairSum11
  89.         int s = selectedCards.size();
  90.         for (int i=0; i<s; i++){
  91.             for (int j=i; j<s; j++){
  92.                 int card1=cardAt(selectedCards.get(i)).getRank().getPointValue();
  93.                 int card2=cardAt(selectedCards.get(j)).getRank().getPointValue();
  94.                 if (card1+card2==11){
  95.                     return true;
  96.                 }
  97.             }
  98.         }
  99.         return false;
  100.     }
  101.    
  102.     /**
  103.      * Check for a JQK in the selected cards.
  104.      * @param selectedCards selects a subset of this board.  It is list
  105.      *                      of indexes into this board that are searched
  106.      *                      to find a JQK group.
  107.      * @return true if the board entries in selectedCards
  108.      *              include a jack, a queen, and a king; false otherwise.
  109.      */
  110.     private boolean containsJQK(List<Integer> selectedCards) {
  111.         //TODO: implement containsJQK
  112.         int s = selectedCards.size();
  113.         boolean jack = false;
  114.         boolean queen = false;
  115.         boolean king = false;
  116.        
  117.         for (int i=0; i<s; i++){
  118.             Rank card1 = cardAt(selectedCards.get(i)).getRank();
  119.             if (card1==Rank.JACK){
  120.                 jack = true;
  121.             }
  122.             if (card1==Rank.QUEEN){
  123.                 queen = true;
  124.             }
  125.             if (card1==Rank.KING){
  126.                 king = true;
  127.             }
  128.         }
  129.         if (jack && queen && king){
  130.             return true;
  131.         }
  132.         else {
  133.             return false;
  134.         }        
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement