手记

扑克牌(两人一人一张,比较大小)(第三季最后练习)

package games;

/**

  • 牌类
  • @author Administrator
  • */
    public class Poker {
    public String color;
    public String point;

    public Poker(String color,String point){
    this.color = color;
    this.point = point;
    }
    }
    package games;

import java.util.ArrayList;
import java.util.List;

/**

  • 玩家类
  • @author Administrator
  • */
    public class Person {
    public Integer id;
    public String name;

    public Person(Integer id,String name){
    this.id = id;
    this.name = name;

    }
    public Person(){

    }

}
package games;

import java.util.Comparator;

public class PokerComparator implements Comparator<Poker> {
//此为重点比较方法,我也是参考来的

@Override
public int compare(Poker o1, Poker o2) {
    String color = "黑桃 红桃 梅花 方块";
    String point = "2 3 4 5 6 7 8 9 10 J Q K A";
    int result = point.indexOf(o1.point)-point.indexOf(o2.point);
    if(result<0)
        return -1;
    else if(result>0)
        return 1;
    else{
        int result2 = color.indexOf(o1.color)-color.indexOf(o2.color);
        if(result2<0)
            return -1;
        else if(result>0)
            return 1;
        else return 0;
    }       
}

}
package games;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class GameTest {
/**

  • 一副扑克牌:List类型的集合
    */
    public List<Poker> pokerList;
    /**
  • 洗牌后的牌:List类型的集合
    */
    public List<Poker> pokerListLast;
    /**
  • 用来承装玩家类型对象
    */
    public List<Person> persons;
    /**
  • 玩家手牌:List类型的集合
    */
    public List<Poker> pokers1;
    public List<Poker> pokers2;
    /**
  • 最终比较牌的集合
    */
    public List<Poker> lastPoker;

    public GameTest(){
    this.pokerList = new ArrayList<Poker>();
    this.pokerListLast = new ArrayList<Poker>();
    this.persons = new ArrayList<Person>();
    this.pokers1 = new ArrayList<Poker>();
    this.pokers2 = new ArrayList<Poker>();
    this.lastPoker = new ArrayList<Poker>();
    }

    /**

  • 创建一副牌
    */
    public void testAdd(){
    System.out.println("------创建扑克牌……------");
    String[] color = {"黑桃","红桃","梅花","方块"};
    String[] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    for (String string : color) {
    for (int i=0;i<point.length;i++) {
    Poker poker = new Poker(string,point[i]);
    pokerList.add(poker);
    }
    }
    System.out.println("-----创建扑克牌成功!-----");

    }

    /**

  • 打印输也:fer-each方法
    */
    public void testForEachForpokerList(){
    System.out.print("创建的牌为:[");
    for (Poker poker : pokerList) {
    System.out.print(poker.color + poker.point + ",");
    }
    System.out.print("]");
    System.out.println();
    }

// /*
//
打印输也:fer-each方法
// */
// public void testForEachForpokerListLast(){
// System.out.print("新洗的牌为:[");
// for (Poker poker : pokerListLast) {
// System.out.print(poker.color + poker.point + ",");
// }
// System.out.print("]");
// System.out.println();
// }

/**
 * 洗牌:使用随机生成不重复数方法打乱顺序  
 */
public void testShuffle(){
    System.out.println("-----开始洗牌……-----");
    List<Integer> integerList = new ArrayList<Integer>();
    Random random = new Random();
    Integer k;
    int i = pokerList.size();
    //取得pokerList每一个下标的不重复的随机数,保存在integerList中
    for(int in=0;in<i;in++){
        do{
            k = random.nextInt(i);
        } while(integerList.contains(k));
        integerList.add(k); 
    }
    //遍历integerList,从pokerList中取元素,然后全部添加入pokerListLast中
    for (Integer integer : integerList) {
        Poker poker =pokerList.get(integer);
        pokerListLast.add(poker);

    }       
    System.out.println("-----洗牌结束!-----");
}

/**
 * 创建玩家  
 */
public void testPersonAdd(){
    System.out.println("-----创建玩家……-----");
    for(int i =0;i<2;i++){
        System.out.println("请输入第" + (i+1) +"位玩家的ID和姓名:");           
        while(true){
            try{
                System.out.println("输入ID:");
                Scanner input=new Scanner(System.in);
                Integer id1=input.nextInt();                    
                System.out.println("输入姓名:");
                String name = input.next();
                Person newperson = new Person(id1,name);
                persons.add(newperson); 
                break;                                  
            } catch (InputMismatchException e){
                System.out.println("请输入整数类型的ID!");}     

        }
    }

    //用keySet获取玩家        

// Set<Integer> keySet = persons.keySet();
// for (Integer integer : keySet) {
// Person person = persons.get(integer);
// System.out.println("欢迎玩家:" + person.name);
// }
for(int i=0;i<persons.size();i++){
System.out.println("
欢迎玩家:" + persons.get(i).name);
}
}

/**
 * 发牌
 */
public void testDeal(){
    System.out.println("------开始发牌……------");       
    pokers1.add(pokerListLast.get(0));
    pokers2.add(pokerListLast.get(1));
    pokers1.add(pokerListLast.get(2));
    pokers2.add(pokerListLast.get(3));
    System.out.println("------发牌结束!------");
}

/**
 * 手牌
 */
public void testPoker(){
    System.out.println("玩家各自的手牌为:");
    System.out.println(persons.get(0).name + ":[" + pokers1.get(0).color + pokers1.get(0).point + "," + 
            pokers1.get(1).color + pokers1.get(1).point + "]");
    System.out.println(persons.get(1).name + ":[" + pokers2.get(0).color + pokers2.get(0).point + "," + 
            pokers2.get(1).color + pokers2.get(1).point + "]");
}

/**
 * 开始游戏 
 */
public void testPlayGame(){
    System.out.println("------开始游戏……------");
    Collections.sort(pokers1,new PokerComparator());
    System.out.println("玩家:" + persons.get(0).name+"最大的手牌为:" + 
             pokers1.get(1).color + pokers1.get(1).point);
    Collections.sort(pokers2,new PokerComparator());
    System.out.println("玩家:" + persons.get(1).name+"最大的手牌为:" +
             pokers2.get(1).color + pokers2.get(1).point);              
}

/**
 * 游戏结果 
 */
public void testResult(){
    lastPoker.add(pokers1.get(1));
    lastPoker.add(pokers2.get(1));
    Collections.sort(lastPoker,new PokerComparator());

// System.out.println("两个玩家中最大的牌为:" + lastPoker.get(1).color+lastPoker.get(1).point);
if(lastPoker.get(1).color.equals(pokers1.get(1).color))
System.out.println("-----玩家:" + persons.get(0).name + "获胜!-----");
else
System.out.println("-----玩家:" + persons.get(1).name + "获胜!-----");
}

public static void main(String[] args) {
    GameTest gt = new GameTest();
    gt.testAdd();
    gt.testForEachForpokerList();
    gt.testShuffle();

// gt.testForEachForpokerListLast();
gt.testPersonAdd();
gt.testDeal();
gt.testResult();
gt.testPoker();
}

}

7人推荐
随时随地看视频
慕课网APP

热门评论

十分感谢你的排序方法  让我减少了50行代码

查看全部评论