Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package runner;
- import java.util.*;
- class Card{
- private int rank, suit;
- private String[] suitStrings = {"Hearts", "Spades", "Clubs", "Diamonds",""};
- private String[] rankStrings = {"2","3","4","5","6","7","8","9","10","J","Q","K","A",""}; //ints correspond with each string in array.
- Card(int rank, int suit){
- this.rank = rank;
- this.suit = suit;
- }
- Card(){
- rank = 13;
- suit = 4;
- }
- public int getRank(){
- return rank;
- }
- public int getSuit(){
- return suit;
- }
- public String readCard(){
- return (rankStrings[rank]+" of "+suitStrings[suit]);
- }
- }
- class DeckClass{
- Card[] deck = new Card[52];
- int counter = 0;
- DeckClass(){
- for (int i=0;i<4;i++){
- for (int j = 0; j<13;j++){
- deck[counter] = new Card(j,i);
- counter++;
- }
- }
- }
- public void shuffle(){
- Collections.shuffle(Arrays.asList(deck));
- }
- }
- class Player{
- private ArrayList<Card> hand = new ArrayList<Card>();
- public void addCard(Card cardToAdd){
- hand.add(cardToAdd);
- }
- public void removeCard(int indexOfCard){
- hand.remove(indexOfCard);
- }
- public int lengthOfHand(){
- return hand.size();
- }
- public void displayCards(){
- System.out.println("=================================================================");
- int counter = 0;
- for (int i = 0;i<hand.size();i++){
- System.out.print(hand.get(i).readCard()+"\t"); //read the cards in each hand as a test.
- counter++;
- if (counter==4){
- System.out.println();
- counter = 0;
- }
- }
- System.out.println("\n=================================================================");
- }
- public int handIndexOf(String card){
- int index = -1;
- for (int i = 0;i<hand.size();i++){
- if (hand.get(i).readCard().equals(card)){
- index = i;
- }
- }
- return index;
- }
- public Card getCard(int index){
- return hand.get(index);
- }
- }
- public class Hearts {
- Random r = new Random();
- private static DeckClass mainDeck = new DeckClass();
- private static Player one = new Player();
- private static Player two = new Player(); //init deck and players
- private static Player three = new Player();
- private static Player four = new Player();
- public static void dealCardsFromDeck(){
- mainDeck.shuffle();
- int player = 1;
- for (int i = 0; i<mainDeck.deck.length;i++){
- switch (player){
- case 1:
- one.addCard(mainDeck.deck[i]);
- player++;
- break;
- case 2:
- two.addCard(mainDeck.deck[i]);
- player++;
- break;
- case 3:
- three.addCard(mainDeck.deck[i]);
- player++;
- break;
- case 4:
- four.addCard(mainDeck.deck[i]);
- player = 1;
- break;
- }
- }
- }
- public static void main(String[] args){
- Random r = new Random();
- Scanner s = new Scanner(System.in);
- dealCardsFromDeck();
- System.out.println("Cards have been shuffled and dealt. You are player one.\n");
- System.out.println("Select three cards to swap with player two.");
- one.displayCards();
- Card[][] cardsToAdd = new Card[4][3];
- //This array stores the cards that will be swapped to the next hand.
- //The cards are first stored in this array, and then swapped all at once
- //This prevents cards from being cycled around the table
- System.out.print("First card: ");
- cardsToAdd[0][0] = one.getCard(one.handIndexOf(s.nextLine()));
- System.out.print("Second card: ");
- cardsToAdd[0][1] = one.getCard(one.handIndexOf(s.nextLine()));
- System.out.print("Third card: ");
- cardsToAdd[0][2] = one.getCard(one.handIndexOf(s.nextLine()));
- for (int i = 0;i<3;i++){
- cardsToAdd[1][i] = two.getCard(r.nextInt(two.lengthOfHand()));
- }
- for (int i = 0;i<3;i++){
- cardsToAdd[2][i] = three.getCard(r.nextInt(three.lengthOfHand())); //Collecting random cards to swap
- }
- for (int i = 0;i<3;i++){
- cardsToAdd[3][i] = four.getCard(r.nextInt(four.lengthOfHand()));
- }
- for (int i = 0; i<3;i++){
- two.addCard(cardsToAdd[0][i]);
- //System.out.println(cardsToAdd[0][i].readCard());
- one.removeCard(one.handIndexOf(cardsToAdd[0][i].readCard()));
- }
- for (int i = 0; i<3;i++){
- three.addCard(cardsToAdd[1][i]);
- two.removeCard(two.handIndexOf(cardsToAdd[1][i].readCard()));
- }
- for (int i = 0; i<3;i++){
- four.addCard(cardsToAdd[2][i]);
- three.removeCard(three.handIndexOf(cardsToAdd[2][i].readCard()));
- }
- for (int i = 0; i<3;i++){
- one.addCard(cardsToAdd[3][i]);
- four.removeCard(four.handIndexOf(cardsToAdd[3][i].readCard()));
- }
- one.displayCards();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment