## Search ObiokusThoughts (Posts Only)

### The Inherent Rationale Of Perception

Perception is one of the most important factors affecting our day to day lives as human beings.  I believe it is one of the most underrate...

## Donate

Use the button below to make one time or recurring donations to help generate income. Thank you very much for your generosity. Please continue to enjoy ObiokusThoughts.com ### Template for Spades Program in Java (some bugs may occur)

/*
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//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;
/**
*
* @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;
//array containing the four cards of the book
public static Card[] current = new Card;
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;
public boolean matchSuit = false;
//Count for how many cards remain;
public int cardsLeft;
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;
cardsLeft = 13;
}
}
//procedure to create deck of cards
public static void DeckOfCards() {
//Outer loop represents suits in order of hearts, diamonds,
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
} 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()) {
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()) {
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()) {
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()) {
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;
//Search for a card that is higher and is not a Spade
for (int i = 0; i < p1.cardsLeft; i++) {
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 = 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;
//Search for card that matches the suit of first card
played
for (int j = 0; j < p2.cardsLeft; j++) {
if (current.getSuit() == p2.hand[j].getSuit()) {
temp = p2.hand[j];
if (current.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.getSuit() &&
high.getValue() > current.getValue()) {
current = high;
}//else if suit matches but is not higher, search for low
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int j = 0; j < p2.cardsLeft; j++) {
if (current.getSuit() == p2.hand[j].getSuit()
&& p2.hand[j].getValue() < low.getValue() && p2.hand[j].getValue() <
current.getValue()) {
temp = p2.hand[j];
low = temp;
index = j;
}
}
}
current = low;
}//else if cannot find a card to match suit, search for
else if (high.getSuit() != current.getSuit()) {
low = high;
for (int j = 0; j < p2.cardsLeft; j++) {
p2.hand[j].getValue() < low.getValue() && p2.hand[j].getValue() <
current.getValue()) {
low = p2.hand[j];
index = j;
}
}
current = 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;
//Search for card that matches suit of first card play
for (int k = 0; k < p3.cardsLeft; k++) {
if (current.getSuit() == p3.hand[k].getSuit()) {
temp = p3.hand[k];
if (current.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.getSuit() &&
high.getValue() > current.getValue() && high.getValue() >
current.getValue()) {
current = high;
}//else if suit matches but is not high, search for low
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int k = 0; k < p3.cardsLeft; k++) {
if (current.getSuit() == p3.hand[k].getSuit()
&& p3.hand[k].getValue() < low.getValue() && p3.hand[k].getValue() <
current.getValue()) {
temp = p3.hand[k];
low = temp;
index = k;
}
}
}
current = low;
}//else if player cannot match suit, search for lowest non
else if (high.getSuit() != current.getSuit()) {
low = high;
for (int k = 0; k < p3.cardsLeft; k++) {
p3.hand[k].getValue() < low.getValue() && p3.hand[k].getValue() <
current.getValue()) {
low = p3.hand[k];
index = k;
}
}
current = 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;
//Search for card that matches suit of first card played
for (int l = 0; l < p4.cardsLeft; l++) {
if (current.getSuit() == p4.hand[l].getSuit()) {
temp = p4.hand[l];
if (current.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.getSuit() &&
high.getValue() > current.getValue() && high.getValue() >
current.getValue() && high.getValue() > current.getValue()) {
current = high;
}//else if suit matches but is not higher, search for low
card to play
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int l = 0; l < p4.cardsLeft; l++) {
if (current.getSuit() == p4.hand[l].getSuit()
&& p4.hand[l].getValue() < low.getValue()) {
temp = p4.hand[l];
low = temp;
index = l;
}
}
}
current = low;
}//else if suit does not match, search for lowest non
else if (high.getSuit() != current.getSuit()) {
low = high;
for (int l = 0; l < p4.cardsLeft; l++) {
p4.hand[l].getValue() < low.getValue()) {
low = p4.hand[l];
index = l;
}
}
current = 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;
//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.cardImage));
card2 = new JLabel(new ImageIcon(current.cardImage));
card3 = new JLabel(new ImageIcon(current.cardImage));
card4 = new JLabel(new ImageIcon(current.cardImage));
winner = new JLabel("\nWinning Card if " + high.toString()
);
bookPic.setSize(300, 250);
//bookPic.setLocation(300, 0);
bookPic.setVisible(true);
//bookPic.setLocation(300*xloc, 250*yloc);
tablePic.setLocationByPlatform(true);
//xloc += 1;
//which ever player wins, claims the current book and
switch (winHand) {
case 0: {
p1.book += 1;
System.out.println(p1.id + " wins book");
play(p1, p2, p3, p4);
break;
}
case 1: {
p2.book += 1;
System.out.println(p2.id + " wins book");
play(p2, p3, p4, p1);
break;
}
case 2: {
p3.book += 1;
System.out.println(p3.id + " wins book");
play(p3, p4, p1, p2);
break;
}
case 3: {
p4.book += 1;
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.setExtendedState(MAXIMIZED_BOTH);
tablePic.setLocationByPlatform(true);
tablePic.setVisible(true);
tablePic.setDefaultCloseOperation(EXIT_ON_CLOSE);
//System.exit(0);
}
//else leading player plays their card
//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 ");
//System.out.println((Arrays.toString(first.hand)));
//Assume first card is high card
high = first.hand;
//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 = 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;
//Search for a card to match suit of first card played
for (int j = 0; j < 13; j++) {
if (current.getSuit() == second.hand[j].getSuit())
{
temp = second.hand[j];
if (current.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.getSuit() &&
high.getValue() > current.getValue()) {
current = high;
}//else if card matches suit but is not higher, search for
lowest card to play
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int j = 0; j < 13; j++) {
if (current.getSuit() ==
second.hand[j].getSuit() && second.hand[j].getValue() < low.getValue()
){
low = second.hand[j];
index = j;
}
}
current = low;
}//else search for non matching card to play
else if (high.getSuit() != current.getSuit() &&
high.getSuit() != JOKER) {
low = high;
for (int j = 0; j < 13; j++) {
//if no cards match first card plyed, play high
second.hand[j].getSuit() != current.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 = 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;
//Search for another card which matches suit
for (int k = 0; k < 13; k++) {
if (current.getSuit() == third.hand[k].getSuit()) {
temp = third.hand[k];
if (current.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.getSuit() &&
high.getValue() > current.getValue() && high.getValue() >
current.getValue()) {
current = high;
}//else if suit matches but is not high card, search for
low card to play
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int k = 0; k < 13; k++) {
if (current.getSuit() ==
third.hand[k].getSuit() && third.hand[k].getValue() < low.getValue() )
{
low = third.hand[k];
index = k;
}
}
current = low;
}//else if player has no card to match suit, search for
non Joker to play
else if (high.getSuit() != current.getSuit() &&
high.getSuit() != JOKER) {
low = high;
for (int k = 0; k < 13; k++) {
//search for spades to play as trump card
third.hand[k].getSuit() != current.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 = 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;
//Search for a card that matches suit of cards in book
for (int l = 0; l < 13; l++) {
if (current.getSuit() == fourth.hand[l].getSuit())
{
temp = fourth.hand[l];
if (current.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.getSuit() &&
high.getValue() > current.getValue() && high.getValue() >
current.getValue() && high.getValue() > current.getValue()) {
current = high;
}//If card matches suit but is not high card, search for
low card to play
else if (high.getSuit() == current.getSuit()) {
low = high;
for (int l = 0; l < 13; l++) {
if (current.getSuit() ==
fourth.hand[l].getSuit() && fourth.hand[l].getValue() <
low.getValue()) {
low = fourth.hand[l];
index = l;
}
}
current = low;
}//if card soes not match, search for non Joker to play
else if (high.getSuit() != current.getSuit() &&
high.getSuit() != JOKER) {
low = high;
for (int l = 0; l < 13; l++) {
fourth.hand[l].getSuit() != current.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 = 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;
//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 = current[w];
winHand = w;
System.out.println(w);
}

}
//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.cardImage));
card2 = new JLabel(new ImageIcon(current.cardImage));
card3 = new JLabel(new ImageIcon(current.cardImage));
card4 = new JLabel(new ImageIcon(current.cardImage));
winner = new JLabel("\nWinning Card if " +
high.toString() );
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.setLocationByPlatform(true);
//Value of winhand determines which player claims book and
switch (winHand) {
case 0: {
first.book += 1;
System.out.println(first.id + " wins book");
play(first, second, third, fourth);
break;
}
case 1: {
second.book += 1;
System.out.println(second.id + " wins book");
play(second, third, fourth, first);
break;
}
case 2: {
third.book += 1;
System.out.println(third.id + " wins book");
play(third, fourth, first, second);
break;
}
case 3: {
fourth.book += 1;
System.out.println(fourth.id + " wins book");
play(fourth, first, second, third);
break;
}
}
else {
System.out.println("Error in playing order!");
}

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//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();
}
}