目录
前言:
1.初始化牌
2.洗牌
3.揭牌
总代码:
Card类:
CardGame类:
Main类:
结语:
斗地主是全国范围内的一种桌面游戏,本节我们来实现一下斗地主中的简单初始化牌、发牌和看牌功能。按照斗地主的规则,完成洗牌发牌的动作。具体规则为使用 52 张牌(不含大小王)打乱顺序,3 个玩家参与游戏,3 人交替摸牌,每人 5(可以自己修改) 张牌。
实现思路步骤:
1.初始化牌
2.洗牌
3.揭牌
4.剩余牌
效果展示:
实现如下:
牌有数字和花色,故我们要自己创建一个类来进行操作。toString方法要记得重写不然后面println不能直接输出。
public class Card{
public String suit;//花色
public int num;
public Card(String suit,int num){
this.suit = suit;
this.num = num;
}
@Override
public String toString(){
return suit+" "+num;
}
}
用suits来存储花色。List
public static final String[] suits = {"♥","♣","♦","♠"};
//初始化牌
public List buyCard(){
List cardList = new ArrayList<>();
for(int i = 0;i < 4;i++){
for(int j = 1;j <= 13;j++){
String suit = suits[i];
Card card = new Card(suit,j);
cardList.add(card);
}
}
return cardList;
}
效果如下:
利用random来生成随机数,这里有个小细节i是从最后一个数来进行遍历的利用rand来生成i之前的随机数进行交换这样就能保证每个牌都有被洗到。
//洗牌
public void shuffle(List cardList){
Random random = new Random();
for(int i = cardList.size()-1;i > 0;i--){
swap(cardList,i,random.nextInt(i));
}
}
public void swap(List cardList,int i,int j){
Card temp = cardList.get(i);
cardList.set(i,cardList.get(j));
cardList.set(j,temp);
}
效果如下:
这里解释一下List>,可以看成一个二维数组如图,List
//揭牌
public List> getCard(List cardList){
List hand1 = new ArrayList<>();
List hand2 = new ArrayList<>();
List hand3 = new ArrayList<>();
List> hand = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for(int i = 0;i < 5;i++){
for(int j = 0;j < 3;j++){
Card card = cardList.remove(0);
hand.get(j).add(card);
}
}
return hand;
}
public class Card{
public String suit;//花色
public int num;
public Card(String suit,int num){
this.suit = suit;
this.num = num;
}
@Override
public String toString(){
return suit+" "+num;
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class CardGame{
public static final String[] suits = {"♥","♣","♦","♠"};
//初始化牌
public List buyCard(){
List cardList = new ArrayList<>();
for(int i = 0;i < 4;i++){
for(int j = 1;j <= 13;j++){
String suit = suits[i];
Card card = new Card(suit,j);
cardList.add(card);
}
}
return cardList;
}
//洗牌
public void shuffle(List cardList){
Random random = new Random();
for(int i = cardList.size()-1;i > 0;i--){
swap(cardList,i,random.nextInt(i));
}
}
public void swap(List cardList,int i,int j){
Card temp = cardList.get(i);
cardList.set(i,cardList.get(j));
cardList.set(j,temp);
}
//揭牌
public List> getCard(List cardList){
List hand1 = new ArrayList<>();
List hand2 = new ArrayList<>();
List hand3 = new ArrayList<>();
List> hand = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for(int i = 0;i < 5;i++){
for(int j = 0;j < 3;j++){
Card card = cardList.remove(0);
hand.get(j).add(card);
}
}
return hand;
}
}
import java.util.List;
public class Main {
public static void main(String[] args){
CardGame cardgame = new CardGame();
List ret = cardgame.buyCard();
System.out.println("初始化牌");
System.out.println(ret.subList(0,13));
System.out.println(ret.subList(13,26));
System.out.println(ret.subList(26,39));
System.out.println(ret.subList(39,52));
System.out.println("洗牌");
cardgame.shuffle(ret);
System.out.println(ret.subList(0,13));
System.out.println(ret.subList(13,26));
System.out.println(ret.subList(26,39));
System.out.println(ret.subList(39,52));
System.out.println("揭牌");
List> hand = cardgame.getCard(ret);
for(int i = 0;i < 3;i++){
System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));
}
System.out.println("剩下的牌:");
System.out.println(ret);
}
}
其实写博客不仅仅是为了教大家,同时这也有利于我巩固自己的知识点,和一个学习的总结,由于作者水平有限,对文章有任何问题的还请指出,接受大家的批评,让我改进,如果大家有所收获的话还请不要吝啬你们的点赞收藏和关注,这可以激励我写出更加优秀的文章。