java如何实现斗地主发牌系统

发布时间:2020-08-01 10:50:47 作者:小猪
来源:亿速云 阅读:126

这篇文章主要讲解了java如何实现斗地主发牌系统,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

玩家类

package com.softeem.example;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 玩家类
 * 
 */
public class Player {
 
 private String name; //名字
 private boolean landowner; //是否地主
 private List<Card> cards = new ArrayList<>(); //手中的所有牌
 
 public Player() {
 // TODO Auto-generated constructor stub
 }
 
 public Player(String name) {
 super();
 this.name = name;
 }
 
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public boolean isLandowner() {
 return landowner;
 }
 public void setLandowner(boolean landowner) {
 this.landowner = landowner;
 }
 public List<Card> getCards() {
 return cards;
 }
 public void setCards(List<Card> cards) {
 this.cards = cards;
 }
 
 @Override
 public String toString() {
 return name+"-是否地主:"+landowner+"->"+cards;
 }
 
 
 
}

牌类

package com.softeem.example;
 
/**
 * 牌类
 * 
 */
public class Card implements Comparable<Card>{
 
 private int num;//点数 
 private String name;//牌面
 private String flower;//花色
 
 public Card() {
 // TODO Auto-generated constructor stub
 }
 
 public Card(int num, String name, String flower) {
 super();
 this.num = num;
 this.name = name;
 this.flower = flower;
 }
 
 public int getNum() {
 return num;
 }
 public void setNum(int num) {
 this.num = num;
 }
 public String getFlower() {
 return flower;
 }
 public void setFlower(String flower) {
 this.flower = flower;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 @Override
 public int compareTo(Card c) {
 return this.num - c.num;
 }
 
 @Override
 public String toString() {
 return name+"-"+flower+"-"+num;
 }
 
 
}
package com.softeem.example;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
 
public class PokerGame {
 
 static List<Card> list = new ArrayList<>(); //所有牌集合
 static List<Player> players = new ArrayList<>(); //玩家集合
 static int position; //初始化当前发牌的位置
 static String[] flowers = {"红桃","方片","梅花","黑桃"};
 static String[] names = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"};
 
 /**
 * 初始化所有牌面和玩家
 */
 static{
 int i = 0;
 //遍历所有的牌面值(除大小王:大小王无花色)
 for (; i < names.length - 2; i++) {
 for (int j = 0; j < flowers.length; j++) {
 list.add(new Card(i, names[i], flowers[j]));
 }
 }
 list.add(new Card(i++, names[names.length-2], null));
 list.add(new Card(i++, names[names.length-1], null));
 
 
 }
 
 /**
 * 初始化玩家
 */
 public void initPlayer(){
 players.clear();
 players.add(new Player("盖伦"));
 players.add(new Player("艾希"));
 players.add(new Player("玛尔扎哈"));
 }
 
 /**
 * 洗牌
 */
 public void shuffle(){
 //索引重置
 position = 0;
 Collections.shuffle(list);
 Collections.shuffle(list);
 Collections.shuffle(list); 
 start();
 }
 
 /**
 * 发牌
 */
 public void sendCards(){
 //准备玩家
 initPlayer();
 //随机地主(索引)
 int index = new Random().nextInt(players.size());
 //设置为地主
 players.get(index).setLandowner(true);
 Card now = null;
 while(position < list.size() - 3){
 //获取最上面的一张牌
 now = list.get(position);
 if(position % 3 == 0){
 players.get(0).getCards().add(now);
 }else if(position % 3 == 1){
 players.get(1).getCards().add(now);
 }else{
 players.get(2).getCards().add(now);
 }
 position++;
 }
 //获取集合中的最后三张牌
 List<Card> last = list.subList(list.size()-3, list.size());
 //将最后三张牌给地主
 players.get(index).getCards().addAll(last);
 //显示
 show();
 
 }
 
 /**
 * 显示发牌结果
 */
 public void show(){
 //排序
 Collections.sort(players.get(0).getCards());
 Collections.sort(players.get(1).getCards());
 Collections.sort(players.get(2).getCards());
 players.stream().forEach(p->System.out.println(p));
 start();
 }
 
 
 public void start(){
 System.out.println("***********欢乐斗地主***********");
 System.out.println("***********【1】洗牌***********");
 System.out.println("***********【2】发牌***********");
 Scanner sc = new Scanner(System.in);
 
 String i = sc.nextLine();
 if(!i.matches("^\\d{1}$")){
 System.out.println("请输入正确的指令!");
 start();
 return;
 }
 switch(i){
 case "1":
 shuffle();
 break;
 case "2":
 sendCards();
 break;
 default:
 System.out.println("指令不正确");
 start();
 break;
 }
 }
 
 public static void main(String[] args) {
 
 new PokerGame().start();
 }
 
}

看完上述内容,是不是对java如何实现斗地主发牌系统有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python实现模拟斗地主发牌的方法
  2. python模拟实现斗地主发牌

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ava

上一篇:vsftpd记录删除日志

下一篇:如何使用MySQL 8.0用户和角色管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》