慕粉3170877
2016-07-03 18:23
请大家多多指教,使程序更完美!!! 主程序: 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; } }
提几点建议吧
就是没有设置异常处理,假如输入的用户名类型不匹配怎么办
有些代码可以简化一下,仅供参考
public List<Cards> list; //存放创建好的牌对象
public List<String> list1;//存放牌模型的集合框架
public Map<Integer,TestUserName> user;//创建学生对象并且随机选牌
Scanner input;
// TestUserName username;
public Testlist(){
list = new ArrayList<Cards>();
list1 = new ArrayList();
user = new HashMap<Integer,TestUserName>();
input = new Scanner(System.in);
}
/**
* 创建一个从2~A的集合
*/
public void TestAdd(){
for(int i = 2; i <= 10; i++){
list1.add(String.valueOf(i));
}
list1.addAll(Arrays.asList("J","Q","K","A"));
// System.out.println("测试添加牌模型:");
// for (String cards : list1) {
// System.out.print(cards+",");
// }
}
/**
* 创建牌
*/
public void TestAdd2(){
System.out.println("----------创建扑克牌!-----------");
for (String cards : list1) {
list.add(new Cards("方块",cards));
}
for (String cards : list1) {
list.add(new Cards("梅花",cards));
}
for (String cards : list1) {
list.add(new Cards("红桃",cards));
}
for (String cards : list1) {
list.add(new Cards("黑桃",cards));
}
//测试是否创建成功
System.out.println("------------扑克牌创建成功!------------");
System.out.print("为:");
for (Cards cards : list) {
System.out.print(cards.getDesign()+cards.getNumber()+",");
}
}
代码弄的复杂化了,有些地放可以简化代码你全写了,好佩服你的毅力
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("-------------创建了一副扑克牌--------------");
System.out.println("-------------扑克牌创建成功!--------------");
apoker.addAll( Arrays.asList(pokers));
}
/*
* 通过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("]");
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+"赢了!--------------------");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PokerTest pt=new PokerTest();
pt.testAdd();
pt.testForeach();
pt.testSort();
pt.createPlayerAndSendPokers();
}
}
public void addPocker(){ Pocker[] pocker=new Pocker[52]; String value="";//这个值是牌值 int m=0; int j=0; //花色 for(int i=0;i<52;i++) { j=i/13; //花色按照所在位置除以十三的值可分为每13张一套,共四套 m=i%13+1; //如果把JQK变成11,12,13那么四套牌值刚好是所在位置除以13的余数+1 if(m<11){ //对JQK三个值做处理 value=m+""; } else if(m==11){ value="J"; } else if(m==12){ value="Q"; } else if(m==13){ value="K"; } switch(j){ //按照花色(j)来分类给四种牌,52张实例化 case 0: pocker[i]=new Pocker("黑桃",value); break; case 1: pocker[i]=new Pocker("红桃",value); break; case 2: pocker[i]=new Pocker("梅花",value); break; case 3: pocker[i]=new Pocker("方块",value); break; } } this.pockers.addAll(Arrays.asList(pocker)); } 这样呢?我才发现可以用程序格式写程序,不过看你的程序还可以提亮啊,我的就没有呢
我在给扑克牌数组赋值上面做了一点算法改进(懒得打那么多字哈哈),我注意到黑桃红桃方块梅花,还有字符JQK出现的挺有规律的,而且从1-10也是挺有规律的,话不多说,代码如下:
前言补充:
public class Pocker {
public String color;
public String value;
Pocker(String color,String value){
this.color=color;
this.value=value;
}
}
重点在这里:
public void addPocker(){
Pocker[] pocker=new Pocker[52];
String value="";
int m=0;
int j=0;
for(int i=0;i<52;i++)
{
j=i/13;
m=i%13+1;
if(m<11){
value=m+"";
}
else if(m==11){
value="J";
}
else if(m==12){
value="Q";
}
else if(m==13){
value="K";
}
switch(j){
case 0:
pocker[i]=new Pocker("黑桃",value);
break;
case 1:
pocker[i]=new Pocker("红桃",value);
break;
case 2:
pocker[i]=new Pocker("梅花",value);
break;
case 3:
pocker[i]=new Pocker("方块",value);
break;
}
}
this.pockers.addAll(Arrays.asList(pocker));
}
Java入门第三季
409792 学习 · 4340 问题
相似问题