欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java简单实现斗地主发牌功能

程序员文章站 2022-06-27 15:00:35
本文实例为大家分享了java简单实现斗地主发牌的具体代码,供大家参考,具体内容如下问题:参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)要求实...

本文实例为大家分享了java简单实现斗地主发牌的具体代码,供大家参考,具体内容如下

问题:

参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)
要求实现以下功能:

1、洗牌
2、发牌(发牌按照常规方式a-b-c-a-b-c-a…,不允许一次性随机17张给某个玩家)
3、玩家手中牌的展示(要求按点数实现排序)

分析

运用面向对象的程序设计思维分为扑克、玩家和游戏三部分,扑克类属性有花色,点数,次序;玩家类属性有玩家id,昵称,是否地主,牌的集合;游戏过程分为洗牌,随机产生地主,发牌,完成牌的排序,玩家手里的牌展示功能

程序实现

package com.lesson17.homework;

/**
 * @author huc
 *
 */

/**扑克类*/
public class poker {

 /** 扑克花色 */
 private string flower;
 /** 扑克点数 */
 private string pointer;
 /** 扑克顺序 */
 private int sort;

 public poker(string flower, string pointer, int sort) {
  super();
  this.flower = flower;
  this.pointer = pointer;
  this.sort = sort;
 }

 public string getflower() {
  return flower;
 }

 public void setflower(string flower) {
  this.flower = flower;
 }

 public string getpointer() {
  return pointer;
 }

 public void setpointer(string pointer) {
  this.pointer = pointer;
 }

 public int getsort() {
  return sort;
 }

 public void setsort(int sort) {
  this.sort = sort;
 }

 @override
 public string tostring() {
  return flower + pointer;
 }

package com.lesson17.homework;
   
public class player {

 /** 玩家id */
 private int id;
 /** 玩家昵称 */
 private string name;
 /** 玩家拿到的牌 */
 private arraylist<poker> pokers = new arraylist<>();
 /** 玩家是否地主 */
 private boolean boss;

 public player(int id, string name) {
  super();
  this.id = id;
  this.name = name;
 }

 public int getid() {
  return id;
 }

 public void setid(int id) {
  this.id = id;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public arraylist<poker> getpokers() {
  return pokers;
 }

 public void setpokers(arraylist<poker> pokers) {
  this.pokers = pokers;
 }

 public boolean isboss() {
  return boss;
 }

 public void setboss(boolean boss) {
  this.boss = boss;
 }

package com.lesson17.homework;

import java.util.arraylist;
import java.util.arrays;
import java.util.collections;
import java.util.list;
import java.util.random;

public class pokergame {

 static list<player> players;
 static arraylist<poker> list = new arraylist<>();
 static string[] flowers = {"方块","梅花","黑桃","红心"};
 static string[] pointers = {"3","4","5","6","7","8","9","10","j","q","k","a","2"};
 int bossindex; //地主索引
 
 static{
  //初始化扑克
  for (int i = 0; i < flowers.length; i++) {
   for (int j = 0; j < pointers.length; j++) {
    //生成一张扑克对象
    poker p = new poker(flowers[i], pointers[j], j);
    list.add(p);
   }
  }
  //单独将大小王加入集合
  list.add(new poker("小", "王", 13));
  list.add(new poker("大", "王", 14));
  
  //初始化玩家
  players = arrays.aslist(
    new player(1, "刘备"),
    new player(2, "关羽"),
    new player(3, "张飞"));
 }
 
 /**洗牌*/
 public void washpoker(){
  collections.shuffle(list);
 }
 
 /**随机地主*/
 public void pushboss(){
  random r = new random();
  bossindex = r.nextint(players.size());
  //设置被随机到的玩家为地主
  players.get(bossindex).setboss(true);
 }
 

 /**依次发牌*/
 public void pushpoker(){
  for (int i = 0; i < 51; i+=3) {
   players.get(0).getpokers().add(list.get(i));
   players.get(1).getpokers().add(list.get(i+1));
   players.get(2).getpokers().add(list.get(i+2));
  }
  for(int i = 51;i < 54 ;i++){
   //将最后剩余的牌(3张)全部给地主
   players.get(bossindex).getpokers().add(list.get(i));
  }
 }
 
 /**排序*/
 public void sortpoker() {
  collections.sort(players.get(0).getpokers(), (a,b)->a.getsort()-b.getsort());
  collections.sort(players.get(1).getpokers(), (a,b)->a.getsort()-b.getsort());
  collections.sort(players.get(2).getpokers(), (a,b)->a.getsort()-b.getsort());
 }
 
 public void start(){
  //随机地主
  pushboss();
  //洗牌
  washpoker();
  //发牌
  pushpoker();
  //排序
  sortpoker();
  //展示牌
  for(player p : players){
   system.out.println(p.getpokers());
  }
 }
 
 public static void main(string[] args) {
  new pokergame().start();
 }
}

结果

java简单实现斗地主发牌功能

java简单实现斗地主发牌功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。