用java写一个斗地主小游戏
时间: 2023-06-17 15:08:01 浏览: 99
用java写的斗地主游戏
好的,以下是一个简单的斗地主小游戏的Java代码实现:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class DouDiZhu {
public static void main(String[] args) {
List<String> poker = new ArrayList<String>();
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
for (String color : colors) {
for (String number : numbers) {
poker.add(color + number);
}
}
poker.add("小王");
poker.add("大王");
Collections.shuffle(poker);
List<String> player1 = new ArrayList<String>();
List<String> player2 = new ArrayList<String>();
List<String> player3 = new ArrayList<String>();
List<String> diPai = new ArrayList<String>();
for (int i = 0; i < poker.size(); i++) {
String p = poker.get(i);
if (i >= poker.size() - 3) {
diPai.add(p);
} else if (i % 3 == 0) {
player1.add(p);
} else if (i % 3 == 1) {
player2.add(p);
} else if (i % 3 == 2) {
player3.add(p);
}
}
System.out.println("玩家1的牌:" + player1);
System.out.println("玩家2的牌:" + player2);
System.out.println("玩家3的牌:" + player3);
System.out.println("底牌:" + diPai);
Scanner scanner = new Scanner(System.in);
System.out.println("请玩家1出牌:");
String input1 = scanner.nextLine();
System.out.println("玩家1出牌:" + input1);
System.out.println("请玩家2出牌:");
String input2 = scanner.nextLine();
System.out.println("玩家2出牌:" + input2);
System.out.println("请玩家3出牌:");
String input3 = scanner.nextLine();
System.out.println("玩家3出牌:" + input3);
}
}
```
以上代码实现了一局斗地主的初始化和玩家出牌的基本功能,但其中还有很多细节需要完善,例如对牌的大小比较、牌型判断、牌的出牌规则等等。
阅读全文