斗地主玩法

public class Card {
	/*大体思路
	1扑克牌一共有54张,首先创建一个集合来存储扑克牌编号,再创建一个Map集合来存储扑克牌编号与扑克牌牌面的映射关系,
	牌面有花色4个,数字13个,利用for循环,将52张牌的编号和映射关系分别添加进Arraylist集合和map集合。大王小王的编号和映射独立添加。
	2 洗牌 Collections.shuffle(poker)  //poker 为保存编号的集合
	3发牌,编号除以3取余,余1发给第一个人,。。余三发给第三个人。创建三个人的集合,将余1的存第一个人集合,。。。
	4看牌。看代码注释。。。。
	    斗地主的步骤:
	        1. 准备牌 
	        2. 洗牌
	        3. 发牌
	        4. 看牌
	 */
	public static void main(String[] args) {
		// 1. 准备牌
		// 创建一个ArrayList集合,保存扑克牌的编号
		ArrayList<Integer> poker = new ArrayList<>();
		// 创建一个Map集合,保存编号和扑克牌的对应关系
		// key是编号,value是对应的扑克牌
		HashMap<Integer, String> map = new HashMap<>();
		// 往ArrayList添加编号,往HashMap集合添加对应关系。
		// 定义数组,保存花色
		String[] colors = {"♠", "♥", "♣", "♦"};
		// 定义数组,保存数字
		String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k",
				"A", "2"};
		// 定义牌的编号,从0开始
		int index = 0;
		// 对数字和花色进行组合,然后添加到对应的集合
		for (String num : nums) {
			for (String color : colors) {
				// 往ArrayList集合中添加编号
				poker.add(index);
				// 往Map集合中添加对应关系
				map.put(index, color + num);
				// 把index++
				index++;
			}
		}
		// 添加小王
		poker.add(index);
		map.put(index, "小王");
		index++;
		// 添加大王
		poker.add(index);
		map.put(index, "大王");
		// 2. 洗牌
		// 调用Collections工具类的shuffle方法,打乱集合的顺序。
		// 洗牌我们直接洗编号。
		Collections.shuffle(poker);
		// 3. 发牌
		// 定义三个集合,保存三个玩家手中的牌(真正保存的是编号)
		ArrayList<Integer> playOne = new ArrayList<>();
		ArrayList<Integer> playTwo = new ArrayList<>();
		ArrayList<Integer> playThree = new ArrayList<>();
		// 定义一个集合,保存底牌
		ArrayList<Integer> diPai = new ArrayList<>();
		// 开始发牌,真正发给每个玩家的都是编号。
		// 遍历存放编号的集合,把里面的编号按照规律发给每一个玩家.
		// 根据编号在集合中的索引进行发牌.
		for (int i = 0; i < poker.size(); i++) {
			// 拿到每张牌(真正拿到的是编号)
			Integer card = poker.get(i);
			// 处理底牌
			if (i >= 51) {// 51 52 53
				// 如果不足三张牌了,就添加到底牌集合中
				diPai.add(card);
				continue;
			}
			// 判断并发牌
			if (i % 3 == 0) {// 如果这张牌的编号的索引  对3取余结果是0,就发给第一个人
				playOne.add(card);
			} else if (i % 3 == 1) {// 如果这张牌的编号的索引 对3取余结果是1,就发给第二个人
				playTwo.add(card);
			} else {// 如果这张牌的编号的索引 对3取余结果是2,就发给第3个人
				playThree.add(card);
			}
		}
		// 4. 调用方法进行看牌
		lookCard("刘德华", playOne, map);
		lookCard("周星驰", playTwo, map);
		lookCard("周润发", playThree, map);
		lookCard("底牌", diPai, map);
	}/*
			定义方法,完成看牌。
			参数:
			    玩家的姓名。
			    玩家手里的牌(真正是编号)
			    保存编号和牌对应关系的Map集合。
			步骤:
			在方法中,我们对玩家手中的编号进行排序。
			打印各种东西...
			 */

	public static void lookCard(String name, ArrayList<Integer> list,
			HashMap<Integer, String> map) {
		// 对玩家手里的牌进行排序
		Collections.sort(list);
		// 对这个集合进行反转,就变成了从小到大进行排序
		Collections.reverse(list);// reverse方法用来对集合中的内容进行反转
		// 开始打印
		System.out.print(name + ": ");
		// 打印玩家的每一张牌
		// 遍历ArrayList集合,也就是每个玩家手中的牌的编号,然后根据这个编号去Map集合中找到对应真正的牌。
		for (Integer cardNumber : list) {
			// 调用map的get方法,根据牌的编号找到这张牌真正是是什么
			String card = map.get(cardNumber);
			System.out.print(card + " ");
		}
		System.out.println();
	}

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里是一个简单的Python斗地主游戏代码,可以在命令行轮流输入3位玩家的出牌信息,每次出牌都会判断出牌规则是否符合斗地主玩法,不符合规则的出牌方式会提示重新出牌。本局结束后会输出结果以及得分。 ```python import random # 初始化牌堆 poker = [] for i in range(1, 14): for j in range(4): poker.append((i, j)) poker.append((14, 0)) poker.append((14, 1)) # 洗牌 random.shuffle(poker) # 发牌 player1 = poker[:17] player2 = poker[17:34] player3 = poker[34:51] landlord = poker[51:] # 输出初始牌堆 print("初始牌堆:") print(poker) # 确定地主 print("地主牌:") print(landlord) landlord_id = input("请输入地主玩家编号(1, 2, 3):") if landlord_id == "1": player1 += landlord elif landlord_id == "2": player2 += landlord else: player3 += landlord # 输出每个玩家的牌 print("玩家1的牌:") print(player1) print("玩家2的牌:") print(player2) print("玩家3的牌:") print(player3) # 初始化出牌信息 last_play = [] current_play = [] # 判断出牌规则是否符合斗地主玩法 def is_valid_play(play): if len(play) == 0: return False if len(last_play) == 0: if play[0][0] == 3: return True else: return False if len(play) == len(last_play) and play[0][0] > last_play[0][0]: return True if len(play) == 4 and len(last_play) == 2 and play[0][0] == play[1][0] == play[2][0] == play[3][0]: return True if len(play) == 2 and len(last_play) == 2 and play[0][0] == play[1][0] and play[0][0] > last_play[0][0]: return True return False # 输出当前出牌信息 def print_play_info(current_play): print("当前出牌信息:") for card in current_play: print(card, end=" ") print("") # 轮流出牌 while True: # 玩家1出牌 print("玩家1出牌:") current_play = input().split() current_play = [(int(card[:-1]), int(card[-1:])) for card in current_play] if not is_valid_play(current_play): print("出牌无效,请重新出牌!") continue print_play_info(current_play) last_play = current_play # 玩家2出牌 print("玩家2出牌:") current_play = input().split() current_play = [(int(card[:-1]), int(card[-1:])) for card in current_play] if not is_valid_play(current_play): print("出牌无效,请重新出牌!") continue print_play_info(current_play) last_play = current_play # 玩家3出牌 print("玩家3出牌:") current_play = input().split() current_play = [(int(card[:-1]), int(card[-1:])) for card in current_play] if not is_valid_play(current_play): print("出牌无效,请重新出牌!") continue print_play_info(current_play) last_play = current_play # 判断是否结束 if len(player1) == 0 or len(player2) == 0 or len(player3) == 0: break # 计算得分 player1_score = sum([card[0] for card in player1]) player2_score = sum([card[0] for card in player2]) player3_score = sum([card[0] for card in player3]) print("玩家1得分:", player1_score) print("玩家2得分:", player2_score) print("玩家3得分:", player3_score) ``` 如果要增加网络通信功能,可以将游戏交互过程改成通过socket通信完成。以下是一个简单的通信规则: 1. 服务器先启动,等待客户端连接。 2. 客户端连接服务器后,服务器发送初始牌堆信息给所有客户端,并确定地主。 3. 地主客户端先出牌,之后轮流出牌,每次出牌需要向服务器发送出牌信息。 4. 服务器根据出牌信息进行判断,如果符合规则则将出牌信息广播给所有客户端,否则提示客户端重新出牌。 5. 当一个玩家出完所有的牌,本局游戏结束,服务器计算得分并广播给所有客户端。 具体实现方式可以参考Python的socket库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值