Java斗地主

@CP先生的领域

java编写斗地主

第一步先建一个实体类

/**

  • @ClassName: PokerPojo
  • @Description: TODO(这里用一句话描述这个类的作用)
  • @author CP

*/
package com.poker.pojo;

public class PokerPojo {

private String color;
private String value;


public PokerPojo(String color, String value) {
	super();
	this.color = color;
	this.value = value;
}
public String getColor() {
	return color;
}
public void setColor(String color) {
	this.color = color;
}
public String getValue() {
	return value;
}
public void setValue(String value) {
	this.value = value;
}
@Override
public String toString() {
	return color + value+"\t";
}

}

第二步使用Map或者List集合来操作

/**

  • @ClassName: Poker
  • @Description: ChinesePoker
  • @author CP

*/
package com.poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.poker.pojo.PokerPojo;

public class Poker {

static List<PokerPojo> list = new ArrayList<PokerPojo>();
static Map<Integer,PokerPojo> mapForSelection = new HashMap<Integer, PokerPojo>();
static List<PokerPojo> player01 =new ArrayList<PokerPojo>();
static List<PokerPojo> player02 =new ArrayList<PokerPojo>();
static List<PokerPojo> player03 =new ArrayList<PokerPojo>();
static List<PokerPojo> zhuo =new ArrayList<PokerPojo>();

public static void main(String[] args) {
	
	String[] colArr ={"♥","♠","♦","♣"};
	String val = "A 2 3 4 5 6 7 8 9 10 J Q K";
	String[] valArr = val.split(" ");
	initPoker(colArr,valArr);	
	Collections.shuffle(list);
	sendPoker();
	pickChinesePoker();
	sortPoker(player01);
	sortPoker(player02);
	sortPoker(player03);
	showAllPoker();
	
}

private static void sortPoker(List<PokerPojo> list) {
	List<Integer> selectionPlayer = new ArrayList<Integer>();
	for (int i = 0; i < list.size(); i++) {		
		for (int j = 0; j < mapForSelection.size(); j++) {
			if (list.get(i).getColor().equals(mapForSelection.get(Integer.valueOf(j)).getColor())
					&& list.get(i).getValue() == mapForSelection.get(Integer.valueOf(j)).getValue()) {
				selectionPlayer.add(j);
			}
		}
	}
	
	Collections.sort(selectionPlayer);
	
	/*
	 * for (int i = 0; i < selectionPlayer.size(); i++) { for (int j = i + 1; j <
	 * selectionPlayer.size(); j++) { if (selectionPlayer.get(i) >
	 * selectionPlayer.get(j)) { int temp = selectionPlayer.get(j);
	 * selectionPlayer.set(j,selectionPlayer.get(i)); selectionPlayer.set(i,temp); }
	 * } }
	 */
	
	for (int i = 0; i < selectionPlayer.size(); i++) {
		Set<Integer> keySet = mapForSelection.keySet();
		for (Integer integer : keySet) {
			if (integer.equals(selectionPlayer.get(i))) {
				list.set(i, mapForSelection.get(integer));
			}
		}
	}
	
}

private static void showAllPoker() {
	System.out.println("玩家01的手牌:");
	for (int i = 0; i < player01.size(); i++) {		
		System.out.print(player01.get(i)+" ");
	}
	System.out.println("\n玩家02的手牌:");
	for (int i = 0; i < player02.size(); i++) {		
		System.out.print(player02.get(i)+" ");
	}
	System.out.println("\n玩家03的手牌:");
	for (int i = 0; i < player03.size(); i++) {		
		System.out.print(player03.get(i)+" ");
	}
	
}

private static void pickChinesePoker() {
	Random r = new Random();
	List<PokerPojo> _list = new ArrayList<PokerPojo>();
	_list.addAll(list);
	_list.removeAll(zhuo);
	PokerPojo pojoForPickChinesePoker = _list.get(r.nextInt(_list.size()));
	for (int i = 0; i < player01.size(); i++) {
		if (player01.get(i).getColor().equals(pojoForPickChinesePoker.getColor())
				&& player01.get(i).getValue().equals(pojoForPickChinesePoker.getValue())) {
			player01.addAll(zhuo);
			System.out.println("我是一号玩家,我是地主");
			break;
		}
	}
	
	for (int i = 0; i < player02.size(); i++) {
		if (player02.get(i).getColor().equals(pojoForPickChinesePoker.getColor())
				&& player02.get(i).getValue().equals(pojoForPickChinesePoker.getValue())) {
			player02.addAll(zhuo);
			System.out.println("我是二号玩家,我是地主");
			break;
		}
	}
	
	for (int i = 0; i < player03.size(); i++) {
		if (player03.get(i).getColor().equals(pojoForPickChinesePoker.getColor())
				&& player03.get(i).getValue().equals(pojoForPickChinesePoker.getValue())) {
			player03.addAll(zhuo);
			System.out.println("我是三号玩家,我是地主");
			break;
		}
	}
}

private static void sendPoker() {
	for (int i = 0; i < list.size(); i++) {
		if (i > 50) {
			zhuo.add(list.get(i));
		}else {
			if (i % 3 == 0) {
				player01.add(list.get(i));
			}
			if (i % 3 == 1) {
				player02.add(list.get(i));
			}
			if (i % 3 == 2) {
				player03.add(list.get(i));
			}
		}
	}
	
}

private static void initPoker(String[] colArr, String[] valArr) {
	for (int i = 0; i < valArr.length; i++) {
		for (int j = 0; j < colArr.length; j++) {
			list.add(new PokerPojo(colArr[j],valArr[i]));
		}
	}
	list.add(new PokerPojo("","大王"));
	list.add(new PokerPojo("","小王"));
	
	for (int i = 0; i < list.size(); i++) {
		mapForSelection.put(Integer.valueOf(i), list.get(i));
	}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值