//花色
public class CardsType {
private static final String [] ct={
"黑桃",
"紅桃",
"梅花",
"方塊"
};
public static String getType(int i){
return ct[i];
}
public static int com(String s,String c){
int s1=find(s);
int s2=find(c);
if(s1==-1)return 100;
if(s2==-1)return -100;
return s2-s1;
}
private static int find(String s){
int i=-1;
for(String st:ct ){
i++;
if(st.equals(s))return i;
}
return i;
}
}
package justforjoke.pkGame;
//牌碼
public class Num {
private static final String[]n={
"0","2","3","4","5","6","7","8","9","10","J","Q","K","A"
};
public static String getN(int i){
if(i<1||i>13)return null;
return n[i];
}
}
package justforjoke.pkGame;
//每張牌
public class Cards implements Comparable{
private String type;
private int num;
public Cards(){}
public Cards(String s,int n){
this.type=s;
this.num=n;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean equals(Object o){
if(!(o instanceof Cards))return false;
Cards c=(Cards)o;
if(this.num==c.getNum()&&this.type.equals(c.getType()))return true;
return false;
}
public int compareTo(Object o) {
if(!(o instanceof Cards))return 1;
Cards c=(Cards)o;
int bjjg;
if((bjjg=CardsType.com(this.type,c.getType()))!=0)return bjjg;
return c.getNum()-this.num;
}
public String toString(){
return type+" : "+Num.getN(num);
}
}
package justforjoke.pkGame;
//壹副新撲克
public class PkC {
public static Cards[] getNewCards(){
Cards[] nc=new Cards[52];
int k=0;
for(int i=0;i<4;i++){
String t=CardsType.getType(i);
for(int j=1;j<14;j++){
nc[k++]=new Cards(t,j);
}
}
return nc;
}
}
package justforjoke.pkGame;
import java.util.Arrays;
import java.util.Random;
//遊戲,包含兩幅撲克
public class Game {
private Cards[]gc=new Cards[104];
private int[] num=new int[104];
private int length=103;
public Game(){
int i=0;
for(Cards c:PkC.getNewCards()){
gc[i++]=c;
}
for(Cards c:PkC.getNewCards()){
gc[i++]=c;
}
for(int j=0;j<104;j++)num[j]=j;
}
//發牌
public Cards[] fp(){
Cards[] pf=new Cards[5];
Random r=new Random();
for(int i=0;i<5;i++){
if(length<1)break;
int t=r.nextInt(length);
int tem=num[t];
num[t]=num[length];
num[length--]=tem;
pf[i]=gc[tem];
}
return pf;
}
//去掉函數中的註釋會打印遊戲的中間過程
public static String cp(Cards[] c){
String result1="同花";
String result2="順子";
String result3="";
for(int i=0;i<4;i++){
if(!c[i].getType().equals(c[i+1].getType()))result1="";
if(c[i+1].getNum()-c[i].getNum()!=1)result2="";
if(c[i].equals(c[i+1]))result3="對子";
//System.out.print(c[i]+" || ");
}
//System.out.println(c[4]);
return result1+result2+result3;
}
public static void main(String []args){
Game g=new Game();
while(true){
Cards[] pf=g.fp();
if(pf[4]==null){
System.out.println("遊戲失敗!!");
break;
}
Arrays.sort(pf);
String r=Game.cp(pf);
if(!r.equals("")){
System.out.println(r);
for(Cards cs:pf)System.out.print(cs+" || ");
break;
}
}
}
}
····上面有5個類。Came類就是用來遊戲的。它會打印遊戲結果。
題目所要求的都解決了,不過有壹點,那就是排序時又大到小排的。
這個在寫代碼的時候沒有註意到。
代碼只會打印遊戲的結果,如果妳想打印其他的可以在代碼中添加。也可以去掉壹些註釋查看中間過程