You are on page 1of 21

import java.util.

Arrays;
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class apple {


static Vector deck = new Vector(52); //we create a static variable deck that
is a global variable.
static Vector Ram = new Vector(13); //player 1
static Vector Shyam = new Vector(13); //player 2
static Vector Hari = new Vector(13); //player 3
static Vector Subash = new Vector(13); //player 4, you

static BitSet allowedCardsRam = new BitSet(13); //this will hold the set of
allowed cards for Ram.
static BitSet allowedCardsShyam = new BitSet(13); //this will hold the set of
allowed cards for Shyam.
static BitSet allowedCardsHari = new BitSet(13); //this will hold the set of
allowed cards for Hari.
static BitSet allowedCardsSubash = new BitSet(13); //this will hold the set
of allowed cards for Subash.

static BitSet thrownCardsRam = new BitSet (13); //this will hold the set of
thrown cards for Ram.
static BitSet thrownCardsShyam = new BitSet (13); //this will hold the set of
thrown cards for Ram.
static BitSet thrownCardsHari = new BitSet (13); //this will hold the set of
thrown cards for Ram.
static BitSet thrownCardsSubash = new BitSet (13); //this will hold the set
of thrown cards for Ram.

static BitSet firstPlayer = new BitSet(4); //the bitset for storing the info
of the first player
static int RAMf=0;//for accessing the BitSet object
static int SHYAMf=1;
static int HARIf=2;
static int SUBASHf=3;

static Vector currentGame = new Vector(4); //this will hold the set of cards
currently thrown

static int forGameScore[] = {2,3,4,5,6,7,8,9,10,11,12,13,1}; //array for


calculating the game score
static int suitList[] = new int[4]; // array for holding the suit info of all
the cards thrown
static int scoreList[] = new int[4]; //array for holding the game scores

public static void main(String args[]){


//Initially we start by setting Ram as the first player
firstPlayer.set(RAMf);
//Then we allow all the cards in all the players
allowedCardsRam.set(0,13,true);
allowedCardsShyam.set(0,13,true);
allowedCardsHari.set(0,13,true);
allowedCardsSubash.set(0,13,true);
Card trialCard = new Card(0,0);
currentGame.add(trialCard);
currentGame.add(trialCard);
currentGame.add(trialCard);
currentGame.add(trialCard);

System.out.println("--Beginning of main()--");
System.out.println("allowedCardsRam");
printBitSet(allowedCardsRam);
System.out.println("allowedCardsShyam");
printBitSet(allowedCardsShyam);
System.out.println("allowedCardsHari");
printBitSet(allowedCardsHari);
System.out.println("allowedCardsSubash");
printBitSet(allowedCardsSubash);

System.out.println("thrownCardsRam");
printBitSet(thrownCardsRam);
System.out.println("thrownCardsShyam");
printBitSet(thrownCardsShyam);
System.out.println("thrownCardsHari");
printBitSet(thrownCardsHari);
System.out.println("thrownCardsSubash");
printBitSet(thrownCardsSubash);

System.out.println("--End of beginning of main()--");

populateCards();
shuffleCards(); //to shuffle the global variable deck
giveCards();//to give 13 cards each to the 4 players

System.out.println("----After giving-----");
System.out.println("-----RAM-----");
printDeck(Ram);
System.out.println("");
System.out.println("-----SHYAM-----");
printDeck(Shyam);
System.out.println("");
System.out.println("-----HARI-----");
printDeck(Hari);
System.out.println("");

System.out.println("-----SUBASH-----");
printDeck(Subash);

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


calculateWinner(gameRules(Ram, Shyam, Hari, Subash));
calculateFirstPlayer(scoreList);
prepareCardsNewGame();

currentGame.add(trialCard);
currentGame.add(trialCard);
currentGame.add(trialCard);
currentGame.add(trialCard);

System.out.println("--------------------------From the for loop of the


main() -----------------------");
System.out.println("allowedCardsRam");
printBitSet(allowedCardsRam);
System.out.println("allowedCardsShyam");
printBitSet(allowedCardsShyam);
System.out.println("allowedCardsHari");
printBitSet(allowedCardsHari);
System.out.println("allowedCardsSubash");
printBitSet(allowedCardsSubash);

System.out.println("thrownCardsRam");
printBitSet(thrownCardsRam);
System.out.println("thrownCardsShyam");
printBitSet(thrownCardsShyam);
System.out.println("thrownCardsHari");
printBitSet(thrownCardsHari);
System.out.println("thrownCardsSubash");
printBitSet(thrownCardsSubash);
System.out.println("--------------------------End of for loop of the
main() -----------------------");
}// end of the the game loop for
}//end of main

private static void prepareCardsNewGame() {


//This method will prepare cards for the new round of games because from our
earlier game, the allowed cards set is
//a little mixed. It contains ones in those places which have the suit of the
card thrown by the first player. For
//the new game, we want to allow all the cards the player has except the
cards he has already thrown.
BitSet tempCard = new BitSet(13);
System.out.println("---From the prepareCardsNewGame() function ---");
System.out.println("Showing the thrown cards for all players");
System.out.println("");
tempCard = (BitSet) thrownCardsRam.clone();
tempCard.flip(0,13);
allowedCardsRam = (BitSet) tempCard.clone();
System.out.println("$$"+thrownCardsRam+"$$");

tempCard = (BitSet) thrownCardsShyam.clone();


tempCard.flip(0,13);
allowedCardsShyam = (BitSet) tempCard.clone();
System.out.println("$$"+thrownCardsShyam+"$$");

tempCard = (BitSet) thrownCardsHari.clone();


tempCard.flip(0,13);
allowedCardsHari = (BitSet) tempCard.clone();
System.out.println("$$"+thrownCardsHari+"$$");

tempCard = (BitSet) thrownCardsSubash.clone();


tempCard.flip(0,13);
allowedCardsSubash = (BitSet) tempCard.clone();
System.out.println("$$"+thrownCardsSubash+"$$");

System.out.println("");
System.out.println("Now showing the allowed cards");
System.out.println("$$"+allowedCardsRam+"$$");
System.out.println("$$"+allowedCardsShyam+"$$");
System.out.println("$$"+allowedCardsHari+"$$");
System.out.println("$$"+allowedCardsSubash+"$$");
System.out.println("");
currentGame.clear();
System.out.println("CURRENT GAME VECTOR SIZE:"+currentGame.size());
printDeck(currentGame);

System.out.println("---End of prepareCardsNewGame() function ---");

private static void calculateFirstPlayer(int[] scoreList2) {


int max = Arrays.stream(scoreList2).max().getAsInt();
int indexMax = 0;

for (int i=0; i<scoreList2.length; i++){


if (max == scoreList2[i]){
indexMax = i;
break;
}//end of if
}//end of for loop

firstPlayer.clear();
firstPlayer.set(indexMax,true);
System.out.println("");
System.out.println("Now printing the first player bitset");
printBitSet(firstPlayer);
System.out.println("");
} //end of calculate first player

private static void calculateWinner(Vector gameRules) {


// The input to this method is the gameRules vector and the output should be
the player number who has won.
// 1 = Ram, 2 = Shyam, 3 = Hari, 4 = Subash

Card currentCard;
int firstPlayerSuit = 0;

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


currentCard = (Card)gameRules.get(i);
suitList[i] = currentCard.suit;
//REMEMBER that this score calculation does not consider the case where
neither player has the card the first player
//has thrown, except the club.
for (int scoreRun=0; scoreRun<13; scoreRun++){
if (currentCard.rank == forGameScore[scoreRun]){
scoreList[i] = scoreRun;
}
}

if (suitList[i] == 1) {
//if there is a club, we add 20 to indicate that it is from
another league.
scoreList[i]+=20;
} //end of if

} //end of for loop

//Now we want to decrease the score of the player who has thrown a card which
is neither a club, nor of a suite thrown
// by the first player. This player gets -1.
if (isFirst(RAMf)){
firstPlayerSuit = suitList[0];
}
if (isFirst(SHYAMf)){
firstPlayerSuit = suitList[1];
}
if (isFirst(HARIf)){
firstPlayerSuit = suitList[2];
}
if (isFirst(SUBASHf)){
firstPlayerSuit = suitList[3];
}

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


if ((suitList[i]!=1)&&(suitList[i]!=firstPlayerSuit)){
System.out.println("");
System.out.println("Player "+(i+1)+" has no club and no suit of
first player.");
System.out.println("");
scoreList[i]=-1;
}//end of if
}//end of for

System.out.println("");
System.out.println("Now Printing the Scores");
for (int i =0; i<4; i++){
System.out.println(scoreList[i]);
}

private static Vector gameRules(Vector ram2, Vector shyam2, Vector hari2,


Vector subash2) {
// This method will determine which players can throw the card first and once
the first card is thrown, what cards the
//other players can throw
//The result of this method is the global vector currentGame, populated as
per the game rules.
if (isFirst(RAMf)){
//What to do if Ram is the first player
//First check for the set of allowed cards, and then from there pick a
random card for now.

//Now we have the set of allowed cards for Ram, we call the strategy
function
//which takes in the bit set of allowed cards for this player and
(maybe later)
//considers what other players have thrown and returns a card object
that should be the best card to throw.
currentGame.set(0,strategy(allowedCardsRam,1)); //so the first card in
the currentGame vector will be the
//card from Ram considering his strategy
//Now once the card has been thrown, we want to remove that from the
allowed cards of Ram
pruneCard(currentGame,1);

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsShyam,2);
currentGame.set(1,strategy(allowedCardsShyam,2)); //call shyam strategy
pruneCard(currentGame,2); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsHari,3);
currentGame.set(2,strategy(allowedCardsHari,3)); //call hari strategy
pruneCard(currentGame,3); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsSubash,4);
currentGame.set(3,strategy(allowedCardsSubash,4)); //call subash
strategy
pruneCard(currentGame,4); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("Result of this game");
System.out.println("");
printDeck(currentGame);
}//end of the case where ram is the first player

if (isFirst(SHYAMf)){
//What to do if Shyam is the first player

currentGame.set(1,strategy(allowedCardsShyam,2)); //call shyam strategy


pruneCard(currentGame,2); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsHari,3);
currentGame.set(2,strategy(allowedCardsHari,3)); //call hari strategy
pruneCard(currentGame,3); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsSubash,4);
currentGame.set(3,strategy(allowedCardsSubash,4)); //call subash
strategy
pruneCard(currentGame,4); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsRam,1);
currentGame.set(0,strategy(allowedCardsRam,1));
pruneCard(currentGame,1);

System.out.println("");
System.out.println("Result of this game");
System.out.println("");
printDeck(currentGame);

}//end of the case where Shyam is the first player

if (isFirst(HARIf)){
//What to do if Hari is the first player
currentGame.set(2,strategy(allowedCardsHari,3)); //call hari strategy
pruneCard(currentGame,3); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsSubash,4);
currentGame.set(3,strategy(allowedCardsSubash,4)); //call subash
strategy
pruneCard(currentGame,4); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsRam,1);
currentGame.set(0,strategy(allowedCardsRam,1));
pruneCard(currentGame,1);

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);
prepareCard(currentGame, allowedCardsShyam,2);
currentGame.set(1,strategy(allowedCardsShyam,2));
pruneCard(currentGame,2);

System.out.println("");
System.out.println("Result of this game");
System.out.println("");
printDeck(currentGame);
}//end of the case where Hari is the first player

if (isFirst(SUBASHf)){
//What to do if Subash is the first player
currentGame.set(3,strategy(allowedCardsSubash,4)); //call subash
strategy
pruneCard(currentGame,4); //to remove the already thrown card i.e.
modify the list of allowed cards

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsRam,1);
currentGame.set(0,strategy(allowedCardsRam,1));
pruneCard(currentGame,1);

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsShyam,2);
currentGame.set(1,strategy(allowedCardsShyam,2));
pruneCard(currentGame,2);

System.out.println("");
System.out.println("CURRENT GAME");
System.out.println("");
printDeck(currentGame);

prepareCard(currentGame, allowedCardsHari,3);
currentGame.set(2,strategy(allowedCardsHari,3));
pruneCard(currentGame,3);

System.out.println("");
System.out.println("Result of this game");
System.out.println("");
printDeck(currentGame);
}//end of the case where Subash is the first player

//Make sure to include after game housekeeping chores here. THINK if there
are any.
return currentGame;

}//end of gameRules class

private static void pruneCard(Vector currentGame2, int i) {


// TODO this method takes in current game vector and the player number and
determines which
//card has been thrown and marks it it the thrown cards vector of that
player.
//player number 1 = Ram, 2 = Shyam, 3 = Hari, 4 = Subash
int indexThrown;
if (i == 1){
indexThrown = Ram.indexOf(currentGame2.get(i-1));
//Now we have the index of the thrown card, we want to mark it in the
thrown card and restrict it.

allowedCardsRam.set(indexThrown,false);
thrownCardsRam.set(indexThrown,true);
System.out.println("");
System.out.println("---From pruneCard()------");
System.out.println("##allowedCardsRam##"+allowedCardsRam);
System.out.println("##thrownCardsRam##"+thrownCardsRam);
System.out.println("---From pruneCard()------");
} //end of if

if (i == 2){
indexThrown = Shyam.indexOf(currentGame2.get(i-1));
//Now we have the index of the thrown card, we want to mark it in the
thrown card and restrict it.

allowedCardsShyam.set(indexThrown,false);
thrownCardsShyam.set(indexThrown,true);
System.out.println("");
System.out.println("---From pruneCard()------");
System.out.println("##allowedCardsShyam##"+allowedCardsShyam);
System.out.println("##thrownCardsShyam##"+thrownCardsShyam);
System.out.println("---From pruneCard()------");

} //end of if

if (i == 3){
indexThrown = Hari.indexOf(currentGame2.get(i-1));
//Now we have the index of the thrown card, we want to mark it in the
thrown card and restrict it.

allowedCardsHari.set(indexThrown,false);
thrownCardsHari.set(indexThrown,true);

System.out.println("");
System.out.println("---From pruneCard()------");
System.out.println("##allowedCardsHari##"+allowedCardsHari);
System.out.println("##thrownCardsHari##"+thrownCardsHari);
System.out.println("---From pruneCard()------");

} //end of if

if (i == 4){
indexThrown = Subash.indexOf(currentGame2.get(i-1));
//Now we have the index of the thrown card, we want to mark it in the
thrown card and restrict it.

allowedCardsSubash.set(indexThrown,false);
thrownCardsSubash.set(indexThrown,true);

System.out.println("");
System.out.println("---From pruneCard()------");
System.out.println("##allowedCardsSubash##"+allowedCardsSubash);
System.out.println("##thrownCardsSubash##"+thrownCardsSubash);
System.out.println("---From pruneCard()------");
} //end of if

private static void printBitSet(BitSet input) {

System.out.println(input);

} //end of printBitSet method

public static void prepareCard(Vector currentGame2, BitSet allowedCards, int


playerNumber) {
// This method takes in the current game vector, finds the card thrown by the
first player, checks whether that card is
//available with the current player and if that card is available, marks all
the suits of that card allowed.
//Then it checks whether that card has already been thrown and if thrown,
marks that particular suit unavailable.
//If the suit of that card is not available, it shows the suits of Club as
available.REMAINING.IMPORTANT!!!!

//HANDLE THE Array index out of bound EXCEPTION

//If the suit of club is also not available, then the player can throw any
card in that case, other suits are allowed.
int indexOfCard;
Card currentCard = new Card();
Card compareCard = new Card();
Vector currentPlayer = new Vector();
BitSet currentAllowed = new BitSet(13);
BitSet tempCard = new BitSet(13); //for the and operation of the flip of
thrown cards
BitSet currentThrown = new BitSet(13);
BitSet higherRanks = new BitSet(13);

currentAllowed.set(0,13,false);

boolean containsNoSuite; //to flag whether the current card set contains the
card of same suite as that of the first player
boolean containsNoClub; //to flag whether the current card set contains any
club.

if (playerNumber == 1){
currentPlayer = Ram;
currentThrown = (BitSet) thrownCardsRam.clone();
}

if (playerNumber == 2){
currentPlayer = Shyam;
currentThrown = (BitSet) thrownCardsShyam.clone();
}

if (playerNumber == 3){
currentPlayer = Hari;
currentThrown = (BitSet) thrownCardsHari.clone();
}

if (playerNumber == 4){
currentPlayer = Subash;
currentThrown = (BitSet) thrownCardsSubash.clone();
}

if (isFirst(RAMf)) {

System.out.println("");
System.out.println("Since Ram is the first player");
//if Ram is the first player here, we want to look at index 0 of the
current game vector.
compareCard = (Card) currentGame2.get(0); // hold the current card to
compare to. 0 for the first player.
containsNoSuite = true; //initially set this to true and if there are
no cards of the same suite we proceed to
//allow clubs and if there are no clubs, we allow remaining cards.
for (int i =0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try {
if ((currentCard.suit == compareCard.suit)&&(!
currentThrown.get(i))){ //if the suits are equal and the card is not thrown right
now
System.out.println("");
System.out.println("--From isFirst(RAMf)--");
printCurrentCompare(currentCard.suit,
compareCard.suit);
System.out.println("--");
indexOfCard = currentPlayer.indexOf(currentCard);
System.out.println("--indexOfCard--"+indexOfCard);
//we have the index of the card with the same suit as
the compare card
//Now we want to enable that card in the allowed card
currentAllowed.set(indexOfCard,true);

if (currentCard.rank > maxRankUptoNow(currentGame2)){


System.out.println("--");
System.out.println("Higher rank available");
System.out.println("--");
higherRanks.set(indexOfCard,true);
}//end of if for higher ranks

containsNoSuite = false;
} //end of if
} catch(IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is no suit of the card
thrown by first player.");
System.out.println("Going to check for Club, if
any...");
}//end of catch
}// end of for

//If the search through the loop has found no card with the same suit
as that of the first player, then
//this player should be allowed to throw a card of club. So we will be
allowing the clubs for this player.
containsNoClub = true;
if (containsNoSuite) {
for (int i=0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try{
if ((currentCard.suit == 1)&&(!currentThrown.get(i)))
{
//we have found a club
indexOfCard =
currentPlayer.indexOf(currentCard);
currentAllowed.set(indexOfCard,true);
containsNoClub = false;
} //end of inner if
} catch (IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is neither the suit
of the card thrown by first player.Nor the Club");
System.out.println("Going to check for other
cards, if any...");
}//end of catch
}//end of for
} //end of outer if

//Now if there is no suit and no club card, the player can throw any of
the card, so we allow all of the cards
//provided containsNoSuite and containsNoClub are both true.
if ((containsNoSuite) && (containsNoClub)) {
System.out.println("");
System.out.println("Allowing other cards because no same suit and
no club either.");
System.out.println("...");
for (int i=0; i < currentPlayer.size(); i++){
if (!currentThrown.get(i)){
currentAllowed.set(i,true);
}//end of inner if
}//end of for
}//end of if
}//end of isFirst

if (isFirst(SHYAMf)) {
System.out.println("");
System.out.println("Since Shyam is the first player");
//if Shyam is the first player here, we want to look at index 1 of the
current game vector.
compareCard = (Card) currentGame2.get(1); // hold the current card to
compare to. 1 for the second player.
containsNoSuite = true; //initially set this to true and if there are
no cards of the same suite we proceed to
//allow clubs and if there are no clubs, we allow remaining cards.
for (int i =0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try {

if ((currentCard.suit == compareCard.suit)&&(!
currentThrown.get(i))){
System.out.println("");
System.out.println("--From isFirst(SHYAMf)--");
printCurrentCompare(currentCard.suit,
compareCard.suit);
System.out.println("--");
indexOfCard = currentPlayer.indexOf(currentCard);
System.out.println("--indexOfCard--"+indexOfCard);
//we have the index of the card with the same suit as
the compare card
//Now we want to enable that card in the allowed card
currentAllowed.set(indexOfCard,true);
if (currentCard.rank > maxRankUptoNow(currentGame2)){
System.out.println("--");
System.out.println("Higher rank available");
System.out.println("--");
higherRanks.set(indexOfCard,true);
}//end of if for higher ranks
containsNoSuite = false;
}//end of if
}catch(IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is no suit of the card thrown by
first player.");
System.out.println("Going to check for Club, if any...");
}//end of catch
}// end of for

//If the search through the loop has found no card with the same suit
as that of the first player, then
//this player should be allowed to throw a card of club. So we will be
allowing the clubs for this player.
containsNoClub = true;
if (containsNoSuite) {
for (int i=0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try{
if ((currentCard.suit == 1)&&(!currentThrown.get(i)))
{
//we have found a club
indexOfCard =
currentPlayer.indexOf(currentCard);
currentAllowed.set(indexOfCard,true);
containsNoClub = false;
} //end of inner if
}catch (IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is neither the suit of the
card thrown by first player.Nor the Club");
System.out.println("Going to check for other cards,
if any...");
}//end of catch
}//end of for
} //end of outer if

//Now if there is no suit and no club card, the player can throw any of
the card, so we allow all of the cards
//provided containsNoSuite and containsNoClub are both true.
if ((containsNoSuite) && (containsNoClub)) {
System.out.println("");
System.out.println("Allowing other cards because no same suit and
no club either.");
System.out.println("...");
for (int i=0; i < currentPlayer.size(); i++){
if (!currentThrown.get(i)) {
currentAllowed.set(i,true);
}//end of if
}//end of for
}//end of outer if

}//end of isFirst

if (isFirst(HARIf)) {
//if Hari is the first player here, we want to look at index 2 of the
current game vector.
System.out.println("");
System.out.println("Since Hari is the first player");
compareCard = (Card) currentGame2.get(2); // hold the current card to
compare to. 2 for the third player.
containsNoSuite = true; //initially set this to true and if there are
no cards of the same suite we proceed to
//allow clubs and if there are no clubs, we allow remaining cards.
for (int i =0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try{

if ((currentCard.suit == compareCard.suit)&&(!
currentThrown.get(i))){
System.out.println("");
System.out.println("--From isFirst(HARIf)--");
printCurrentCompare(currentCard.suit,
compareCard.suit);
System.out.println("--");
indexOfCard = currentPlayer.indexOf(currentCard);
System.out.println("--indexOfCard--"+indexOfCard);
//we have the index of the card with the same suit as
the compare card
//Now we want to enable that card in the allowed card
currentAllowed.set(indexOfCard,true);
if (currentCard.rank > maxRankUptoNow(currentGame2)){
System.out.println("--");
System.out.println("Higher rank available");
System.out.println("--");
higherRanks.set(indexOfCard,true);
}//end of if for higher ranks
containsNoSuite = false;
}//end of if
}catch(IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is no suit of the card thrown by
first player.");
System.out.println("Going to check for Club, if any...");
}//end of catch
}// end of for

//If the search through the loop has found no card with the same suit as that
of the first player, then
//this player should be allowed to throw a card of club. So we will be
allowing the clubs for this player.
containsNoClub = true;
if (containsNoSuite) {
for (int i=0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try {
if ((currentCard.suit == 1)&&(!currentThrown.get(i)))
{
//we have found a club
indexOfCard =
currentPlayer.indexOf(currentCard);
currentAllowed.set(indexOfCard,true);
containsNoClub = false;
} //end of inner if
}catch (IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is neither the suit of the
card thrown by first player.Nor the Club");
System.out.println("Going to check for other cards,
if any...");
}//end of catch
}//end of for
} //end of outer if

//Now if there is no suit and no club card, the player can throw any of
the card, so we allow all of the cards
//provided containsNoSuite and containsNoClub are both true.
if ((containsNoSuite) && (containsNoClub)) {
System.out.println("");
System.out.println("Allowing other cards because no same suit and
no club either.");
System.out.println("...");
for (int i=0; i < currentPlayer.size(); i++){
if(!currentThrown.get(i)) {
currentAllowed.set(i,true);
}//end of if
}//end of for
}//end of outer if

}//end of isFirst

if (isFirst(SUBASHf)) {
//if Subash is the first player here, we want to look at index 3 of the
current game vector.
System.out.println("");
System.out.println("Since Subash is the first player");
compareCard = (Card) currentGame2.get(3); // hold the current card to
compare to. 3 for the fourth player.
containsNoSuite = true; //initially set this to true and if there are
no cards of the same suite we proceed to
//allow clubs and if there are no clubs, we allow remaining cards.
for (int i =0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try{

if ((currentCard.suit == compareCard.suit)&&(!
currentThrown.get(i))){
System.out.println("");
System.out.println("--From isFirst(SUBASHf)--");
printCurrentCompare(currentCard.suit,
compareCard.suit);
System.out.println("--");
indexOfCard = currentPlayer.indexOf(currentCard);
System.out.println("--indexOfCard--"+indexOfCard);
//we have the index of the card with the same suit as
the compare card
//Now we want to enable that card in the allowed card
currentAllowed.set(indexOfCard,true);
if (currentCard.rank > maxRankUptoNow(currentGame2)){
System.out.println("--");
System.out.println("Higher rank available");
System.out.println("--");
higherRanks.set(indexOfCard,true);
}//end of if for higher ranks
containsNoSuite = false;
}//end of if
}catch(IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is no suit of the card thrown by
first player.");
System.out.println("Going to check for Club, if any...");
}//end of catch
}// end of for

//If the search through the loop has found no card with the same suit as that
of the first player, then
//this player should be allowed to throw a card of club. So we will be
allowing the clubs for this player.
containsNoClub = true;
if (containsNoSuite) {
for (int i=0; i<currentPlayer.size(); i++){
currentCard = (Card) currentPlayer.get(i);
try {
if ((currentCard.suit == 1)&&(!currentThrown.get(i)))
{
//we have found a club
indexOfCard =
currentPlayer.indexOf(currentCard);
currentAllowed.set(indexOfCard,true);
containsNoClub = false;
} //end of inner if
}catch (IndexOutOfBoundsException e){
System.out.println("");
System.out.println("There is neither the suit of the
card thrown by first player.Nor the Club");
System.out.println("Going to check for other cards,
if any...");
}//end of catch
}//end of for
} //end of outer if

//Now if there is no suit and no club card, the player can throw any of
the card, so we allow all of the cards
//provided containsNoSuite and containsNoClub are both true.
if ((containsNoSuite) && (containsNoClub)) {
System.out.println("");
System.out.println("Allowing other cards because no same suit and
no club either.");
System.out.println("...");
for (int i=0; i < currentPlayer.size(); i++){
if(!currentThrown.get(i)) {
currentAllowed.set(i,true);
}
}//end of for
}//end of outer if

}//end of isFirst

//Now we want to further disallow the cards that have already been thrown

if (playerNumber == 1){
allowedCardsRam = currentAllowed;
tempCard = (BitSet) thrownCardsRam.clone();
tempCard.flip(0,13);
allowedCardsRam.and(tempCard);
if (!higherRanks.isEmpty()){
//if we have higher ranks available, we want to throw higher rank
cards
tempCard = (BitSet) higherRanks.clone();
allowedCardsRam.and(tempCard);
}//end of inner if
System.out.println("");
System.out.println("Higher ranks"+higherRanks);
System.out.println("");
}//end of outer if

if (playerNumber == 2){
allowedCardsShyam = currentAllowed;
tempCard = (BitSet) thrownCardsShyam.clone();
tempCard.flip(0,13);
allowedCardsShyam.and(tempCard);
if (!higherRanks.isEmpty()){
//if we have higher ranks available, we want to throw higher rank
cards
tempCard = (BitSet) higherRanks.clone();
allowedCardsShyam.and(tempCard);
}//end of inner if
System.out.println("");
System.out.println("Higher ranks"+higherRanks);
System.out.println("");
}

if (playerNumber == 3){
allowedCardsHari = currentAllowed;
tempCard = (BitSet) thrownCardsHari.clone();
tempCard.flip(0,13);
allowedCardsHari.and(tempCard);
if (!higherRanks.isEmpty()){
//if we have higher ranks available, we want to throw higher rank
cards
tempCard = (BitSet) higherRanks.clone();
allowedCardsHari.and(tempCard);
}//end of inner if
System.out.println("");
System.out.println("Higher ranks"+higherRanks);
System.out.println("");
}

if (playerNumber == 4){
allowedCardsSubash = currentAllowed;
tempCard = (BitSet) thrownCardsSubash.clone();
tempCard.flip(0,13);
allowedCardsSubash.and(tempCard);
if (!higherRanks.isEmpty()){
//if we have higher ranks available, we want to throw higher rank
cards
tempCard = (BitSet) higherRanks.clone();
allowedCardsSubash.and(tempCard);
}//end of inner if
System.out.println("");
System.out.println("Higher ranks"+higherRanks);
System.out.println("");
}

System.out.println("");
System.out.println("--From the prepareCard()--");
System.out.println("allowedCardsRam"+allowedCardsRam);
System.out.println("allowedCardsShyam"+allowedCardsShyam);
System.out.println("allowedCardsHari"+allowedCardsHari);
System.out.println("allowedCardsSubash"+allowedCardsSubash);
System.out.println("");
System.out.println("thrownCardsRam"+thrownCardsRam);
System.out.println("thrownCardsShyam"+thrownCardsShyam);
System.out.println("thrownCardsHari"+thrownCardsHari);
System.out.println("thrownCardsSubash"+thrownCardsSubash);
System.out.println("--End of prepareCard() --");

} //end of allowedCards

private static int maxRankUptoNow(Vector currentGame2) {


int maxRank;
Card compareCard = new Card(0,0);
Card tempCard = new Card();
int[] rank = new int[4];
for (int i=0; i<4; i++){
tempCard = (Card) currentGame2.get(i);
rank[i]=0;
if (!(tempCard.equals(compareCard))){
//if the current card is not empty
rank[i]=tempCard.rank;
}//end of if
}//end of for
maxRank=0;
for (int i=0; i<4; i++){
if (rank[i]>maxRank){
maxRank = rank[i];
}
}
return maxRank;
}

private static void printCurrentCompare(int suit, int suit2) {


// TODO Auto-generated method stub
System.out.println("--");
System.out.println("CurrentCardSuit ="+suit);
System.out.println("CompareCardSuit ="+suit2);
System.out.println("--");
}

private static Card strategy(BitSet allowedCards, int playerNumber) {


//for now we simplify our strategy and return the first card that is allowed
for this player
//player number 1 = Ram, 2 = Shyam, 3 = Hari, 4 = Subash
if (playerNumber == 1){
for (int i=0; i<=Ram.size(); i++){
if(isAllowed(i,allowedCardsRam)){ //this strategy is for Ram

return (Card) Ram.get(i);


}
}
} //end of if

if (playerNumber == 2){
for (int i=0; i<=Shyam.size(); i++){
if(isAllowed(i,allowedCardsShyam)){ //this strategy is for Shyam

return (Card) Shyam.get(i);


}
}
} //end of if

if (playerNumber == 3){
for (int i=0; i<=Hari.size(); i++){
if(isAllowed(i,allowedCardsHari)){ //this strategy is for Hari

return (Card) Hari.get(i);


}
}
} //end of if

if (playerNumber == 4){
for (int i=0; i<=Subash.size(); i++){
if(isAllowed(i,allowedCardsSubash)){ //this strategy is for
Subash

return (Card) Subash.get(i);


}
}
} //end of if
return null; //return nothing if everything fails
} //end of strategy class

private static void giveCards() {


// method to assign cards to individual players from a shuffled deck
for (int i=0; i<=12;i++){
Ram.add(deck.get(i));
Shyam.add(deck.get(i+13));
Hari.add(deck.get(i+26));
Subash.add(deck.get(i+39));
}
}//end of giveCards()

private static void shuffleCards() {


// method for shuffling the cards of the deck
java.util.Collections.shuffle(deck);//what a statement!

}//end of shuffle method

private static void printDeck(Vector deck2) {


//prints the currently passed deck

Iterator iterator = deck2.iterator();


while (iterator.hasNext()){
printCard((Card)iterator.next());
}

/* for (int i=0; i<deck2.size();i++){


Card trialCard = new Card();
trialCard = (Card)deck2.get(i);

printCard((Card) deck2.get(i));
}*/

}//end of printDeck method

public static void populateCards() {


// This method populates 52 cards into a vector

int index = 0;
for (int suit=1; suit<=4; suit++){
for (int rank=1; rank<=13; rank++){
deck.add(index,new Card(suit,rank));
index++;
}
}
}//end of populate cards method

public static void printCard(Card c){


//This method takes a card object and
//prints its rank and suit

String[] suits = {"narf","♠","♥","♣","♦"};


String[] ranks = {"narf","A","2","3","4","5",
"6","7","8","9","10","J","Q","K"};
System.out.print(" "+ranks[c.rank]+"."+suits[c.suit]);
}//end of printCard method

public static boolean isFirst (int playerToCheck){


//checks whether the given player number is the first player
boolean result = firstPlayer.get(playerToCheck);
return result;
} //end of isFirst method

public static boolean isAllowed (int cardNumToCheck, BitSet checkList ){


//checks whether the given card number is allowed. Note that this bitset is
the same for all players.
boolean result = checkList.get(cardNumToCheck);
return result;
} //end of isFirst method

}//end of class

You might also like