有些方法還是不太會,要參照老師講的用法才能寫對~
package fourth;
/**
?* 撲克牌類
?* @author jiuge
?*
?*/
public class poker implements Comparable<poker>{
public String type; ? ?//花色
public String name; ? ?//牌名
public int typevalue; ?//通過兩個value方便對牌進行大小比較
public int namevalue;
public poker(){
}
public poker(String type,String name){
this.type=type;
this.name=name;
if(type=="紅桃") this.typevalue=4;
else if(type=="黑桃") this.typevalue=3;
else if(type=="方塊") this.typevalue=2;
else if(type=="梅花") this.typevalue=1;
if(name=="J") this.namevalue=11;
else if(name=="Q") this.namevalue=12;
else if(name=="K") this.namevalue=13;
else if(name=="A") this.namevalue=14;
else this.namevalue=Integer.valueOf(name);
}
@Override
public int compareTo(poker o) { //重寫compare方法
int returnvalue = -1000;
if(this.namevalue>o.namevalue)
returnvalue= 1;
else if(this.namevalue<o.namevalue)
returnvalue= -1;
else if(this.namevalue==o.namevalue){
if(this.typevalue>o.typevalue)
returnvalue= 1;
else if(this.typevalue<o.typevalue)
returnvalue= -1;
else returnvalue=0;
}
return returnvalue;
}
}
package fourth;
public class player {
String id;
String name;
public player(String id,String name){
this.id=id;
this.name=name;
}
}
package fourth;
import java.util.*;
public class test {
public void creat(){
/***********************給牌****************************/
poker[] pokers = new poker[52];
String[] types={"紅桃","黑桃","方塊","梅花"};
String[] names={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++){ ? ? ? ? ?//創(chuàng)建一副牌,這時的撲克牌組是有順序的
for(int j=0;j<13;j++){
pokers[(i*13+j)]=new poker(types[i],names[j]);
}
}
/************************洗牌***************************/
Integer[] arr=new Integer[52]; ? //建立一個52長度(一副牌的長度)的數(shù)組,將下標作為值
for(int i=0;i<52;i++){
arr[i]=i;
}
List<Integer> num=new ArrayList<Integer>();
num.addAll(Arrays.asList(arr)); ?//把該數(shù)組轉換成序列后打亂順序
Collections.shuffle(num);
for (int i : num) { ? ? ?//把亂序的值再返回給數(shù)組,得到一個亂序的數(shù)組,且每次運行程序這個數(shù)組都不一樣
arr[i]=num.get(i); ? //把這個亂序的數(shù)組作為撲克牌組的下標,能夠達到洗牌的效果
}
/************************發(fā)牌****************************/
Scanner in=new Scanner(System.in);//準備創(chuàng)建兩名玩家
System.out.print("請輸入第一位玩家的name:");
String name=in.next();
player player1=new player("1",name);
System.out.print("請輸入第二位玩家的name:");
name=in.next();
player player2=new player("2",name);
List<poker> p1=new ArrayList<poker>();//player1的牌組
List<poker> p2=new ArrayList<poker>();//player2的牌組
System.out.print("請輸入發(fā)牌數(shù)量(26張以內):");
int i;
do{
i=in.nextInt();
if(i<0||i>26) System.out.print("選擇錯誤!請重新選擇發(fā)牌數(shù)量:");
}while(i<0||i>26);
for(int n=0;n<2*i;n++){ ? ?//給兩名玩家交替發(fā)牌
if(n%2==0){ ? ? ? ? ??
p1.add(pokers[arr[n]]); ?//pokers中的對象已固定,通過亂序的數(shù)組arr作為下標,實現(xiàn)洗牌功能
System.out.println("玩家"+player1.name+"發(fā)到一張牌");
}
else{
p2.add(pokers[arr[n]]);
System.out.println("玩家"+player2.name+"發(fā)到一張牌");
}
}
System.out.println("**********發(fā)牌完畢***********");
/*************************輸贏***************************/
System.out.print("玩家"+player1.name+"的牌:"); //玩家一獲得的牌
for(int a=0;a<p1.size();a++){ ? ? ??
poker po1=p1.get(a);
System.out.print(po1.type+po1.name+" ");
}
System.out.println();
System.out.print("玩家"+player2.name+"的牌:"); //玩家二獲得的牌
for(int a=0;a<p2.size();a++){
poker po2=p2.get(a);
System.out.print(po2.type+po2.name+" ");
}
System.out.println();
Collections.sort(p1);//這里對牌進行排序,排序是遞增的,故最后一張牌是最大的
Collections.sort(p2);//下面獲取兩名玩家各自最大的牌
System.out.println("玩家"+player1.name+"最大的牌:"+p1.get((p1.size()-1)).type+p1.get((p1.size()-1)).name);
System.out.println("玩家"+player2.name+"最大的牌:"+p2.get((p2.size()-1)).type+p2.get((p2.size()-1)).name);
int result=p1.get((p1.size()-1)).compareTo(p2.get((p2.size()-1)));//對兩名玩家最大的牌進行比較
switch(result){
case -1:System.out.println("玩家"+player2.name+"贏了!");break;
case 1:System.out.println("玩家"+player1.name+"贏了!");break;
}
in.close();
}
public static void main(String[] args) {
System.out.println("-------------------歡迎使用撲克牌!--------------------");
System.out.println("規(guī)則一:雙方發(fā)等量的牌,比較其中最大的牌,牌的點數(shù)是主要因素。");
System.out.println("規(guī)則二:若點數(shù)相等,則繼續(xù)比較牌的花色,花色權重為紅桃>黑桃>方塊>梅花。");
test t=new test();
t.creat();
}
}
2019-09-02
?
2019-07-24
大佬求膜拜
2019-07-23