方法肯定不是最优的,不过最终功能是能实现的。
讲一下思路:一个List集合用来存放玩家,一个Map集合用来存放,扑克牌的键,以及键值(权和名字),另外一个List用来存放玩家摸到的牌的权和花色。
代码如下:
//一个扑克牌类
package com.pukepai;
public class puke {
public int id;
public String name;
public puke(int id,String name){
this.id=id;
this.name=name;
}
}
//一个玩家类
package com.pukepai;
import java.util.ArrayList;
import java.util.List;
public class user {
public String id;
public String name;
public List <puke> str;
public user(String id,String name){
this.id=id;
this.name=name;
this.str=new ArrayList<puke>();
}
}
//最终是实现类
package com.pukepai;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class Initial3 {
/**
* @param args
*/
//public List <String> str2;
public Map <Integer,puke> pukemap;
public List <user> us;
Random random=new Random();
Scanner sc=new Scanner(System.in);
public Initial3(){
this.pukemap=new HashMap<Integer,puke> ();
this.us=new ArrayList<user>();
}
public void createPuke(){
System.out.println("*******创建扑克牌*******");
String [] sss={"方块2","梅花2","红桃2","黑桃2","方块3","梅花3","红桃3","黑桃3"
,"方块4","梅花4","红桃4","黑桃4","方块5","梅花5","红桃5","黑桃5","方块6"
,"梅花6","红桃6","黑桃6","方块7","梅花7","红桃7","黑桃7","方块8","梅花8",
"红桃8","黑桃8","方块9","梅花9","红桃9","黑桃9","方块10","梅花10","红桃10",
"黑桃10","方块J","梅花J","红桃J","黑桃J","方块Q","梅花Q","红桃Q","黑桃Q",
"方块K","梅花K","红桃K","黑桃K","方块A","梅花A","红桃A","黑桃A"};
for(int i=0;i<sss.length;i++){
pukemap.put(i,new puke(i,sss[i]));
}
System.out.println("*******扑克牌创建成功*******");
System.out.println("扑克牌为:");
Set<Integer> KeySet=pukemap.keySet();
for(Integer ks : KeySet){
String r=pukemap.get(ks).name;
System.out.print(r+",");
}
System.out.println();
}
public void washPuke(){
System.out.println("*******开始洗牌*******");
//洗牌过程
for(int i=0;i<pukemap.size();i++){
String beforeChange=pukemap.get(i).name;
int p=pukemap.get(i).id;
int k=random.nextInt(pukemap.size());
int q=pukemap.get(k).id;
String afterChange=pukemap.get(k).name;
pukemap.put(i, new puke(q,afterChange));
pukemap.put(k, new puke(p,beforeChange));
}
System.out.println("*******洗牌结束*******");
/* 洗牌之后再次打印
for(int i=0;i<pukemap.size();i++){
System.out.print(pukemap.get(i).name+",");
}*/
}
public void createUser(){
try{
System.out.println("*******创建玩家*******");
System.out.println("请输入玩家数:");
int c=sc.nextInt();
for(int i=1;i<=c;i++){
System.out.println("请输入第"+i+"位玩家的ID和姓名:");
System.out.println("请输入ID");
String uid=sc.next();
System.out.println("请输入姓名");
String name=sc.next();
user useri=new user(uid,name);
us.add(useri);
}
System.out.println("*******玩家创建成功*******");
}
catch (Exception e){
System.out.println("异常提示!! 输入的类型错误");
}
}
public void moPai(){
for(user u : us){
System.out.println("***欢迎玩家:"+u.name);
}
System.out.println("*******开始发牌*******");
for(int i=0;i<us.size();i++){
for(user u : us){
System.out.print(u.name+"摸牌");
}
System.out.println();
}
//通过entrySet方法,返回键值对,
Set<Entry<Integer, puke>> entr = pukemap.entrySet();
for(Entry<Integer, puke> en : entr){
for(int i=0;i<us.size();i++){
us.get(i).str.add(new puke(en.getValue().id,en.getValue().name));
}
}
}
public void playPuke(){
System.out.println("下面开始验证点数:");
int k=us.size();
int Allmax=0;
int temp=0;
int [] max=new int[k];
for(int i=0;i<us.size();i++){
int one=us.get(i).str.get(i).id;
int two=us.get(i).str.get(i+us.size()).id;
if(one>=two){
max[i]=one;
}
else{
max[i]=two;
}
}
int m=0;
for(int j=0;j<max.length-1;j++){
if(max[m]>max[j+1] ){
temp=max[m];
if(temp>Allmax){
Allmax=temp;
}
}
else if(max[m]<=max[j+1] ){
temp=max[j+1];
if(temp>Allmax){
Allmax=temp;
}
}
}
int ind=0;
int z=0;
for(int i=0;i<us.size();i++){
if(Allmax==max[i]){
z=i;
int one=us.get(i).str.get(i).id;
int two=us.get(i).str.get(i+us.size()).id;
if(one>=two){
ind=i;
}
else{
ind=i+us.size();
}
}
}
System.out.println("恭喜玩家:"+us.get(z).name+"胜利!"+" "+",最大的手牌是:"+us.get(z).str.get(ind).name);
int q=0;
for(int i=0;i<us.size();i++){
System.out.println("玩家"+us.get(i).name+"的手牌为:");
for(int j=0;j<2*(us.size());){
System.out.println(us.get(i).str.get(j+q).name+" ");
j=j+us.size();
}
q++;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Initial3 i1=new Initial3();
i1.createPuke();
i1.washPuke();
i1.createUser();
i1.moPai();
i1.playPuke();
}
}
//方法比较烦琐,所以希望能得到更好的方法,欢迎交流。