Translate To Preferred Language

Search ObiokusThoughts

Please Read Today's Featured Post

Alliteration Ere Zeitgeist

Actually ask archaic adult Broadband boy baked bad batch Cold case cant consistently catch Dawn developed dusk do-over Enact emerge...

Template for Spades Program in Java (some bugs may occur)


/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package spadesprogram;


//import Java libraries

import java.awt.BorderLayout;

import static java.awt.Frame.MAXIMIZED_BOTH;

import java.awt.Graphics;

import java.io.File;

import java.io.IOException;

import java.util.Arrays;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import javax.swing.JInternalFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

import spadesprogram.Card.*;


/**

 *

 * @author ObObotette0

 */

//Spades program accesses the Card class

public class SpadesProgram extends Card {


    //array to hold deck of Cards

    public static Card[] deckOfCards = new Card[52];


    //array containing the four cards of the book

    public static Card[] current = new Card[4];

    public static Card blank = new Card();


    //Object for each player

    public Player p1 = new Player();

    public Player p2 = new Player();

    public Player p3 = new Player();

    public Player p4 = new Player();


    //Calculate team's points

    public static int team1;

    public static int team2;


    //vvariable for which player won the hand

    public static int winHand = 0;


    //variables to help retrieve images by file

    public static String cardName;

    public static File getImage;


    //public static Card[] more;

    public static final int NumCards = 52;


    //index for creating deck

    public static int c = 0;

   

    //variable for internal window position

    int xloc = 1;

    int yloc = 0;


    public static JFrame tablePic = new JFrame();

    //public static JPanel fourCards = new JPanel();


    public JLabel card1 = new JLabel();

    public JLabel card2 = new JLabel();

    public JLabel card3 = new JLabel();

    public JLabel card4 = new JLabel();

    public JLabel winner = new JLabel();

    public JLabel newline = new JLabel();


    public static class Player {


        //Player's hand of cards

        public Card[] hand;


        //Determines who has lead

        public boolean lead = false;

        public boolean matchSuit = false;


        //Count for how many cards remain;

        public int cardsLeft;

        public int handSpades = 0;

        public int handHearts = 0;

        public int handDiamonds = 0;

        public int handClubs = 0;


        //Count for winning hands

        public int book = 0;

        int turn = 0;


        //Says which player object

        public String id = new String();


        //Class has constructor to begin with thirteen cards

        Player() {

            hand = new Card[13];

            cardsLeft = 13;


        }


    }


    //procedure to create deck of cards

    public static void DeckOfCards() {


        //Outer loop represents suits in order of hearts, diamonds, clubs and spades

        for (int suit = 0; suit <= 3; suit++) {

            //Inner loop represents card value from 2 to Ace

            for (int value = 2; value <= 14; value++) {


                deckOfCards[c] = new Card(value, suit);

                //As card is created retrieve file for card

                try {

                    cardName = deckOfCards[c].getValueAsString() + deckOfCards[c].getSuitAsString() + ".svg.png";

                    getImage = new File("..\\SpadesProgram\\Card_Images\\" + cardName);

                    deckOfCards[c].cardImage = ImageIO.read(getImage);

                } catch (IOException e) {

                    System.out.println(cardName);

                }

                c++;

            }


        }


    }


    //Procedure to shuffle

    public static void Shuffle() {

        int newI;

        Card temp;

        Random randIndex = new Random();


        for (int i = 0; i < 52; i++) {


            // pick a random index between 0 and cardsInDeck - 1

            newI = randIndex.nextInt(52);


            // swap cards[i] and cards[newI]

            temp = deckOfCards[i];

            deckOfCards[i] = deckOfCards[newI];

            deckOfCards[newI] = temp;

        }


    }


    //Procedure to deal cards to players

    public void Deal() {


        int i;

        int j;

        //Assign player IDs here

        p1.id = "Player 1";

        p2.id = "Player 2";

        p3.id = "Player 3";

        p4.id = "Player 4";


        for (int count = 0; count < 52; count++) {


            //i returns the correct index for each hand

            i = count / 4;

            //j return which player the card should be assigned to

            j = count % 4;


            switch (j) {

                case 0:

                    p1.hand[i] = deckOfCards[count];

                    switch (deckOfCards[count].getSuit()) {

                        case SPADES:

                            p1.handSpades++;

                            break;

                        case HEARTS:

                            p1.handHearts++;

                            break;

                        case CLUBS:

                            p1.handClubs++;

                            break;

                        case DIAMONDS:

                            p1.handDiamonds++;

                            break;


                    }

                    break;


                case 1:

                    p2.hand[i] = deckOfCards[count];

                    switch (deckOfCards[count].getSuit()) {

                        case SPADES:

                            p2.handSpades++;

                            break;

                        case HEARTS:

                            p2.handHearts++;

                            break;

                        case CLUBS:

                            p2.handClubs++;

                            break;

                        case DIAMONDS:

                            p2.handDiamonds++;

                            break;


                    }

                    break;

                case 2:

                    p3.hand[i] = deckOfCards[count];

                    switch (deckOfCards[count].getSuit()) {

                        case SPADES:

                            p3.handSpades++;

                            break;

                        case HEARTS:

                            p3.handHearts++;

                            break;

                        case CLUBS:

                            p3.handClubs++;

                            break;

                        case DIAMONDS:

                            p3.handDiamonds++;

                            break;


                    }

                    break;

                case 3:

                    p4.hand[i] = deckOfCards[count];

                    switch (deckOfCards[count].getSuit()) {

                        case SPADES:

                            p4.handSpades++;

                            break;

                        case HEARTS:

                            p4.handHearts++;

                            break;

                        case CLUBS:

                            p4.handClubs++;

                            break;

                        case DIAMONDS:

                            p4.handDiamonds++;

                            break;


                    }

                    break;

            }


        }

        //Sort each hand

        Sort(p1.hand, p1.cardsLeft);

        Sort(p2.hand, p2.cardsLeft);

        Sort(p3.hand, p3.cardsLeft);

        Sort(p4.hand, p4.cardsLeft);


        //See the hand of each player

        System.out.println((Arrays.toString(p1.hand)) + "\n");

        System.out.println((Arrays.toString(p2.hand)) + "\n");

        System.out.println((Arrays.toString(p3.hand)) + "\n");

        System.out.println((Arrays.toString(p4.hand)) + "\n");

    }


    //modified buuble sort

    public static void Sort(Card[] sorted, int numCards) {

        int swapin; //= start;

        int swapout; //= length;

        Card swap;


        //search the length of the array

        for (swapout = 0; swapout < numCards; swapout++) {

            //prepare card for swap

            Card temp = sorted[swapout];

            swapin = swapout;


            //Conditions are as follows

            //suit is greater the temp suit

            //or suit equal temp suit and value is greater than temp value

            while (swapin > 0 && sorted[swapin - 1].getSuit() > temp.getSuit() || swapin > 0 && sorted[swapin - 1].getSuit() == temp.getSuit() && sorted[swapin - 1].getValue() > temp.getValue()) {

                {

                    sorted[swapin] = sorted[swapin - 1];

                    --swapin;

                }


            }

            sorted[swapin] = temp;

        }


    }


    public static Card[] newHand(Player array, int element) {

        int n1 = 0;

        Card[] temp = new Card[array.cardsLeft - 1];

        for (int n = 0; n < array.cardsLeft; n++) {

            if (array.hand[n].getSuit() == JOKER) {

                n++;

            } else if (array.hand[n].getSuit() != JOKER) {

                temp[n1] = array.hand[n];

                n1++;

            }

           

        }

        Sort(temp, n1);

        return temp;

    }


    //Procedure for Playing the first hand

    public void firstPlay() {


        //if no cards are remaining print error message

        if (p1.cardsLeft == 0 && p2.cardsLeft == 0 && p3.cardsLeft == 0 && p4.cardsLeft == 0) {

            {

                System.out.println("Error in calculating score!");

            }

        } else {


            Card temp = new Card();

            //Object for high card or low card

            Card high = new Card();

            Card low = new Card();

            int index = 0;

            //First card is assumed high card

            high = p1.hand[0];


            //Search for a card that is higher and is not a Spade

            for (int i = 0; i < p1.cardsLeft; i++) {


                if (p1.hand[i].getSuit() != SPADES || p1.hand[i].getSuit() != JOKER) {

                    temp = p1.hand[i];


                    if (temp.getSuit() != SPADES && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = i;


                    }

                }


            }

            //Assign high card to book array

            current[0] = high;

            //replace card played

            p1.hand[index] = new Card();

            //decrease the amount of cards left

            p1.cardsLeft -= 1;


            //First card is assumed high card

            high = p2.hand[0];


            //Search for card that matches the suit of first card played

            for (int j = 0; j < p2.cardsLeft; j++) {


                if (current[0].getSuit() == p2.hand[j].getSuit()) {

                    temp = p2.hand[j];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = j;

                    }

                }

            }

            //if suit matches and value is high, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue()) {

                current[1] = high;


            }//else if suit matches but is not higher, search for low card to add to array

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int j = 0; j < p2.cardsLeft; j++) {


                    if (current[0].getSuit() == p2.hand[j].getSuit() && p2.hand[j].getValue() < low.getValue() && p2.hand[j].getValue() < current[0].getValue()) {

                        temp = p2.hand[j];

                        if (p2.hand[j].getSuit() != SPADES) {

                            low = temp;

                            index = j;

                        }

                    }


                }

                current[1] = low;


            }//else if cannot find a card to match suit, search for lowest non spades

            else if (high.getSuit() != current[0].getSuit()) {


                low = high;

                for (int j = 0; j < p2.cardsLeft; j++) {


                    if (low.getSuit() != SPADES && p2.hand[j].getValue() < low.getValue() && p2.hand[j].getValue() < current[0].getValue()) {

                        low = p2.hand[j];

                        index = j;

                    }


                }

                current[1] = low;

            }

            //replace card played

            p2.hand[index] = new Card();

            //decrease amount of cards left

            p2.cardsLeft -= 1;


            //Assume first card is high card

            high = p3.hand[0];


            //Search for card that matches suit of first card play

            for (int k = 0; k < p3.cardsLeft; k++) {


                if (current[0].getSuit() == p3.hand[k].getSuit()) {

                    temp = p3.hand[k];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = k;

                    }

                }

            }

            //If suit matches and has a higher value that both cars played, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue() && high.getValue() > current[1].getValue()) {

                current[2] = high;


            }//else if suit matches but is not high, search for low card to add to array

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int k = 0; k < p3.cardsLeft; k++) {


                    if (current[0].getSuit() == p3.hand[k].getSuit() && p3.hand[k].getValue() < low.getValue() && p3.hand[k].getValue() < current[0].getValue()) {

                        temp = p3.hand[k];

                        if (p3.hand[k].getSuit() != SPADES) {

                            low = temp;

                            index = k;

                        }

                    }


                }

                current[2] = low;


            }//else if player cannot match suit, search for lowest non spades to play

            else if (high.getSuit() != current[0].getSuit()) {


                low = high;

                for (int k = 0; k < p3.cardsLeft; k++) {


                    if (low.getSuit() != SPADES && p3.hand[k].getValue() < low.getValue() && p3.hand[k].getValue() < current[0].getValue()) {

                        low = p3.hand[k];

                        index = k;

                    }


                }

                current[2] = low;

            }


            //replace card played

            p3.hand[index] = new Card();

            //decrease amount of cards left

            p3.cardsLeft -= 1;


            //Assume for card is the high card

            high = p4.hand[0];


            //Search for card that matches suit of first card played

            for (int l = 0; l < p4.cardsLeft; l++) {


                if (current[0].getSuit() == p4.hand[l].getSuit()) {

                    temp = p4.hand[l];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = l;

                    }

                }

            }//if card matches suit and hs a higher value than the others, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue() && high.getValue() > current[1].getValue() && high.getValue() > current[2].getValue()) {

                current[3] = high;


            }//else if suit matches but is not higher, search for low card to play

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int l = 0; l < p4.cardsLeft; l++) {


                    if (current[0].getSuit() == p4.hand[l].getSuit() && p4.hand[l].getValue() < low.getValue()) {

                        temp = p4.hand[l];

                        if (p4.hand[l].getSuit() != SPADES) {

                            low = temp;

                            index = l;

                        }

                    }


                }

                current[3] = low;


            }//else if suit does not match, search for lowest non spade to play

            else if (high.getSuit() != current[0].getSuit()) {


                low = high;

                for (int l = 0; l < p4.cardsLeft; l++) {


                    if (low.getSuit() != SPADES && p4.hand[l].getValue() < low.getValue()) {

                        low = p4.hand[l];

                        index = l;

                    }


                }

                current[3] = low;

            }


            //replace played card

            p4.hand[index] = new Card();

            //decrease amount of cards left

            p4.cardsLeft -= 1;

            //Print cards in the book

            System.out.println("\n\n" + (Arrays.toString(current)));


            //Assume first card is the high card

            high = current[0];


            //Search for card with highest value and matches suit of first card in book

            //Call to play procedure for the next round

            for (int w = 0; w < 4; w++) {


                if (high.getValue() < current[w].getValue() && high.getSuit() == current[w].getSuit()) {

                    high = current[w];

                    winHand = w;

                    System.out.println(w);

                }

              

            }

            //Display for cards

            //JFrame tablePic = new JFrame();

            JInternalFrame bookPic = new JInternalFrame();

            JPanel fourCards = new JPanel();           


            //Retrieve image for each card and indentify winning play

            card1 = new JLabel(new ImageIcon(current[0].cardImage));

            card2 = new JLabel(new ImageIcon(current[1].cardImage));

            card3 = new JLabel(new ImageIcon(current[2].cardImage));

            card4 = new JLabel(new ImageIcon(current[3].cardImage));

            winner = new JLabel("\nWinning Card if " + high.toString() );

            //Add components to panel

            fourCards.add(card1);

            fourCards.add(card2);

            fourCards.add(card3);

            fourCards.add(card4);

            fourCards.add(winner);

            //Add panel to frame

            bookPic.add(fourCards);

            //tablePic.add(bookPic);

            bookPic.setSize(300, 250);

            //bookPic.setLocation(300, 0);

            bookPic.setVisible(true);

            tablePic.add(bookPic);

            //bookPic.setLocation(300*xloc, 250*yloc);

            tablePic.setLocationByPlatform(true);

            //xloc += 1;


            //which ever player wins, claims the current book and leads the next hand

            switch (winHand) {

                case 0: {

                    p1.book += 1;

                    p1.lead = true;

                    p2.lead = false;

                    p3.lead = false;

                    p4.lead = false;

                    System.out.println(p1.id + " wins book");

                    play(p1, p2, p3, p4);


                    break;

                }

                case 1: {

                    p2.book += 1;

                    p2.lead = true;

                    p1.lead = false;

                    p3.lead = false;

                    p4.lead = false;

                    System.out.println(p2.id + " wins book");

                    play(p2, p3, p4, p1);


                    break;

                }

                case 2: {

                    p3.book += 1;

                    p3.lead = true;

                    p1.lead = false;

                    p2.lead = false;

                    p4.lead = false;

                    System.out.println(p3.id + " wins book");

                    play(p3, p4, p1, p2);


                    break;

                }

                case 3: {

                    p4.book += 1;

                    p4.lead = true;

                    p1.lead = false;

                    p2.lead = false;

                    p3.lead = false;

                    System.out.println(p4.id + " wins book");

                    play(p4, p1, p2, p3);


                    break;

                }

            }


        }


    }


    //Procedure for the remainder of the game

    public void play(Player first, Player second, Player third, Player fourth) {


        //when all player ave zero cards left print score

        if (first.cardsLeft == 0 || second.cardsLeft == 0 || third.cardsLeft == 0 || fourth.cardsLeft == 0) {

            team1 = p1.book + p3.book;

            team2 = p2.book + p4.book;


            if (team1 > team2) {

                System.out.println("\nTeam one wins with " + team1 * 10 + " points");

                winner = new JLabel("\nTeam one wins with " + team1 * 10 + " versus " + team2 * 10 + " points for team 2");

            } else if (team1 < team2) {

                System.out.println("\nTeam two wins with " + team2 * 10 + " points");

                winner = new JLabel("\nTeam two wins with " + team2 * 10 + " versus " + team1 * 10 + " points for team 1");

            } else {

                System.out.println("\nError in calculating score!");

            }

            tablePic.add(winner);

            tablePic.setExtendedState(MAXIMIZED_BOTH);

            tablePic.setLocationByPlatform(true);

            tablePic.setVisible(true);

            tablePic.setDefaultCloseOperation(EXIT_ON_CLOSE);

            //System.exit(0);

        }        

        //else leading player plays their card

        else if (first.lead) {

            //resort each hand before play

            Sort(first.hand, first.cardsLeft);

            Sort(second.hand, second.cardsLeft);

            Sort(third.hand, third.cardsLeft);

            Sort(fourth.hand, fourth.cardsLeft);


            //variable for high or low card

            Card temp = new Card();

            Card high = new Card();

            Card low = new Card();

            int index = 0;

            JPanel fourCards = new JPanel();

            JInternalFrame bookPic = new JInternalFrame();

            //Print who is playing first and how many cards they have left

            System.out.println("\n\n" + first.id + " plays first and has " + first.cardsLeft + " playable cards remaining.");

            winner = new JLabel("\n" + first.id + " won last book and plays first.\n");

            newline = new JLabel("\n        ");

            fourCards.add(winner);

            fourCards.add(newline);

            //System.out.println((Arrays.toString(first.hand)));


            //Assume first card is high card

            high = first.hand[0];


            //Search for a non Joker which has a higher value

            for (int i = 0; i < 13; i++) {


                if (first.hand[i].getSuit() != JOKER) {

                    temp = first.hand[i];

                    //System.out.println(first.hand[i]);

                    if (temp.getValue() > high.getValue()) {

                        high = temp;

                        index = i;

                        //System.out.println(i);

                    }

                }


            }

            //Add high card from hand to array

            current[0] = high;

            //Replace card that was played

            first.hand[index] = new Card();

            //Decrease amount of cards in Hand

            first.cardsLeft -= 1;


            //Assume first card is high card

            high = second.hand[0];


            //Search for a card to match suit of first card played

            for (int j = 0; j < 13; j++) {


                if (current[0].getSuit() == second.hand[j].getSuit()) {

                    temp = second.hand[j];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = j;

                    }

                }

            }

            //If card matches suit and is higher, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue()) {

                current[1] = high;


            }//else if card matches suit but is not higher, search for lowest card to play

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int j = 0; j < 13; j++) {


                    if (current[0].getSuit() == second.hand[j].getSuit() && second.hand[j].getValue() < low.getValue() ){

                        low = second.hand[j];

                        index = j;

                    }


                }

                current[1] = low;


            }//else search for non matching card to play

            else if (high.getSuit() != current[0].getSuit() && high.getSuit() != JOKER) {


                low = high;

                for (int j = 0; j < 13; j++) {

                    //if no cards match first card plyed, play high spade as trump card

                    if (second.hand[j].getSuit() == SPADES && second.hand[j].getSuit() != current[0].getSuit()) {

                        temp = second.hand[j];

                        if (temp.getSuit() == SPADES && low.getValue() < temp.getValue()) {

                            low = temp;

                            index = j;

                        }

                    }//else play low card

                    else if (second.hand[j].getSuit() != JOKER && second.hand[j].getValue() < low.getValue() ){

                        low = second.hand[j];

                        index = j;

                    }


                }

                current[1] = low;

            }

            //replace played card

            second.hand[index] = new Card();

            //decrease amount of cards left in hand

            second.cardsLeft -= 1;


            //Assume first card is high card

            high = third.hand[0];


            //Search for another card which matches suit

            for (int k = 0; k < 13; k++) {


                if (current[0].getSuit() == third.hand[k].getSuit()) {

                    temp = third.hand[k];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = k;

                    }

                }

            }

            //if suit matches and has the highest value, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue() && high.getValue() > current[1].getValue()) {

                current[2] = high;


            }//else if suit matches but is not high card, search for low card to play

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int k = 0; k < 13; k++) {


                    if (current[0].getSuit() == third.hand[k].getSuit() && third.hand[k].getValue() < low.getValue() ){

                        low = third.hand[k];

                        index = k;

                    }


                }

                current[2] = low;


            }//else if player has no card to match suit, search for non Joker to play

            else if (high.getSuit() != current[0].getSuit() && high.getSuit() != JOKER) {


                low = high;

                for (int k = 0; k < 13; k++) {

                    //search for spades to play as trump card

                    if (third.hand[k].getSuit() == SPADES && third.hand[k].getSuit() != current[0].getSuit()) {

                        temp = third.hand[k];

                        if (temp.getSuit() == SPADES && low.getValue() < temp.getValue()) {

                            low = temp;

                            index = k;

                        }

                    }//else search for low card to play

                    else if (third.hand[k].getSuit() != JOKER && third.hand[k].getValue() < low.getValue() ){

                        low = third.hand[k];

                        index = k;

                    }


                }

                current[2] = low;

            }


            //REplace card played

            third.hand[index] = new Card();

            //Decrease the amount of cards left in hand

            third.cardsLeft -= 1;


            //Assume first card is high card

            high = fourth.hand[0];


            //Search for a card that matches suit of cards in book

            for (int l = 0; l < 13; l++) {


                if (current[0].getSuit() == fourth.hand[l].getSuit()) {

                    temp = fourth.hand[l];

                    if (current[0].getSuit() == temp.getSuit() && temp.getValue() > high.getValue()) {

                        high = temp;

                        index = l;

                    }

                }

            }

            //If card matches and has the highest value, add to array as high card

            if (high.getSuit() == current[0].getSuit() && high.getValue() > current[0].getValue() && high.getValue() > current[1].getValue() && high.getValue() > current[2].getValue()) {

                current[3] = high;


            }//If card matches suit but is not high card, search for low card to play

            else if (high.getSuit() == current[0].getSuit()) {

                low = high;

                for (int l = 0; l < 13; l++) {


                    if (current[0].getSuit() == fourth.hand[l].getSuit() && fourth.hand[l].getValue() < low.getValue()) {

                        low = fourth.hand[l];

                        index = l;

                    }


                }

                current[3] = low;


            }//if card soes not match, search for non Joker to play

            else if (high.getSuit() != current[0].getSuit() && high.getSuit() != JOKER) {


                low = high;

                for (int l = 0; l < 13; l++) {


                    //Play spade as trump card

                    if (fourth.hand[l].getSuit() == SPADES && fourth.hand[l].getSuit() != current[0].getSuit() && high.getSuit() != JOKER) {

                        temp = fourth.hand[l];

                        if (temp.getSuit() == SPADES && low.getValue() < temp.getValue()) {

                            low = temp;

                            index = l;

                        }

                    }//else search for lowest cards to play

                    else if (fourth.hand[l].getSuit() != JOKER && fourth.hand[l].getValue() < low.getValue() ){

                        low = fourth.hand[l];

                        index = l;

                    }


                }

                current[3] = low;

            }


            //Replace card that was played

            fourth.hand[index] = new Card();

            //Decrease amount of cards in hand

            fourth.cardsLeft -= 1;


            //Show cards in the current book

            System.out.println((Arrays.toString(current)));


            //Assume first card is high card

            high = current[0];


            //Search for another card to win hand

            for (int w = 0; w < 4; w++) {


                //Assign card with highest value which matches suit of previous high card of the array

                if (high.getValue() < current[w].getValue() && high.getSuit() == current[w].getSuit()) {

                    high = current[w];

                    winHand = w;

                    System.out.println(w);

                } //If there is a spade as a trump card, assign it as high card

                else if (current[w].getSuit() == SPADES && high.getSuit() != SPADES) {

                    high = current[w];

                    winHand = w;

                    System.out.println(w);

                }

              

            }

            //Add display for book

             //JFrame tablePic = new JFrame();

             //JInternalFrame bookPic = new JInternalFrame();

             //JPanel fourCards = new JPanel();

             //Retrieve each card's image and identify winning play

             card1 = new JLabel(new ImageIcon(current[0].cardImage));

             card2 = new JLabel(new ImageIcon(current[1].cardImage));

             card3 = new JLabel(new ImageIcon(current[2].cardImage));

             card4 = new JLabel(new ImageIcon(current[3].cardImage));

             winner = new JLabel("\nWinning Card if " + high.toString() );

             //Add component to panel

             fourCards.add(card1);

             fourCards.add(card2);

             fourCards.add(card3);

             fourCards.add(card4);

             fourCards.add(winner);

             //Add panel to frame

             bookPic.add(fourCards);            

             bookPic.setSize(300, 250);

             //create the framing so only 4 windows appear on each line

             if (xloc == 5)

             {

                 yloc += 1;

                 xloc = 1;

             }

             //add book display to next location

             bookPic.setLocation(300*xloc, 250*yloc);

             xloc += 1;

             bookPic.setVisible(true);

             tablePic.add(bookPic);

            

             tablePic.setLocationByPlatform(true);


            //Value of winhand determines which player claims book and leads next play

            switch (winHand) {

                case 0: {

                    first.book += 1;

                    first.lead = true;

                    second.lead = false;

                    third.lead = false;

                    fourth.lead = false;

                    System.out.println(first.id + " wins book");

                    play(first, second, third, fourth);


                    break;

                }

                case 1: {

                    second.book += 1;

                    second.lead = true;

                    first.lead = false;

                    third.lead = false;

                    fourth.lead = false;

                    System.out.println(second.id + " wins book");

                    play(second, third, fourth, first);


                    break;

                }

                case 2: {

                    third.book += 1;

                    third.lead = true;

                    first.lead = false;

                    second.lead = false;

                    fourth.lead = false;

                    System.out.println(third.id + " wins book");

                    play(third, fourth, first, second);


                    break;

                }

                case 3: {

                    fourth.book += 1;

                    fourth.lead = true;

                    first.lead = false;

                    second.lead = false;

                    third.lead = false;

                    System.out.println(fourth.id + " wins book");

                    play(fourth, first, second, third);


                    break;

                }

            }


        }//If first play does not have lead, report error

        else {

            System.out.println("Error in playing order!");

        }


       

    }

   


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        SpadesProgram x = new SpadesProgram();

        //First create deck

        x.DeckOfCards();

        //Shuffle cards before dealing

        x.Shuffle();

        //Create hands for players

        x.Deal();

        //Players begin hand and firstPlay calls Play

        //Play calls itself recursively till game ends

        x.firstPlay();


    }


}

No comments:

Post a Comment

Thank you for reading.
Please share your thoughts.
Be blessed and enjoy life!