我是靠谱客的博主 明理大门,最近开发中收集的这篇文章主要介绍java实现牛牛游戏源代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package com.zyy.java;

import java.util.List;

public class Bull {
	private static CreatPlayers cplayers;
	private static CreatPoker cpoker;
	private static Shuffle shuffle;
	private static SendPoker spoker; // 发牌
	private static View view;
	private static Player dealer; // 庄家
	private static List washedCards; // 洗过的牌
	private static Player[] players0; // 建好的玩家
	private static Player[] players1; // 发好牌的玩家

	public static void main(String[] args) {
		System.out.println("欢迎来到文字牛牛游戏");
		System.out.println("正在建牌中,请稍等");
		cpoker = new CreatPoker(); // 牌已经建好
		List cardsAll = cpoker.getCardsAll(); // 一、获取建好的牌
		int flag = 0;
		for (Object obj : cardsAll) {
			if (flag++ % 13 == 0) {
				System.out.println();
			}
			System.out.print(obj + " ");
		}
		shuffle = new Shuffle(cardsAll); // 二、洗牌
		washedCards = shuffle.getWashedCards(); // 三、获取一副洗好了的牌
		cplayers = new CreatPlayers(); // 四、创建玩家
		players0 = cplayers.getPlayers();
		// System.out.println(players0[1]); //检验是否建立好玩家
		dealer = new Dealer().getDealer(players0); // 五、从四名玩家中选出一名庄家
		System.out.println("nn" + "本局庄家是:" + dealer.getName() + " " + "nn");
		spoker = new SendPoker(); // 发牌
		players1 = spoker.sendPoker(players0, washedCards); // 牌发完
		Player player = new Player();
		for (int i = 0; i < 4; i++) {
			player.getBiggestCards(players1[i].getPocket());
			// System.out.println(players1[i].getOneOfPocket(3));
			// System.out.println(player.getBiggestCards(players1[i].getPocket()));
		}
		for (int i = 0; i < 4; i++) {
			Compare.pointOfBull(players1[i]);
			Compare.isBull(players1[i]);
			// System.out.println(players1[i].getPointOfBull());
		}
		view = new View();
		view.view(players1);

	}

}

package com.zyy.java;

public  class Compare {
	public static void isBull(Player player) { // 判断手牌手否有牛
		for (int i = 0; i < 3; i++) {
			for (int j = i + 1; j < 4; j++) {
				for (int k = j + 1; k < 5; k++) {
					if ((player.getIndexOfPoker(i).getPoints()
							+ player.getIndexOfPoker(j).getPoints()
							+ player.getIndexOfPoker(k).getPoints()) % 10 == 0) {
						player.setIsBull(true); // 如果玩家手中的任意三张牌之和是10的倍数,则有牛
					}
				}
			}
		}
	}

	public static void pointOfBull(Player player) { // 判断手牌为牛几
		int allPoints = 0; // 五张牌的总点数
		for (int i = 0; i < 5; i++) {
			allPoints += player.getIndexOfPoker(i).getPoints();
		}
		
		player.setPointOfBull(allPoints % 10);
	}

	public Player compare(Player p1, Player p2) {
		if (p1.getIsBull() && p2.getIsBull()) {
			if (p1.getPointOfBull() > p2.getPointOfBull()) { // 如果谁牛的点数大,就返回谁
				return p1;
			} else if (p1.getPointOfBull() < p2.getPointOfBull()) {
				return p2;
			} else {                                   // 如果牛的点数相同,则比较最大牌
				if (p1.getBiggestCard().getCount() > p2.getBiggestCard().getCount()) {
					return p1;
				} else {
					return p2;
				}
			}
		}else{
			return null;
		}
		
	}

	
}

package com.zyy.java;

import java.util.Arrays;

public class CreatPlayers {
	Player[] players =new Player[4];    //建立四个玩家

	public CreatPlayers() {
		players[0] = new Player("张钰洁");
		players[1] = new Player("周颜颖");
		players[2] = new Player("毛繁宇");
		players[3] = new Player("李慧敏");
	}
	public Player[] getPlayers(){
		return players;
	}
	@Override
	public String toString() {
		return "CreatPlayers [players=" + Arrays.toString(players) + "]";
	}
	
	

}
package com.zyy.java;

import java.util.ArrayList;
import java.util.List;

//建好52张按照默认排序的牌
public class CreatPoker {
	private List cardsAll = new ArrayList();
	private static final String[] colors = { "黑桃", "红桃", "梅花", "方块" };
	private static final String[] points = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
	public CreatPoker() {
//		StringBuffer c = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) {
				cardsAll.add(new Poker(colors[i] , points[j]));
			}
		}
	}
	public List getCardsAll(){
		return cardsAll;
	}
}
package com.zyy.java;

import java.util.List;

//选出 庄家
public class Dealer {

	public Player getDealer(Player[] players){
		int index = (int)(Math.random() * players.length);
		return players[index];
	}

}
package com.zyy.java;

import java.util.Arrays;

public class Player {
	private String name;
	private Poker[] pocket = new Poker[5]; // 玩家手上的5张牌
	private Boolean isBull = false; // 手牌是否有牛
	private int pointOfBull; // 牛几
	private Poker biggestCard; // 最大的是哪张牌

	public Player() {
		super();
	}

	public Player(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Poker[] getPocket() {
		return pocket;
	}

	public void setPocket(Poker[] pocket) {
		this.pocket = pocket;
	}

	public void setOneOfPocket(int index, Poker poker) {
		this.pocket[index] = poker;
	}

	public Poker getOneOfPocket(int index) {
		return pocket[index];
	}

	public Boolean getIsBull() {
		return isBull;
	}

	public void setIsBull(Boolean isBull) {
		this.isBull = isBull;
	}

	public int getPointOfBull() {
		return pointOfBull;
	}

	public void setPointOfBull(int pointOfBull) {
		this.pointOfBull = pointOfBull;
	}

	

	public Poker getBiggestCard() {
		return biggestCard;
	}

	public void setBiggestCard(Poker biggestCard) {
		this.biggestCard = biggestCard;
	}

	public Poker getIndexOfPoker(int index) {
		return pocket[index];
	}

	@Override
	public String toString() {
		return name + "的手牌为" + Arrays.toString(pocket);
	}

	
	
	public Poker getBiggestCards(Poker[] pocket) { // 获取手牌中的最大牌:有个问题没解决,如果出现点数相同,花色不同
		Poker poker = new Poker();
		int count = 0;
		for (int i = 0; i < 5; i++) {
			if (pocket[i].getCount() > count) {
				poker = pocket[i];
				count = pocket[i].getCount();
			}
		}
		return poker;
//		System.out.println(poker);
//		System.out.println(this.getBiggestCard());
	}

}
package com.zyy.java;

//牌类
public class Poker {
	
	private String color;
	private String point;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getPoint() {
		return point;
	}

	public void setPoint(String point) {
		this.point = point;
	}


	public Poker() {
		super();
	}

	public Poker(String color, String point) {
		super();
		this.color = color;
		this.point = point;
	}

	@Override
	public String toString() {
		return color +  point;
	}

	public int getPoints() {        // 比牛时的点数,A为1,J、Q、K为10
		int points = 0;
		switch (this.point) {
		case "A":
			points = 1;
			break;
		case "2":
			points = 2;
			break;
		case "3":
			points = 3;
			break;
		case "4":
			points = 4;
			break;
		case "5":
			points = 5;
			break;
		case "6":
			points = 6;
			break;
		case "7":
			points = 7;
			break;
		case "8":
			points = 8;
			break;
		case "9":
			points = 9;
			break;
		case "10":
			points = 10;
			break;
		case "J":
			points = 10;
			break;
		case "Q":
			points = 10;
			break;
		case "K":
			points = 10;
			break;
		}
		return points;
	}

	public int getCount() { // 每张牌的实际点数
		int count = 0;
		switch (this.point) {
		case "A":
			count = 1;
			break;
		case "2":
			count = 2;
			break;
		case "3":
			count = 3;
			break;
		case "4":
			count = 4;
			break;
		case "5":
			count = 5;
			break;
		case "6":
			count = 6;
			break;
		case "7":
			count = 7;
			break;
		case "8":
			count = 8;
			break;
		case "9":
			count = 9;
			break;
		case "10":
			count = 10;
			break;
		case "J":
			count = 11;
			break;
		case "Q":
			count = 12;
			break;
		case "K":
			count = 13;
			break;
		}
		return count;
	}

	public int valuesOfColor(Poker poker) { // 花色的权值
		int values = 0;
		switch (poker.point) {
		case "黑桃":
			values = 4;
			break;
		case "红桃":
			values = 3;
			break;
		case "梅花":
			values = 2;
			break;
		case "方块":
			values = 1;
			break;
		}
		return values;
	}
}
package com.zyy.java;

import java.util.List;

public class SendPoker {
	// Player[] players = new Player[4];

	public Player[] sendPoker(Player[] ps, List washedCards) {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 5; j++) {

				Poker temp = new Poker();
				temp = (Poker) washedCards.get(i * 4 + j);
				ps[i].setOneOfPocket(j, temp);

			}
		}
		return ps;
	}

}
package com.zyy.java;

import java.util.Collections;
import java.util.List;
//洗牌
public class Shuffle {
	List washedCards;
	public Shuffle(List cardsAll){
		washedCards = cardsAll;
		Collections.shuffle(washedCards);
	}
	public List getWashedCards(){
		return washedCards;
	}
}
package com.zyy.java;

public class View {
	Player player = new Player();
	public void view(Player[] players) {
		for (int j = 0; j < 4; j++) {
			System.out.print(players[j].getName() + "的牌为:");
			for (int i = 0; i < 5; i++) {
				System.out.print(players[j].getOneOfPocket(i) + "t");
			}
			// System.out.println(players[j].getIsBull());
			if ((players[j].getIsBull() == true) && (players[j].getPointOfBull() != 0)) {
				System.out.println(" 【牛" + players[j].getPointOfBull() + "】"  + ": 最大的牌是"
						+ player.getBiggestCards(players[j].getPocket()).getColor() 
						+ player.getBiggestCards(players[j].getPocket()).getPoint());
			} else if((players[j].getIsBull() == true) &&  (players[j].getPointOfBull() == 0)){
				System.out.println(" 【牛牛】"  + ": 最大的牌是"
						+ player.getBiggestCards(players[j].getPocket()).getColor() 
						+ player.getBiggestCards(players[j].getPocket()).getPoint());
			}else{
				
				System.out.println("   没牛:最大的牌是" + player.getBiggestCards(players[j].getPocket()).getColor()
						+ player.getBiggestCards(players[j].getPocket()).getPoint());
			}

		}
	}
}

最后

以上就是明理大门为你收集整理的java实现牛牛游戏源代码的全部内容,希望文章能够帮你解决java实现牛牛游戏源代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部