C++中的石头剪刀布循环方法

Rock Paper Scissors looping method in C++

本文关键字:循环 方法 石头剪刀布 C++      更新时间:2023-10-16

所以基本上我正在设计一个石头剪刀布游戏。

我有三种方法(移动(可供选择,具体取决于他们的胜率。我们称之为:methodA()methodB()methodC()

现在我需要每 30 轮执行一次检查以检查胜率。

我有一段这样的代码:

Move nextMove(){ //Move is a enum including Rock,Paper,Scissors
    Move makeMove;
    vector<float> checkWinRate = {winRateA,winRateB,winRateC};
    //saving my win rates into a vector and sort it
    std::sort(checkWinRate.begin(),checkWinRate.end()); 
    if (checkWinRate.back() == winRateA)
        makeMove = methodA();
    else if (checkWinRate.back() == winRateB)
        makeMove = methodB();
    else
        makeMove = methodC();
    //choose methods according to win rates
    return makeMove;
}

我现在正在做的是每次调用此函数时都让它检查。但是,我的最终目标是让它每 30 轮检查一次,在接下来的 30 轮中运行相同的方法并再次检查。这听起来很容易,但无论如何我都想不出这样做。

知道我应该在这里做什么吗?请帮忙。

您可以创建一个整数,每轮递增 1,当它达到 29(30 轮(时,您进行检查并再次将其设为零。您的回合数一开始将是 29,以便能够在开始时输入任何 if。

Move nextMove(static int numOfRounds){
    //...
    if ((checkWinRate.back() == winRateA) && numOfRounds == 29)
        makeMove = methodA();
        numOfRounds = 0;
    //...
    else{
        /*You need to choose a method here, my question is, will it
        break your original logic? I would choose randomly.*/
        numOfRounds++;
    }
    return makeMove;
}