Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- // import java.util.Arrays;
- public class Player {
- // Variables
- String name;
- int points;
- ArrayList <Card> hand;
- ArrayList <Card> grabbedCards;
- // Constructors
- Player(){
- name = "";
- points = 0;
- hand = new ArrayList <Card> ();
- grabbedCards = new ArrayList <Card> ();
- }
- Player(String name){
- this.name = name;
- this.points = 0;
- this.hand = new ArrayList <Card> ();
- grabbedCards = new ArrayList <Card> ();
- }
- // Methods
- // "SHOWING" METHODS
- public void showStatus(){
- System.out.println("Name: " + name);
- System.out.println("Points: " + points);
- System.out.println("**** Cards in hand ****");
- for(int i=0; i<hand.size(); i++){
- System.out.print(hand.get(i).abbreviation + ", ");
- }
- System.out.println();
- System.out.println("**** Grabbed Cards ****");
- for(int i=0; i<grabbedCards.size(); i++){
- System.out.print(grabbedCards.get(i).abbreviation + ", ");
- }
- }
- public void showCards(){
- if(hand.size() != 0){
- for(int i=0; i<hand.size(); i++){
- if(i != hand.size()-1){
- System.out.print(hand.get(i).abbreviation + ", ");
- }
- else{
- System.out.println(hand.get(i).abbreviation);
- }
- }
- }
- else{
- System.out.println("I don't have cards in my hand right now. Can't show anything yet!");
- }
- }
- // METHODS THAT WILL BE USEFUL WHILE PLAYING
- public void addCardInHand(Card card){
- hand.add(card);
- }
- public void addCardsInHand(ArrayList <Card> extraCards){
- for(int i=0; i<extraCards.size(); i++){
- hand.add(extraCards.get(i));
- }
- }
- public void throwCardFromHand(){
- if(hand.size() == 0){
- System.out.println("Can't do that now!");
- }
- else{
- ArrayList <String> listOfAbbreviations = new ArrayList <String> ();
- for(int i=0; i<hand.size(); i++){
- listOfAbbreviations.add(hand.get(i).abbreviation);
- }
- // Now, the variable "listOfAbbreviations" has the abbreviations of the cards that the player has in his "hand" list
- System.out.println("Please select one of these cards in order to throw it down.");
- for(int i=0; i<hand.size(); i++){
- System.out.print(hand.get(i).abbreviation + ", ");
- }
- System.out.println();
- Scanner scanner = new Scanner(System.in);
- System.out.print("Select: ");
- String choice = scanner.nextLine();
- boolean flag = false;
- for(int i=0; i<listOfAbbreviations.size(); i++){
- // Remove the element/Card_instance of the array
- if(choice.equals(listOfAbbreviations.get(i))){
- // System.out.println("Success");
- // String removedAbb = removeTheElement(listOfAbbreviations, i);
- hand.remove(hand.get(i));
- flag = true;
- break;
- }
- }
- if(flag == false){
- System.out.println("The card '" + choice + "' is not one of the cards in your hand now.");
- // In this case, we do this thing again, until the user selects valid input
- throwCardFromHand();
- }
- } // END OF ELSE
- }
- public void grabCards(ArrayList <Card> deckCards){
- for(int i=0; i<deckCards.size(); i++){
- grabbedCards.add(deckCards.get(i));
- }
- }
- // MAIN FUNCTION
- public static void main(String[] args) {
- Player thomas = new Player("Thomas");
- // Create 4 cards
- Card card1 = new Card("1", "heart");
- Card card2 = new Card("2", "club");
- Card card3 = new Card("3", "diamond");
- Card card4 = new Card("4", "spade");
- Card card5 = new Card("5", "nothing");
- // Create a cards array
- ArrayList <Card> myCards = new ArrayList <Card> ();
- myCards.add(card1);
- myCards.add(card2);
- myCards.add(card3);
- myCards.add(card4);
- myCards.add(card5);
- // Add this cards array in player Thomas
- thomas.addCardsInHand(myCards);
- thomas.showStatus();
- System.out.println();
- System.out.println();
- thomas.throwCardFromHand();
- thomas.showStatus();
- } // END OF MAIN
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement