用java编写“21点”的扑克游戏(每个需求用一个测试类来完成测试) 需求一: 计算机随机地向用户发5张牌,如果牌的总点数小於或等於21点,则用户赢;超过21点则计算机赢。 注意类的没有参数的构造器随机产生一张牌,并且不会重复发牌。类模仿真实牌局的行为,可以用getValue方法从一副牌中重复抽取牌。 你的程序应该: 抽取一张牌; 向用户显示这张牌; 显示到目前为止已发牌的总点数 需求二: 为使程序更象真实的牌局,用户应该能与计算机互动玩牌。修改刚才的程序,计算机仍然抽取5张牌,不超过21点的是赢家,如果双方都不超过21点,则双方都不算赢,你的程序应该能准确地报告结果。 程序应该: 为用户和计算机抽牌; 向用户显示牌; 分别显示用户和计算机的当前总点数; 为使游戏更加有趣,每抽一张新牌时都要显示牌和总点数 需求三: 修改程序使用户可以选择是否抽牌,而规定计算机必须抽5张牌或直到牌的总点数大於等於21 需求四: 修改程序使得用户可以一直玩下去,并分别累计用户和计算机赢的牌局数
时间: 2024-02-07 15:01:56 浏览: 15
好的,以下是一个简单的Java实现,包括扑克牌类、扑克牌组类、游戏类和比赛类。
扑克牌类:
```java
public class Card {
private String suit;
private int value;
public Card(String suit, int value) {
this.suit = suit;
this.value = value;
}
public String getSuit() {
return suit;
}
public int getValue() {
return value;
}
}
```
扑克牌组类:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Deck {
private List<Card> cards;
public Deck() {
cards = new ArrayList<>();
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
for (String suit : suits) {
for (int value = 1; value <= 13; value++) {
cards.add(new Card(suit, value));
}
}
}
public Card drawCard() {
Random random = new Random();
int index = random.nextInt(cards.size());
return cards.remove(index);
}
}
```
游戏类:
```java
import java.util.Scanner;
public class Game {
private Deck deck;
private Card[] userCards;
private Card[] computerCards;
public Game() {
deck = new Deck();
userCards = new Card[5];
computerCards = new Card[5];
}
public void start() {
for (int i = 0; i < 5; i++) {
userCards[i] = deck.drawCard();
computerCards[i] = deck.drawCard();
}
int userTotal = getTotal(userCards);
int computerTotal = getTotal(computerCards);
System.out.println("Your cards:");
for (Card card : userCards) {
System.out.println(card.getSuit() + " " + getFaceValue(card.getValue()));
}
System.out.println("Total: " + userTotal);
System.out.println("Computer's first card:");
System.out.println(computerCards[0].getSuit() + " " + getFaceValue(computerCards[0].getValue()));
if (userTotal <= 21 && (userTotal > computerTotal || computerTotal > 21)) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
int userTotal = getTotal(userCards);
int computerTotal = getTotal(computerCards);
while (userTotal <= 21) {
System.out.println("Your cards:");
for (Card card : userCards) {
System.out.println(card.getSuit() + " " + getFaceValue(card.getValue()));
}
System.out.println("Total: " + userTotal);
System.out.println("Computer's first card:");
System.out.println(computerCards[0].getSuit() + " " + getFaceValue(computerCards[0].getValue()));
System.out.print("Do you want to draw another card? (y/n) ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("y")) {
userCards[getEmptyIndex(userCards)] = deck.drawCard();
userTotal = getTotal(userCards);
} else {
break;
}
}
while (computerTotal < 21 && getEmptyIndex(computerCards) != -1) {
computerCards[getEmptyIndex(computerCards)] = deck.drawCard();
computerTotal = getTotal(computerCards);
}
System.out.println("Your cards:");
for (Card card : userCards) {
System.out.println(card.getSuit() + " " + getFaceValue(card.getValue()));
}
System.out.println("Total: " + userTotal);
System.out.println("Computer's cards:");
for (Card card : computerCards) {
System.out.println(card.getSuit() + " " + getFaceValue(card.getValue()));
}
System.out.println("Total: " + computerTotal);
if (userTotal > 21) {
System.out.println("You bust! Computer wins!");
} else if (computerTotal > 21) {
System.out.println("Computer busts! You win!");
} else if (userTotal > computerTotal) {
System.out.println("You win!");
} else if (userTotal < computerTotal) {
System.out.println("Computer wins!");
} else {
System.out.println("It's a tie!");
}
}
private int getTotal(Card[] cards) {
int total = 0;
for (Card card : cards) {
total += Math.min(card.getValue(), 10);
}
return total;
}
private int getEmptyIndex(Card[] cards) {
for (int i = 0; i < cards.length; i++) {
if (cards[i] == null) {
return i;
}
}
return -1;
}
private String getFaceValue(int value) {
if (value == 1) {
return "Ace";
} else if (value == 11) {
return "Jack";
} else if (value == 12) {
return "Queen";
} else if (value == 13) {
return "King";
} else {
return Integer.toString(value);
}
}
}
```
比赛类:
```java
import java.util.Scanner;
public class Match {
private int userWins;
private int computerWins;
public Match() {
userWins = 0;
computerWins = 0;
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
Game game = new Game();
game.play();
if (game.getTotal(game.userCards) > 21) {
computerWins++;
} else if (game.getTotal(game.computerCards) > 21) {
userWins++;
} else if (game.getTotal(game.userCards) > game.getTotal(game.computerCards)) {
userWins++;
} else if (game.getTotal(game.userCards) < game.getTotal(game.computerCards)) {
computerWins++;
}
System.out.println("User wins: " + userWins);
System.out.println("Computer wins: " + computerWins);
System.out.print("Do you want to play again? (y/n) ");
String input = scanner.nextLine();
if (!input.equalsIgnoreCase("y")) {
break;
}
}
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
// Test requirement 1
Game game1 = new Game();
game1.start();
// Test requirement 2
Game game2 = new Game();
game2.play();
// Test requirement 3 and 4
Match match = new Match();
match.start();
}
}
```