[英]Python: How to draw a new and different set of cards for a card game?
我正在尋找有關我正在開發的簡單紙牌游戲的幫助。 前提是:
我的問題是,一旦首次嘗試結束,我就無法找到新卡的提取方法。 我的代碼改為調用已經玩過的紙牌(這會導致錯誤)。 有什么有效的方法可以在使用代碼時繪制新卡? 謝謝。
import random
Deck = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'Y1', 'Y2', 'Y3', 'Y4', 'Y5', 'Y6', 'Y7', 'Y8', 'Y9', 'Y10', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10']
random.shuffle(Deck)
print(Deck)
Player1Card = Deck[0]
print("Player One, Your Card Is {0}" .format(Deck[0]))
Player2Card = Deck[1]
print("Player Two, Your Card Is {0}" .format(Deck[1]))
PlayerOneScore = 0
PlayerTwoScore = 0
GameCount = 0
PlayerOneCardList = list()
PlayerTwoCardList = list()
1 < 2
2 < 3
3 < 4
4 < 5
5 < 6
7 < 8
8 < 9
9 < 10
while GameCount == 0:
if "R" in Player1Card and "B" in Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
PlayerOneCardList.append(Player1Card)
PlayerOneCardList.append(Player2Card)
print("Player 1, These Are Your Currently Held Cards:")
print(PlayerOneCardList)
print("Player 2, These Are Your Currently Held Cards:")
print(PlayerTwoCardList)
Deck.remove(Player1Card)
Deck.remove(Player2Card)
elif "R" in Player2Card and "B" in Player1Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "B" in Player1Card and "Y" in Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "B" in Player2Card and "Y" in Player1Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "Y" in Player1Card and "R" in Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "Y" in Player2Card and "R" in Player1Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "Y" in Player1Card and "Y" in Player2Card:
if Player1Card > Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif Player1Card < Player2Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "B" in Player1Card and "B" in Player2Card:
if Player1Card > Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif Player1Card < Player2Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif "R" in Player1Card and "R" in Player2Card:
if Player1Card > Player2Card:
print("Player One Wins")
PlayerOneScore = PlayerOneScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
elif Player1Card < Player2Card:
print("Player Two Wins")
PlayerTwoScore = PlayerTwoScore+2
print("Player One, your score is {0} and Player Two, your score is {1}" . format(PlayerOneScore, PlayerTwoScore))
if PlayerOneScore + PlayerTwoScore == 30:
if PlayerOneScore > PlayerTwoScore:
print("Player One Wins")
print("The game is over")
gamecount = 1
elif PlayerTwoScore > PlayerOneScore:
print("Player Two Wins")
print("The game is over")
gamecount = 1
else:
gamecont1 = input("Would you like to continue the game Y for yes or N for no?")
if gamecont1 == "Y" or "y":
gamecont = 0
else:
gamecont = 1
我建議您在像這樣繪制卡片后立即使用.remove()
語句
Player1Card = Deck[0]
print("Player One, Your Card Is {0}" .format(Deck[0]))
Player2Card = Deck[1]
print("Player Two, Your Card Is {0}" .format(Deck[1]))
Deck.remove(Player1Card)
Deck.remove(Player2Card)
這樣可以確保當前的卡不會出現在選卡過程中。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.