**
Main.java
**
package pukepai;
import java.util.Scanner;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
CreateNewPai all = new CreateNewPai();
}
}
CreateNewPai.java
package pukepai;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class CreateNewPai {
static String[] num = {"A","2","3", "4", "5", "6", "7", "8", "9", "10", "J", "Q","K"};
static String[] huase = { "", "", "", "" };
{
List<String> allpai = new ArrayList<String>();
List<String> newpai = new ArrayList<String>();
for(int i=0;i<huase.length;i++){
for(int j=0;j<num.length;j++){
String singlepai = huase[i] + num[j];
allpai.add(singlepai);
} //将huase和num两个数组中的元素进行组合
}
// System.out.println("所有的牌为下:");
for(int z=allpai.size();z>0;z--){
double x = Math.random()*allpai.size();
int xx = (int)x;
// System.out.println(xx);
String addpai = allpai.remove(xx);
newpai.add(addpai); //将所有组合好的牌添加进allpai集合中
/* Iterator it = allpai.iterator(); //将所有牌添加到集合中
while(it.hasNext()){
String str = (String) it.next();
System.out.print(str + " ");
}
System.out.println();
*/
}
int c=1;
// System.out.println("重新洗牌如下:"); //随机打乱顺序将所有的牌进行重新组合添加到newpai中
// System.out.println("----------------------------");
Iterator it = newpai.iterator(); //将所有牌添加到集合中
/* while(it.hasNext()){
String str = (String) it.next();
System.out.print(str + "\t");
if(c%7==0){
System.out.println();
}
c++;
}*/
// System.out.println(newpai.size()); //重新洗好的牌为newpai
List<String> p1 = new ArrayList<String>();
for(int z=0;z<2;z++){
double x = Math.random()*newpai.size();
int x1 = (int)x;
String addpai = newpai.remove(x1);
p1.add(addpai);
}
System.out.println("玩家A的手牌为:");
Iterator it1 = p1.iterator(); //将所有牌添加到集合中
while(it1.hasNext()){
String str = (String) it1.next();
System.out.print(str + " ");
}
List<String> p2 = new ArrayList<String>();
for(int z=0;z<2;z++){
double x = Math.random()*newpai.size();
int x1 = (int)x;
String addpai = newpai.remove(x1);
p2.add(addpai);
}
System.out.println();
System.out.println("玩家B的手牌为:");
Iterator it2 = p2.iterator(); //将所有牌添加到集合中
while(it2.hasNext()){
String str = (String) it2.next();
System.out.print(str + " ");
}
System.out.println();
compare p = new compare();
String result1 = p.compare(p1.get(0), p1.get(1));
String result2 = p.compare(p2.get(0), p2.get(1));
System.out.println();
System.out.println("玩家A手牌中较大的牌是"+result1);
System.out.println("玩家B手牌中较大的牌是"+result2);
if(p.compare(result1,result2)==result1){
System.out.println("两张牌中较大的是"+"'"+p.compare(result1,result2)+"'"+"所以,玩家A获胜!");
}
else{
System.out.println("两张牌中较大的是"+ "'"+p.compare(result1,result2)+"'"+"所以,玩家B获胜!");
}
}
}
compare.java
package pukepai;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class compare {
String E1;
String E2;
public String compare(String E1,String E2){
this.E1 = E1;
this.E2 = E2;
int a11 = 0;
int a22 = 0;
char a1 = E1.charAt(1); //取出牌的数字部分
char a2 = E2.charAt(1);
if(E1.length()==3){
a11=10;
}
else if(a1=='A'){
a11=1;
}
else if(a1=='J'){
a11=11;
}
else if(a1=='Q'){
a11=12;
}
else if(a1=='K'){
a11=13;
}
else{
a11 = (int)a1-48;
}
// System.out.print(a11+"\t");
if(E2.length()==3){
a22=10;
}
else if(a2=='A'){
a22=1;
}
else if(a2=='J'){
a22=11;
}
else if(a2=='Q'){
a22=12;
}
else if(a2=='K'){
a22=13;
}
else{
a22 = (int)a2-48;
}
// System.out.print(a22+"\t");
if(a11>a22){
return E1;
}
else if(a11==a22){//设置的花色为''>''>''>''
char c1 = E1.charAt(0);
char c2 = E2.charAt(0);
char D1 = '';
char D2 = '';
char D3 = '';
char D4 = '';
if(c1==D1){
return E1;
}//第一种情况
else if(c1==D2){
if(c2==D1){
return E2;
}
else
return E1;
}//第二种情况
else if(c1==D3){
if(c2==D1||c2==D2){
return E2;
}
else
return E1;
}
else
return E2;
}
else
return E2;
}
}