日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习之模拟纸牌游戏,List的ArrayList,Map的HashMap,重写Collections类的sort方法对指定类进行通过特定属性排序,输入异常处理等的学习...

發布時間:2024/4/17 java 36 豆豆
  • 首先放上測試效果圖


  • 設計框架


?

  • 具體的代碼實現

  • 創建玩家類

?

public class Player implements Comparable<Player>{int id;String name;List<Card> cardList;Integer maxCard;public Player(int id, String name){this.id = id;this.name = name;this.cardList = new ArrayList<Card>();}public int compareTo(Player o) {return this.maxCard.compareTo(o.maxCard);} }

?

?


?

  • 創建撲克牌類
public class Card implements Comparable<Card>{Integer id;String value;public Card(Integer id, String value) {this.id = id;this.value = value;}public int compareTo(Card o) {return this.id.compareTo(o.id);} }

?

  • 創建進行游戲類
public class PlayCards {Scanner console;List<Card> cardlist;Map<Integer, Player> playermap;List<Card> shufflelist;public PlayCards(){console = new Scanner(System.in);cardlist = new ArrayList<Card>();playermap = new HashMap<Integer, Player>();shufflelist = new ArrayList<Card>();} //創建撲克牌的方法public void creatCard() {System.out.println("------創建撲克牌------");String[] head = {"方片","梅花","紅桃","黑桃"};String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};int id = 0;for (int i = 0; i < 13; i++) {for (int j = 0; j < 4; j++) {String tempstr = head[j] + number[i];Card card = new Card(id++, tempstr);cardlist.add(card);}}System.out.println("------撲克牌創建成功!------");System.out.println("*********************************************");for (int i = 0; i < 52; i++) {if(i % 13 == 0) {System.out.println();}System.out.print(cardlist.get(i).value);}System.out.println("\n**********************************************");} //進行洗牌的方法public void shuffle() {Random random = new Random();System.out.println("------開始洗牌------");for (int i = 52; i > 0; i--) {int tempNum = random.nextInt(i);Card tempcard = cardlist.get(tempNum);cardlist.remove(tempNum);shufflelist.add(tempcard);}System.out.println("------洗牌結束!------");} //創建玩家并錄入相關id與姓名的方法 public void creatPlayer() {System.out.println("------創建玩家------");int i = 1;while(i < 3) {try {System.out.println("請輸入第" + i + "位玩家的ID和姓名");System.out.println("請輸入整數ID:");Scanner input = new Scanner(System.in);Integer integer = input.nextInt();Player player = playermap.get(integer);if(player == null) {System.out.println("請輸入姓名:");String name = console.next();Player newPlayer = new Player(integer, name);playermap.put(integer, newPlayer);i++;}else {System.out.println("玩家ID被已占用,請重新輸入!");continue;}}catch(InputMismatchException e) {System.out.println("玩家ID輸入有誤,請輸入規定的整數");continue;}}Set<Entry<Integer, Player>> entry = playermap.entrySet();for (Entry<Integer, Player> ey: entry) {System.out.println("----歡迎玩家:" + ey.getValue().name);}} //進行發牌給玩家的方法public void sendCardToPyer() {System.out.println("規定兩位玩家的手牌數小于27張");try {System.out.println("請輸入每位玩家的手牌數:");Scanner input = new Scanner(System.in);int cardNum = input.nextInt();if (cardNum > 26 || cardNum < 1) {sendCardToPyer();}else {System.out.println("------開始發牌------");int k = 0;Set<Entry<Integer, Player>> entry = playermap.entrySet();for (int i = 0; i < cardNum; i++) {for (Entry<Integer, Player> ey: entry) {ey.getValue().cardList.add(shufflelist.get(k++));System.out.println("玩家:" + ey.getValue().name + "-拿牌");}}System.out.println("------發牌結束------");}}catch(InputMismatchException e) {sendCardToPyer();}} //兩位玩家取出最大的手牌比較并決定勝負以及亮手牌的方法public void whoIsWinner() {Set<Entry<Integer, Player>> entry = playermap.entrySet();List<Player> playerlist = new ArrayList<Player>();for (Entry<Integer, Player> ey: entry) {Collections.sort(ey.getValue().cardList);int listsize = ey.getValue().cardList.size();ey.getValue().maxCard = ey.getValue().cardList.get(listsize -1).id ;playerlist.add(ey.getValue());System.out.println("玩家:" + ey.getValue().name + "最大的手牌為:" + ey.getValue().cardList.get(listsize - 1).value);}Collections.sort(playerlist);System.out.println("------玩家:" + playerlist.get(playerlist.size() - 1).name + "獲勝!------");System.out.println("玩家各自的手牌為:");for (Player player: playerlist){System.out.print(player.name + ": ");for (Card card: player.cardList) {System.out.print(card.value + " ");}System.out.println(); } } }

?


?

  • 創建初始類
public class Initial {public static void main(String[] args) {Scanner input = null;int i = 0;while(true) {System.out.println("按 任意數字 開始游戲");try {input = new Scanner(System.in);i = input.nextInt(); break;}catch(InputMismatchException e) {System.out.println("請按要求輸入!!!");}}PlayCards playcards = new PlayCards();playcards.creatCard();playcards.shuffle();playcards.creatPlayer();playcards.sendCardToPyer();playcards.whoIsWinner();System.out.println("歡迎下次游戲!!!");System.exit(0);if (input != null) {input.close();input = null;}} }

?


?

轉載于:https://www.cnblogs.com/xinglichao/p/8945661.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Java学习之模拟纸牌游戏,List的ArrayList,Map的HashMap,重写Collections类的sort方法对指定类进行通过特定属性排序,输入异常处理等的学习...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。