card类
package Easycards;
public class Card {
private String color;
private String point;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (color == null) {
if (other.color != null) return false;
} else if (!color.equals(other.color))
return false;
if (point == null) {
if (other.point != null) return false;
} else if (!point.equals(other.point))
return false;
return true;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
@Override
public String toString() {
return color+point;
}
}
cards类
package Easycards;
import java.util.ArrayList;
import java.util.List;
/*
- 1、创建一幅扑克牌(包含4种花色,黑红美方;十三种点数:2-10,A、J、Q、K。
- 2、创建两名玩家(至少拥有ID、姓名、手牌等属性,手牌为扑克牌集合
- 3、打乱扑克牌的顺序,洗牌。
- 4、发牌,每人两张,一人一张从头发的方式
- 5、先用牌里大比较牌的大小,相同在比较花色
*/
public class Cards {
public List<Card> getCards() {
List<Card> cards=new ArrayList<Card>();
String[] point={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] color={"方片","梅花","红桃","黑桃"};
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < point.length; j++) {
Card card=new Card();
card.setColor(color[i]);
card.setPoint(point[j]);
cards.add(card);
}
}
return cards;
}
}
Player类
package Easycards;
import java.util.List;
public class Player {
private Integer id;
private String Player_name;
private List<Card> Player_card;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPlayer_name() {
return Player_name;
}
public void setPlayer_name(String player_name) {
Player_name = player_name;
}
public List<Card> getPlayer_card() {
return Player_card;
}
public void setPlayer_card(List<Card> player_card) {
Player_card = player_card;
}
}
CardComparetor 类
package Easycards;
import java.util.Comparator;
public class CardComparetor implements Comparator<Card> {
public int compare(Card o1, Card o2) {
String p="2345678910JQKA";
String[] c={"方片","梅花","红桃","黑桃"};
int c1=0;
int c2=0;
int p1=p.indexOf(o1.getPoint());
int p2=p.indexOf(o2.getPoint());
for (int i = 0; i < c.length; i++) {
if(o1.getColor().equals(c[i])){
c1=i+1;
}
if(o2.getColor().equals(c[i])){
c2=i+1;
}
}
int num=0;
if(p1>p2){
num= 1;
}else if(p1==p2){
if(c1>c2){
num= 1;
}else if(c1==c2){
num= 0;
}else if(c1<c2){
num= -1;
}
}else{
num= -1;
}
return num;
}
private void mian() {
}
}
Test 类
package Easycards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
public class Test {
public static void main(String[] args) {
Cards card = new Cards();
List<Card> list = card.getCards();
System.out.println("———————————创建扑克牌————————————" + "\n"
- "————————创建成功————————" + "\n" + list.toString());
System.out.println("————————————开始洗牌————————————");
Collections.shuffle(list);
System.out.println("————————————洗牌结束————————————" + "\n"
+list.toString());
System.out.println("开始创建玩家:");
Scanner in = new Scanner(System.in);
int step = 1;
Player p1 = new Player();
Player p2 = new Player();
while (step<3) {
if (step == 1) {
try {
System.out.println("请输入第一个玩家的ID:");
p1.setId(in.nextInt());
System.out.println("请输入第一个玩家的姓名:");
p1.setPlayer_name(in.next());
} catch (Exception e) {
System.out.println("请输入正整数的ID");
in = new Scanner(System.in);
step=0;
}
} else if (step == 2) {
try {
System.out.println("请输入第二个玩家的ID");
p2.setId(in.nextInt());
System.out.println("请输入第二个玩家的姓名:");
p2.setPlayer_name(in.next());
} catch (Exception e) {
System.out.println("请输入正整数的ID");
in = new Scanner(System.in);
step=1;
}
}
step++;
}
System.out.println("——————欢迎玩家:"+p1.getPlayer_name()
+"\n——————欢迎玩家:"+ p2.getPlayer_name());
List<Card> L1=new ArrayList<Card>();
List<Card> L2=new ArrayList<Card>();
for (int i = 0; i < 4; i++) {
if(i%2==0){
L1.add(list.get(i));
System.out.println(p1.getPlayer_name()+"拿了一张牌。");
}else {
L2.add(list.get(i));
System.out.println(p2.getPlayer_name()+"拿了一张牌。");
}
p1.setPlayer_card(L1);
p2.setPlayer_card(L2);
}
Card c1=new Card();
Card c2=new Card();
CardComparetor cc=new CardComparetor();
int num=cc.compare(p1.getPlayer_card().get(0), p1.getPlayer_card().get(1));
if(num>0){
c1=p1.getPlayer_card().get(0);
System.out.println(p1.getPlayer_name()+"最大的牌为:"+p1.getPlayer_card().get(0));
}else if(num==0){
c1=p1.getPlayer_card().get(0);
System.out.println(p1.getPlayer_name()+"最大的牌为:"+p1.getPlayer_card().get(0));
}else{
c1=p1.getPlayer_card().get(1);
System.out.println(p1.getPlayer_name()+"最大的牌为:"+p1.getPlayer_card().get(1));
}
int num2=cc.compare(p2.getPlayer_card().get(0), p2.getPlayer_card().get(1));
if(num2>0){
c2=p2.getPlayer_card().get(0);
System.out.println(p2.getPlayer_name()+"最大的牌为:"+p2.getPlayer_card().get(0));
}else if(num2==0){
c2=p2.getPlayer_card().get(0);
System.out.println(p2.getPlayer_name()+"最大的牌为:"+p2.getPlayer_card().get(0));
}else{
c2=p2.getPlayer_card().get(1);
System.out.println(p2.getPlayer_name()+"最大的牌为:"+p2.getPlayer_card().get(1));
}
int num3=cc.compare(c1, c2);
if(num3>0){
System.out.println(p1.getPlayer_name()+"获胜了!");
}else if(num==0){
System.out.println("打平了!");
}else{
System.out.println(p2.getPlayer_name()+"获胜了!");
}
System.out.println(p1.getPlayer_name()+"拿的牌为:");
for (int i = 0; i <p1.getPlayer_card().size() ; i++) {
System.out.println(p1.getPlayer_card().get(i).toString());
}
System.out.println(p2.getPlayer_name()+"拿的牌为:");
for (int i = 0; i < p2.getPlayer_card().size(); i++) {
System.out.println(p2.getPlayer_card().get(i).toString());
}
}
}