主类
package com.xbmu.DemoFightTheLandlord;
import java.util.Scanner;
public class FightTheLandlord {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
boolean flag = true;
while(flag) {
System.out.println(" 大家一起斗地主! ");
System.out.println("**************************************************");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("~~~~ 请按1:玩家进场! ~~~~");
System.out.println("~~~~ 请按2:随机发牌! ~~~~");
System.out.println("~~~~ 请按0:退出游戏! ~~~~");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("**************************************************");
System.out.println("请选择:");
Landlords land = new Landlords();
int number = scan.nextInt();
switch (number) {
case 0: {
System.out.println("你已经成功退出游戏!");
flag = false;
break;
}
case 1: {
System.out.println("游戏开始,玩家入场,请依次自定义三位玩家信息:");
land.CreateRole();
System.out.println("当前玩家已准备好,请继续.");
System.out.println("请选择:");
int num = scan.nextInt();
if (num != 2){
System.out.println("输入有误,自动退出.");
flag = false;
break;
}
}
case 2: {
land.RandomPoker();
flag = false;
break;
}
default:
System.out.println("输入有误!自动退出.");
break;
}
}
}
}
Landlords类
package com.xbmu.DemoFightTheLandlord;
import java.util.*;
public class Landlords {
static Scanner scan = new Scanner(System.in);
static String name01, name02, name03;
static int total01, total02, total03;
static Player player01 = new Player();
static Player player02 = new Player();
static Player player03 = new Player();
//自定义角色
public static void CreateRole(){
//自定义第一位玩家信息
System.out.println("请自定义第一位玩家姓名:");
name01 = scan.next();
player01.setName(name01);
System.out.println("请自定义该玩家积分:");
total01 = scan.nextInt();
player01.setTotal(total01);
System.out.println();
//自定义第二位玩家信息
System.out.println("请自定义第二位玩家姓名:");
name02 = scan.next();
player02.setName(name02);
System.out.println("请自定义该玩家积分:");
total02 = scan.nextInt();
player02.setTotal(total02);
System.out.println();
//自定义第三位玩家信息
System.out.println("请自定义第三位玩家姓名:");
name03 = scan.next();
player03.setName(name03);
System.out.println("请自定义该玩家积分:");
total03 = scan.nextInt();
player03.setTotal(total03);
System.out.println();
}
//随机发牌
public static void RandomPoker(){
//存储一整副牌(54张)
HashMap<Integer, String> poker = new HashMap<>();
//存储每张牌的索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
//13张牌的数字
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//四种花色
String[] colors = {"(♣)", "(♦)", "(♥)", "(♠)"};
int index = 0;
//生成一副牌
for (String number: numbers){
for (String color: colors){
poker.put(index, number + color);
pokerIndex.add(index ++);
}
}
//加入大小王
poker.put(index, "小王");
pokerIndex.add(index ++);
poker.put(index, "大王");
pokerIndex.add(index ++);
System.out.println("正在洗牌ing...\n");
//洗牌
Collections.shuffle(pokerIndex);
System.out.println("洗牌已结束.\n");
System.out.println("正在抽取明牌ing...\n");
//随机抽取地主牌索引
Random number = new Random();
int num = number.nextInt(51) + 3;
System.out.println("明牌抽取成功.\n");
//底牌存储集合
ArrayList<Integer> landlord = new ArrayList<>();
System.out.println("正在发牌ing...\n");
//发牌
for (int i = 0; i < pokerIndex.size(); i ++){
if (i > 2){
if (i % 3 == 0) {
player01.setOwnPokerIndex(i);
if (i == num){
player01.setRole("地主");
player02.setRole("平民");
player03.setRole("平民");
player01.setOwnPokerIndex(0);
player01.setOwnPokerIndex(1);
player01.setOwnPokerIndex(2);
}
}
if (i % 3 == 1) {
player02.setOwnPokerIndex(i);
if (i == num){
player01.setRole("平民");
player02.setRole("地主");
player03.setRole("平民");
player02.setOwnPokerIndex(0);
player02.setOwnPokerIndex(1);
player02.setOwnPokerIndex(2);
}
}
if (i % 3 == 2) {
player03.setOwnPokerIndex(i);
if (i == num){
player01.setRole("平民");
player02.setRole("平民");
player03.setRole("地主");
player03.setOwnPokerIndex(0);
player03.setOwnPokerIndex(1);
player03.setOwnPokerIndex(2);
}
}
}else landlord.add(pokerIndex.get(i));
}
System.out.println("发牌结束.\n");
System.out.println("正在整理牌ing...\n");
//对牌进行排序
Collections.sort(player01.getOwnPokerIndex());
Collections.sort(player02.getOwnPokerIndex());
Collections.sort(player03.getOwnPokerIndex());
Collections.sort(landlord);
System.out.println("整理结束.\n");
//看牌
lookPoker(player01.getRole(), player01.getNames(), player01.getOwnPokerIndex(), poker, player01.getTotals());
System.out.println();
lookPoker(player02.getRole(), player02.getNames(), player02.getOwnPokerIndex(), poker, player02.getTotals());
System.out.println();
lookPoker(player03.getRole(), player03.getNames(), player03.getOwnPokerIndex(), poker, player03.getTotals());
System.out.println();
lookPoker("底牌","底牌", landlord, poker, 0);
System.out.println();
}
//看牌方法
public static void lookPoker(String role, String name, ArrayList<Integer> player, HashMap<Integer, String> poker, int total) {
if (role == "底牌") System.out.println(role + "为:");
else System.out.println(role + "--" + name + "的牌为:");
int cnt = 0;
for (Integer idx : player) {
System.out.print(poker.get(idx) + " ");
cnt ++;
if (cnt % 4 == 0 && cnt < 20) System.out.println();
}
System.out.println();
if (role != "底牌") System.out.println("现有积分: " + total + " 分.");
}
}
Player类
package com.xbmu.DemoFightTheLandlord;
import java.util.ArrayList;
public class Player {
private String names = "无名游客";//姓名
private int totals = 1000;//积分
private String role;//角色
private ArrayList<Integer> ownPokerIndex = new ArrayList<>();//发到的牌的索引
public String getNames(){
return names;
}
public void setName(String name){
this.names = name;
}
public int getTotals(){
return totals;
}
public void setTotal(int total){
this.totals = total;
}
public String getRole(){
return role;
}
public void setRole(String role){
this.role = role;
}
public ArrayList<Integer> getOwnPokerIndex() {
return ownPokerIndex;
}
public void setOwnPokerIndex(int ownPoker) {
ownPokerIndex.add(ownPoker);
}
}