python - Python War纸牌游戏运行正常但无限
问题描述
from random import shuffle
import random
# Two useful variables for creating Cards.
SUITE = 'H D S C'.split()
RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
class Deck:
"""
This is the Deck Class. This object will create a deck of cards to initiate
play. You can then use this Deck list of cards to split in half and give to
the players. It will use SUITE and RANKS to create the deck. It should also
have a method for splitting/cutting the deck in half and Shuffling the deck.
"""
newlist = []
playerone = []
playertwo = []
def __init__(self) -> None:
pass
def create(self, suite, ranks):
self.suite = suite
self.ranks = ranks
for i in suite:
for j in ranks:
Deck.newlist.append([i,j])
def splitshuffle(self):
random.shuffle(Deck.newlist)
for i in range(26):
Deck.playerone.append(Deck.newlist[i])
Deck.playertwo.append(Deck.newlist[i+26])
class Hand:
'''
This is the Hand class. It is to remove two cards from deck and add them to one player's deck whichever is higher also war
happens here
'''
commonlist = []
war = 0
def __init__(self, playerone, playertwo):
self.playerone = playerone
self.playertwo = playertwo
def remove(self):
self.removed_card1 = self.playerone.pop()
self.removed_card2 = self.playertwo.pop()
print("Card removed are: [", self.removed_card1, ", ", self.removed_card2, "]")
return [self.removed_card1, self.removed_card2]
def add(self, cardone, cardtwo):
self.cardone = cardone
self.cardtwo = cardtwo
self.commonlist.clear()
if RANKS.index(self.cardone[1])!=RANKS.index(self.cardtwo[1]):
if RANKS.index(self.cardone[1])>RANKS.index(self.cardtwo[1]):
self.playerone.insert(0,self.cardone)
self.playerone.insert(0,self.cardtwo)
print("Player one has higher card, adding")
print("Length =", len(self.playerone)+len(self.playertwo))
elif RANKS.index(self.cardone[1])<RANKS.index(self.cardtwo[1]):
self.playertwo.insert(0,self.cardone)
self.playertwo.insert(0,self.cardtwo)
print("Player two has higher card, adding")
print("Length =", len(self.playerone)+len(self.playertwo))
else:
self.war+=1
if len(self.playerone)<3:
self.playertwo.insert(0,self.cardone)
self.playertwo.insert(0,self.cardtwo)
while i in self.playerone:
self.playertwo.insert(0,self.playerone.pop())
elif len(self.playertwo)<3:
self.playerone.insert(0,self.cardone)
self.playerone.insert(0,self.cardtwo)
while i in self.playertwo:
self.playerone.insert(0,self.playertwo.pop())
else:
while RANKS.index(self.cardone[1])==RANKS.index(self.cardtwo[1]):
if RANKS.index(self.cardone[1])==RANKS.index(self.cardtwo[1]):
self.commonlist.append([self.cardone][0])
self.commonlist.append([self.cardtwo][0])
self.commonlist.append([self.playerone.pop()][0])
self.commonlist.append([self.playertwo.pop()][0])
self.cardone=self.playerone.pop()
self.cardtwo=self.playertwo.pop()
print("Commonlist = ", self.commonlist)
if RANKS.index(self.cardone[1])>RANKS.index(self.cardtwo[1]):
for i in range(len(self.commonlist)):
self.playerone.insert(0,self.commonlist[i])
self.playerone.insert(0,self.cardone)
self.playerone.insert(0,self.cardtwo)
print("Player one has higher card, adding")
# print("P1 = ", self.playerone)
# print("P2 = ", self.playertwo)
print("Length =", len(self.playerone)+len(self.playertwo))
elif RANKS.index(self.cardone[1])<RANKS.index(self.cardtwo[1]):
for i in range(len(self.commonlist)):
self.playertwo.insert(0,self.commonlist[i])
self.playertwo.insert(0,self.cardone)
self.playertwo.insert(0,self.cardtwo)
print("Player two has higher card, adding")
# print("P1 = ", self.playerone)
# print("P2 = ", self.playertwo)
print("Length =", len(self.playerone)+len(self.playertwo))
print("Wars: = ", self.war)
class Player:
"""
This is the Player class, which takes in a name and an instance of a Hand
class object. It returns result and also checks if player still have cards
"""
def __init__(self, name, myhand):
self.name=name
self.myhand=myhand
def game(self):
self.count=0
while len(self.myhand.playerone)!=0 or len(self.myhand.playertwo)!=0:
if len(self.myhand.playerone)==0:
break
elif len(self.myhand.playertwo)==0:
break
x = self.myhand.remove()
self.myhand.add(x[0], x[1])
self.count+=1
print("Count = ", self.count, "turns")
print("Great game it lasted ", self.count, "turns")
# ######################
# #### GAME PLAY #######
# ######################
print("Welcome to War, let's begin...")
name = input("What is your name? ")
mydeck = Deck()
mydeck.create(SUITE, RANKS)
mydeck.splitshuffle()
myhand = Hand(mydeck.playerone, mydeck.playertwo)
myplayer = Player(name, myhand)
myplayer.game()
一切正常,没有额外的卡添加,整个游戏的卡总数也一样,战争也正确发生,但游戏无限运行,代码有错误还是游戏是这样的可以无限运行。总转数4000以上
解决方案
推荐阅读
- python - numpy 轮子不是此平台上支持的轮子
- google-apps-script - 如何在保留条件格式的同时使用谷歌应用程序脚本复制新的谷歌工作表?
- python - Django 中的持久化蛞蝓
- wordpress - Wordpress 下拉菜单突然不可见
- python - 在递归函数中添加条件以读取嵌套 json 文件中的特定键及其值
- python - Django / DRF - 响应对象中的无效语法
- javascript - 如何将离子项值传递给函数
- c++ - 是否有 strrchr 和 wcsrchr 不区分大小写的版本?(c++)
- android - 是否有任何用于在 android 设备上获取 Widevine (DRM) id 的颤振包?
- solidity - TypeError:无法读取未定义的属性“BuyToken”