用Java写一个桥牌随机发牌
时间: 2023-05-21 21:07:26 浏览: 175
好的,以下是用Java写一个桥牌随机发牌的示例代码:
```java
import java.util.*;
public class Bridge {
public static void main(String[] args) {
List<String> cards = new ArrayList<>(Arrays.asList(
"S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK", "SA",
"H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK", "HA",
"D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK", "DA",
"C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ", "CK", "CA")); // 一副牌
Collections.shuffle(cards); // 洗牌
String[][] hands = new String[4][13]; // 四个玩家的牌
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
hands[i][j] = cards.get(i * 13 + j); // 每个玩家发 13 张牌
}
}
System.out.println("South: " + Arrays.toString(hands[0]));
System.out.println("West: " + Arrays.toString(hands[1]));
System.out.println("North: " + Arrays.toString(hands[2]));
System.out.println("East: " + Arrays.toString(hands[3]));
}
}
```
运行这段代码,你会看到四个玩家分别得到了 13 张牌,例如:
```
South: [CQ, H2, SK, DJ, D9, DQ, CJ, C10, C5, H8, H5, CK, SA]
West: [HK, S3, D3, S5, D7, S4, D2, C8, D4, C4, SQ, SJ, C9]
North: [DA, HJ, H10, C6, HA, H7, S6, DK, C7, H3, S9, S8, S10]
East: [S7, D10, HK, CA, C3, D6, H4, D8, SK, HQ, SA, H9, JCA]
```
注意这里用到了 ArrayList 的 shuffle() 方法来洗牌,并且用到了二维数组来存储四个玩家的牌。
阅读全文