游戏规则:2名玩家,每人随机拿2张牌,取各自大的那张,进行比较,得出获胜者。
共52张牌,没有大小王。先比较点数,若点数相同则比较花色。
从大到小为:A,K,Q,J,10,9,8,7,6,5,4,3,2
从大到小为:”黑桃”,”红桃”,”梅花”,”方块“”
Cards
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//创建一副牌。这副牌的索引用来比较牌的大小。
public class Cards {
private String cardPoints; //牌的点数
public String getCardPoints() {
return cardPoints;
}
public String getCardSuit() {
return cardSuit;
}
private String cardSuit; //花色
final String[] cardPointsArr= {"A","K","Q","J","10","9","8","7","6","5","4","3","2"};
final String[] cardSuitArr= {"黑桃","红桃","梅花","方块"};
public List<Cards> newCards=new ArrayList<Cards>(); //用于存放一副牌,没有大小王,52张。
public List<Cards> newCards1=new ArrayList<Cards>();
public Cards(String suit,String point) { //单张牌的构造方法
cardPoints=point;
cardSuit=suit;
}
public void creatCards() { //创建一副牌 存入 newCards,
for(String temp1:cardPointsArr) {
for(String temp:cardSuitArr) {
Cards c=new Cards(temp1,temp);
newCards.add(c);
newCards1.add(c);
}
}
System.out.println("*************洗牌成功!*************");//虽然没有洗牌,但是发牌是随机发的,这是句话给客户看的。
}
}
Plays
package com.imooc;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
//创建玩家
public class Plays {
private String name; //姓名
public String getName() {
return name;
}
private List<Cards> handCards;// 手牌
public List<Cards> getHandCards() {
return handCards;
}
Plays play,play1;
public Plays(String name) { //玩家构造方法
this.name=name;
handCards=new ArrayList<Cards>();
}
public void creatPlays() { //创建2个玩家 play和paly1
System.out.println("***********************************");
System.out.println("***********请输入第一位玩家姓名:*********");
Scanner input=new Scanner(System.in);
String temp=input.nextLine();
play=new Plays(temp);
System.out.println("***********请输入第二位玩家姓名:*********");
String temp1=input.nextLine();
play1=new Plays(temp1);
System.out.println("*************玩家初始化成功!*************");
}
public void getHandCards(Plays one,Plays two,List<Cards> neards) { //随机给每个玩家各发2张牌
System.out.println("***************开始发牌*****************");
Random random=new Random();
for(int j=0;j<2;j++) {
do{int i=random.nextInt(52);
one.handCards.add(neards.get(i));
}while(one.handCards.contains(neards.get(j)));
}
neards.removeAll(one.handCards);
for(int j=0;j<2;j++) {
do{int i=random.nextInt(50);
two.handCards.add(neards.get(i));
}while(two.handCards.contains(neards.get(j)));
}
System.out.println("****************发牌完毕***************");
}
}
Compare
package com.imooc;
import java.util.List;
//用来比较2个人手牌的大小,得出获胜者。
public class Compare {
static public void compare(Plays one,Plays two ,List<Cards> aq) {
int oneMax,twoMax;
Cards temp= one.getHandCards().get(0);//分别得到2个人的2张牌。
Cards temp1= one.getHandCards().get(1);
Cards temp2= two.getHandCards().get(0);
Cards temp3= two.getHandCards().get(1);
int oneIndex=aq.indexOf(temp); //得到这4张牌的索引。
int oneIndex1=aq.indexOf(temp1);
int twoIndex=aq.indexOf(temp2);
int twoIndex1=aq.indexOf(temp3);
if(oneIndex<oneIndex1) { //通过索引比较大小。
oneMax=oneIndex;
}else { oneMax=oneIndex1; }
if(twoIndex<twoIndex1) {
twoMax=twoIndex;
}else { twoMax=twoIndex1; }
if(oneMax<twoMax) { //比较出获胜者
System.out.println("**********玩家"+one.getName()+"获胜************");
}else {System.out.println("**********玩家"+two.getName()+"获胜************");}
System.out.println("**********玩家"+one.getName()+"的手牌是************");
System.out.println(temp.getCardSuit()+temp.getCardPoints()+"\t"+temp1.getCardSuit()+temp1.getCardPoints());
System.out.println("**********玩家"+two.getName()+"的手牌是************");
System.out.println(temp2.getCardSuit()+temp2.getCardPoints()+"\t"+temp3.getCardSuit()+temp3.getCardPoints());
}
}
Test
package com.imooc;
//测试窗口。
public class Test {
public static void main(String[] args) {
Cards aq=new Cards("a","a");//创建1个对象,用来调用方法
Plays aw=new Plays("aa"); //创建1个对象,用来调用方法
aq.creatCards();
aw.creatPlays();
aw.getHandCards(aw.play, aw.play1, aq.newCards);
Compare.compare(aw.play, aw.play1, aq.newCards1);
}
}