SWUST派森练习题:P106 扑克牌游戏

描述

本题限定用以下方法打乱序列中的元素

random.shuffle()

几个人用一副扑克牌玩游戏,游戏过程通常有洗牌、发牌、理牌等动作,编写程序模拟游戏过程。新牌花色顺序为♠、♥、♣、♦,花色相同时按2、3、4、5、6、7、8、9、10、J、Q、K、A,最后是小王和大王,小王用'jokers'、大王用 'JOKERS'表示。按以下要求编写程序:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

1、按顺序输出新牌

2、洗牌

3、按洗好的顺序输出洗过的牌

4、将牌轮流分给参与游戏的人,按分牌的顺序输出每个人手上的牌

5、对每个人手上的牌升序排序并输出

6、输出时,每张牌间用空格分隔

格式

输入

输入一个正整数表示参与游戏的人数,题目限定使每个人分到的牌数相同,即人数为54的约数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

输入一个正整数表示随机数种子,本题使用随机数种子保证产生序列的稳定性来完成自动评测

输出

按顺序输出新牌 按洗好的顺序输出洗过的牌 按分牌的顺序输出每个人手上的牌 输出每个人手上升序排序的牌

样例

输入

2
2021

输出

参与游戏的人数:2
新牌顺序
♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K ♠A ♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K ♥A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K ♣A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K ♦A jokers JOKERS
洗牌顺序
♣K ♠2 ♠7 ♠J ♦9 jokers ♣8 ♥2 ♦8 ♠3 ♣A ♥K ♠9 ♥J ♦J ♣5 ♠A ♠K ♣J ♥5 ♠10 ♥Q ♣3 ♦4 ♥8 ♣9 ♣7 ♥3 ♥10 ♣2 ♦10 ♦K ♦2 ♠8 ♠5 ♦6 ♠Q ♦5 ♦7 ♥7 ♦Q ♥9 ♠6 ♣Q ♣6 ♣4 ♠4 ♦A ♥4 ♥6 ♣10 ♦3 ♥A JOKERS
每个人手上分到的牌
♣K ♠7 ♦9 ♣8 ♦8 ♣A ♠9 ♦J ♠A ♣J ♠10 ♣3 ♥8 ♣7 ♥10 ♦10 ♦2 ♠5 ♠Q ♦7 ♦Q ♠6 ♣6 ♠4 ♥4 ♣10 ♥A
♠2 ♠J jokers ♥2 ♠3 ♥K ♥J ♣5 ♠K ♥5 ♥Q ♦4 ♣9 ♥3 ♣2 ♦K ♠8 ♦6 ♦5 ♥7 ♥9 ♣Q ♣4 ♦A ♥6 ♦3 JOKERS
每个人手上排序的牌
♠4 ♠5 ♠6 ♠7 ♠9 ♠10 ♠Q ♠A ♥4 ♥8 ♥10 ♥A ♣3 ♣6 ♣7 ♣8 ♣10 ♣J ♣K ♣A ♦2 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q
♠2 ♠3 ♠8 ♠J ♠K ♥2 ♥3 ♥5 ♥6 ♥7 ♥9 ♥J ♥Q ♥K ♣2 ♣4 ♣5 ♣9 ♣Q ♦3 ♦4 ♦5 ♦6 ♦K ♦A jokers JOKERS

tips:

解题思路:

  1. 通过输入获取参与游戏的人数(n)和随机种子(s)。

  2. 定义了扑克牌的花色和数字的对应关系(rank)以及花色的等级(color_level)。

  3. 定义了扑克牌的组合(poker),包括所有的花色和数字组合以及大小王。

  4. 输出参与游戏的人数。

  5. 输出新牌的顺序。

  6. 输出洗牌的顺序。

  7. 将洗好的牌按顺序分给每个人。

  8. 输出每个人手上分到的牌。

  9. 输出每个人手上排序后的牌。

代码:

import random
n = eval(input())
s = eval(input())
rank = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 13, "Q": 12, "J": 11, "A": 14, "okers": 53, "OKERS": 54}
account = [[] for _ in range(n)]
color_level = {"♠":1, "♥":2, "♣":3, "♦":4,"j":5,"J":6}
color = ["♠", "♥", "♣", "♦"]
num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
poker = [i+j for i in color for j in num]+["jokers", "JOKERS"]
print(f"参与游戏的人数:{n}")
print("新牌顺序")
print(" ".join(poker))
print("洗牌顺序")
random.seed(s)
random.shuffle(poker)
print(" ".join(poker))
print("每个人手上分到的牌")
for i in range(0, 54):
    j = (i) % n
    account[j].append(poker[i])
for i in account:
    print(" ".join(i))
print("每个人手上排序的牌")
for i in account:
    i.sort(key=lambda x: (color_level[x[0]], rank[x[1:]]))
    print(" ".join(i))

代码解释:

account = [[] for _ in range(n)]这一行代码创建了一个名为account的列表,其中包含了n个空列表。这里使用了列表推导式来生成n个空列表,每个空列表代表一个玩家的手牌。

这样的数据结构可以用来存储每个玩家的手牌。在洗牌后,牌将按顺序分配给每个玩家,并存储在相应的列表中。每个列表代表一个玩家的手牌,通过索引来访问每个玩家的手牌。

例如,如果有4个玩家参与游戏(n=4),则account列表将包含4个空列表,分别表示4个玩家的手牌。在之后的代码中,每张牌都将按顺序分配给这些列表中的一个,以模拟洗牌和发牌的过程。

for i in account: 循环遍历了 account 列表中的每个子列表,即每个玩家的手牌。

i.sort(key=lambda x: (color_level[x[0]], rank[x[1:]])) 这一行代码对当前玩家的手牌进行排序操作。sort() 方法用于对列表进行排序,其中 key=lambda x: (color_level[x[0]], rank[x[1:]]) 是一个排序关键字函数。这个函数将每张牌作为参数 x,然后根据花色和数字的优先级对牌进行排序。

color_level[x[0]] 返回牌的花色在 color_level 字典中对应的值,即花色的优先级。rank[x[1:]] 返回牌的数字在 rank 字典中对应的值,即数字的优先级。通过将花色和数字的优先级进行组合,可以确保牌按照规定的顺序进行排序。

最后,print(" ".join(i)) 打印出当前玩家手牌排序后的结果,使用空格将牌之间的元素连接起来形成一个字符串。这样可以展示每个玩家手牌排序后的结果。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PKCard extends JLabel implements MouseListener, MouseMotionListener{ //纸牌的位置 Point point = null; Point initPoint = null; int value = 0; int type = 0; String name = null; Container pane = null; Spider main = null; boolean canMove = false; boolean isFront = false; PKCard previousCard = null; public void mouseClicked(MouseEvent arg0){ } public void flashCard(PKCard card){ //启动Flash线程 new Flash(card).start(); //不停的获得下一张牌,直到完成 if(main.getNextCard(card) != null){ card.flashCard(main.getNextCard(card)); } } class Flash extends Thread{ private PKCard card = null; public Flash(PKCard card){ this.card = card; } /* **线程的run()方法 **为纸牌的正面设置白色图片 */ public void run(){ boolean is = false; ImageIcon icon = new ImageIcon("images/white.gif"); for (int i = 0; i < 4; i++){ try{ Thread.sleep(200); } catch (InterruptedException e){ e.printStackTrace(); } if (is){ this.card.turnFront(); is = !is; } else{ this.card.setIcon(icon); is = !is; } // 根据当前外观将card的UI属性重置 card.updateUI(); } } } /** **点击鼠标 */ public void mousePressed(MouseEvent mp){ point = mp.getPoint(); main.setNA(); this.previousCard = main.getPreviousCard(this); } /** **释放鼠标 */ public void mouseReleased(MouseEvent mr){ Point point = ((JLabel) mr.getSource()).getLocation(); //判断可行列 int n = this.whichColumnAvailable(point); if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){ this.setNextCardLocation(null); main.table.remove(this.getLocation()); this.setLocation(this.initPoint); main.table.put(this.initPoint, this); return; } point = main.getLastCardLocation(n); boolean isEmpty = false; PKCard card = null; if (point == null){ point = main.getGroundLabelLocation(n); isEmpty = true; } else{ card = (PKCard) main.table.get(point); } if (isEmpty || (this.value + 1 == card.getCardValue())){ point.y += 40; if (isEmpty) point.y -= 20; this.setNextCardLocation(point); main.table.remove(this.getLocation()); point.y -= 20; this.setLocation(point); main.table.put(point, this); this.initPoint = point; if (this.previousCard != null){ this.previousCard.turnFront(); this.previousCard.setCanMove(true); } this.setCanMove(true); } else{ this.setNextCardLocation(null); main.table.remove(this.getLocation()); this.setLocation(this.initPoint); main.table.put(this.initPoint, this); return; } point = main.getLastCardLocation(n); card = (PKCard) main.table.get(point); if (card.getCardValue() == 1){ point.y -= 240; card = (PKCard) main.table.get(point); if (card != null && card.isCardCanMove()){ main.haveFinish(n); } } } /* **方法:放置纸牌 */ public void setNextCardLocation(Point point){ PKCard card = main.getNextCard(this); if (card != null){ if (point == null){ card.setNextCardLocation(null); main.table.remove(card.getLocation()); card.setLocation(card.initPoint); main.table.put(card.initPoint, card); } else{ point = new Point(point); point.y += 20; card.setNextCardLocation(point); point.y -= 20; main.table.remove(card.getLocation()); card.setLocation(point); main.table.put(card.getLocation(), card); card.initPoint = card.getLocation(); } } } /** **返回值:int **方法:判断可用列 */ public int whichColumnAvailable(Point point){ int x = point.x; int y = point.y; int a = (x - 20) / 101; int b = (x - 20) % 101; if (a != 9){ if (b > 30 && b <= 71){ a = -1; } else if (b > 71){ a++; } } else if (b > 71){ a = -1; } if (a != -1){ Point p = main.getLastCardLocation(a); if (p == null) p = main.getGroundLabelLocation(a); b = y - p.y; if (b <= -96 || b >= 96){ a = -1; } } return a; } public void mouseEntered(MouseEvent arg0){ } public void mouseExited(MouseEvent arg0){ } /** **用鼠标拖动纸牌 */ public void mouseDragged(MouseEvent arg0){ if (canMove){ int x = 0; int y = 0; Point p = arg0.getPoint(); x = p.x - point.x; y = p.y - point.y; this.moving(x, y); } } /** **返回值:void **方法:移动(x,y)个位置 */ public void moving(int x, int y){ PKCard card = main.getNextCard(this); Point p = this.getLocation(); //将组件移动到容器中指定的顺序索引。 pane.setComponentZOrder(this, 1); //在Hashtable中保存新的节点信息 main.table.remove(p); p.x += x; p.y += y; this.setLocation(p); main.table.put(p, this); if (card != null) card.moving(x, y); } public void mouseMoved(MouseEvent arg0){ } /** **构造函数 */ public PKCard(String name, Spider spider){ super(); this.type = new Integer(name.substring(0, 1)).intValue(); this.value = new Integer(name.substring(2)).intValue(); this.name = name; this.main = spider; this.pane = this.main.getContentPane(); this.addMouseListener(this); this.addMouseMotionListener(this); this.setIcon(new ImageIcon("images/rear.gif")); this.setSize(71, 96); this.setVisible(true); } /** **返回值:void **方法:令纸牌显示正面 */ public void turnFront(){ this.setIcon(new ImageIcon("images/" + name + ".gif")); this.isFront = true; } /** **返回值:void **方法:令纸牌显示背面 */ public void turnRear(){ this.setIcon(new ImageIcon("images/rear.gif")); this.isFront = false; this.canMove = false; } /** **返回值:void **方法:将纸牌移动到点point */ public void moveto(Point point){ this.setLocation(point); this.initPoint = point; } /** **返回值:void **方法:判断牌是否能移动 */ public void setCanMove(boolean can){ this.canMove = can; PKCard card = main.getPreviousCard(this); if (card != null && card.isCardFront()){ if (!can){ if (!card.isCardCanMove()){ return; } else{ card.setCanMove(can); } } else{ if (this.value + 1 == card.getCardValue() && this.type == card.getCardType()){ card.setCanMove(can); } else{ card.setCanMove(false); } } } } /** **返回值:boolean **方法:判断card是否是正面 */ public boolean isCardFront(){ return this.isFront; } /* **返回值:boolean **方法:返回是否能够移动 */ public boolean isCardCanMove(){ return this.canMove; } /** **返回值:int **方法:获得card的内容值 */ public int getCardValue(){ return value; } /** **返回值:int **方法:获得card的类型 */ public int getCardType(){ return type; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值