we where given a player class but we want to create an extra class for the game
ID: 3868310 • Letter: W
Question
we where given a player class but we want to create an extra class for the game
ExtraCards will risk taking cards from the draw pile in an effort to get power cards. They will be clever with this though. If the next player only has 1 card left, they will keep picking a card until they get a power card (if they do not already have one) so that they can try to prevent the next player from winning. They will not take more than one extra card in the early rounds of the game. They will not take extra cards if they already have power cards in their hand.
- Create an interesting (non-trivial) new player.
- Add file i/o to load players (and their win/los history), play matches and then update the saved data
- Add graphics to the game
Crazy Eights, also known as Eights and as Swedish Rummy, is a distant relative of Rummy. It's known as a "stops" game because players can be stopped from discarding if they don't have a proper card. It's believed that Crazy Eights can trace its heritage back to the mid-1600s and a French gambling game known as Hoc.
Players: 2 to 4 players.
Deck: Standard 52-card deck.
Goal: To discard all of your cards.
Setup: Choose a dealer. (The program is the dealer)
In a two-player game, each player is dealt seven cards. In a game with three or four players, each player is dealt five cards.
The remaining cards are placed face down in the center of the table, forming a draw pile. The top card of the draw pile is removed and turned face up to start the discard pile.
Gameplay: A player is chosen as the first player. Play moves clockwise. Here, clockwise means the next player in the list holding the players (wrapping around to the first when we go past the end).
On a turn, the given player adds to the discard pile by playing one card from their hand that matches the top card on the discard pile either by suit (diamonds, spades, etc) or by rank (6, 10, jack, etc.).
A player who cannot match the top card on the discard pile by suit or rank must draw cards from the draw pile until they can play one. A player is allowed to pull cards from the draw pile even if they already have a legal play (when it is their turn, before they discard to the discard pile). When the draw pile is empty, a player who cannot add to the discard pile passes their turn.
Power Cards: All eights are wild and can be played on any card during a player's turn. When a player discards an eight, they chooses which suit is now in play. They do this in our game by removing the 8 from their hand and placing a new card object on the discard pile that has rank 8 and the desired new suit.
The next player must play either a card of that suit or another eight.
All twos are special cards. When a player discards a two, the next player must take two cards from the draw pile and then end their turn without playing a card to the discard pile (or taking more cards from the draw pile).
All fours are special. When a player discards a four, the next player misses their turn and play goes to the following player.
All sevens are special. When a player discards a seven, the direction of play is reversed. The next player is the now the player who played just before the player who discarded the seven.
Winning: The first player to discard all of his cards wins.
(With four players, it is possible to play partnership. If you do this, the game ends when both members of a partnership discard all their cards.)
To play multiple games, add up the cards remaining in the losers' hands and give the points to the winner: 10 points for each face card, 1 point for each ace, 50 points for each eight, 25 points for each two or four, 20 points for each seven and the face value for the other number cards.
Games are then played until a player reaches some predetermined goal such as 150, 200, or 1000.
code for the crazy 8;
}
Game Play
You must ensure that a proper game of crazy 8's is played. This means that all played cards (cards added to the discard pile) are valid. At the end of a game, points should be awarded to the winning player.
There should be choice for 2,3 or 4 player games and whether or not it is a single game play or multiple game (with a set points goal).
we where given the player class as an abstract class
import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; import java.util.Random; public class Crazy8Game{ public static void main(String[] args){ /* create the deck */ Card[] deck = new Card[52]; int index = 0; for(int r=2; r<=14; r+=1){ for(int s=0; s<4; s+=1){ deck[index++] = new Card(Card.SUITS[s], Card.RANKS[r]); } } /* shuffle the deck */ Random rnd = new Random(); Card swap; for(int i = deck.length-1; i>=0; i=i-1){ int pos = rnd.nextInt(i+1); swap = deck[pos]; deck[pos] = deck[i]; deck[i] = swap; } /* players in the game */ Player[] players = new Player[3]; players[0] = new BadPlayer( Arrays.copyOfRange(deck, 0, 5) ); System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 0, 5))); players[1] = new BadPlayer( Arrays.copyOfRange(deck, 5, 10) ); System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 5, 10))); players[2] = new BadPlayer( Arrays.copyOfRange(deck, 10, 15) ); System.out.println("0 : " + Arrays.toString( Arrays.copyOfRange(deck, 10, 15))); /* discard and draw piles */ DiscardPile discardPile = new DiscardPile(); Stack<Card> drawPile = new Stack<Card>(); for(int i=15; i<deck.length; i++){ drawPile.push(deck[i]); } System.out.println("draw pile is : " + Arrays.toString( Arrays.copyOfRange(deck, 15, deck.length) )); deck = null; boolean win = false; int player = -1; // start game play with player 0 ArrayList<Player> people = new ArrayList<Player>(Arrays.asList(players)); discardPile.add( drawPile.pop() ); while( !win ){ player = (player + 1) % players.length; System.out.println("player " + player); System.out.println("draw pile : " + drawPile.peek() ); System.out.println("discard pile : " + discardPile.top() ); win = people.get(player).play(discardPile, drawPile, people); System.out.println("draw pile : " + drawPile.peek() ); System.out.println("discard pile : " + discardPile.top() ); } System.out.println("winner is player " + player); }}
Game Play
You must ensure that a proper game of crazy 8's is played. This means that all played cards (cards added to the discard pile) are valid. At the end of a game, points should be awarded to the winning player.
There should be choice for 2,3 or 4 player games and whether or not it is a single game play or multiple game (with a set points goal).
we where given the player class as an abstract class
import java.util.ArrayList; import java.util.Stack; public abstract class Player{ protected ArrayList<Card> hand; public int getSizeOfHand(){ return hand.size(); } /* play a card */ public abstract boolean play(DiscardPile discardPile, Stack<Card> drawPile, ArrayList<Player> players); // return true if player wins game by playing last card // returns false otherwise // side effects: plays a card to top of discard Pile, possibly taking zero // or more cards from the top of the drawPile // card played must be valid card }Explanation / Answer
BlackJack.java
import com.almasb.blackjack.Deck;
import com.almasb.blackjack.Hand;
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* Game's logic and UI
*
* @author Akshay Bisht
*/
public class BlackjackApp extends Application {
private Deck dck = new Deck();
private Hand dealr, playa;
private Text mssg = new Text();
private SimpleBooleanProperty played = new SimpleBooleanProperty(false);
private HBox dCrd = new HBox(20);
private HBox pCrd = new HBox(20);
private Parent createContent() {
dealr = new Hand(dCrd.getChildren());
playa = new Hand(pCrd.getChildren());
Pane rt = new Pane();
rt.setPrefSize(800, 600);
Region bg = new Region();
bg.setPrefSize(800, 600);
bg.setStyle("-fx-bg-color: rgba(0, 0, 0, 1)");
HBox rtLay = new HBox(5);
rtLay.setPadding(new Insets(5, 5, 5, 5));
Rectangle ltBG = new Rectangle(550, 560);
ltBG.setArcWidth(50);
ltBG.setArcHeight(50);
ltBG.setFill(Color.GREEN);
Rectangle rtBG = new Rectangle(230, 560);
rtBG.setArcWidth(50);
rtBG.setArcHeight(50);
rtBG.setFill(Color.ORANGE);
VBox ltVBox = new VBox(50);
ltVBox.setAlignment(Pos.TOP_CENTER);
Text dealScre = new Text("Dealer: ");
Text playaSc = new Text("Player: ");
ltVBox.getChildren().addAll(dealScre, dCrd, mssg, pCrd, playaSc);
VBox rtVbox = new VBox(20);
rtVbox.setAlignment(Pos.CENTER);
final TextField tf = new TextField("BET");
tf.setDisable(true);
tf.setMaxWidth(50);
Text txt = new Text("MONEY");
Button playBtn = new Button("PLAY");
Button hitBtn = new Button("HIT");
Button stndBtn = new Button("STAND");
HBox btnHBox = new HBox(15, hitBtn, stndBtn);
btnHBox.setAlignment(Pos.CENTER);
rtVbox.getChildren().addAll(tf, playBtn, txt, btnHBox);
rtLay.getChildren().addAll(new StackPane(ltBG, ltVBox), new StackPane(rtBG, rtVbox));
rt.getChildren().addAll(bg, rtLay);
playBtn.disableProperty().bind(played);
hitBtn.disableProperty().bind(played.not());
stndBtn.disableProperty().bind(played.not());
playaSc.textProperty().bind(new SimpleStringProperty("Player: ").concat(playa.valueProperty().asString()));
dealScre.textProperty().bind(new SimpleStringProperty("Dealer: ").concat(dealr.valueProperty().asString()));
playa.valueProperty().addListener((obs, old, newValue) -> {
if (newValue.intValue() >= 21) {
quitGame();
}
});
dealr.valueProperty().addListener((obs, old, newValue) -> {
if (newValue.intValue() >= 21) {
quitGame();
}
});
playBtn.setOnAction(event -> {
strtGame();
});
hitBtn.setOnAction(event -> {
playa.takeCard(dck.drawingCards());
});
stndBtn.setOnAction(event -> {
while (dealr.valueProperty().get() < 17) {
dealr.takeCard(dck.drawingCards());
}
quitGame();
});
return rt;
}
private void strtGame() {
played.set(true);
mssg.setText("");
dck.refilling();
dealr.res();
playa.res();
dealr.takeCard(dck.drawingCards());
dealr.takeCard(dck.drawingCards());
playa.takeCard(dck.drawingCards());
playa.takeCard(dck.drawingCards());
}
private void quitGame() {
played.set(false);
int dealVal = dealr.valueProperty().get();
int playVal = playa.valueProperty().get();
String winningSide = "Exceptional case: d: " + dealVal + " p: " + playVal;
if (dealVal == 21 || playVal > 21 || dealVal == playVal
|| (dealVal < 21 && dealVal > playVal)) {
winningSide = "DEALER";
}
else if (playVal == 21 || dealVal > 21 || playVal > dealVal) {
winningSide = "PLAYER";
}
mssg.setText(winningSide + " WON");
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.setWidth(800);
primaryStage.setHeight(600);
primaryStage.setResizable(false);
primaryStage.setTitle("BlackJack");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Card.java
package com.almasb.blackjack;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
/**
*
* @author Akshay Bisht
*/
public class Card extends Parent {
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 140;
double length;
void add(Card crds) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
enum Suitss {
DIL, EET, HUKUM, KALAPAAN;
final Image img;
Suitss() {
this.img = new Image(Card.class.getResourceAsStream("images/".concat(name().toLowerCase()).concat(".png")),
32, 32, true, true);
}
}
enum ranks {
TOO(2), TREE(3), CHAAR(4), PAACH(5), CHEH(6), SAAT(7), AATH(8), NAU(9), DAS(10),
GHULAM(10), RANI(10), RAJA(10), IKKA(11);
final int val;
ranks(int val) {
this.val = val;
}
String displayName() {
return ordinal() < 9 ? String.valueOf(val) : name().substring(0, 1);
}
}
public final Suitss suits;
public final ranks ranking;
public final int val;
public Card(Suitss suits, ranks ranking) {
this.suits = suits;
this.ranking = ranking;
this.val = ranking.val;
Rectangle bg = new Rectangle(CARD_WIDTH, CARD_HEIGHT);
bg.setArcWidth(20);
bg.setArcHeight(20);
bg.setFill(Color.WHITE);
Text txt1 = new Text(ranking.displayName());
txt1.setFont(Font.font(18));
txt1.setX(CARD_WIDTH - txt1.getLayoutBounds().getWidth() - 10);
txt1.setY(txt1.getLayoutBounds().getHeight());
Text txt2 = new Text(txt1.getText());
txt2.setFont(Font.font(18));
txt2.setX(10);
txt2.setY(CARD_HEIGHT - 10);
ImageView iv = new ImageView(suits.img);
iv.setRotate(180);
iv.setX(CARD_WIDTH - 32);
iv.setY(CARD_HEIGHT - 32);
getChildren().addAll(bg, new ImageView(suits.img), iv, txt1, txt2);
}
@Override
public String toString() {
return ranking.toString() + " of " + suits.toString();
}
}
Deck.java
package com.almasb.blackjack;
import com.almasb.blackjack.Card.ranks;
import com.almasb.blackjack.Card.Suitss;
/**
*
* @author Akshay Bisht
*/
public class Deck {
private Card[] crds = new Card[52];
public Deck() {
refilling();
}
public final void refilling() {
int uu = 0;
for (Suitss suits : Suitss.values()) {
for (ranks ranking : ranks.values()) {
crds[uu++] = new Card(suits, ranking);
}
}
}
public Card drawingCards() {
Card crds = null;
while (crds == null) {
int indices = (int)(Math.random()*crds.length);
}
return crds;
}
}
Hand,java
package com.almasb.blackjack;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import com.almasb.blackjack.Card.ranks;
/**
*
* @author Akshay Bisht
*/
public class Hand {
private ObservableList<Node> crds;
private SimpleIntegerProperty val = new SimpleIntegerProperty(0);
private int aces = 0;
public Hand(ObservableList<Node> crds) {
this.crds = crds;
}
public void takeCard(Card crds) {
crds.add(crds);
if (crds.ranking == ranks.IKKA) {
aces++;
}
if (val.get() + crds.val > 21 && aces > 0) {
val.set(val.get() + crds.val - 10); //then count ace as '1' not '11'
aces--;
}
else {
val.set(val.get() + crds.val);
}
}
public void res() {
crds.clear();
val.set(0);
aces = 0;
}
public SimpleIntegerProperty valueProperty() {
return val;
}
}
Rate the answer an upvote......Thankyou
Hope this helps.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.