简体   繁体   中英

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

Dice game rules: if dice 1 came, turn is on the other player "computer" if its not, ı can throw again now for earning points. Dice would give as much as what came. Its adding to my points but if 1 cames, ı cant take these points. I should have stopped before 1 cames. I say yes, (thats enough): ı earn point and the turn is on the computer. İf 1 came, no points for me and the turn is on computer either. For winn, one of us have to reach point 100. (99 is not win but the last dice is 6 points, that would made 105- still winn)

Can somebody help me with adding computer on my game? I dont know how.

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__())

You should add computer related logic. You can change your algorithm maybe like this:

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__())

Your algorithm works fine and you can reuse a lot of the logic for your player turn in the computer turn.

The only thing that needs to be implemented differently for the computer is some sort of logic that helps it decide when to end its own turn so that the game is fun and not purely luck related.

For example, in the below algorithm the computer will roll the dice until it reaches a turnpoints score of >= 20, then the players turn begins.

It would also be a good idea to add a slight delay for the computers dice rolls so you can visually see the progress of its turn as opposed to it instantly flashing across the screen, this can be implemented with the time.sleep() function from time library.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM