Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can u explin this plz import java.util.*; public class Player { ArrayList<String

ID: 3692833 • Letter: C

Question

can u explin this plz

import java.util.*;
public class Player {
  
   ArrayList<String> cards=new ArrayList<String>();
   Game game=new Game(); // Game is anoter class
   static int id;
   String name;
   String trump;
   int trk;
  
   public Player(Game g){
       this.game=g;
       id++;
       this.name="Player"+id;
   }
  
   public String getName(){
       return name;
   }
  
   public void getTrick(){
       trk++;
   }
  
   public int returnTrick(){
       return trk;
   }
  
   public void add(String s){   
       cards.add(s); // s is string which holds the randomize deck of 52 cards
   }
  
   public void disp(){
       for(int i=0;i<cards.size();i++){
           System.out.println(cards.get(i));
       }
   }
  
   public void setTrump(String t){
       this.trump=t;
   }
  
   public String selectCard1(){
       Random r=new Random();
       int z=r.nextInt(cards.size());
       String a=cards.get(z);
       cards.remove(z);
       return a;
   }
   public String selectCard2(String c1){
       ArrayList<String> sameCards=new ArrayList<String>();
       ArrayList<String> trumpCards=new ArrayList<String>();
       String pc1[]= c1.split(" ");
       for(int i=0;i<cards.size();i++)
       {
           String pc[]=cards.get(i).split(" ");
           if(pc[2].equals(pc1[2])){
               sameCards.add(cards.get(i));
           }
           if(pc[2].equals(trump)){
               trumpCards.add(cards.get(i));
           }
       }
       String sc[]=new String[sameCards.size()];
       String tc[]=new String[trumpCards.size()];
       sameCards.toArray(sc);
       trumpCards.toArray(tc);
       Arrays.sort(sc);
       Arrays.sort(tc);
       if(sameCards.size()!=0){
           //System.out.println(this.name+" "+sc[sameCards.size()-1]);
           return sc[sameCards.size()-1];
       }
       else if(trumpCards.size()!=0){
           return tc[0];
       }
       return null;
   }
   public String selectCard3(String c1, String c2){
       ArrayList<String> sameCards=new ArrayList<String>();
       ArrayList<String> trumpCards=new ArrayList<String>();
       String pc1[]= c1.split(" ");
       String pc2[]= c2.split(" ");
       for(int i=0;i<cards.size();i++){
           String pc[]=cards.get(i).split(" ");
           if(pc[2].equals(pc1[2])){
               sameCards.add(cards.get(i));
           }
           if(pc[2].equals(trump)){
               trumpCards.add(cards.get(i));
           }
       }
       String sc[]=new String[sameCards.size()];
       String tc[]=new String[trumpCards.size()];
       sameCards.toArray(sc);
       trumpCards.toArray(tc);
       Arrays.sort(sc);
       Arrays.sort(tc);
       if(sameCards.size()==0 && trumpCards.size()==0){
           Random r=new Random();
           int x=r.nextInt(cards.size());
           return cards.get(x);
       }   
       else if(sameCards.size()!=0 && !pc1[2].equals(trump) && !pc2[2].equals(trump)){
          
           return sc[sameCards.size()-1];
       }
       else if(sameCards.size()!=0 && !pc1.equals(trump) && pc2[2].equals(trump)){
           return sc[0];
       }
       else if(pc1[2].equals(trump) && trumpCards.size()!=0){
           return tc[trumpCards.size()-1];
       }
       else if(pc1[2].equals(trump) && trumpCards.size()==0){
           Random r=new Random();
           int x=r.nextInt(cards.size());
           return cards.get(x);
       }
       else if(sameCards.size()==0 && trumpCards.size()!=0 && !pc2[2].equals(trump) && !pc1[2].equals(trump)){
           return tc[0];
       }
       return null;
      
   }
   public String selectCard4(String c1,String c2,String c3){
       ArrayList<String> sameCards=new ArrayList<String>();
       ArrayList<String> trumpCards=new ArrayList<String>();
       String pc1[]= c1.split(" ");
       String pc2[]= c2.split(" ");
       String pc3[]=c3.split(" ");
       for(int i=0;i<cards.size();i++){
           String pc[]=cards.get(i).split(" ");
           if(pc[2].equals(pc1[2])){
               sameCards.add(cards.get(i));
           }
           if(pc[2].equals(trump)){
               trumpCards.add(cards.get(i));
           }
       }
       String sc[]=new String[sameCards.size()];
       String tc[]=new String[trumpCards.size()];
       sameCards.toArray(sc);
       trumpCards.toArray(tc);
       Arrays.sort(sc);
       Arrays.sort(tc);
      
       if(pc1[2].equals(trump) && trumpCards.size()!=0){
           return tc[trumpCards.size()-1];
       }
       else if(pc1[2].equals(trump) && trumpCards.size()==0){
           Random r=new Random();
           int x=r.nextInt(cards.size());
           return cards.get(x);
       }
       else if(!pc1[2].equals(trump) && sameCards.size()!=0 && !pc2[2].equals(trump) && !pc3[2].equals(trump)){
           return sc[sameCards.size()-1];
       }
       else if(!pc1[2].equals(trump) && sameCards.size()!=0 && (pc2[2].equals(trump)||pc3[2].equals(trump))){
           return sc[0];
       }
       else if(!pc1[2].equals(trump) && sameCards.size()==0 && trumpCards.size()!=0 && !pc2[2].equals(trump) && !pc3[2].equals(trump)){
           return tc[0];
       }
       return null;
   }

}

Explanation / Answer

This is a program to illustrate a cards game, which can be explained as follows

The class player has the block of code for representing the players information, the cards he is going to get and function to display all the information. As already said game is another class, each time a new game is started the number of players are specified at “Player(Game g)” constructor where in each player is given a player id.

It also has function to get name , take a new card, return a card, add cards to the deck present after taking and returning. It then displays the cards through the display function.

The setTrump() sets a particular value as trump which represents the trump card for that game.

The selectCard1() helps in selecting a card from the deck and removing them from the deck.

The next block of code specifies the actual gameplay in which the player splits the cards present with him to same cards and trump cards and the ones which do not match, the respective arrays are sorted regularly and the game continues until theres no unmatched cards along with the presence of trump cards.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote