互相学习,有发现不足的欢迎发信息提醒,修改了随机发牌可能发到一样的牌的bug。
package puke_crc;
/**
* 单张扑克牌类
*
* @author crc
*
*/
public class PuKe {
private int id;
private String name;
/**
* 单张扑克构造方法
*
* @param i
* @param string
*/
public PuKe(int i, String string) {
this.setId(i);
this.setName(string);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
============================================
package puke_crc;
import java.util.HashMap;
import java.util.Map;
/**
* 一副扑克牌的Map集合
*
* @author crc
*
*/
public class PuKeList {
public Map<Integer, PuKe> paiKes;
/**
* 构造方法,创建Map集合实例并添加一副牌
*/
public PuKeList() {
this.paiKes = new HashMap<Integer, PuKe>(); // 创建实例集合
// 利用循环添加一整副牌
String[] huase = { "方块", "梅花", "红桃", "黑桃" };
String[] shuzhi = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
PuKe aKe = new PuKe(i * 4 + j, huase[j] + shuzhi[i]);
paiKes.put(i * 4 + j, aKe);
}
}
}
// /**
// * 试验按keyset值遍历输出,结果按顺序排列了
// */
// public void KeysetSort() {
// Set<Integer> temSet=new HashSet<Integer>();
// temSet=this.paiKes.keySet();
// for (Integer integer : temSet) {
// String nameString=paiKes.get(integer).getName();
// System.out.println(nameString);
// }
// }
/**
* 按id号在屏幕顺序显示
*/
public void IdToString() {
for (int i = 0; i < paiKes.size(); i++) {
if (paiKes.get(i) != null)
System.out.print(paiKes.get(i).getName() + " ");
if ((i + 1) % 16 == 0)
System.out.println();
}
System.out.println();
}
}
=================================================
package puke_crc;
import java.util.HashSet;
import java.util.Set;
/**
* 玩家类
*
* @author crc
*
*/
public class Player {
private int id;
private String name;
Set<PuKe> shoupaiKes;
public Player() {
this.shoupaiKes = new HashSet<PuKe>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 输出玩家所有手牌中最大的一张牌的id
*/
public int MaxPai(){
int max=0;
for (PuKe puKe : shoupaiKes) {
if(max<puKe.getId())
max=puKe.getId();
}
return max;
}
/**
* 输出玩家手中所有的牌
*/
public void ShowPai(){
System.out.println(this.name+":");
for (PuKe puKe : shoupaiKes) {
System.out.print("["+puKe.getName()+"]");
}
System.out.println();
}
}
===================================================
package puke_crc;
import java.util.Random;
import java.util.Scanner;
public class PlayGame {
/**
* 开始玩游戏
*
* @param args
*/
public static void main(String[] args) {
System.out.println("---------------创建扑克牌---------------");
PuKeList paiKeList = new PuKeList();
if (paiKeList != null)
System.out.println("---------------创建成功---------------");
paiKeList.IdToString();//调用方法,按顺序打印牌组
System.out.println("---------------开始洗牌---------------");
System.out.println("---------------洗牌结束---------------");
System.out.println("---------------创建玩家---------------");
Player[] p = new Player[2];
p[0]=new Player();
p[1]=new Player();
for (int i = 0; i < 2; i++) {//键盘输入创建玩家
System.out.println("请输入第" + (i + 1) + "位玩家id");
while (true) {
try {
@SuppressWarnings("resource")
Scanner srScanner = new Scanner(System.in);
int idtem = srScanner.nextInt();
if(idtem==p[0].getId()||idtem==p[1].getId()){
System.out.println("玩家id已被占用,请重新输入");
continue;
}
p[i].setId(idtem);
break;
} catch (Exception e) {
System.out.println("输入错误,请输入数字id");
continue;
}
}
System.out.println("请输入第" + (i + 1) + "位玩家姓名");
while (true) {
@SuppressWarnings("resource")
Scanner srScanner = new Scanner(System.in);// 输入
String nametem = srScanner.next();
if (nametem != null) {
p[i].setName(nametem);
break;
} else {
System.out.println("输入不能为空,请重新输入");
continue;
}
}
System.out.println("欢迎玩家:" + p[i].getName());
}
System.out.println("---------------开始发牌---------------");
Random it =new Random();
int ps=52;//记录牌组剩余牌数
for (int i = 0; i < 4; i++) {//分别随机获得两张牌
PuKe temPuKe = paiKeList.paiKes.get(it.nextInt(ps--));
//获得一张牌后将牌从牌组删除
paiKeList.paiKes.remove(it.nextInt(52));
if (i % 2 == 0) {
p[0].shoupaiKes.add(temPuKe);
System.out.println("玩家1["+p[0].getName() + "]拿牌");
} else {
p[1].shoupaiKes.add(temPuKe);
System.out.println("玩家2["+p[1].getName() + "]拿牌");
}
}
System.out.println("===========双方手牌如下===========");
p[0].ShowPai();
p[1].ShowPai();
System.out.println("===========结果===========");
if(p[0].MaxPai()>p[1].MaxPai()){
System.out.println("玩家:["+p[0].getName()+"] 获胜!");
}else{
System.out.println("玩家:["+p[1].getName()+"] 获胜!");
}
}
}