忙活了許久,知識掌握的還是不牢靠

flynike發表於2021-09-09

package com.imooc;

import java.util.Comparator;

public class Card implements Comparator {

public String suit;//花色

public String point;//點數

public Card(){

}

public Card(String suit,String point){
    this.suit = suit;
    this.point = point;
}

@Override
public String toString() {

    return suit + point;
}

@Override
public int compare(Card o1, Card o2) {

    String suit = "方片 梅花 紅桃  黑桃";
    String point = "2 3 4 5 6 7 8 9 10 J Q K A";
    int result = point.indexOf(o1.point) - point.indexOf(o2.point);
    if(result  0){
        return 1;
    }else{
        int result2 = suit.indexOf(o1.suit) - suit.indexOf(o2.suit);
        if(result2  0){
            return 1;
        }else{
            return 0;
        }
    }
}

}

package com.imooc;
import java.util.ArrayList;
import java.util.List;


public class Player {

public int id;//玩家id
public String name;//玩家姓名
public List handCards;//玩家手牌

public Player(int id,String name){
this.id = id;
this.name = name;
handCards = new ArrayList();
}
}

package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;


public class CardsTest {

public static void main(String[] args) {
System.out.println("**建立撲克牌****");
//建立存放撲克牌的list集合
List cardsList = new ArrayList();
//建立撲克牌的花色陣列
String[] suits = {"方片","梅花","紅桃","黑桃"};
//建立撲克牌的點數陣列
String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0;i for(int j = 0;j Card card = new Card(suits[i],points[j]);
cardsList.add(card);
}
}
System.out.println("**撲克牌建立成功****");
System.out.println("**建立的撲克牌為:" + "n" + cardsList);

System.out.println("******************洗牌開始*************************");
Collections.shuffle(cardsList);//集合中的混排
System.out.println("******************洗牌結束*************************");

System.out.println("******************建立玩家*************************");
//建立map用於存放玩家的id和name
Map map = new HashMap();
//建立list集合用於儲存玩家物件
List playerList = new ArrayList();
for(int i = 0;i  entry : map.entrySet()){
    System.out.println("*******歡迎玩家:" + entry.getValue());
}

System.out.println("******************開始發牌********************");

//將第一張牌發給玩家1
playerList.get(0).handCards.add(cardsList.get(0));
System.out.println("*****玩家:" + playerList.get(0).name + "-拿牌");
//將第二張牌發給玩家2
playerList.get(1).handCards.add(cardsList.get(1));
System.out.println("*****玩家:" + playerList.get(1).name + "-拿牌");
//將第三張牌發給玩家1
playerList.get(0).handCards.add(cardsList.get(2));
System.out.println("*****玩家:" + playerList.get(0).name + "-拿牌");
//將第四張牌發給玩家2
playerList.get(1).handCards.add(cardsList.get(3));
System.out.println("*****玩家:" + playerList.get(1).name + "-拿牌");

System.out.println("******************發牌結束*********************");

System.out.println("******************開始遊戲*********************");
//先比較玩家1手中的牌
Collections.sort(playerList.get(0).handCards, new Card());
//得到玩家1手中的最大牌
Card card1 = playerList.get(0).handCards.get(1);
System.out.println("玩家:" + playerList.get(0).name + "最大的牌為:" + card1);
//再比較玩家2手中的牌
Collections.sort(playerList.get(1).handCards, new Card());
//得到玩家2手中的最大牌
Card card2 = playerList.get(1).handCards.get(1);
System.out.println("玩家:" + playerList.get(1).name + "最大的牌為:" + card2);
//最後比較兩個玩家手中的最大牌,分出勝負
List lastCardList = new ArrayList();
lastCardList.add(card1);
lastCardList.add(card2);
Collections.sort(lastCardList, new Card());
if(lastCardList.get(1).suit.equals(card1.suit) && lastCardList.get(1).point.equals(card1.point)){
    System.out.println("******************玩家:"+playerList.get(0).name+"獲勝!*********************");
}else{
    System.out.println("******************玩家:"+playerList.get(1).name+"獲勝!*********************");
}
//玩家各自的手牌
System.out.println("玩家各自的手牌為:" + "n" + playerList.get(0).name + ":" + playerList.get(0).handCards
        + "n" + playerList.get(1).name + ":" +playerList.get(1).handCards);

}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2618/viewspace-2798954/,如需轉載,請註明出處,否則將追究法律責任。

相關文章