Instructions
Objective
To create card games in Java, you need to write a Java assignment that involves implementing a deck of cards, shuffling, dealing, and managing the game logic. Start by defining a Card class representing each card's suit and rank. Then, build a Deck class containing an array of cards and methods to shuffle and deal them. Next, design specific game classes like Poker, Blackjack, or Solitaire, depending on the requirements of the assignment. Each game class should handle the game rules and player interactions. Finally, test your program thoroughly to ensure smooth execution and adherence to the rules of the chosen card game.
Requirements and Specifications
Source Code
BLACK JACK
package Game;
import Structure.*;
import java.util.ArrayList;
public class BlackJack extends CardGame {
private int maxScore = 21;
private int noOfCards = 2;
public BlackJack(){
super();
}
public BlackJack(String override){
super(override);
}
protected String generateHelp(){
String help = "Please select one of the following options\n";
for (BlackJackActions action : BlackJackActions.values()) {
help += action.display() + "\n";
}
return help;
}
protected BlackJackActions getPlayerAction(Player player){
String userChoice = " ";
BlackJackActions userAction;
help();
if (player.hasHand()) {
output(player.getHand().toString());
}
userChoice = getString();
userAction = BlackJackActions.getAction(userChoice.substring(0,1).toUpperCase());
output("You chose " + userAction.display());
return userAction;
}
protected void userPlays(Player player, Deck deck){
BlackJackActions userAction = BlackJackActions.PLAY;
while (getScore(player.getHand()) <= maxScore && userAction != BlackJackActions.STICK){
userAction = getPlayerAction(player);
if (userAction == BlackJackActions.TWIST){
player.getHand().add(deck.playACard());
}
}
setFinishGame(true);
}
protected void computerPlays(Player player, Deck deck){
Hand hand = player.getHand();
while (getScore(hand) <= player.levelOfRisk){
hand.add(deck.playACard());
}
}
protected Player determineWinner(ArrayList players){
Integer winningScore = 0;
Player winningPlayer = null;
int currentScore = 0;
for (Player player : players){
currentScore = getScore(player.getHand());
if (currentScore <= maxScore && currentScore > winningScore){
winningPlayer = player;
winningScore = currentScore;
}
}
if (winningPlayer != null) {
winningPlayer.setWinner(true);
}
return winningPlayer;
}
public int getScore(Hand hand){
int score = 0;
Boolean hasAnAce = false;
for (Card card: hand.getHandOfCards()){
if (card.getCardRank() == CardRank.ACE){
hasAnAce = true;
}
score += card.getCardRank().getValue();
}
if (score > maxScore && hasAnAce){
score -= CardRank.ACE.getValue() - 1;
}
return score;
}
public static void main(String[ ] args) {
BlackJack blackJack = new BlackJack();
blackJack.setNoOfCards(blackJack.noOfCards);
blackJack.play();
}
}
BLACK JACK ACTION
package Game;
import java.util.HashMap;
import java.util.Map;
public enum BlackJackActions {
TWIST("Twist"),
STICK("Stick"),
PLAY("Play New Game"),
END("End");
private String fullName;
private static final Map lookup = new HashMap();
BlackJackActions(String fullName) {
this.fullName = fullName;
}
static {
for (BlackJackActions action : BlackJackActions.values()) {
lookup.put(action.fullName.substring(0, 1), action);
}
}
public String display() {
return fullName;
}
public static BlackJackActions getAction(String shortCode) {
return lookup.get(shortCode);
}
}
CARD GAME
package Game;
import java.util.ArrayList;
import java.util.List;
import Console.Input;
import Console.Output;
import Structure.*;
import Console.ConsoleOutput;
import Console.ConsoleInput;
import Structure.LoadConfig;
public class CardGame {
protected Integer noOfCards = 2;
protected Deck deck;
public ArrayList players;
protected Output userOutput = new ConsoleOutput();
protected Input userInput = new ConsoleInput(userOutput);
protected boolean finshGame = false;
private Config loadConfig = new LoadConfig();
public void setNoOfCards(Integer noOfCards) {
this.noOfCards = noOfCards;
}
public void setFinishGame(boolean finshGame){
this.finshGame = finshGame;
}
public void setLoadConfig(Config loadConfig){
this.loadConfig = loadConfig;
}
public void setUserInput(Input userInput){
this.userInput = userInput;
}
public Deck getDeck() {
return deck;
}
protected void output(String message){
userOutput.output(message);
}
protected String generateHelp(){
return "";
}
protected void help(){
output(generateHelp());
}
protected String getString(){
return userInput.getString();
}
public int getInteger(){
return userInput.getInteger();
}
public CardGame(){
this.deck = new Deck();
this.players = new ArrayList();
}
public CardGame(String override){
this.deck = new Deck(override);
this.players = new ArrayList();
}
protected void createHumanPlayer(){
output("What is your name");
String name = getString();
players.add(new Player(PlayerType.USER,name,0));
}
protected List getComputerPlayersNames(){
return loadConfig.getConfig();
}
protected void createComputerPlayers(Integer noOfComputerPlayers) {
List computerNames = getComputerPlayersNames();
Player dealer = new Player(PlayerType.DEALER,computerNames.remove(0),17);
players.add(dealer);
for (int counter=1;counter < noOfComputerPlayers;counter++){
players.add(new Player(PlayerType.COMPUTER,computerNames.remove(0),0));
}
}
protected int getNumberOfComputerPlayers(){
int noOfPlayers = 0;
while ( noOfPlayers < 1 || noOfPlayers > 8) {
output("How many computer players, minimum of one and max eight?");
noOfPlayers = getInteger();
}
return noOfPlayers;
}
public void initiatePlayers(){
players.clear();
createHumanPlayer();
createComputerPlayers(getNumberOfComputerPlayers());
}
public void resetPlayers(){
for (Player player : players){
player.setWinner(false);
}
}
public void dealCards(){
boolean allCards = false;
int noOfCards;
if (this.noOfCards == 0) {
noOfCards = (int) Math.floor(deck.size()/players.size());
allCards = true;
} else {
noOfCards = this.noOfCards;
}
for (Player player: players){
Hand hand = new Hand();
hand = dealHand(hand,noOfCards);
player.setHand(hand);
}
if (allCards){
for (Player player: players){
if (deck.size() > 0){
player.getHand().add(deck.playACard());
}
}
}
}
protected Hand dealHand(Hand hand, int noOfCards){
for (int cardCounter=0;cardCounter
if (deck.size() > 0) {
hand.add(deck.playACard());
}
}
return hand;
}
public void initiate(){
initiatePlayers();
deck.shuffleDeck();
dealCards();
}
protected Player determineWinner(ArrayList players){
Integer winningScore = 0;
Player winningPlayer = null;
int currentScore = 0;
for (Player player : players){
currentScore = getScore(player.getHand());
if (currentScore > winningScore) {
winningScore = currentScore;
winningPlayer = player;
}
}
return winningPlayer;
}
public void play(){
initiate();
Integer counterOfPlayers = 0;
do {
playerPlaysHand(players.get(counterOfPlayers));
counterOfPlayers = (counterOfPlayers + 1) % players.size();
} while (!players.get(counterOfPlayers).hasWon() && !finshGame);
resetPlayers();
Player winner = determineWinner(players);
showPlayers();
}
public int getScore(Hand hand){
return 0;
}
protected void userPlays(Player player, Deck deck){}
protected void computerPlays(Player player, Deck deck){}
protected void playerPlaysHand(Player player){
if (player.getPlayerType() == PlayerType.USER){
userPlays(player, deck);
} else {
computerPlays(player, deck);
}
}
public void showPlayers(){
String theWinner = "";
for (Player player: players){
if (player.hasWon()){
theWinner = "The winner is ";
} else {
theWinner = "";
}
output(theWinner + player.getName() + " - " + player.getHand().toString());
}
}
public static void main(String[ ] args) {
CardGame cardGame = new CardGame();
cardGame.play();
cardGame.showPlayers();
}
}
SEVENS
package Game;
import Structure.*;
import java.util.ArrayList;
public class Sevens extends CardGame {
private int noOfCards = 0;
private Hand[] sevenTableHands = createSevensTableHands();
public Sevens(){
super();
}
public Sevens(String override){
super(override);
}
protected String generateHelp(){
return "Please enter a card to play.";
}
protected void displaySevensTableHands(){
int counter = 0;
for (Hand hand : sevenTableHands){
output(Suit.values()[counter].displayCamelCase() + " " + hand.toString());
counter += 1;
}
}
protected void userPlays(Player player, Deck deck){
displaySevensTableHands();
output("Your hand is");
output(player.getHand().toString());
help();
boolean cardPlayed = false;
String userCardRaw = "";
Card userCard = null;
while (!cardPlayed && player.getHand().size() > 0) {
userCardRaw = getString();
userCard = player.getHand().findACard(userCardRaw);
if (userCard == null) {
if (userCardRaw.length() >= 2) {
output("Structure.Card not found in your hand");
}
cardPlayed = true;
} else {
cardPlayed = playToSevenTableHands(userCard, sevenTableHands, true);
if (cardPlayed) {
cardPlayed = player.getHand().playACard(userCard);
}
}
}
checkIfPlayerHasWon(player);
}
protected void computerPlays(Player player, Deck deck){
boolean cardPlayed = false;
int counter = 0;
ArrayList compupterHand = player.getHand().getHandOfCards();
while (!cardPlayed && counter < compupterHand.size()){
cardPlayed = playToSevenTableHands(compupterHand.get(counter), sevenTableHands, true);
if (cardPlayed){
compupterHand.remove(counter);
}
counter += 1;
}
checkIfPlayerHasWon(player);
}
protected void checkIfPlayerHasWon(Player player){
if (player.getHand().size() == 0){
player.setWinner(true);
}
}
protected Player determineWinner(ArrayList players) {
Player winningPlayer = null;
for (Player player : players) {
if (player.getHand().size() == 0){
player.setWinner(true);
winningPlayer = player;
}
}
return winningPlayer;
}
protected Hand[] createSevensTableHands(){
Hand[] sevensTableHands = new Hand[]{new Hand(), new Hand(), new Hand(), new Hand()};
return sevensTableHands;
}
protected boolean playToSevenHand(Card card, Hand sevensHand, boolean playCard){
boolean cardHasBeenPlayed = false;
if (sevensHand.size() == 0 && card.getCardRank() == CardRank.SEVEN){
cardHasBeenPlayed = true;
if (playCard) {
sevensHand.add(card);
}
} else if (sevensHand.size() > 0 && card.getCardRank() != CardRank.SEVEN){
if (sevensHand.getFirstCard().getCardRank().getRank() == (card.getCardRank().getRank() + 1 )){
cardHasBeenPlayed = true;
if (playCard) {
sevensHand.addAtStart(card);
}
} else if ( sevensHand.getLastCard().getCardRank().getRank() == (card.getCardRank().getRank() -1)) {
cardHasBeenPlayed = true;
if (playCard) {
sevensHand.add(card);
}
}
}
return cardHasBeenPlayed;
}
protected boolean playToSevenTableHands(Card card, Hand[] sevenTableHands, boolean playCard){
Suit cardSuit = card.getSuit();
return playToSevenHand(card, sevenTableHands[cardSuit.ordinal()], playCard);
}
protected boolean canPlayACard(Card card, Hand[] sevensTableHands){
return playToSevenTableHands(card, sevensTableHands, false);
}
public static void main(String[ ] args) {
Sevens sevens = new Sevens();
sevens.setNoOfCards(sevens.noOfCards);
sevens.play();
}
}
SNAP
package Game;
import Structure.Card;
import Structure.Deck;
import Structure.Hand;
import Structure.Player;
import java.util.ArrayList;
public class Snap extends CardGame {
private int noOfCards = 0;
Hand tableHand = new Hand();
private boolean snapOnSuit = true;
public void setSnapOnSuit(boolean snapOnSuit){
this.snapOnSuit = snapOnSuit;
}
protected String generateHelp(){
return "Please enter a card to (P)lay or (S)nap.";
}
protected void userPlays(Player player, Deck deck){
if (tableHand.size() == 0) {
tableHand.add(player.getHand().playACard(0));
}
output("Table hand " + tableHand.getLastCard());
Card userCard = player.getHand().playACard(0);
output("User plays " + userCard.toString());
help();
String userChoice = getString();
if (userChoice.equals("S") && isSnap(tableHand.getLastCard(), userCard, snapOnSuit)){
setFinishGame(true);
} else if (userChoice.equals("S")) {
output("It was not snap");
}
tableHand.add(userCard);
}
protected boolean isSnap(Card previousCard, Card currentCard, boolean snapOnSuit){
boolean cardsMatch = false;
if (snapOnSuit && previousCard.getSuit() == currentCard.getSuit()) {
cardsMatch = true;
} else if (!snapOnSuit && previousCard.getCardRank() == currentCard.getCardRank()){
cardsMatch = true;
}
return cardsMatch;
}
protected void computerPlays(Player player, Deck deck){
Card computerCard = player.getHand().playACard(0);
//output("Computer plays " + computerCard.toString());
tableHand.add(computerCard);
}
protected Player determineWinner(ArrayList players) {
Player winningPlayer = null;
for (Player player : players) {
if (player.hasWon()){
winningPlayer = player;
}
}
return winningPlayer;
}
public static void main(String[ ] args) {
Snap snap = new Snap();
snap.setNoOfCards(snap.noOfCards);
snap.play();
}
}
Similar Samples
Explore our sample assignments showcasing expertly crafted solutions across various programming languages and topics. Get insights into our approach and quality, ensuring your programming challenges are met with precision and clarity.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java