Advertisement
makispaiktis

CardsGame - Player.java

Mar 16th, 2020 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. // import java.util.Arrays;
  4.  
  5. public class Player {
  6.    
  7.     // Variables
  8.     String name;
  9.     int points;
  10.     ArrayList <Card> hand;
  11.     ArrayList <Card> grabbedCards;
  12.    
  13.     // Constructors
  14.     Player(){
  15.         name = "";
  16.         points = 0;
  17.         hand = new ArrayList <Card> ();
  18.         grabbedCards = new ArrayList <Card> ();
  19.     }
  20.     Player(String name){
  21.         this.name = name;
  22.         this.points = 0;
  23.         this.hand = new ArrayList <Card> ();
  24.         grabbedCards = new ArrayList <Card> ();
  25.     }
  26.    
  27.     // Methods
  28.     // "SHOWING" METHODS
  29.     public void showStatus(){
  30.         System.out.println("Name: " + name);
  31.         System.out.println("Points: " + points);
  32.         System.out.println("**** Cards in hand ****");
  33.         for(int i=0; i<hand.size(); i++){
  34.             System.out.print(hand.get(i).abbreviation + ", ");
  35.         }
  36.         System.out.println();
  37.         System.out.println("**** Grabbed Cards ****");
  38.         for(int i=0; i<grabbedCards.size(); i++){
  39.             System.out.print(grabbedCards.get(i).abbreviation + ", ");
  40.         }
  41.     }
  42.    
  43.     public void showCards(){
  44.         if(hand.size() != 0){
  45.             for(int i=0; i<hand.size(); i++){
  46.                 if(i != hand.size()-1){
  47.                     System.out.print(hand.get(i).abbreviation + ", ");
  48.                 }
  49.                 else{
  50.                     System.out.println(hand.get(i).abbreviation);
  51.                 }
  52.             }
  53.         }
  54.         else{
  55.             System.out.println("I don't have cards in my hand right now. Can't show anything yet!");
  56.         }
  57.     }
  58.    
  59.    
  60.     // METHODS THAT WILL BE USEFUL WHILE PLAYING
  61.     public void addCardInHand(Card card){
  62.         hand.add(card);
  63.     }
  64.    
  65.    
  66.     public void addCardsInHand(ArrayList <Card> extraCards){
  67.         for(int i=0; i<extraCards.size(); i++){
  68.             hand.add(extraCards.get(i));
  69.         }
  70.     }
  71.    
  72.    
  73.     public void throwCardFromHand(){
  74.        
  75.         if(hand.size() == 0){
  76.             System.out.println("Can't do that now!");
  77.         }
  78.         else{
  79.             ArrayList <String> listOfAbbreviations = new ArrayList <String> ();
  80.             for(int i=0; i<hand.size(); i++){
  81.                 listOfAbbreviations.add(hand.get(i).abbreviation);
  82.             }
  83.            
  84.             // Now, the variable "listOfAbbreviations" has the abbreviations of the cards that the player has in his "hand" list
  85.             System.out.println("Please select one of these cards in order to throw it down.");
  86.             for(int i=0; i<hand.size(); i++){
  87.                 System.out.print(hand.get(i).abbreviation + ", ");
  88.             }
  89.             System.out.println();
  90.            
  91.             Scanner scanner = new Scanner(System.in);
  92.             System.out.print("Select: ");
  93.             String choice = scanner.nextLine();
  94.             boolean flag = false;
  95.             for(int i=0; i<listOfAbbreviations.size(); i++){
  96.                 // Remove the element/Card_instance of the array
  97.                 if(choice.equals(listOfAbbreviations.get(i))){
  98.                     // System.out.println("Success");
  99.                     // String removedAbb = removeTheElement(listOfAbbreviations, i);
  100.                     hand.remove(hand.get(i));
  101.                     flag = true;
  102.                     break;
  103.                 }
  104.             }
  105.            
  106.             if(flag == false){
  107.                 System.out.println("The card '" + choice + "' is not one of the cards in your hand now.");
  108.                 // In this case, we do this thing again, until the user selects valid input
  109.                 throwCardFromHand();
  110.             }
  111.            
  112.         }   // END OF ELSE
  113.     }
  114.    
  115.    
  116.    
  117.     public void grabCards(ArrayList <Card> deckCards){
  118.         for(int i=0; i<deckCards.size(); i++){
  119.             grabbedCards.add(deckCards.get(i));
  120.         }
  121.     }
  122.    
  123.    
  124.    
  125.     // MAIN FUNCTION
  126.     public static void main(String[] args) {
  127.         Player thomas = new Player("Thomas");
  128.         // Create 4 cards
  129.         Card card1 = new Card("1", "heart");
  130.         Card card2 = new Card("2", "club");
  131.         Card card3 = new Card("3", "diamond");
  132.         Card card4 = new Card("4", "spade");
  133.         Card card5 = new Card("5", "nothing");
  134.         // Create a cards array
  135.         ArrayList <Card> myCards = new ArrayList <Card> ();
  136.         myCards.add(card1);
  137.         myCards.add(card2);
  138.         myCards.add(card3);
  139.         myCards.add(card4);
  140.         myCards.add(card5);
  141.         // Add this cards array in player Thomas
  142.         thomas.addCardsInHand(myCards);
  143.         thomas.showStatus();
  144.         System.out.println();
  145.         System.out.println();
  146.         thomas.throwCardFromHand();
  147.         thomas.showStatus();
  148.        
  149.     }   // END OF MAIN
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement