簡體   English   中英

我正在 python 中編寫骰子游戲,但我卡住了

[英]I'm coding a dice game in python but ı stuck

骰子游戲規則:如果骰子 1 來了,輪到其他玩家“計算機”,如果沒有,我現在可以再次投擲以獲得積分。 骰子會付出多少。 它增加了我的積分,但如果 1 來了,我不能拿這些積分。 我應該在 1 到來之前停下來。 我說是的,(這就夠了):我賺點,輪到電腦了。 如果 1 來了,對我來說沒有積分,而且輪到電腦了。 為了贏,我們中的一個人必須達到 100 點。(99 不是贏,但最后一個骰子是 6 分,這將使 105-仍然贏)

有人可以幫我在我的游戲中添加電腦嗎? 我不知道怎么做。

from random import randint

run = True
point = 0

while run:
    inp = input("throw dice: ")
   
    if inp == "y":
        dice = randint(1, 7)
        
        if dice != 1:
            point = point + dice
           
            print(dice.__str__() + " came")
            print("your point is: " + point.__str__())
    
        else:
            #1e gelince
            run = False
            
            print("1 came, you cant continue. Your point was: " + point.__str__())
            
    else: 
        #say false to end game
        run = False
       
        print("last point: " + point.__str__())

您應該添加與計算機相關的邏輯。 您可以像這樣更改您的算法:

from random import randint

run = True
computerTurn = False
point = 0
computerPoint = 0

while run:
    if computerTurn:
        dice = randint(1, 7)

        if dice != 1:
            computerPoint += dice
            print(dice.__str__() + " came")
            print("computer point is: " + computerPoint.__str__())
        else:
            print("computer point is: " + computerPoint.__str__())
            print("1 came, your turn!!!")
            computerTurn = False
    else:
        inp = input("throw dice: ")
    
        if inp == "y":
            dice = randint(1, 7)
            
            if dice != 1:
                point = point + dice
            
                print(dice.__str__() + " came")
                print("your point is: " + point.__str__())
        
            else:
                
                print("your point is: " + point.__str__())
                print("1 came, computer turn!!!")
                computerTurn = True

                
        else: 
            #say false to end game
            run = False
        
            print("last point: " + point.__str__())

您的算法運行良好,您可以在計算機回合中為您的玩家回合重用很多邏輯。

唯一需要為計算機實現不同的東西是某種邏輯,幫助它決定何時結束自己的回合,這樣游戲就很有趣,而不是純粹與運氣有關。

例如,在下面的算法中,計算機將擲骰子,直到它達到 >= 20 的轉折點分數,然后玩家開始轉牌。

為計算機擲骰子添加一點延遲也是一個好主意,這樣您就可以直觀地看到輪到的進度,而不是立即在屏幕上閃爍,這可以通過 time.sleep() function 來實現時間圖書館。

from random import randint
import time

run = True
playerturn = True
turnpoints = 0
playerpoints = 0
computerpoints = 0

while run:
    if playerturn:
        inp = input("do you want to throw dice?: (y or n) ")
        if inp == "y":
            dice = randint(1, 7)
            if dice != 1:
                turnpoints = turnpoints + dice
                print(dice.__str__() + " came")
                print("your points for this turn are: " + turnpoints.__str__())
                if (playerpoints + turnpoints) >= 100:
                    playerpoints = playerpoints + turnpoints
                    print("You won the game with a point score of: " + playerpoints.__str__())
                    break
            else:
                print("1 came, you cant continue. Your earned no points this turn. Current Points: " + playerpoints.__str__())
                playerturn = False
                turnpoints = 0
        else:
            playerpoints = playerpoints + turnpoints
            print("You finished the turn. You turn is now over. Current Points: " + playerpoints.__str__())
            playerturn = False
            turnpoints = 0
    else: #computer's turn
        dice = randint(1, 7)
        if dice != 1:
            turnpoints = turnpoints + dice
            print(dice.__str__() + " came")
            print("computer points for this turn are: " + turnpoints.__str__())
            time.sleep(1)
            if (computerpoints + turnpoints) >= 100:
                computerpoints = computerpoints + turnpoints
                print("Computer won the game with a point score of: " + computerpoints.__str__())
                break
            if turnpoints > 20:
                computerpoints = computerpoints + turnpoints
                print("Computers turn is now over. Current Points: " + computerpoints.__str__())
                playerturn = True
                turnpoints = 0
        else:
            playerturn = True
            print("1 came, Computer's turn is over. Computer earned no points this turn. Current Points: " + computerpoints.__str__())
            turnpoints = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM