用python怎们编写一个斗地主游戏
时间: 2023-11-13 15:02:43 浏览: 101
编写一个斗地主游戏需要掌握以下知识点:
1. Python基础语法
2. 面向对象编程
3. 随机数生成
4. 列表和字典的使用
下面是一个简单的斗地主游戏实现:
```python
import random
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __repr__(self):
return f"{self.suit}{self.value}"
class Deck:
def __init__(self):
self.cards = []
for suit in ['♥', '♠', '♣', '♦']:
for value in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
self.cards.append(Card(suit, value))
random.shuffle(self.cards)
def deal(self, num):
return [self.cards.pop() for _ in range(num)]
class Player:
def __init__(self, name):
self.name = name
self.cards = []
def receive_cards(self, cards):
self.cards.extend(cards)
def play_cards(self, card_indices):
played_cards = [self.cards[i] for i in card_indices]
for card in played_cards:
self.cards.remove(card)
return played_cards
class Game:
def __init__(self, player_names):
self.deck = Deck()
self.players = [Player(name) for name in player_names]
self.landlord_index = None
self.current_player_index = None
self.last_played_cards = None
def deal_cards(self):
for player in self.players:
player.receive_cards(self.deck.deal(17))
self.landlord_index = random.randint(0, 2)
self.players[self.landlord_index].receive_cards(self.deck.deal(3))
def start(self):
self.deal_cards()
self.current_player_index = self.landlord_index
self.last_played_cards = []
while True:
print(f"当前出牌玩家:{self.players[self.current_player_index].name}")
print("你的手牌:", self.players[self.current_player_index].cards)
played_cards = input("请输入你要出的牌,用空格分隔,不出请输入pass:").strip()
if played_cards == "pass":
if len(self.last_played_cards) == 0:
print("首轮必须出牌!")
continue
else:
print("过牌")
self.current_player_index = (self.current_player_index + 1) % 3
continue
played_cards = played_cards.split()
played_cards = [int(i) for i in played_cards]
played_cards = self.players[self.current_player_index].play_cards(played_cards)
if not self.is_valid_play(played_cards):
print("出牌不符合规则,请重新出牌!")
self.players[self.current_player_index].receive_cards(played_cards)
continue
self.last_played_cards = played_cards
if len(self.players[self.current_player_index].cards) == 0:
print(f"{self.players[self.current_player_index].name}获胜!")
break
self.current_player_index = (self.current_player_index + 1) % 3
def is_valid_play(self, played_cards):
if len(played_cards) == 0:
return False
if len(self.last_played_cards) == 0:
if len(played_cards) == 1:
return True
if len(played_cards) == 2 and played_cards[0].value == played_cards[1].value:
return True
if len(played_cards) == 3 and played_cards[0].value == played_cards[1].value and played_cards[1].value == played_cards[2].value:
return True
if len(played_cards) == 4 and played_cards[0].value == played_cards[1].value and played_cards[1].value == played_cards[2].value and played_cards[2].value == played_cards[3].value:
return True
if len(played_cards) == 5 and played_cards[0].value == played_cards[1].value and played_cards[1].value == played_cards[2].value and played_cards[2].value == played_cards[3].value and played_cards[3].value == played_cards[4].value:
return True
return False
if len(played_cards) != len(self.last_played_cards):
return False
if played_cards[0].value == '2':
return False
if played_cards[0].value == 'JOKER' or played_cards[0].value == 'joker':
return True
return played_cards[0].value == self.last_played_cards[0].value
```
游戏开始前,先创建一副牌,然后随机分发给三个玩家。其中一个玩家被选为地主,从牌堆中拿三张底牌,并且该玩家先出牌。玩家可以出单张、对子、三张、炸弹或者顺子。如果当前玩家没有合法牌可出,则必须选择跳过。如果所有玩家都跳过,则当前出牌玩家可以出任意合法牌。最后一个出完牌的玩家获胜。
阅读全文