java三个玩家的扑克牌游戏
时间: 2023-10-20 16:06:46 浏览: 209
Java扑克牌游戏
以下是一个简单的Java三个玩家的扑克牌游戏的示例代码:
```java
import java.util.*;
public class PokerGame {
public static void main(String[] args) {
List<String> deck = createDeck(); // 创建一副扑克牌
Collections.shuffle(deck); // 洗牌
List<String> player1 = new ArrayList<String>();
List<String> player2 = new ArrayList<String>();
List<String> player3 = new ArrayList<String>();
// 发牌
for (int i = 0; i < deck.size(); i++) {
String card = deck.get(i);
if (i % 3 == 0) {
player1.add(card);
} else if (i % 3 == 1) {
player2.add(card);
} else {
player3.add(card);
}
}
// 显示每个玩家的牌
System.out.println("Player 1: " + player1);
System.out.println("Player 2: " + player2);
System.out.println("Player 3: " + player3);
// 计算每个玩家的得分
int score1 = calculateScore(player1);
int score2 = calculateScore(player2);
int score3 = calculateScore(player3);
// 显示每个玩家的得分
System.out.println("Player 1 score: " + score1);
System.out.println("Player 2 score: " + score2);
System.out.println("Player 3 score: " + score3);
// 显示获胜者
if (score1 > score2 && score1 > score3) {
System.out.println("Player 1 wins!");
} else if (score2 > score1 && score2 > score3) {
System.out.println("Player 2 wins!");
} else if (score3 > score1 && score3 > score2) {
System.out.println("Player 3 wins!");
} else {
System.out.println("It's a tie!");
}
}
// 创建一副扑克牌
public static List<String> createDeck() {
List<String> deck = new ArrayList<String>();
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + " of " + suit);
}
}
return deck;
}
// 计算一手牌的得分
public static int calculateScore(List<String> hand) {
int score = 0;
for (String card : hand) {
String rank = card.split(" ")[0];
if (rank.equals("Jack") || rank.equals("Queen") || rank.equals("King")) {
score += 10;
} else if (rank.equals("Ace")) {
score += 11;
} else {
score += Integer.parseInt(rank);
}
}
return score;
}
}
```
这个程序创建了一副扑克牌,洗牌,然后将牌发给三个玩家。每个玩家的牌都被存储在一个列表中,然后计算每个玩家的得分。最后,程序显示每个玩家的得分,并宣布获胜者。
阅读全文