In: Computer Science
*in Java
1.Create a card class with two attributes, suit and rank.
2.In the card class, create several methods:
a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards.
b.shuffleDeck – input an unshuffled deck array and return a shuffled one.
+ Create a swap method to help shuffle the deck
c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of high-card points
d. writeHandOutput – input a bridge-hand and prints it out on the monitor
e. writeDeckOuput – inputs a deck of bridge cards and prints it out on the monitor
a. Ask the user what kind of deck he/she wants to create (pinochle or bridge). For this project, have the user input ‘bridge’.
b. Create the user-asked-for deck
c. Print out the unshuffled deck
d. Shuffle the deck
e. Create 4 Bridge hands of 13 cards each, call them North, South, East and West.
+ Deal every fourth card to each of the hands
f. Calculate the high-card-points in each hand
g. Print out each hand and the number of high-card-points associated with each hand
h. Sort the hands from highest to lowest number of points – use Insertion Sort we did before
i. Print out each hands points from highest to lowest.
I'm not sure whether my code is correct or wrong, but I hope it works. Actually I don't know the play of pinochle or the Bridge Game, so I wrote the code for the HighLow Game with a slight modifications in the method names! Hope it works with you.
Source Code:
public class Card { public final static int SPADES = 0; // Codes for the 4 suits, plus Joker. public final static int HEARTS = 1; public final static int DIAMONDS = 2; public final static int CLUBS = 3; public final static int JOKER = 4; public final static int ACE = 1; // Codes for the non-numeric cards. public final static int JACK = 11; // Cards 2 through 10 have their public final static int QUEEN = 12; // numerical values for their codes. public final static int KING = 13; private final int suit; private final int value; public Card() { suit = JOKER; value = 1; } public Card(int theValue, int theSuit) { if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && theSuit != CLUBS && theSuit != JOKER) throw new IllegalArgumentException("Illegal playing card suit"); if (theSuit != JOKER && (theValue < 1 || theValue > 13)) throw new IllegalArgumentException("Illegal playing card value"); value = theValue; suit = theSuit; } public int getSuit() { return suit; } public int getValue() { return value; } public String getSuitAsString() { switch ( suit ) { case SPADES: return "Spades"; case HEARTS: return "Hearts"; case DIAMONDS: return "Diamonds"; case CLUBS: return "Clubs"; default: return "Joker"; } } public String getValueAsString() { if (suit == JOKER) return "" + value; else { switch ( value ) { case 1: return "Ace"; case 2: return "2"; case 3: return "3"; case 4: return "4"; case 5: return "5"; case 6: return "6"; case 7: return "7"; case 8: return "8"; case 9: return "9"; case 10: return "10"; case 11: return "Jack"; case 12: return "Queen"; default: return "King"; } } } public String toString() { if (suit == JOKER) { if (value == 1) return "Joker"; else return "Joker #" + value; } else return getValueAsString() + " of " + getSuitAsString(); } } // end class Card
This is the required card class.
Now the program for the HighLow game is given below:
Source Code:
import textio.TextIO; public class HighLow { public static void main(String[] args) { System.out.println("This program lets you play the simple card game,"); System.out.println("HighLow. A card is dealt from a deck of cards."); System.out.println("You have to predict whether the next card will be"); System.out.println("higher or lower. Your score in the game is the"); System.out.println("number of correct predictions you make before"); System.out.println("you guess wrong."); System.out.println(); int gamesPlayed = 0; // Number of games user has played. int sumOfScores = 0; // The sum of all the scores from // all the games played. double averageScore; // Average score, computed by dividing // sumOfScores by gamesPlayed. boolean playAgain; // Record user's response when user is // asked whether he wants to play // another game. do { int scoreThisGame; // Score for one game. scoreThisGame = play(); // Play the game and get the score. sumOfScores += scoreThisGame; gamesPlayed++; System.out.print("Play again? "); playAgain = TextIO.getlnBoolean(); } while (playAgain); averageScore = ((double)sumOfScores) / gamesPlayed; System.out.println(); System.out.println("You played " + gamesPlayed + " games."); System.out.printf("Your average score was %1.3f.\n", averageScore); } // end main() private static int play() { Deck deck = new Deck(); // Get a new deck of cards, and // store a reference to it in // the variable, deck. Card currentCard; // The current card, which the user sees. Card nextCard; // The next card in the deck. The user tries // to predict whether this is higher or lower // than the current card. int correctGuesses ; // The number of correct predictions the // user has made. At the end of the game, // this will be the user's score. char guess; // The user's guess. 'H' if the user predicts that // the next card will be higher, 'L' if the user // predicts that it will be lower. deck.shuffle(); // Shuffle the deck into a random order before // starting the game. correctGuesses = 0; currentCard = deck.dealCard(); System.out.println("The first card is the " + currentCard); while (true) { // Loop ends when user's prediction is wrong. /* Get the user's prediction, 'H' or 'L' (or 'h' or 'l'). */ System.out.print("Will the next card be higher (H) or lower (L)? "); do { guess = TextIO.getlnChar(); guess = Character.toUpperCase(guess); if (guess != 'H' && guess != 'L') System.out.print("Please respond with H or L: "); } while (guess != 'H' && guess != 'L'); /* Get the next card and show it to the user. */ nextCard = deck.dealCard(); System.out.println("The next card is " + nextCard); /* Check the user's prediction. */ if (nextCard.getValue() == currentCard.getValue()) { System.out.println("The value is the same as the previous card."); System.out.println("You lose on ties. Sorry!"); break; // End the game. } else if (nextCard.getValue() > currentCard.getValue()) { if (guess == 'H') { System.out.println("Your prediction was correct."); correctGuesses++; } else { System.out.println("Your prediction was incorrect."); break; // End the game. } } else { // nextCard is lower if (guess == 'L') { System.out.println("Your prediction was correct."); correctGuesses++; } else { System.out.println("Your prediction was incorrect."); break; // End the game. } } currentCard = nextCard; System.out.println(); System.out.println("The card is " + currentCard); } // end of while loop System.out.println(); System.out.println("The game is over."); System.out.println("You made " + correctGuesses + " correct predictions."); System.out.println(); return correctGuesses; } // end play() } // end class HighLow
Hope this helps you well!!