请大家多多指教,使程序更完美!!!
主程序:
package imooc_collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PokerTest {
public List apoker;
Apoker[] pokers= {new Apoker("黑桃", "2"),new Apoker("黑桃", "3"),new Apoker("黑桃", "4"),new Apoker("黑桃", "5"),
new Apoker("黑桃", "6"),new Apoker("黑桃", "7"),new Apoker("黑桃", "8"),new Apoker("黑桃", "9"),new Apoker("黑桃", "10"),
new Apoker("黑桃", "J"),new Apoker("黑桃", "Q"),new Apoker("黑桃", "K"),new Apoker("黑桃", "A"),new Apoker("红桃", "2"),new Apoker("红桃", "3"),
new Apoker("红桃", "4"),new Apoker("红桃", "5"),new Apoker("红桃", "6"),new Apoker("红桃", "7"),new Apoker("红桃", "8"),new Apoker("红桃", "9"),
new Apoker("红桃", "10"),new Apoker("红桃", "J"),new Apoker("红桃", "Q"),new Apoker("红桃", "K"),new Apoker("红桃", "A"),new Apoker("梅花", "2"),
new Apoker("梅花", "3"),new Apoker("梅花", "4"),new Apoker("梅花", "5"),new Apoker("梅花", "6"),new Apoker("梅花", "7"),new Apoker("梅花", "8"),
new Apoker("梅花", "9"),new Apoker("梅花", "10"),new Apoker("梅花", "J"),new Apoker("梅花", "Q"),new Apoker("梅花", "K"),new Apoker("梅花", "A"),
new Apoker("方片", "2"),new Apoker("方片", "3"),new Apoker("方片", "4"),new Apoker("方片", "5"),new Apoker("方片", "6"),new Apoker("方片", "7"),
new Apoker("方片", "8"),new Apoker("方片", "9"),new Apoker("方片", "10"),new Apoker("方片", "J"),new Apoker("方片", "Q"),new Apoker("方片", "K"),
new Apoker("方片", "A")};
public PokerTest(){
this.apoker=new ArrayList();
}
/*
* 用于往apoker中添加扑克牌
*/
public void testAdd(){
//,并通过调用add方法,添加到扑克牌的List中
System.out.println("-------------创建了一副扑克牌--------------");
/*Apoker[] pokers= {new Apoker("黑桃", "2"),new Apoker("黑桃", "3"),new Apoker("黑桃", "4"),new Apoker("黑桃", "5"),
new Apoker("黑桃", "6"),new Apoker("黑桃", "7"),new Apoker("黑桃", "8"),new Apoker("黑桃", "9"),new Apoker("黑桃", "10"),
new Apoker("黑桃", "J"),new Apoker("黑桃", "Q"),new Apoker("黑桃", "K"),new Apoker("黑桃", "A"),new Apoker("红桃", "2"),new Apoker("红桃", "3"),
new Apoker("红桃", "4"),new Apoker("红桃", "5"),new Apoker("红桃", "6"),new Apoker("红桃", "7"),new Apoker("红桃", "8"),new Apoker("红桃", "9"),
new Apoker("红桃", "10"),new Apoker("红桃", "J"),new Apoker("红桃", "Q"),new Apoker("红桃", "K"),new Apoker("红桃", "A"),new Apoker("梅花", "2"),
new Apoker("梅花", "3"),new Apoker("梅花", "4"),new Apoker("梅花", "5"),new Apoker("梅花", "6"),new Apoker("梅花", "7"),new Apoker("梅花", "8"),
new Apoker("梅花", "9"),new Apoker("梅花", "10"),new Apoker("梅花", "J"),new Apoker("梅花", "Q"),new Apoker("梅花", "K"),new Apoker("梅花", "A"),
new Apoker("方片", "2"),new Apoker("方片", "3"),new Apoker("方片", "4"),new Apoker("方片", "5"),new Apoker("方片", "6"),new Apoker("方片", "7"),
new Apoker("方片", "8"),new Apoker("方片", "9"),new Apoker("方片", "10"),new Apoker("方片", "J"),new Apoker("方片", "Q"),new Apoker("方片", "K"),
new Apoker("方片", "A")};*/
System.out.println("-------------扑克牌创建成功!--------------");
apoker.addAll( Arrays.asList(pokers));
/*int size = apoker.size();
System.out.print("扑克牌为:[");
for(int i= 0 ; i < size; i++) {
Apoker ap= (Apoker) apoker.get(i);
System.out.print( ap.huase+ap.dianshu+",");
}
System.out.println("]");*/
}
/*
* 通过foreach方法遍历循环
*/
public void testForeach(){
System.out.print("扑克牌为:[");
for(Object obj:apoker){ //思考这里为何要用Object呢?因为元素取出的时候它会被认为是类
Apoker ap=(Apoker)obj;
System.out.print( ap.huase + ap.dianshu+",");
}
System.out.println("]");
}
public void testSort(){
System.out.println("-------------开始洗牌--------------");
List<Apoker> apokerList=new ArrayList<Apoker>();
apokerList.addAll( Arrays.asList(pokers));
Collections.sort(apokerList,new PokeComparator());
System.out.print("打乱顺序后的扑克牌为:[");
for(Apoker poker:apokerList){ //思考这里为何要用Object呢?因为元素取出的时候它会被认为是类
System.out.print(poker.huase + poker.dianshu+",");
}
System.out.println("]");
/*for(Apoker poker:apokerList){
System.out.println("学生:"+poker.dianshu+","+poker.huase);
}*/
System.out.println("-------------洗牌成功--------------");
}
public void createPlayerAndSendPokers(){
System.out.println("-------------创建玩家--------------");
System.out.println("请输入第一位玩家的ID和姓名:");
//创建一个玩家对象
Scanner console=new Scanner(System.in);
System.out.println("请输入ID:");
String ID=console.next();
System.out.println("请输入姓名:");
String name=console.next();
System.out.println("请输入第二位玩家的ID和姓名:");
Scanner console1=new Scanner(System.in);
System.out.println("请输入ID:");
String ID1=console1.next();
System.out.println("请输入姓名:");
String name1=console.next();
System.out.println("欢迎玩家:"+name);
System.out.println("欢迎玩家:"+name1);
System.out.println("-------------开始发牌--------------");
apoker.addAll( Arrays.asList(pokers));
System.out.print("玩家:"+name+"拿牌--");
Random ran=new Random();
int num0=ran.nextInt(pokers.length);
Apoker ap0= (Apoker) apoker.get(num0);
System.out.println( ap0.huase+ap0.dianshu);
System.out.print("玩家:"+name1+"拿牌--");
int num1=ran.nextInt(pokers.length);
Apoker ap1= (Apoker) apoker.get(num1);
System.out.println( ap1.huase+ap1.dianshu);
System.out.print("玩家:"+name+"拿牌--");
int num2=ran.nextInt(pokers.length);
Apoker ap2= (Apoker) apoker.get(num2);
System.out.println( ap2.huase+ap2.dianshu);
System.out.print("玩家:"+name1+"拿牌--");
int num3=ran.nextInt(pokers.length);
Apoker ap3= (Apoker) apoker.get(num3);
System.out.println(ap3.huase+ap3.dianshu);
Apoker[] poker1={new Apoker(ap0.huase,ap0.dianshu),new Apoker(ap2.huase,ap2.dianshu)};
Apoker[] poker2={new Apoker(ap1.huase,ap1.dianshu),new Apoker(ap3.huase,ap3.dianshu)};
System.out.println("-------------发牌结束--------------");
System.out.println("-------------开始游戏--------------");
List<Apoker> apokerList=new ArrayList<Apoker>();
apokerList.addAll( Arrays.asList(poker1));
Collections.sort(apokerList,new PokeComparator());
Apoker maxpoker1=apokerList.get(1);
System.out.println("玩家:"+name+"最大的手牌为:"+maxpoker1.huase+maxpoker1.dianshu);
List<Apoker> apokerList1=new ArrayList<Apoker>();
apokerList1.addAll( Arrays.asList(poker2));
Collections.sort(apokerList1,new PokeComparator());
Apoker maxpoker2=apokerList1.get(1);
System.out.println("玩家:"+name1+"最大的手牌为:"+maxpoker2.huase+maxpoker2.dianshu);
List<Apoker> maxList=new ArrayList<Apoker>();
Apoker[] maxpokers={new Apoker(maxpoker1.huase,maxpoker1.dianshu),new Apoker(maxpoker2.huase,maxpoker2.dianshu)};
maxList.addAll( Arrays.asList(maxpokers));
Collections.sort(maxList,new PokeComparator());
Apoker maxpoker=maxList.get(0);
if(maxpoker.equals(maxpoker1)){
System.out.println("----------------玩家:"+name+"赢了!--------------------");
}
else
System.out.println("----------------玩家:"+name1+"赢了!--------------------");
}
/*System.out.println("玩家:"+name+"拿牌");
Random ran2=new Random();
int num2=ran.nextInt(pokers.length);
Apoker ap0= (Apoker) apoker.get(num0);
System.out.print( ap0.huase+ap0.dianshu);
System.out.println("玩家:"+name1+"拿牌");
int num1=ran.nextInt(pokers.length);
if(num0!=num1){
Apoker ap1= (Apoker) apoker.get(num1);
System.out.print( ap1.huase+ap1.dianshu);
}else continue;
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PokerTest pt=new PokerTest();
pt.testAdd();
pt.testForeach();
pt.testSort();
pt.createPlayerAndSendPokers();
}
}
比较:
package imooc_collection;
import java.util.Comparator;
public class PokeComparator implements Comparator<Apoker> {
@Override
public int compare(Apoker o1, Apoker o2) {
// TODO Auto-generated method stub
return o1.dianshu.compareTo(o2.dianshu);
}
}
玩家:
package imooc_collection;
import java.util.HashSet;
import java.util.Set;
public class Player {
public String id;
public String name;
public Set<Apoker> pokers;//玩家具有手牌的手牌的属性
public Player(String id,String name){
this.id=id;
this.name=name;
this.pokers=new HashSet<Apoker>();//由于Set不可进行实例化,所以用set的实现类对HashSet对课程进行实例化
}
}
扑克牌:
package imooc_collection;
public class Apoker {
public String huase;
public String dianshu;
public Apoker(String huase,String dianshu){
this.huase=huase;
this.dianshu=dianshu;
}
}