使用函數的戰爭Python Game

我必須完成這項任務,下面是我目前為止所做的,我附上了輸出應該是什么樣子的圖片。我對函數不是很在行,也不確定這個程序的格式是否正確,或者如何調用函數來運行游戲。

對于本任務,您將創建一個更現實的“戰爭游戲”。

您的程序應執行以下操作:

  • 介紹如何玩游戲
  • 打10輪
  • 每一輪,顯示牌手1和牌手2所抽的牌
  • 確定誰贏了,或者這是一場平局,然后展示出來
  • 兩輪之間等待2秒
  • 在每輪結束時,顯示最終分數,并顯示“XYZ贏了!!”

Requirements:

  1. 使用以下原型“def getCardValue()”創建一個名為getCardValue的函數,該函數返回一個介于2和14之間的隨機數。在此函數中使用randint()獲得一個隨機數。
  2. 使用以下原型“def displayCardValue(value)”創建一個名為displayCardValue的函數,該原型返回卡值的字符串版本。也就是說,11=J,12=Q,13=K,14=A,所有其他數字都轉換為字符串。
  3. 使用以下原型制作一個名為determineMax的函數:“def determineMax(a,b)”。如果a大于b,則此函數將返回1,否則如果a小于b,則返回2,否則返回0。使用此函數確定每輪的贏家以及最后的整場比賽的贏家。
  4. 使用以下原型“def playRound()”創建一個名為playrounds()的函數,該函數返回回合的獲勝者。使用此函數可以簡化主游戲循環。在主循環中跟蹤球員的得分。
  5. 在全局級別編寫主游戲循環(或使用ifmain+main()函數)。在這里,您應該跟蹤玩家的分數,顯示游戲文本,并使用上述部分中編寫的函數確定/顯示最終贏家。讓游戲感覺自然。在課程開始時添加簡短介紹。使用time類中的sleep()函數在每一輪之后暫停屏幕輸出一小段時間。
     import random
     import time
    
     def main():
         print("Welcome to the Game of War!")
         time.sleep(2)
         print("The player who draws the higher card wins the round.")
         time.sleep(2)
         print("Whoever has more points at the end of 10 rounds wins the game! Here we 
         go!")
         time.sleep(4)
         print()
    
    
        num_rounds = 10
        wins_player1 = 0
        wins_player2 = 0
    
        def getCardValue():
            card1 = random.randint(2,15)
    
       def displayCardValue(value):
           if card1 == 11:
               return "J"
           elif card1 == 12:
              return "Q"
           elif card1 == 13:
              return "K"
           elif card1 == 14:
              return "A"
           else:
              return str(card1)
    
       def determineMax(a, b):
           a = getCardValue
           b = getCardValue
           if a > b:
               return 1
               wins_player1 += 1
           elif a < b:
               return 2
               wins_player2 += 1
           else:
               return 0
    
       def playRound():
    
       for i in range(10):
           print('------- ROUND',(i+1),'-------')
    
     if __name__ == "__main__":                                                              
         main() 
    

? 最佳回答:

我猶豫了一下,因為你自己并沒有做太多,但至少你做了努力。我認為你對函數有一些基本的誤解。所以,不要只是把這件事交上來,并為此而受到贊揚。相反,請閱讀我在這里所做的,看看函數如何交互。一個函數應該有一個目的。它將輸入轉化為輸出。它接受一個輸入(通過參數),對這些輸入做一些事情,然后返回一個輸出。然后,函數的調用方需要對輸出進行處理。

例如,getCardValue不需要知道有兩個玩家。它的工作就是歸還一張新卡。來電者決定如何使用它。與displayCardValue相同。它顯示給它的牌的價值——它不知道游戲正在進行,也不知道有多少玩家。

import random
import time

def getCardValue():
    return random.randrange(2,14)

def displayCardValue(value):
    if value > 10:
        return "JQKA"[value-11]
    return str(value)

def determineMax(a, b):
   if a > b:
       return 1
   elif a < b:
       return 2
   return 0

def playRound():
    card1 = getCardValue()
    card2 = getCardValue()
    print( "Player 1:", displayCardValue(card1), " | Player 2:", displayCardValue(card2))
    result = determineMax(card1, card2)
    if result == 1:
        print( "   Player 1 wins!" )
    elif result == 2:
        print( "   Player 2 wins!" )
    else:
        print( "   Tie!" )
    return result

def main():
    print("Welcome to the Game of War!")
    print("The player who draws the higher card wins the round.")
    print("Whoever has more points at the end of 10 rounds wins the game! Here we go!")
    print()

    num_rounds = 10
    wins_player1 = 0
    wins_player2 = 0

    for i in range(10):
        print('------ ROUND', i+1, '------')
        result = playRound()
        if result == 1:
            wins_player1 += 1
        else:
            wins_player2 += 1
        time.sleep(2)

    if wins_player1 > wins_player2:
        print( "   Player 1 wins the game!" )
    elif wins_player1 < wins_player2:
        print( "   Player 2 wins the game!" )
    else:
        print( "   It's a tie!")

if __name__ == "__main__":                                                              
     main() 
主站蜘蛛池模板: 日韩亚洲一区二区三区| 国模无码一区二区三区不卡| 成人区人妻精品一区二区不卡网站| 任你躁国产自任一区二区三区| 天天视频一区二区三区| 亚洲一区二区三区不卡在线播放| 精品视频一区二区三区四区五区| 伊人久久精品无码麻豆一区| 久久久久国产一区二区 | 日韩精品无码一区二区三区AV| 国产视频福利一区| 天天视频一区二区三区| 无码人妻一区二区三区一| 日韩内射美女人妻一区二区三区 | 国产91精品一区二区麻豆网站 | 波多野结衣在线观看一区 | 风间由美在线亚洲一区| 中文字幕av日韩精品一区二区| 日韩免费无码一区二区三区| 亚洲AV成人一区二区三区AV| 亚洲AV日韩AV天堂一区二区三区| 亚洲一区二区三区香蕉| 人妻少妇AV无码一区二区| 国产麻豆精品一区二区三区v视界| 中文字幕一区二区三区日韩精品| 岛国无码av不卡一区二区| 亚洲国产综合无码一区二区二三区 | 亚洲AV综合色区无码一区| 亚洲av鲁丝一区二区三区| 日韩免费无码视频一区二区三区 | 国产精品毛片VA一区二区三区 | 精品亚洲AV无码一区二区三区| 中文字幕视频一区| 亚洲AV无码一区二区大桥未久| 日韩成人无码一区二区三区| 狠狠综合久久av一区二区| 亚洲视频在线一区二区| 久久久精品人妻一区亚美研究所| 色老头在线一区二区三区| 性色av无码免费一区二区三区| 精品国产一区二区三区在线|