Java 相关的纸牌游戏项目
时间: 2024-05-05 08:22:54 浏览: 145
Java 相关的纸牌游戏项目可以是一个基于命令行界面或图形用户界面的扑克牌游戏,比如常见的斗地主、21点、德州扑克等。以下是一个简单的纸牌游戏项目的代码示例,供您参考:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PokerGame {
private ArrayList<Card> deck;
private ArrayList<Card> playerHand;
private ArrayList<Card> dealerHand;
private int playerTotal;
private int dealerTotal;
private Scanner scanner;
public PokerGame() {
deck = new ArrayList<>();
playerHand = new ArrayList<>();
dealerHand = new ArrayList<>();
scanner = new Scanner(System.in);
createDeck();
}
private void createDeck() {
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for (String suit : suits) {
for (String rank : ranks) {
Card card = new Card(suit, rank);
deck.add(card);
}
}
}
private void shuffleDeck() {
Collections.shuffle(deck);
}
private void dealCards() {
for (int i = 0; i < 2; i++) {
playerHand.add(deck.get(0));
deck.remove(0);
dealerHand.add(deck.get(0));
deck.remove(0);
}
}
private void printCards() {
System.out.println("Your hand: ");
for (Card card : playerHand) {
System.out.println(card.getSuit() + " " + card.getRank());
}
System.out.println("Dealer's hand: ");
System.out.println(dealerHand.get(0).getSuit() + " " + dealerHand.get(0).getRank());
}
private void calculateTotal() {
playerTotal = 0;
dealerTotal = 0;
for (Card card : playerHand) {
playerTotal += card.getValue();
}
for (Card card : dealerHand) {
dealerTotal += card.getValue();
}
}
private void playerTurn() {
while (playerTotal < 21) {
System.out.println("Do you want to hit or stand? (h/s)");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("h")) {
playerHand.add(deck.get(0));
deck.remove(0);
calculateTotal();
printCards();
} else {
break;
}
}
}
private void dealerTurn() {
while (dealerTotal < 17) {
dealerHand.add(deck.get(0));
deck.remove(0);
calculateTotal();
printCards();
}
}
private void checkWinner() {
if (playerTotal > 21) {
System.out.println("You busted! Dealer wins.");
} else if (dealerTotal > 21) {
System.out.println("Dealer busted! You win.");
} else if (playerTotal > dealerTotal) {
System.out.println("You win!");
} else if (dealerTotal > playerTotal) {
System.out.println("Dealer wins.");
} else {
System.out.println("It's a tie!");
}
}
public void playGame() {
shuffleDeck();
dealCards();
calculateTotal();
printCards();
playerTurn();
dealerTurn();
checkWinner();
}
public static void main(String[] args) {
PokerGame game = new PokerGame();
game.playGame();
}
}
class Card {
private String suit;
private String rank;
public Card(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public String getSuit() {
return suit;
}
public String getRank() {
return rank;
}
public int getValue() {
if (rank.equals("Ace")) {
return 11;
} else if (rank.equals("King") || rank.equals("Queen") || rank.equals("Jack") || rank.equals("10")) {
return 10;
} else {
return Integer.parseInt(rank);
}
}
}
```
此代码演示了一个基于命令行界面的21点游戏。您可以按照自己的需求修改代码以创建您自己的纸牌游戏。
阅读全文