交作业啦!
/* Poker类 */
public class Poker implements Comparable<Poker>{
List<Poker> stringList = new ArrayList<Poker>();//有顺序的扑克集合
public static List<Poker> stringList1 = new ArrayList<Poker>();// 打乱顺序的扑克集合
Random random = new Random(); //随机数
public static List<Poker> pokerList = new ArrayList<Poker>(); //存储玩家一的手牌
public static List<Poker> pokerList1 = new ArrayList<Poker>(); //存储玩家二的手牌
Person person ;
private String color;
private String num;
public Poker() {
}
public Poker(String color,String num) {
this.color = color;
this.num = num;
// this.stringList = new ArrayList<Poker>();
}
public String toString() {
return color+num;
}
public List<Poker> add() {
String [] colors= {"黑桃","红桃","梅花","方块"};
String [] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for(String color : colors) {
for(String nums : num) {
stringList.add(new Poker(color,nums));
}
}
return stringList;
}
public void showPoker() {
for (Poker string : stringList) {
System.out.print(string+",");
}
}
/*
* 打乱扑克顺序,并传到新的集合中
* */
public void ShuffleTheCards() {
stringList1 = stringList;
Collections.shuffle(stringList1);
for (Poker string : stringList1) {
System.out.print(string+",");
}
}
/*
*发牌
**/
public void Licensing() {
Poker a ;
for(int i=0;i<4;i++) {
a = stringList1.get(i);
if(i%2==0) {
pokerList.add(a);
System.out.println("-----玩家:"+Person.personList.get(0).getName()+"-拿牌");
}else {
pokerList1.add(a);
System.out.println("-----玩家:"+Person.personList.get(1).getName()+"-拿牌");
}
}
for(Poker string : pokerList1) {
Person.personList.get(0).handCards.add(string);
}
for(Poker string : pokerList) {
Person.personList.get(1).handCards.add(string);
}
}
/*
* 判断牌面大小
* */
public void Compare() {
Poker p1 = Person.personList.get(0).handCards.get(0);
Poker p2 = Person.personList.get(0).handCards.get(1);
Poker p3 = Person.personList.get(1).handCards.get(0);
Poker p4 = Person.personList.get(1).handCards.get(1);
if(p1.compareTo(p2)>0) {
System.out.println("玩家:"+Person.personList.get(0).getName()+"最大的手牌为:"+p1);
}else{
System.out.println("玩家:"+Person.personList.get(0).getName()+"最大的手牌为:"+p2);
}
if(p3.compareTo(p4)>0) {
System.out.println("玩家:"+Person.personList.get(1).getName()+"最大的手牌为:"+p3);
}else{
System.out.println("玩家:"+Person.personList.get(1).getName()+"最大的手牌为:"+p4);
}
if(p1.compareTo(p2)>0) {
if(p3.compareTo(p4)>0) {
if(p1.compareTo(p3)>0) {
System.out.println("------------"+Person.personList.get(0).getName()+"获胜!---------------");
}else{
System.out.println("------------"+Person.personList.get(1).getName()+"获胜!---------------");
}
}else {
if(p1.compareTo(p4)>0) {
System.out.println("------------"+Person.personList.get(0).getName()+"获胜!---------------");
}else {
System.out.println("------------"+Person.personList.get(1).getName()+"获胜!---------------");
}
}
}else {
if(p3.compareTo(p4)>0) {
if(p2.compareTo(p3)>0) {
System.out.println("------------"+Person.personList.get(0).getName()+"获胜!---------------");
}else{
System.out.println("------------"+Person.personList.get(1).getName()+"获胜!---------------");
}
}else {
if(p2.compareTo(p4)>0) {
System.out.println("------------"+Person.personList.get(0).getName()+"获胜!---------------");
}else {
System.out.println("------------"+Person.personList.get(1).getName()+"获胜!---------------");
}
}
}
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
/*
* 利用字符串的IndexOf方法获取某个字符的位置,比较两个字符位置下标的大小。
* 下标数字大的则牌面大
* */
@Override
public int compareTo(Poker o) {
String colorOrder = "黑桃,红桃,梅花,方块";
String numOrder = "3,4,5,6,7,8,9,10,J,Q,K,A,2";
int x = numOrder.indexOf(this.getNum());
int y = numOrder.indexOf(o.getNum());
if(x<y) {
return -1;
}else if(x>y) {
return 1;
}else {
x = colorOrder.indexOf(this.getColor());
y = colorOrder.indexOf(o.getColor());
if(x<y) {
return 1;
}else if(x>y){
return -1;
}
}
return 0;
}
}
/*
Person类
*/
public class Person {
public static List<Person> personList = new ArrayList<Person>();
private Integer id;
private String name;
public List<Poker> handCards;
public Person() {
}
public Person(Integer id,String name) {
this.id = id;
this.name = name;
this.handCards = new ArrayList<Poker>();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@SuppressWarnings("resource")
public void creatPlayer(){
int id=0;
String name=null;
while(true) {
try {
for(int i=0;i<2;i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名");
System.out.println("请输入ID:");
id = sc.nextInt();
System.out.println("请输入姓名:");
name = sc.next();
personList.add(new Person(id,name));
}
for(int i=0;i<personList.size();i++) {
System.out.println("------欢迎玩家:"+personList.get(i).getName());
}
break;
}catch(Exception e) {
System.out.println("请输入整型的数字ID:");
continue;
}
}
}
}
/*
测试类
*/
public class Play {
Random random = new Random();
Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws ClassNotFoundException{
System.out.println("------------创建扑克牌---------------");
Poker pk = new Poker();
pk.add();
System.out.println("------------创建扑克牌成功---------------");
System.out.print("扑克牌有:");
pk.showPoker();
System.out.println("------------开始洗牌---------------");
pk.ShuffleTheCards();
System.out.println("------------洗牌结束---------------");
System.out.println("------------创建玩家---------------");
Person person = new Person();
try {
person.creatPlayer();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("------------开始发牌---------------");
pk.Licensing();
System.out.println("------------发牌结束---------------");
System.out.println("------------游戏开始---------------");
pk.Compare();
System.out.println(Person.personList.get(0).getName()+"的手牌为:"+Person.personList.get(0).handCards);
System.out.println(Person.personList.get(1).getName()+"的手牌为:"+Person.personList.get(1).handCards);
}
}