我正在用 Java 创建一个 BlackJack 游戏,并在创建它时尝试使用 MVC 设计。到目前为止,我已经创建了以下类: - Card:保存创建一张牌 - Deck:创建一个装满所有 52 张牌的牌组 - Hand:持有一张牌的“手” - Player:创建一个供游戏使用的 Player - BlackJackTest:在这里我正在测试游戏的所有功能,试图让某些东西正常工作!
我目前已经到了可以展示所有 52 张牌的程度,并且还可以将 2 张牌发给一手牌并展示这手牌。但是我在接下来的步骤中遇到了很多麻烦。理想情况下,在“BlackJackTest”类中,我希望将其用作游戏引擎,在其中输入 X 数量的玩家及其姓名,它会创建玩家,并循环通过系统来玩游戏。
我的问题是我完全坚持下一步应该做什么,或者我做错了什么。我的下一个任务应该是实现一个功能,以便我可以让最多 3 名人类玩家对抗庄家,但我不确定如何做到这一点……我还应该实现我不确定的击中/站立功能我应该在哪里或如何编码这些。
任何帮助表示赞赏!
这是我的代码:
import java.util.Scanner;
public class BlackJackTest {
public static void main(String[] args) {
/*System.out.println(deck.getCard(0).toString());
System.out.println(deck.getCard(0).getSuit());
System.out.println(deck.getCard(0).getSuitCode());
System.out.println(deck.getCard(0).getValue());
System.out.println(deck.getCard(0).getValueCode());
System.out.println();
System.out.println(deck.cardsLeft());
hand.addCard( deck.dealCard() );
hand.addCard( deck.dealCard() );
hand.showHand();
System.out.println(deck.cardsLeft());
System.out.println(hand.getHandSize());
System.out.println(hand.getCard(0));
System.out.println(hand.getCard(1));
System.out.println(hand.getBlackJackValue());
System.out.println();
*/
Deck deck = new Deck();
Hand p1hand = new Hand();
Player player = new Player();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
player.setName(sc.nextLine());
System.out.println("Welcome " + player.getName() + "!");
player.setHand(p1hand);
p1hand.addCard( deck.dealCard() );
p1hand.addCard( deck.dealCard() );
System.out.println(player.getName() + ": ");
p1hand.showHand();
System.out.println("Score: " + p1hand.getBlackJackValue());
System.out.println();
}
}
public class Card {
public enum Value {
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(10),
QUEEN(10),
KING(10),
ACE(1);
public final int value;
Value(int c) {
value = c;
}
}
public enum Suit {
CLUBS('C'),
SPADES('S'),
DIAMONDS('D'),
HEARTS('H');
public final char symbol;
Suit(char c) {
symbol = c;
}
}
private Suit suit;
private Value value;
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
public Suit getSuit() {
return suit;
}
public Value getValue() {
return value;
}
public char getSuitCode() {
return suit.symbol;
}
public int getValueCode() {
return value.value;
}
public String toString() {
return value + " of " + suit;
}
}
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Deck {
private List<Card> deck = new LinkedList<Card>();
public Deck() {
for(Card.Suit suit : Card.Suit.values()) {
for(Card.Value value : Card.Value.values()) {
deck.add(new Card(suit,value));
}
}
shuffle();
}
public void shuffle() {
Collections.shuffle(deck);
}
public int cardsLeft() {
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.
return deck.size();
}
public Card dealCard() {
// Deals one card from the deck and returns it.
if (deck.size() == 52) {
shuffle();
}
Card temp;
temp = deck.get(0);
remove(0);
return temp;
}
public Card getCard(int i) {
return deck.get(i);
}
public Card remove(int i) {
Card c = deck.get(i);
deck.remove(i);
return c;
}
}
import java.util.Vector;
public class Hand {
private Vector<Card> hand;
public Hand() {
hand = new Vector<Card>();
}
public void addCard(Card c) {
if (c != null)
hand.addElement(c);
}
public void showHand() {
for(int i = 0; i < hand.size(); i++){
System.out.println(hand.elementAt(i));
}
}
public int getHandSize() {
return hand.size();
}
public Card getCard(int position) {
if(position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public int getBlackJackValue() {
int val = 0;
boolean ace = false;
for(int i = 0; i < hand.size(); i++) {
Card card;
card = getCard(i);
val += card.getValueCode();
if(card.getValueCode() == 1) {
ace = true;
}
}
if(ace == true && val + 10 <= 21) {
val += 10;
}
return val;
}
}
public class Player {
private String name;
private Hand hand;
public Player() {
this.hand = null;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setHand(Hand h) {
this.hand = h;
}
public void getHand(){
hand.showHand();
}
}