JAVA collection集合之撲克牌游戲?qū)嵗?/h1>
更新時(shí)間:2016年11月03日 11:33:17 作者:小猩
本篇文章主要介紹了JAVA collection集合之撲克牌游戲?qū)嵗?,使用了collection集合開(kāi)發(fā)小游戲,有需要的可以了解一下。
Collection 層次結(jié)構(gòu)中的根接口。Collection表示一組對(duì)象,這些對(duì)象也稱為collection的元素。一些 collection 允許有重復(fù)的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無(wú)序的。JDK 不提供此接口的任何直接 實(shí)現(xiàn):它提供更具體的子接口(如 Set 和 List)實(shí)現(xiàn)。此接口通常用來(lái)傳遞 collection,并在需要最大普遍性的地方操作這些 collection。
主要內(nèi)容:這里使用collection集合,模擬香港電影中大佬們玩的撲克牌游戲。
1、游戲規(guī)則:兩個(gè)玩家每人手中發(fā)兩張牌,進(jìn)行比較。比較每個(gè)玩家手中牌最大的點(diǎn)數(shù),大小由A-2,點(diǎn)數(shù)大者獲勝。如果點(diǎn)數(shù)相同,則比較花色,大小由黑(4)、紅(3)、梅(2)、方(1),花色大者獲勝。
2、實(shí)現(xiàn)步驟:
創(chuàng)建一副撲克牌A-2,四種花色黑(4)、紅(3)、梅(2)、方(1)共52張牌;
創(chuàng)建兩個(gè)玩家包含玩家ID和姓名、所持牌Card信息;
洗牌并向兩位玩家各發(fā)兩張牌;
比較玩家手中牌大小,得出獲勝者;
3、程序?qū)崿F(xiàn)
牌Card類:包含牌的數(shù)字和花色
package collectiontest.games;
public class Card {
private Integer id; //牌的大小
private Integer type;//牌的花色
public Card(Integer id, Integer type) {
this.id = id;
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
@Override
public String toString() {
return "Card [id=" + id + ", type=" + type + "]";
}
}
撲克牌Poker類:包含撲克牌Card A-2
package collectiontest.games;
public class Poker {
private Card id2 ;
private Card id3 ;
private Card id4 ;
private Card id5 ;
private Card id6 ;
private Card id7 ;
private Card id8 ;
private Card id9 ;
private Card id10 ;
private Card J ;
private Card Q ;
private Card K ;
private Card A ;
public Poker() {
}
//四個(gè)類型:黑--4、紅--3、梅--2、方--1
public Poker(Integer type) {
this.id2 = new Card(2, type);
this.id3 = new Card(3, type);
this.id4 = new Card(4, type);
this.id5 = new Card(5, type);
this.id6 = new Card(6, type);
this.id7 = new Card(7, type);
this.id8 = new Card(8, type);
this.id9 = new Card(9, type);
this.id10 = new Card(10, type);
this.J = new Card(11, type);
this.Q = new Card(12, type);
this.K = new Card(13, type);
this.A = new Card(14, type);
}
public Card getId2() {
return id2;
}
public void setId2(Card id2) {
this.id2 = id2;
}
public Card getId3() {
return id3;
}
public void setId3(Card id3) {
this.id3 = id3;
}
public Card getId4() {
return id4;
}
public void setId4(Card id4) {
this.id4 = id4;
}
public Card getId5() {
return id5;
}
public void setId5(Card id5) {
this.id5 = id5;
}
public Card getId6() {
return id6;
}
public void setId6(Card id6) {
this.id6 = id6;
}
public Card getId7() {
return id7;
}
public void setId7(Card id7) {
this.id7 = id7;
}
public Card getId8() {
return id8;
}
public void setId8(Card id8) {
this.id8 = id8;
}
public Card getId9() {
return id9;
}
public void setId9(Card id9) {
this.id9 = id9;
}
public Card getId10() {
return id10;
}
public void setId10(Card id10) {
this.id10 = id10;
}
public Card getJ() {
return J;
}
public void setJ(Card j) {
J = j;
}
public Card getQ() {
return Q;
}
public void setQ(Card q) {
Q = q;
}
public Card getK() {
return K;
}
public void setK(Card k) {
K = k;
}
public Card getA() {
return A;
}
public void setA(Card a) {
A = a;
}
}
玩家Player類:包含玩家ID和姓名、所持卡片信息
package collectiontest.games;
import java.util.ArrayList;
import java.util.List;
public class Player {
//玩家的ID
private String id ;
//玩家姓名
private String name ;
//玩家所持牌
private List<Card> pokerType ;
public Player() {
}
public Player(String id, String name, List<Card> pokerType) {
this.id = id;
this.name = name;
this.pokerType = new ArrayList<>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Card> getPokerType() {
return pokerType;
}
public void setPokerType(List<Card> pokerType) {
this.pokerType = pokerType;
}
}
撲克牌游戲主類:包含1)撲克牌創(chuàng)建 2)玩家創(chuàng)建 3)洗牌 4)發(fā)牌 5)比較勝負(fù)
package collectiontest.games;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class GamsBegin {
// 創(chuàng)建撲克牌
public Set<Poker> cPoker() {
System.out.println("**********開(kāi)始創(chuàng)建撲克牌**********");
// 創(chuàng)建一副poker
// 四個(gè)類型:黑--4、紅--3、梅--2、方--1
Set<Poker> pokers = new HashSet<>();
Poker[] poker = { new Poker(1), new Poker(2), new Poker(3),
new Poker(4) };
/*
* Collections工具類的使用
* Collections.addAll(pokers, new Poker(1), new Poker(2), new Poker(3),new Poker(4));
*
* */
pokers.addAll(Arrays.asList(poker));
System.out.println("**********撲克牌創(chuàng)建成功**********");
return pokers;
}
// 創(chuàng)建兩個(gè)玩家
public Map<String, Player> cPlayer() {
System.out.println("**********開(kāi)始創(chuàng)建玩家**********");
Map<String, Player> map = new HashMap<String, Player>();
// 控制數(shù)量
Integer control = 0;
System.out.println("創(chuàng)建兩名玩家,根據(jù)提示創(chuàng)建");
Scanner console = new Scanner(System.in);
while (true) {
System.out.println("請(qǐng)輸入第 "+(control+1)+" 個(gè)玩家ID:");
String courseId = console.next();
if (isNumeric(courseId)) {
System.out.println("請(qǐng)輸入第 "+(control+1)+" 個(gè)玩家姓名:");
String courseName = console.next();
Player players = new Player(courseId, courseName, null);
//保存數(shù)據(jù)
map.put(courseId, players);
System.out.println("添加第 " + (control + 1) + " 個(gè)玩家 " + courseName
+ " 成功");
//數(shù)量自加
control++;
} else {
System.out.println("*****請(qǐng)輸入數(shù)字ID*****");
continue;
}
if (control == 2) {
break;
}
}
System.out.println("**********玩家創(chuàng)建成功**********");
return map;
}
// 判斷輸入是否為數(shù)字, Character.isDigit()為java方法
public boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 洗牌 :也可以產(chǎn)生52個(gè)不同隨機(jī)數(shù),實(shí)現(xiàn)洗牌
*
**/
public List<Card> wPoker(Set<Poker> pokers) {
System.out.println("**********開(kāi)始洗牌**********");
//利用List的有序排序,洗牌之后保存順序不變
List<Card> listCard = new ArrayList<>();
// 利用Set集合的無(wú)序排序,實(shí)現(xiàn)洗牌
Set<Card> listSet = new HashSet<>();
//保存到Set集合,無(wú)序
for (Poker pk : pokers) {
listSet.add(pk.getId2());
listSet.add(pk.getId3());
listSet.add(pk.getId4());
listSet.add(pk.getId5());
listSet.add(pk.getId6());
listSet.add(pk.getId7());
listSet.add(pk.getId8());
listSet.add(pk.getId9());
listSet.add(pk.getId10());
listSet.add(pk.getJ());
listSet.add(pk.getQ());
listSet.add(pk.getK());
listSet.add(pk.getA());
}
//保存在List集合,有序
for (Card cd : listSet) {
listCard.add(cd);
System.out.println(cd);
}
System.out.println("**********洗牌成功**********");
return listCard;
}
// 發(fā)牌
public Map<String, Player> pushPoker(List<Card> listCard,
Map<String, Player> pMap) {
System.out.println("**********發(fā)牌開(kāi)始**********");
// 控制每人發(fā)兩張牌后結(jié)束
int control = 0;
for (Map.Entry<String, Player> entry : pMap.entrySet()) {
if (control == 0) {
for (int i = 0; i < 3; i = i + 2) {
// 發(fā)牌
entry.getValue().getPokerType().add(listCard.get(i));
}
// 更新map對(duì)象
pMap.put(entry.getKey(), entry.getValue());
control++;
} else if (control == 1) {
for (int i = 1; i < 4; i = i + 2) {
// 發(fā)牌
entry.getValue().getPokerType().add(listCard.get(i));
}
// 更新map對(duì)象
pMap.put(entry.getKey(), entry.getValue());
control++;
} else {
break;
}
}
System.out.println("**********發(fā)牌成功**********");
return pMap;
}
public void compareMatch(Map<String, Player> newMap) {
/*比較勝負(fù)
* 1.首先取得每個(gè)玩家手中最大牌的ID和花色I(xiàn)D。
* 2.比較倆玩家手中最大牌的ID大小,牌大者獲勝。
* 3.如果兩張牌的ID相等,在比較兩張牌的花色I(xiàn)D,花色I(xiàn)D更大著獲勝。
*
* */
List<Player> players = new ArrayList<>();
// 獲得兩個(gè)玩家
for (Map.Entry<String, Player> entry : newMap.entrySet()) {
players.add(entry.getValue());
}
// 玩家一信息和所持牌
List<Card> playerOne = players.get(0).getPokerType();
//獲得最大牌的ID和花色
Integer oneMaxId = Math.max(playerOne.get(0).getId(), playerOne.get(1)
.getId());
Integer oneMaxType = (oneMaxId!=playerOne.get(0).getId()) ? playerOne.get(1).getType() : playerOne.get(0).getType() ;
// 玩家二信息和所持牌
List<Card> playerTwo = players.get(1).getPokerType();
//獲得最大牌的ID和花色
Integer twoMaxId = Math.max(playerTwo.get(0).getId(), playerTwo.get(1)
.getId());
Integer twoMaxType = (twoMaxId!=playerTwo.get(0).getId()) ? playerTwo.get(1).getType() : playerTwo.get(0).getType() ;
if (oneMaxId > twoMaxId) {
System.out.println("玩家 : " + players.get(0).getName() + " 獲勝?。?);
} else if (oneMaxId == twoMaxId) {
if (oneMaxType > twoMaxType) {
System.out
.println("玩家 : " + players.get(0).getName() + " 獲勝!!");
} else {
System.out
.println("玩家 : " + players.get(1).getName() + " 獲勝??!");
}
} else {
System.out.println("玩家 : " + players.get(1).getName() + " 獲勝?。?);
}
System.out.println("**********************************************");
System.out.println("玩家 : " + players.get(0).getName() + "的牌是:"
+ showName(playerOne.get(0).getType(), 0) + "--"
+ showName(playerOne.get(0).getId(), 1) + " "
+ showName(playerOne.get(1).getType(), 0) + "--"
+ showName(playerOne.get(1).getId(), 1));
System.out.println("玩家 : " + players.get(1).getName() + "的牌是:"
+ showName(playerTwo.get(0).getType(), 0) + "--"
+ showName(playerTwo.get(0).getId(), 1) + " "
+ showName(playerTwo.get(1).getType(), 0) + "--"
+ showName(playerTwo.get(1).getId(), 1));
}
// 顯示牌的名稱
private String showName(Integer i, Integer type) {
String str = "";
// 顯示花色
if (type == 0) {
switch (i) {
case 1: {
str = "方塊";
break;
}
case 2: {
str = "梅花";
break;
}
case 3: {
str = "紅桃";
break;
}
case 4: {
str = "黑桃";
break;
}
default: {
break;
}
}
}
// 顯示數(shù)字
if (type == 1) {
if (i < 11) {
return i.toString();
} else {
switch (i) {
case 11: {
str = "J";
break;
}
case 12: {
str = "Q";
break;
}
case 13: {
str = "K";
break;
}
case 14: {
str = "A";
break;
}
default: {
break;
}
}
}
}
return str;
}
public static void main(String[] args) {
GamsBegin gb = new GamsBegin();
// 1、創(chuàng)建撲克牌
Set<Poker> pokers = gb.cPoker();
// 2、創(chuàng)建兩個(gè)玩家
Map<String, Player> pMap = gb.cPlayer();
// 3、洗牌
List<Card> listCard = gb.wPoker(pokers);
// 4、發(fā)牌
Map<String, Player> newMap = gb.pushPoker(listCard, pMap);
// 4、比較勝負(fù)
gb.compareMatch(newMap);
}
}
運(yùn)行結(jié)果:
**********開(kāi)始創(chuàng)建撲克牌**********
**********撲克牌創(chuàng)建成功**********
**********開(kāi)始創(chuàng)建玩家**********
創(chuàng)建兩名玩家,根據(jù)提示創(chuàng)建
請(qǐng)輸入第 1 個(gè)玩家ID:
請(qǐng)輸入第 1 個(gè)玩家姓名:
周星馳
添加第 1 個(gè)玩家 周星馳 成功
請(qǐng)輸入第 2 個(gè)玩家ID:
請(qǐng)輸入第 2 個(gè)玩家姓名:
周潤(rùn)發(fā)
添加第 2 個(gè)玩家 周潤(rùn)發(fā) 成功
**********玩家創(chuàng)建成功**********
**********開(kāi)始洗牌**********
Card [id=9, type=3]
Card [id=11, type=4]
Card [id=13, type=3]
Card [id=8, type=3]
Card [id=5, type=2]
Card [id=6, type=1]
Card [id=4, type=3]
Card [id=5, type=4]
Card [id=2, type=3]
Card [id=9, type=2]
Card [id=9, type=4]
Card [id=14, type=2]
Card [id=9, type=1]
Card [id=2, type=1]
Card [id=2, type=4]
Card [id=7, type=4]
Card [id=11, type=1]
Card [id=10, type=1]
Card [id=14, type=4]
Card [id=14, type=3]
Card [id=12, type=2]
Card [id=2, type=2]
Card [id=10, type=2]
Card [id=7, type=1]
Card [id=7, type=3]
Card [id=8, type=2]
Card [id=4, type=4]
Card [id=13, type=4]
Card [id=14, type=1]
Card [id=12, type=1]
Card [id=5, type=1]
Card [id=6, type=4]
Card [id=12, type=4]
Card [id=11, type=2]
Card [id=10, type=3]
Card [id=3, type=4]
Card [id=12, type=3]
Card [id=4, type=2]
Card [id=4, type=1]
Card [id=6, type=2]
Card [id=5, type=3]
Card [id=8, type=4]
Card [id=3, type=2]
Card [id=13, type=2]
Card [id=7, type=2]
Card [id=3, type=3]
Card [id=3, type=1]
Card [id=6, type=3]
Card [id=8, type=1]
Card [id=11, type=3]
Card [id=13, type=1]
Card [id=10, type=4]
**********洗牌成功**********
**********發(fā)牌開(kāi)始**********
**********發(fā)牌成功**********
玩家 : 周星馳 獲勝??!
**********************************************
玩家 : 周星馳的牌是:紅桃--9 紅桃--K
玩家 : 周潤(rùn)發(fā)的牌是:黑桃--J 紅桃--8
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼
隨著技術(shù)的發(fā)展,分布式web應(yīng)用的普及,通過(guò)session管理用戶登錄狀態(tài)成本越來(lái)越高,因此慢慢發(fā)展成為token的方式做登錄身份校驗(yàn),本文就來(lái)介紹一下SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼,感興趣的可以了解一下 2023-12-12
-
實(shí)體類或?qū)ο笮蛄谢瘯r(shí),忽略為空屬性的操作
這篇文章主要介紹了實(shí)體類或?qū)ο笮蛄谢瘯r(shí),忽略為空屬性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教 2021-06-06
-
RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body
這篇文章主要介紹了RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教 2023-07-07
-
SpringBoot統(tǒng)一功能處理示例詳解(攔截器)
這篇文章主要介紹了SpringBoot統(tǒng)一功能處理(攔截器),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 2023-08-08
-
Java修飾符 abstract,static,final 的區(qū)別詳解
以下是對(duì)Java修飾符abstract,static,final的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下 2013-09-09
-
Java模擬實(shí)現(xiàn)QQ三方登錄(單點(diǎn)登錄2.0)
這篇文章主要為大家詳細(xì)介紹了Java模擬實(shí)現(xiàn)QQ三方登錄,單點(diǎn)登錄2.0,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
2020-06-06
最新評(píng)論
Collection 層次結(jié)構(gòu)中的根接口。Collection表示一組對(duì)象,這些對(duì)象也稱為collection的元素。一些 collection 允許有重復(fù)的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無(wú)序的。JDK 不提供此接口的任何直接 實(shí)現(xiàn):它提供更具體的子接口(如 Set 和 List)實(shí)現(xiàn)。此接口通常用來(lái)傳遞 collection,并在需要最大普遍性的地方操作這些 collection。
主要內(nèi)容:這里使用collection集合,模擬香港電影中大佬們玩的撲克牌游戲。
1、游戲規(guī)則:兩個(gè)玩家每人手中發(fā)兩張牌,進(jìn)行比較。比較每個(gè)玩家手中牌最大的點(diǎn)數(shù),大小由A-2,點(diǎn)數(shù)大者獲勝。如果點(diǎn)數(shù)相同,則比較花色,大小由黑(4)、紅(3)、梅(2)、方(1),花色大者獲勝。
2、實(shí)現(xiàn)步驟:
創(chuàng)建一副撲克牌A-2,四種花色黑(4)、紅(3)、梅(2)、方(1)共52張牌;
創(chuàng)建兩個(gè)玩家包含玩家ID和姓名、所持牌Card信息;
洗牌并向兩位玩家各發(fā)兩張牌;
比較玩家手中牌大小,得出獲勝者;
3、程序?qū)崿F(xiàn)
牌Card類:包含牌的數(shù)字和花色
package collectiontest.games; public class Card { private Integer id; //牌的大小 private Integer type;//牌的花色 public Card(Integer id, Integer type) { this.id = id; this.type = type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } @Override public String toString() { return "Card [id=" + id + ", type=" + type + "]"; } }
撲克牌Poker類:包含撲克牌Card A-2
package collectiontest.games; public class Poker { private Card id2 ; private Card id3 ; private Card id4 ; private Card id5 ; private Card id6 ; private Card id7 ; private Card id8 ; private Card id9 ; private Card id10 ; private Card J ; private Card Q ; private Card K ; private Card A ; public Poker() { } //四個(gè)類型:黑--4、紅--3、梅--2、方--1 public Poker(Integer type) { this.id2 = new Card(2, type); this.id3 = new Card(3, type); this.id4 = new Card(4, type); this.id5 = new Card(5, type); this.id6 = new Card(6, type); this.id7 = new Card(7, type); this.id8 = new Card(8, type); this.id9 = new Card(9, type); this.id10 = new Card(10, type); this.J = new Card(11, type); this.Q = new Card(12, type); this.K = new Card(13, type); this.A = new Card(14, type); } public Card getId2() { return id2; } public void setId2(Card id2) { this.id2 = id2; } public Card getId3() { return id3; } public void setId3(Card id3) { this.id3 = id3; } public Card getId4() { return id4; } public void setId4(Card id4) { this.id4 = id4; } public Card getId5() { return id5; } public void setId5(Card id5) { this.id5 = id5; } public Card getId6() { return id6; } public void setId6(Card id6) { this.id6 = id6; } public Card getId7() { return id7; } public void setId7(Card id7) { this.id7 = id7; } public Card getId8() { return id8; } public void setId8(Card id8) { this.id8 = id8; } public Card getId9() { return id9; } public void setId9(Card id9) { this.id9 = id9; } public Card getId10() { return id10; } public void setId10(Card id10) { this.id10 = id10; } public Card getJ() { return J; } public void setJ(Card j) { J = j; } public Card getQ() { return Q; } public void setQ(Card q) { Q = q; } public Card getK() { return K; } public void setK(Card k) { K = k; } public Card getA() { return A; } public void setA(Card a) { A = a; } }
玩家Player類:包含玩家ID和姓名、所持卡片信息
package collectiontest.games; import java.util.ArrayList; import java.util.List; public class Player { //玩家的ID private String id ; //玩家姓名 private String name ; //玩家所持牌 private List<Card> pokerType ; public Player() { } public Player(String id, String name, List<Card> pokerType) { this.id = id; this.name = name; this.pokerType = new ArrayList<>(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Card> getPokerType() { return pokerType; } public void setPokerType(List<Card> pokerType) { this.pokerType = pokerType; } }
撲克牌游戲主類:包含1)撲克牌創(chuàng)建 2)玩家創(chuàng)建 3)洗牌 4)發(fā)牌 5)比較勝負(fù)
package collectiontest.games; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class GamsBegin { // 創(chuàng)建撲克牌 public Set<Poker> cPoker() { System.out.println("**********開(kāi)始創(chuàng)建撲克牌**********"); // 創(chuàng)建一副poker // 四個(gè)類型:黑--4、紅--3、梅--2、方--1 Set<Poker> pokers = new HashSet<>(); Poker[] poker = { new Poker(1), new Poker(2), new Poker(3), new Poker(4) }; /* * Collections工具類的使用 * Collections.addAll(pokers, new Poker(1), new Poker(2), new Poker(3),new Poker(4)); * * */ pokers.addAll(Arrays.asList(poker)); System.out.println("**********撲克牌創(chuàng)建成功**********"); return pokers; } // 創(chuàng)建兩個(gè)玩家 public Map<String, Player> cPlayer() { System.out.println("**********開(kāi)始創(chuàng)建玩家**********"); Map<String, Player> map = new HashMap<String, Player>(); // 控制數(shù)量 Integer control = 0; System.out.println("創(chuàng)建兩名玩家,根據(jù)提示創(chuàng)建"); Scanner console = new Scanner(System.in); while (true) { System.out.println("請(qǐng)輸入第 "+(control+1)+" 個(gè)玩家ID:"); String courseId = console.next(); if (isNumeric(courseId)) { System.out.println("請(qǐng)輸入第 "+(control+1)+" 個(gè)玩家姓名:"); String courseName = console.next(); Player players = new Player(courseId, courseName, null); //保存數(shù)據(jù) map.put(courseId, players); System.out.println("添加第 " + (control + 1) + " 個(gè)玩家 " + courseName + " 成功"); //數(shù)量自加 control++; } else { System.out.println("*****請(qǐng)輸入數(shù)字ID*****"); continue; } if (control == 2) { break; } } System.out.println("**********玩家創(chuàng)建成功**********"); return map; } // 判斷輸入是否為數(shù)字, Character.isDigit()為java方法 public boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 洗牌 :也可以產(chǎn)生52個(gè)不同隨機(jī)數(shù),實(shí)現(xiàn)洗牌 * **/ public List<Card> wPoker(Set<Poker> pokers) { System.out.println("**********開(kāi)始洗牌**********"); //利用List的有序排序,洗牌之后保存順序不變 List<Card> listCard = new ArrayList<>(); // 利用Set集合的無(wú)序排序,實(shí)現(xiàn)洗牌 Set<Card> listSet = new HashSet<>(); //保存到Set集合,無(wú)序 for (Poker pk : pokers) { listSet.add(pk.getId2()); listSet.add(pk.getId3()); listSet.add(pk.getId4()); listSet.add(pk.getId5()); listSet.add(pk.getId6()); listSet.add(pk.getId7()); listSet.add(pk.getId8()); listSet.add(pk.getId9()); listSet.add(pk.getId10()); listSet.add(pk.getJ()); listSet.add(pk.getQ()); listSet.add(pk.getK()); listSet.add(pk.getA()); } //保存在List集合,有序 for (Card cd : listSet) { listCard.add(cd); System.out.println(cd); } System.out.println("**********洗牌成功**********"); return listCard; } // 發(fā)牌 public Map<String, Player> pushPoker(List<Card> listCard, Map<String, Player> pMap) { System.out.println("**********發(fā)牌開(kāi)始**********"); // 控制每人發(fā)兩張牌后結(jié)束 int control = 0; for (Map.Entry<String, Player> entry : pMap.entrySet()) { if (control == 0) { for (int i = 0; i < 3; i = i + 2) { // 發(fā)牌 entry.getValue().getPokerType().add(listCard.get(i)); } // 更新map對(duì)象 pMap.put(entry.getKey(), entry.getValue()); control++; } else if (control == 1) { for (int i = 1; i < 4; i = i + 2) { // 發(fā)牌 entry.getValue().getPokerType().add(listCard.get(i)); } // 更新map對(duì)象 pMap.put(entry.getKey(), entry.getValue()); control++; } else { break; } } System.out.println("**********發(fā)牌成功**********"); return pMap; } public void compareMatch(Map<String, Player> newMap) { /*比較勝負(fù) * 1.首先取得每個(gè)玩家手中最大牌的ID和花色I(xiàn)D。 * 2.比較倆玩家手中最大牌的ID大小,牌大者獲勝。 * 3.如果兩張牌的ID相等,在比較兩張牌的花色I(xiàn)D,花色I(xiàn)D更大著獲勝。 * * */ List<Player> players = new ArrayList<>(); // 獲得兩個(gè)玩家 for (Map.Entry<String, Player> entry : newMap.entrySet()) { players.add(entry.getValue()); } // 玩家一信息和所持牌 List<Card> playerOne = players.get(0).getPokerType(); //獲得最大牌的ID和花色 Integer oneMaxId = Math.max(playerOne.get(0).getId(), playerOne.get(1) .getId()); Integer oneMaxType = (oneMaxId!=playerOne.get(0).getId()) ? playerOne.get(1).getType() : playerOne.get(0).getType() ; // 玩家二信息和所持牌 List<Card> playerTwo = players.get(1).getPokerType(); //獲得最大牌的ID和花色 Integer twoMaxId = Math.max(playerTwo.get(0).getId(), playerTwo.get(1) .getId()); Integer twoMaxType = (twoMaxId!=playerTwo.get(0).getId()) ? playerTwo.get(1).getType() : playerTwo.get(0).getType() ; if (oneMaxId > twoMaxId) { System.out.println("玩家 : " + players.get(0).getName() + " 獲勝?。?); } else if (oneMaxId == twoMaxId) { if (oneMaxType > twoMaxType) { System.out .println("玩家 : " + players.get(0).getName() + " 獲勝!!"); } else { System.out .println("玩家 : " + players.get(1).getName() + " 獲勝??!"); } } else { System.out.println("玩家 : " + players.get(1).getName() + " 獲勝?。?); } System.out.println("**********************************************"); System.out.println("玩家 : " + players.get(0).getName() + "的牌是:" + showName(playerOne.get(0).getType(), 0) + "--" + showName(playerOne.get(0).getId(), 1) + " " + showName(playerOne.get(1).getType(), 0) + "--" + showName(playerOne.get(1).getId(), 1)); System.out.println("玩家 : " + players.get(1).getName() + "的牌是:" + showName(playerTwo.get(0).getType(), 0) + "--" + showName(playerTwo.get(0).getId(), 1) + " " + showName(playerTwo.get(1).getType(), 0) + "--" + showName(playerTwo.get(1).getId(), 1)); } // 顯示牌的名稱 private String showName(Integer i, Integer type) { String str = ""; // 顯示花色 if (type == 0) { switch (i) { case 1: { str = "方塊"; break; } case 2: { str = "梅花"; break; } case 3: { str = "紅桃"; break; } case 4: { str = "黑桃"; break; } default: { break; } } } // 顯示數(shù)字 if (type == 1) { if (i < 11) { return i.toString(); } else { switch (i) { case 11: { str = "J"; break; } case 12: { str = "Q"; break; } case 13: { str = "K"; break; } case 14: { str = "A"; break; } default: { break; } } } } return str; } public static void main(String[] args) { GamsBegin gb = new GamsBegin(); // 1、創(chuàng)建撲克牌 Set<Poker> pokers = gb.cPoker(); // 2、創(chuàng)建兩個(gè)玩家 Map<String, Player> pMap = gb.cPlayer(); // 3、洗牌 List<Card> listCard = gb.wPoker(pokers); // 4、發(fā)牌 Map<String, Player> newMap = gb.pushPoker(listCard, pMap); // 4、比較勝負(fù) gb.compareMatch(newMap); } }
運(yùn)行結(jié)果:
**********開(kāi)始創(chuàng)建撲克牌**********
**********撲克牌創(chuàng)建成功**********
**********開(kāi)始創(chuàng)建玩家**********
創(chuàng)建兩名玩家,根據(jù)提示創(chuàng)建
請(qǐng)輸入第 1 個(gè)玩家ID:
請(qǐng)輸入第 1 個(gè)玩家姓名:
周星馳
添加第 1 個(gè)玩家 周星馳 成功
請(qǐng)輸入第 2 個(gè)玩家ID:
請(qǐng)輸入第 2 個(gè)玩家姓名:
周潤(rùn)發(fā)
添加第 2 個(gè)玩家 周潤(rùn)發(fā) 成功
**********玩家創(chuàng)建成功**********
**********開(kāi)始洗牌**********
Card [id=9, type=3]
Card [id=11, type=4]
Card [id=13, type=3]
Card [id=8, type=3]
Card [id=5, type=2]
Card [id=6, type=1]
Card [id=4, type=3]
Card [id=5, type=4]
Card [id=2, type=3]
Card [id=9, type=2]
Card [id=9, type=4]
Card [id=14, type=2]
Card [id=9, type=1]
Card [id=2, type=1]
Card [id=2, type=4]
Card [id=7, type=4]
Card [id=11, type=1]
Card [id=10, type=1]
Card [id=14, type=4]
Card [id=14, type=3]
Card [id=12, type=2]
Card [id=2, type=2]
Card [id=10, type=2]
Card [id=7, type=1]
Card [id=7, type=3]
Card [id=8, type=2]
Card [id=4, type=4]
Card [id=13, type=4]
Card [id=14, type=1]
Card [id=12, type=1]
Card [id=5, type=1]
Card [id=6, type=4]
Card [id=12, type=4]
Card [id=11, type=2]
Card [id=10, type=3]
Card [id=3, type=4]
Card [id=12, type=3]
Card [id=4, type=2]
Card [id=4, type=1]
Card [id=6, type=2]
Card [id=5, type=3]
Card [id=8, type=4]
Card [id=3, type=2]
Card [id=13, type=2]
Card [id=7, type=2]
Card [id=3, type=3]
Card [id=3, type=1]
Card [id=6, type=3]
Card [id=8, type=1]
Card [id=11, type=3]
Card [id=13, type=1]
Card [id=10, type=4]
**********洗牌成功**********
**********發(fā)牌開(kāi)始**********
**********發(fā)牌成功**********
玩家 : 周星馳 獲勝??!
**********************************************
玩家 : 周星馳的牌是:紅桃--9 紅桃--K
玩家 : 周潤(rùn)發(fā)的牌是:黑桃--J 紅桃--8
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼
隨著技術(shù)的發(fā)展,分布式web應(yīng)用的普及,通過(guò)session管理用戶登錄狀態(tài)成本越來(lái)越高,因此慢慢發(fā)展成為token的方式做登錄身份校驗(yàn),本文就來(lái)介紹一下SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼,感興趣的可以了解一下2023-12-12實(shí)體類或?qū)ο笮蛄谢瘯r(shí),忽略為空屬性的操作
這篇文章主要介紹了實(shí)體類或?qū)ο笮蛄谢瘯r(shí),忽略為空屬性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body
這篇文章主要介紹了RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot統(tǒng)一功能處理示例詳解(攔截器)
這篇文章主要介紹了SpringBoot統(tǒng)一功能處理(攔截器),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java修飾符 abstract,static,final 的區(qū)別詳解
以下是對(duì)Java修飾符abstract,static,final的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09Java模擬實(shí)現(xiàn)QQ三方登錄(單點(diǎn)登錄2.0)
這篇文章主要為大家詳細(xì)介紹了Java模擬實(shí)現(xiàn)QQ三方登錄,單點(diǎn)登錄2.0,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06