本文實例為大家分享了java實現(xiàn)簡單撲克牌游戲的具體代碼,供大家參考,具體內(nèi)容如下
撲克牌小游戲:
游戲玩法簡介:定義五個People在玩撲克牌游戲,玩法是5個人從一副沒有大小王的撲克牌中各抽取4張,牌的類型有四種:♥,♣,♠,♦。牌的大小為從1~13。(此處為了簡單,只取1 -5).
1.黑桃♠A在哪名玩家手中,哪名玩家就獲勝。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package poker; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; public class Game { public static void main(String[] args) { //定義5名玩家,使用List List<Player> playerList= new ArrayList<>(); playerList.add( new Player( "于美人" )); playerList.add( new Player( "小婷子" )); playerList.add( new Player( "小晨子" )); playerList.add( new Player( "小艾" )); playerList.add( new Player( "小珍子" )); //定義撲克牌順序表 List<Card> cardList= new ArrayList<>(); //初始化撲克牌 initializeCards(cardList); System.out.println(cardList); //調(diào)用collections下的shuffle(洗牌)方法 //進(jìn)行洗牌 Collections.shuffle(cardList); //System.out.println("抽牌前,牌組中的牌:"); System.out.println(cardList); //發(fā)牌 int n= 2 ; //每名玩家發(fā)幾張牌 for ( int i= 0 ;i<n;i++){ //一共發(fā)n輪牌 for (Player player:playerList){ //每名玩家一次抽牌 Card card=cardList.remove( 0 ); //從牌組中,抽一張牌出來 player.cardList.add(card); //把這張牌放到當(dāng)前玩家的手中 } } System.out.println( "現(xiàn)在牌組中剩余的牌:" ); System.out.println(cardList); System.out.println( "交換牌之前:" ); for (Player player:playerList){ System.out.printf( "玩家[%s]的手牌是: %s%n" ,player.name,player.cardList); } //要找的牌,方法1 Card toFoundCard= new Card( "♠" , 1 ); for (Player player:playerList){ for (Card card:player.cardList){ //比較每張牌和要找的是否為同一張牌 if (card.equals(toFoundCard)){ System.out.println(player.name+ "獲勝" ); return ; } } } System.out.println( "無人獲勝" ); } //要找的牌,方法2 Card toFoundCard2= new Card( "♠" , 1 ); for (Player player:playerList){ if (player.cardList.contains(toFoundCard)) { System.out.println(player.name + "獲勝" ); return ; } } private static void initializeCards(List<Card> cards){ for (String suit: new String[]{ "♠" , "♥" , "♦" , "♣" }){ for ( int rank= 1 ;rank<= 5 ;rank++){ Card card= new Card(suit,rank); //把撲克牌放入牌組中 cards.add(card); } } } |
2.讓每名玩家,依次抽取他的下家的一張手牌,然后再找♠A,找到♠A就獲勝。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//添加打印手牌的方法 public static void printPlayerHandCard(List<Player> playerList){ for (Player player:playerList){ System.out.printf( "玩家[%s]的手牌是:%s%n" ,player.name,player.cardList); } } //集體實現(xiàn)交換的過程 //交換牌 //每名玩家隨機(jī)抽取下家的一張牌 Random random= new Random(); for ( int i= 0 ;i<playerList.size();i++){ Player currentPlayer=playerList.get(i); //下家就是按照前后關(guān)系的下一個,最后一名玩家抽取第0個 Player nextPlayer=playerList.get(i!=playerList.size()- 1 ?i+ 1 : 0 ); //要取的牌的下標(biāo) int toDrawIndex=random.nextInt(nextPlayer.cardList.size()); //取牌 Card drawCard=nextPlayer.cardList.remove(toDrawIndex); //放入當(dāng)前玩家的手中 currentPlayer.cardList.add(drawCard); } System.out.println( "交換牌之后:" ); printPlayerHandCard(playerList); |
3.于美人是賭神,她有變牌能力:她手中一旦沒有♠A,她就可以把第一張牌變成♠A。(交換牌之前,有機(jī)會變一次;交換牌之后,有機(jī)會變一次)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Card toFoundCard= new Card( "♠" , 1 ); //已知于美人的下標(biāo)為0 Player 于美人=playerList.get( 0 ); if (!于美人.cardList.contains(toFoundCard)){ 于美人.cardList.set( 0 ,toFoundCard); } System.out.println( "于美人第一次發(fā)功:" ); printPlayerHandCard(playerList); System.out.println( "交換牌之后:" ); printPlayerHandCard(playerList); //此處交換牌過程省略。。。。。。。。。。。。。。 if (!于美人.cardList.contains(toFoundCard)){ 于美人.cardList.set( 0 ,toFoundCard); } System.out.println( "于美人第二次發(fā)功:" ); printPlayerHandCard(playerList); |
定義玩家新的的一個類:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package poker; import java.util.ArrayList; import java.util.List; public class Player { public List<Card> cardList= new ArrayList<>(); public String name; public Player(String name){ this .name=name; } } |
定義撲克牌的一個類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package poker; import java.util.Objects; public class Card { public String suit; //花色 public int rank; //分值 //構(gòu)造方法 public Card(String suit, int rank){ this .rank=rank; this .suit =suit; } @Override public String toString() { return String.format( "[%s%d]" ,suit,rank); } //重寫比較相等的方法 @Override public boolean equals(Object o) { if ( this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Card card = (Card) o; return rank == card.rank && Objects.equals(suit, card.suit); } } |
注意:
(1)要重新定義比較相等方法的重寫。
(2)注意foreach 方法的寫法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/m0_46551861/article/details/108650353