简体   繁体   中英

Python "Dice Game"

I want to create a simple game in which two players "roll the dice against each other". Winner is whoever gets one number three times in a row. I tried many different ways but in the end I always struggled with the evaluation. How can I determine which player got one specific number 3-times in a row? Thanks for your advice!

import random

#Initialisieren der Variablen
wurf1_1 = 0
wurf2_1 = 0
gewuerfelt_s1 = []
gewuerfelt_s2 = []
n = 1

while (True):
    #Bestimmen der Augenzahlen des Würfels
    wurf1_1 = random.randint(1,6)
    wurf2_1 = random.randint(1,6)
    print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_1) + "; Spieler 2: " + str(wurf2_1))

    gewuerfelt_s1.append(wurf1_1)
    gewuerfelt_s2.append(wurf2_1)

    wurf1_2 = random.randint(1,6)
    wurf2_2 = random.randint(1,6)
    n += 1

    print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_2) + "; Spieler 2: " + str(wurf2_2))

    if (wurf1_2 == gewuerfelt_s1[0]):
        gewuerfelt_s1.append(wurf1_2)
        wurf1_3 = random.randint(1,6)
        n += 1
        print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_3) + "; Spieler 2: " + str(wurf2_2))
        if wurf1_3 == gewuerfelt_s1[1]:
            print("Spieler 1 hat dreimal hintereinander die Zahl", gewuerfelt_s1[1], "gewürfelt. Sieger!")
            break
        else:
            del gewuerfelt_s1[:]
            continue
    else:
        del gewuerfelt_s1[:]
        continue

Don't delete elements from your list. You can check the most recently appended elements of a list by indexing from the end

The last element of a list is:

my_list[-1]

The second last is:

my_list[-2]

So for each roll after the second roll, you can check:

my_list[-1] == my_list[-2] == my_list[-3]

(Can't comment, low rep)

First of all, make a function that will simulate the dice, it should return an integer number between [1,6] and should be generated using easily available (pseudo)random functions. Once this is done, Declare variables, continous1, continous2, prev1, prev2. The prev variables would store the prev dice role answer for that player if the current turn is the same as the prev for that player, increasing the continuous count. The first to reach the 3 is your answer. Use a while loop to simulate this.

Here is the code

import random

continous1 = 0
continous2 = 0
prev1 = 0
prev2 = 0

while continous1 != 3 and continous2 != 3:
    person1 = random.randint(1,6)
    person2 = random.randint(1,6)
    
    if person1 == prev1:
        continous1 += 1
    else:
        continous1 = 0

    
    if person2 == prev2:
        continous2 += 1
    else:
        continous2 = 0
    
    prev1 = person1
    prev2 = person2

# Note that even if both persons win the game at the same time, the first person will be chosen as the winner
if continous1 == 3:
    print("Person 1 won the game")
else:
    print("Person 2 won the game")
    

Check this pseudo code

PlayGame()
{
    bool gameover = false;
    int turns ;
    player1_values = []
    player2_values = []
    While(!gameover)
    {
        player1_values.Add(getrandon(1,6));
        player2_values.Add(getrandon(1,6));
        
        bool player1_won = Check(player1_values);
        if(!player1_won && player1_values.Count == 3)
            player1_values.Clear();
        
        bool player2_won = Check(player2_values);
        if(!player1_won && player2_values.Count == 3)
            player2_values.Clear();
            
        gameover = player1_won || player2_won;
    }
}

Check(values)
{
    if(values.Count < 3)
        return false;
    
    int num = values[0];
    for(int i = 1;i<3;i++)
    {
        if(values[i] != num)
            return false;
    }           
    return true;
}

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