用Java写一个简单的扑克牌比大小游戏
时间: 2023-07-01 08:12:50 浏览: 133
好的,以下是一个简单的扑克牌比大小游戏的 Java 代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PokerGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入玩家数量:");
int playerCount = scanner.nextInt();
scanner.nextLine(); // 读取换行符
List<String> deck = generateDeck();
Collections.shuffle(deck); // 洗牌
List<List<String>> players = new ArrayList<>();
for (int i = 0; i < playerCount; i++) {
players.add(new ArrayList<>());
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < playerCount; j++) {
String card = deck.remove(0);
players.get(j).add(card);
}
}
for (int i = 0; i < playerCount; i++) {
System.out.printf("玩家%d的牌为:%s\n", i + 1, players.get(i));
}
int maxScore = 0;
int winner = -1;
for (int i = 0; i < playerCount; i++) {
int score = calculateScore(players.get(i));
System.out.printf("玩家%d的得分为:%d\n", i + 1, score);
if (score > maxScore) {
maxScore = score;
winner = i;
}
}
System.out.printf("玩家%d获胜!\n", winner + 1);
}
private static List<String> generateDeck() {
List<String> deck = new ArrayList<>();
String[] suits = {"♠️", "♥️", "♣️", "♦️"};
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + suit);
}
}
return deck;
}
private static int calculateScore(List<String> hand) {
int score = 0;
for (String card : hand) {
String rank = card.substring(0, card.length() - 1);
switch (rank) {
case "J":
case "Q":
case "K":
score += 10;
break;
case "A":
score += 11;
break;
default:
score += Integer.parseInt(rank);
}
}
return score;
}
}
```
运行程序后,首先要输入玩家数量,然后程序自动洗牌、发牌、计算得分和比较大小,最后输出获胜玩家。每个玩家的牌和得分也会被输出。
这个简单的扑克牌比大小游戏只考虑了单张牌的大小,没有考虑花色和牌型等因素。如果需要更加复杂的游戏规则,可以在此基础上进行扩展。
阅读全文