終于完成了,和老師的一樣,相互學習,望高手指點
/**
?* 創建撲克牌類
?* @author?
?*
?*/
public class Card implements Comparable<Card> {
?? ?String num;
?? ?String type;
?? ?int ?rank; ? ?//rank代表等級,值越大,等級越高 ? ? ? ?
?? ?public Card(String type,String num,int rank) {
?? ??? ?this.num=num;
?? ??? ?this.type=type;
?? ? ? ?this.rank=rank;
?? ?}?? ?
?? ?
?? ?public Card() {
?? ??? ?
?? ?}
?? ?@Override
?? ?public int compareTo(Card o) {
?? ??? ??
?? ??? ?return this.rank-o.rank; ? ? ?//比較rank值的大小
?? ?}
}
/**
?* 創建玩家類
?* @author?
?*
?*/
public class Player ?{
?? ?String id; ? ?
?? ?String name;
?? ?List<Card>pCards; ? //用于存放玩家手中的牌
?? ?
?? ?public Player(String id,String name) {
?? ??? ?this.id=id;
?? ??? ?this.name=name;
?? ? ? ?pCards=new ArrayList<Card>();
?? ?}
}
/**
?*?
?* @author?
?*
?*/
public class CardDemo {
?? ?List<Card>cards;
?? ?List<Player>players;
?? ?Player p1; ? ?//玩家
?? ?Player p2;
?? ?Card p1max; ? //玩家1最大的牌
? ? Card p2max; ? //玩家2最大的牌
?? ?Card p1min; ? //玩家1最小的牌
?? ?Card p2min; ? //玩家2最小的牌
//創建撲克牌資源數組
?? ?String[] CardType=new String[] {"方塊","梅花","紅桃","黑桃"}; ?//順序不可調換,否則會影響大小比較的rank值
?? ?String[] CardNum=new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
?? ??? ?
?? ?public CardDemo() {
?? ??? ?
?? ??? ?this.cards=new ArrayList<Card>();
?? ??? ?this.players=new ArrayList<Player>();
?? ?}?? ?
?? ?
//創建撲克牌?? ?
?? ?public void CreatCard() {
?? ??? ?
?? ??? ?System.out.println("---------創建撲克牌-----------");
?? ??? ?for(int i=0;i<13;i++) {
?? ??? ??? ?for(int j=0;j<4;j++) {
?? ??? ??? ? cards.add(new Card(CardType[j],CardNum[i],i*13+j));
?? ??? ??? ?}
?? ??? ?}
?? ??? ?//遍歷打印撲克牌資源
?? ??? ? System.out.println("--------撲克牌創建成功!----------");
?? ??? ? System.out.print("撲克牌為");
?? ??? ? for(Card c:cards) {?
?? ??? ??? ? System.out.print(c.type+c.num+",");?
?? ??? ? }
?? ??? ? System.out.println("總共52張");
?? ?}
?? ?
//進行洗牌?? ?
?? ?public void washCard() {
?? ?System.out.println("----------開始洗牌--------------");?? ?
?? ??? ?Collections.shuffle(cards); ? ? ? ?//進行洗牌
?? ?System.out.println("----------洗牌結束-------------");?? ?
?? ?
?? ?}
//創建兩名玩家
? ? ?public void creatPlayer() {
? ? ? ? //鍵盤上輸入兩名玩家信息 ?? ??
? ? ?? ?Scanner console=new Scanner(System.in);
? ? ? ? System.out.println("請輸入第一位玩家的ID和姓名:");
? ? ? ? System.out.println("請輸入ID");
? ? ?? ?String id1=console.next();?
? ? ?? ?System.out.println("請輸入姓名:");
? ? ?? ?String name1=console.next();
? ? ?? ?p1=new Player(id1, name1);
? ? ? ? players.add(p1);
? ? ? ? System.out.println("請輸入第二位玩家的ID和姓名:");
? ? ? ? System.out.println("請輸入ID");
? ? ?? ?String id2=console.next();?
? ? ?? ?System.out.println("請輸入姓名:");
? ? ?? ?String name2=console.next();
? ? ?? ?p2=new Player(id2, name2);
? ? ? ? players.add(p2);
? ? ?? ?System.out.println("歡迎玩家:"+p1.name);?
? ? ?? ?System.out.println("歡迎玩家:"+p2.name);?
? ? ?}
? ? ?
?//進行發牌
? ? ?public void distrubiteCard() {
? ? ?? ?System.out.println("---------開始發牌------------");?
? ? ?? ?p1.pCards.add(cards.get(0));
? ? ? ? System.out.println("----玩家:"+p1.name+"-拿牌------");
? ? ? ? p2.pCards.add(cards.get(1));
? ? ? ? System.out.println("----玩家:"+p2.name+"-拿牌------");
? ? ? ? p1.pCards.add(cards.get(2));
? ? ? ? System.out.println("----玩家:"+p1.name+"-拿牌------");
? ? ? ? p2.pCards.add(cards.get(3));
? ? ? ? System.out.println("----玩家:"+p2.name+"-拿牌------");
? ? ?? ?System.out.println("---------發牌結束------------");?
? ? ?}
? ? ?
//開始游戲
? ? ?public void playGame() {
? ? System.out.println("--------開始游戲-----------");?? ?
? ? Collections.sort(p1.pCards); ? ? //對集合pCards進行排序
? ? Collections.sort(p2.pCards);
? ? p1max=p1.pCards.get(1); ? ?
? ? p2max=p2.pCards.get(1);
? ? System.out.println(p1.name+"最大的手牌:"+p1max.type+p1max.num); ? //輸出玩家1的最大手牌
? ? System.out.println(p2.name+"最大的手牌:"+p2max.type+p2max.num); ? //輸出玩家2的最大手牌
? ? if(p1max.rank>p2max.rank)
? ? ?? ?{ ?System.out.println("-----玩家"+p1.name+"獲勝!-------"); ?}
? ? else?? ?
? ? ?? ?{ ?System.out.println("-----玩家"+p2.name+"獲勝!--------"); ?}
? ? //輸出玩家各自的手牌
? ? System.out.println("-------玩家各自的手牌為:----------");
? ? p1min=p1.pCards.get(0);
? ? p2min=p2.pCards.get(0);
? ? //打印兩名玩家各自手上的兩張牌
? ? System.out.println(p1.name+":"+p1max.type+p1max.num+","+p1min.type+p1min.num);
? ? System.out.println(p2.name+":"+p2max.type+p2max.num+","+p2min.type+p2min.num);
? ? System.out.println("--------游戲結束-----------");?? ? ?? ??
? ? ?}
? ? ?
//主函數?? ?
?? ?public static void main(String[] args) {
?? ??? ?CardDemo cd=new CardDemo();
? ? ? ? cd.CreatCard(); ? ? ? ? ?//創建撲克牌
? ? ? ? cd.washCard(); ? ? ? ? ? //洗牌
? ? ? ? cd.creatPlayer(); ? ? ? ?//創建玩家
? ? ? ? cd.distrubiteCard(); ? ? //進行發牌
? ? ? ? cd.playGame(); ? ? ? ? ? //開始游戲
?? ?}
}
2018-09-10
異常處理語句塊并沒有看到呢?
2019-07-29
受到你的啟發,寫了出來。我覺得我的更好一點,而且跟視頻的一樣。
package pick;
public class Card implements Comparable<Card> {
? ? String num;
? ? String type;
? ? int ?rank; ? ?//rank代表等級,值越大,等級越高 ? ? ? ?
? ? public Card(String type,String num,int rank) {
? ? ? ? this.num=num;
? ? ? ? this.type=type;
? ? ? ? this.rank=rank;
? ? } ? ?
? ??
? ? public Card() {
? ? ? ??
? ? }
? ? @Override
? ? public int compareTo(Card o) {
? ? ? ? ?
? ? ? ? return this.rank-o.rank; ? ? ?//比較rank值的大小
? ? }
}
package pick;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Dala {
public List<Card>pukepais;
/**
* 在構造器中初始化pukepais屬性
*/
public Dala(){
this.pukepais=new ArrayList<Card>();
}
/**
* 創建撲克牌
*/
public void fapai(){
System.out.println("-----------創建撲克牌---------------");
String[] hu= {"方片","梅花","紅桃","黑桃"};
String[] ku = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int o=1;
for(int i=0;i<13;i++){
for(int t = 0;t<4;t++){
Card yu=new Card(hu[t],ku[i],o);
o++;
pukepais.add(yu);
}}
}
/**
* 遍歷
*/
public void bianli(){
System.out.println("-----------撲克牌創建成功---------------");
for(int p=0;p<52;p++)
System.out.print(pukepais.get(p).type+pukepais.get(p).num+","+pukepais.get(p).rank);
}
/**
* 洗牌
*/
public void xipai(){
System.out.println("-----------開始洗牌---------------");
for(int i=0;i<52;i++){
Random ra = new Random();
int t = ra.nextInt(51);
Card y = pukepais.get(i);
pukepais.remove(i);
pukepais.add(i,pukepais.get(t));
pukepais.remove(t);
pukepais.add(t,y);
}
System.out.println("-----------洗牌結束---------------");
for(int p=0;p<52;p++)
System.out.print(pukepais.get(p).type+pukepais.get(p).num+",");
}
/**
* 創建玩家
*/
public void Wanjia(){
System.out.println("-----------創建玩家---------------");
Scanner c = new Scanner(System.in);
//輸入整數id
?boolean is = true;
?while (is) {
? ?System.out.println("請輸入第一位玩家的id:");
? ?try {
? ? ? ?int id = c.nextInt();
? ? ? ?is = false;
? ?} catch (InputMismatchException e) {
? ? ? ?// TODO: handle exception
? ? ? ?System.out.println("請輸入整數類型的ID!");
? ? ? ?//清空數據,使下一次可以輸入
? ? ? ?c.next();
? ? ? ?is = true;
? ? ? ?continue;
? ?} catch (Exception e) {
? ? ? ?// TODO: handle exception
? ? ? ?e.printStackTrace();
? ? ? ?break;
? ?}
}
System.out.println("請輸入第一位玩家的姓名:");
String name = c.next();
is = true;
?while (is) {
? ?System.out.println("請輸入第二位玩家的id:");
? ?try {
? ? ? ?int id2 = c.nextInt();
? ? ? ?is = false;
? ?} catch (InputMismatchException e) {
? ? ? ?// TODO: handle exception
? ? ? ?System.out.println("請輸入整數類型的ID!");
? ? ? ?//清空數據,使下一次可以輸入
? ? ? ?c.next();
? ? ? ?is = true;
? ? ? ?continue;
? ?} catch (Exception e) {
? ? ? ?// TODO: handle exception
? ? ? ?e.printStackTrace();
? ? ? ?break;
? ?}
}
System.out.println("請輸入第二位玩家的姓名:");
String name2 = c.next();
System.out.println("歡迎玩家:"+name);
System.out.println("歡迎玩家:"+name2);
//發牌
System.out.println("-----------開始發牌--------------");
Card[] hu1 = new Card[2];
Card[] hu2 = new Card[2];
Random ra = new Random();
int x = 0;
for(int i=51;i>47;i=i-2){
int t = ra.nextInt(i);
hu1[x] = pukepais.get(t);
pukepais.remove(t);
System.out.println("---玩家:"+name+"-拿牌");
int y = ra.nextInt(i-1);
hu2[x] = pukepais.get(y);
pukepais.remove(y);
System.out.println("---玩家:"+name2+"-拿牌");
x++;
}
System.out.println("-----------發牌結束--------------");
//開始游戲
System.out.println("-----------開始游戲--------------");
Card[] ui = new Card[2];
if(hu1[0].rank>hu1[1].rank){
ui[0] = hu1[0];
System.out.println("玩家:"+name+"最大的手牌為:"+hu1[0].type+hu1[0].num);
}else{ui[0] = hu1[1];System.out.println("玩家:"+name+"最大的手牌為:"+hu1[1].type+hu1[1].num);}
if(hu2[0].rank>hu2[1].rank){
ui[1] = hu2[0];
System.out.println("玩家:"+name+"最大的手牌為:"+hu2[0].type+hu2[0].num);
}else{ui[1] = hu2[1];System.out.println("玩家:"+name2+"最大的手牌為:"+hu2[1].type+hu2[1].num);}
//獲勝
if(ui[0].rank>ui[1].rank){
System.out.println("-----------玩家:"+name+"獲勝!--------------");
}else{System.out.println("-----------玩家:"+name2+"獲勝!--------------");}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dala mo = new Dala();
mo.fapai();
mo.bianli();
mo.xipai();
mo.Wanjia();
}
}
2019-07-29
受到你的啟發,寫了出來。我覺得我的更好一點,而且跟視頻的一樣。
package pick;
public class Card implements Comparable<Card> {
? ? String num;
? ? String type;
? ? int ?rank; ? ?//rank代表等級,值越大,等級越高 ? ? ? ?
? ? public Card(String type,String num,int rank) {
? ? ? ? this.num=num;
? ? ? ? this.type=type;
? ? ? ? this.rank=rank;
? ? } ? ?
? ??
? ? public Card() {
? ? ? ??
? ? }
? ? @Override
? ? public int compareTo(Card o) {
? ? ? ? ?
? ? ? ? return this.rank-o.rank; ? ? ?//比較rank值的大小
? ? }
}
package pick;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Dala {
/**
* 用來承裝學生類型的對象
*/
public List<Card>pukepais;
/**
* 在構造器中初始化pukepais屬性
*/
public Dala(){
this.pukepais=new ArrayList<Card>();
}
/**
* 創建撲克牌
*/
public void fapai(){
System.out.println("-----------創建撲克牌---------------");
String[] hu= {"方片","梅花","紅桃","黑桃"};
String[] ku = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int o=1;
for(int i=0;i<13;i++){
for(int t = 0;t<4;t++){
Card yu=new Card(hu[t],ku[i],o);
o++;
pukepais.add(yu);
}}
}
/**
* 遍歷
*/
public void bianli(){
System.out.println("-----------撲克牌創建成功---------------");
for(int p=0;p<52;p++)
System.out.print(pukepais.get(p).type+pukepais.get(p).num+","+pukepais.get(p).rank);
}
/**
* 洗牌
*/
public void xipai(){
System.out.println("-----------開始洗牌---------------");
for(int i=0;i<52;i++){
Random ra = new Random();
int t = ra.nextInt(51);
Card y = pukepais.get(i);
pukepais.remove(i);
pukepais.add(i,pukepais.get(t));
pukepais.remove(t);
pukepais.add(t,y);
}
System.out.println("-----------洗牌結束---------------");
for(int p=0;p<52;p++)
System.out.print(pukepais.get(p).type+pukepais.get(p).num+",");
}
/**
* 創建玩家
*/
public void Wanjia(){
System.out.println("-----------創建玩家---------------");
Scanner c = new Scanner(System.in);
//輸入整數id
?boolean is = true;
?while (is) {
? ?System.out.println("請輸入第一位玩家的id:");
? ?try {
? ? ? ?int id = c.nextInt();
? ? ? ?is = false;
? ?} catch (InputMismatchException e) {
? ? ? ?// TODO: handle exception
? ? ? ?System.out.println("請輸入整數類型的ID!");
? ? ? ?//清空數據,使下一次可以輸入
? ? ? ?c.next();
? ? ? ?is = true;
? ? ? ?continue;
? ?} catch (Exception e) {
? ? ? ?// TODO: handle exception
? ? ? ?e.printStackTrace();
? ? ? ?break;
? ?}
}
System.out.println("請輸入第一位玩家的姓名:");
String name = c.next();
is = true;
?while (is) {
? ?System.out.println("請輸入第二位玩家的id:");
? ?try {
? ? ? ?int id2 = c.nextInt();
? ? ? ?is = false;
? ?} catch (InputMismatchException e) {
? ? ? ?// TODO: handle exception
? ? ? ?System.out.println("請輸入整數類型的ID!");
? ? ? ?//清空數據,使下一次可以輸入
? ? ? ?c.next();
? ? ? ?is = true;
? ? ? ?continue;
? ?} catch (Exception e) {
? ? ? ?// TODO: handle exception
? ? ? ?e.printStackTrace();
? ? ? ?break;
? ?}
}
System.out.println("請輸入第二位玩家的姓名:");
String name2 = c.next();
System.out.println("歡迎玩家:"+name);
System.out.println("歡迎玩家:"+name2);
//發牌
System.out.println("-----------開始發牌--------------");
Card[] hu1 = new Card[2];
Card[] hu2 = new Card[2];
Random ra = new Random();
int x = 0;
for(int i=51;i>47;i=i-2){
int t = ra.nextInt(i);
hu1[x] = pukepais.get(t);
pukepais.remove(t);
System.out.println("---玩家:"+name+"-拿牌");
int y = ra.nextInt(i-1);
hu2[x] = pukepais.get(y);
pukepais.remove(y);
System.out.println("---玩家:"+name2+"-拿牌");
x++;
}
System.out.println("-----------發牌結束--------------");
//開始游戲
System.out.println("-----------開始游戲--------------");
Card[] ui = new Card[2];
if(hu1[0].rank>hu1[1].rank){
ui[0] = hu1[0];
System.out.println("玩家:"+name+"最大的手牌為:"+hu1[0].type+hu1[0].num);
}else{ui[0] = hu1[1];System.out.println("玩家:"+name+"最大的手牌為:"+hu1[1].type+hu1[1].num);}
if(hu2[0].rank>hu2[1].rank){
ui[1] = hu2[0];
System.out.println("玩家:"+name+"最大的手牌為:"+hu2[0].type+hu2[0].num);
}else{ui[1] = hu2[1];System.out.println("玩家:"+name2+"最大的手牌為:"+hu2[1].type+hu2[1].num);}
//獲勝
if(ui[0].rank>ui[1].rank){
System.out.println("-----------玩家:"+name+"獲勝!--------------");
}else{System.out.println("-----------玩家:"+name2+"獲勝!--------------");}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dala mo = new Dala();
mo.fapai();
mo.bianli();
mo.xipai();
mo.Wanjia();
}
}
2019-06-15
作業的原意應該是先比較點數大小,如果點數相同,再比較花色,例如方片7是要大于黑桃2的
但如果只是給Card類加一個rank屬性的話,這種通過先后比較兩個屬性的方法,體現不出來,所以你的compareTo方法的重寫還可以改一下,用個兩層的if循環把num和type先后比較一下;或者也可以繼續用rank,但創建撲克牌的時候順序換一下
2019-04-24
rank的想法很好
2019-03-13
我Collection.sort方法報錯用不了,說不適合List這種類型
2019-03-05
我的發牌代碼和你一樣,但是不知道為什么會報空指針異常
2019-01-14
? Player?player1=new?Player(id1,name1);和??
??player1=new?Player(id1,name1);?
?有什么區別嗎?
我用第二個就可以運行,第一個就報錯?
2018-11-05
我想問一下,在對兩人手牌自己內部排序時使用了Collection.sort方法。傳入的是集合Pcards,這個集合中的單個元素包含Card的三個屬性,這里為什么默認的對rank屬性進行的排序?求解
2018-09-19
rank計算為何是?i*13+j ???