Search code examples
pythondice

I'm coding a 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__())

Solution

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