创建手牌时参考了"中天123"同学的代码,此处附上链接:
http://www.imooc.com/article/5299
写得比较乱,请见谅。
Poker类:
package com.kkkk;
import java.util.Comparator;
public class Poker {
private int huadx;
private int shudx;
private String hua;
private String shu;
public Poker(int huadx,int shudx, String hua, String shu) {
this.huadx=huadx;
this.shudx=shudx;
this.hua = hua;
this.shu = shu;
}
public Integer getHuadx() {
return huadx;
}
public void setHuadx(int huadx) {
this.huadx = huadx;
}
public Integer getShudx() {
return shudx;
}
public void setShudx(int shudx) {
this.shudx = shudx;
}
public String getHua() {
return hua;
}
public void setHua(String hua) {
this.hua = hua;
}
public String getShu() {
return shu;
}
public void setShu(String shu) {
this.shu = shu;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + huadx;
result = prime * result + shudx;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Poker))
return false;
Poker other = (Poker) obj;
if (this.shudx != other.shudx)
{return false;}
else{
return true;
}
}
}
PokerComparator类:
package com.kkkk;
import java.util.Comparator;
public class PokerComparator implements Comparator<Poker> {
@Override
public int compare(Poker o1, Poker o2) {
if(o1.getShudx()!=o2.getShudx())
return (o1.getShudx()).compareTo(o2.getShudx());
else
return(o1.getHuadx().compareTo(o2.getHuadx()));
}
}
Main:
package com.kkkk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public List pokerAll;
public Main() {
this.pokerAll = new ArrayList();
}
public void addCard() {
String num = "";
for (int i = 0; i < 13; i++) {
num = "";
if (i == 0)
num = "A";
else if (i == 10)
num = "J";
else if (i == 11)
num = "Q";
else if (i == 12)
num = "K";
else
num = "" + (i + 1);
pokerAll.add(new Poker(4, i, "黑桃", num));
pokerAll.add(new Poker(3, i, "红桃", num));
pokerAll.add(new Poker(2, i, "梅花", num));
pokerAll.add(new Poker(1, i, "方块", num));
}
for (Object c : pokerAll) {
Poker cd = (Poker) c;
System.out.print(cd.getHua() + cd.getShu() + " ");
}
}
public void xi() {
Collections.shuffle(pokerAll);
}
public void student() {
List getAll = new ArrayList();
Poker get0 = (Poker) pokerAll.get(0);
getAll.add(get0);
Poker get1 = (Poker) pokerAll.get(1);
getAll.add(get1);
Poker get2 = (Poker) pokerAll.get(2);
getAll.add(get2);
Poker get3 = (Poker) pokerAll.get(3);
getAll.add(get3);
System.out.println("请输入第一位学生的ID");
while(true) {
try {
Scanner sc1 = new Scanner(System.in);
int id1 = sc1.nextInt();
break;
} catch (Exception e) {
System.out.println("请输入一个正整数ID值:");
}}
System.out.println("请输入第一位学生的姓名");
Scanner sc=new Scanner(System.in);
String name1 = sc.next();
System.out.println("请输入第二位学生的ID");
while(true) {
try {
Scanner sc1 = new Scanner(System.in);
int id2 = sc1.nextInt();
break;
} catch (Exception e) {
System.out.println("请输入一个正整数ID值:");
}}
System.out.println("请输入第二位学生的姓名");
String name2 = sc.next();
System.out.println("欢迎玩家: " + name1 + " 和 " + name2);
System.out.println("********开始发牌");
System.out.println("********发牌结束");
System.out.println("********开始游戏");
System.out.println("玩家1:" + name1 + " 的手牌为: " + get0.getHua()
+ get0.getShu() + " 和 " + get2.getHua() + get2.getShu());
System.out.println("玩家2:" + name2 + " 的手牌为: " + get1.getHua()
+ get1.getShu() + " 和 " + get3.getHua() + get3.getShu());
Collections.sort(getAll, new PokerComparator());
Poker temp = (Poker) getAll.get(3);
if (temp.equals(get0)temp.equals(get2)) {
System.out.println("恭喜" + name1 + "胜利!!");
} else
System.out.println("恭喜" + name2 + "胜利!!");
}
public static void main(String[] args) {
Main pa = new Main();
System.out.println("*********创建扑克牌");
pa.addCard();
System.out.println("\n**********开始洗牌");
pa.xi();
System.out.println("**********洗牌结束");
System.out.println("***********创建玩家");
pa.student();
}
}