我该如何编写一个程序,将一半的牌面分配给一个玩家,另一半分配给另一个?

时间:2018-11-27 21:21:48

标签: python-3.x

我正在尝试为《战争游戏》编写代码,但是我的代码目前有两个问题。

第一个问题是,当两张卡的值相同时(在dealWar()函数中),我无法进入WAR。它正在确定获胜者,但我不知道结果如何。

我遇到的第二个问题是我正在尝试编写一个函数,该函数将确定最终得分并确定获胜者。我现在编写的代码finalScore()似乎并没有计算所有说playerOne + " Wins!"的字符串,只是简短而已。

import random

# deck of cards
frontOfCard = []

suits = ["Hearts", "Diamonds", "Clubs", "Spades"]

royals = ["Ace", "Jack", "Queen", "King"]

oldDeck = []


for numbers in range(2,11):
  frontOfCard.append(str(numbers))

for royal in range(4):
  frontOfCard.append(royals[royal])

for suit in range(4):
  for card in range(13):
    cards = (frontOfCard[card] + " of " + suits[suit])
    oldDeck.append(cards)

#create players
def nameOfPlayers():
  print("Welcome to the Game of WAR! " + playerOne + " and " + playerTwo)

#explanation of game of war
def explanation():
  print("Here are the rules!")
  print("The deck is divided evenly to each player, with each player receiving 26 cards.")
  print()
  print("The cards will be flipped for the playeres and the player with the higher card takes both cards.")
  print()
  print("If the cards are the same rank, it is WAR.  Each player turns up one card face down and one card face up.  The player with the higher card takes both piles, if they are the same rank,each players places another card face down and turns the other face down card up, continue until the winner of that pile is determined.")

#Deal out cards
#Fix code to go into war if cards are the same
def dealWar():
  random.shuffle(oldDeck)
  hand1 = oldDeck[0:int(len(oldDeck)/2)]
  hand2 = oldDeck[int(len(oldDeck)/2):len(oldDeck)]
  for i in range(0, int(len(oldDeck)/2)):
    handOne = hand1.pop()
    handTwo = hand2.pop()
    print(playerOne + " has played: " + handOne + "  |  " + playerTwo + " has played: " + handTwo)
    if handOne > handTwo:
      print(playerOne + " Wins!")
      print()
    elif handOne == handTwo:
      print("WAR has BEGUN!")
      handOne = hand1.pop()
      handTwo = hand2.pop()
      print(playerOne + " has played: " + handOne + "  |  " + playerTwo + " has played: " + handTwo)
      if handOne > handTwo:
        print(playerOne + " Wins!")
      else:
        print(playerTwo + " Wins!")
        print()
    else:
      print(playerTwo + " Wins!")
      print()

# fix to count every word that says player name and wins
def finalScore():
  playerOneScore = len(playerOne + " Wins!")
  playerTwoScore = len(playerTwo + " Wins!")
  if playerOneScore > playerTwoScore:
    print(playerOne + " ! ")
  elif playerOneScore == playerTwoScore:
    print("NO ONE, the game ended as a DRAW! ")
  else:
    print(playerTwo + " ! ")
#CODE

playerOne = input("What is your name? ")
playerTwo = input("What is your name? ")
print()

nameOfPlayers()

print()

explanation()
print()

userInput = input("Are both players ready to start? (y/n): ")
print()

if userInput == "y":
  print("Here we go! ")
  print()
elif userInput == "n":
  print("Too bad we're starting anyways")
else:
  print("Wrong Input, Try Again!")

print()

dealWar()

print("The winner is...........")
finalScore()
print()

playAgain = input("Would you like to play again? (y/n): ")
if playAgain == "y":
  dealWar()
  print()
  finalScore()
  print()
  print(playAgain)
  print()
  print("The winnder is.......")
  print()
  finalScore()
else:
  print("GOOD BYE")

1 个答案:

答案 0 :(得分:2)

对于战争,您可以使用列表切片将洗牌后的甲板分为两个相等的部分。

hand1 = deck[0:int(len(deck)/2)]
hand2 = deck[int(len(deck)/2):len(deck)]

在这两行之后,hand1将包含从索引0到牌组中间(不包括在内)的牌,hand2将包含从牌组中间(包括端)至结束。

如果要模拟从卡组中发牌的行为,可以在卡组上循环并向每只手添加卡,同时从卡组中弹出卡。

for i in range(0, int(len(deck)/2)):
    hand1.append(deck.pop())
    hand2.append(deck.pop())