简体   繁体   English

Python“骰子游戏”

[英]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?如何确定哪个玩家连续 3 次获得一个特定数字? 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.首先,制作一个模拟骰子的函数,它应该返回一个介于 [1,6] 之间的整数,并且应该使用容易获得的(伪)随机函数生成。 Once this is done, Declare variables, continous1, continous2, prev1, prev2.完成后,声明变量,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.如果当前回合与该玩家的 prev 相同,则 prev 变量将存储该玩家的 prev 骰子角色答案,从而增加连续计数。 The first to reach the 3 is your answer.第一个达到 3 的就是你的答案。 Use a while loop to simulate this.使用while循环来模拟这一点。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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