Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何限制分数?_C#_If Statement - Fatal编程技术网

C# 如何限制分数?

C# 如何限制分数?,c#,if-statement,C#,If Statement,如何限制分数? 玩家和敌人的得分是递增的,每当一个玩家或敌人赢了,我想发生的是谁先达到3分谁就会赢 int playerScore = 0; int enemyScore = 0; private void UpdateScore(bool playerWon) { if (playerWon) { playerScore++; stateLabel.Text = "win"; playerScoreLabel.Text = playerScore.ToStrin

如何限制分数? 玩家和敌人的得分是递增的,每当一个玩家或敌人赢了,我想发生的是谁先达到3分谁就会赢

int playerScore = 0;
int enemyScore = 0;
private void UpdateScore(bool playerWon)
{
  if (playerWon)
  {
    playerScore++;
    stateLabel.Text = "win";
    playerScoreLabel.Text = playerScore.ToString();
  }
  else
  {
    enemyScore++;
    stateLabel.Text = "lose";
    enemyScoreLabel.Text = playerScore.ToString();
  }
}

我知道这不起作用,你需要修改它。也许只是给你一些印象

 int playerScore = 0;
 int enemyScore = 0;
 List<Player> players = new List<Player>();

  private void UpdateScore(bool playerWon)
        {
                if (playerWon)
                {
                    players[Count].score++;
                    stateLabel.Text = "win";
                    playerScoreLabel.Text = playerScore.ToString();
                }
                else
                {
                    players[Count].score++;
                    stateLabel.Text = "lose";
                    enemyScoreLabel.Text = playerScore.ToString();
                }

                CheckPlayerWon();
           }

    private Player CheckPlayerWon()
    {
        foreach(Player play in players)
        {
            if(play.score >= 3)
            {
                // WON
                break;
            }
        }
    }

    private class Player
    {
        string name = string.empty;
        int playerID = 0;
        int score = 0;

        Player()
        {
            //SETUP
        }
    }
int playerScore=0;
int-enemyScore=0;
列表玩家=新列表();
私有void UpdateScore(boolplayerwon)
{
如果(playerWon)
{
球员[计数]。得分++;
stateLabel.Text=“win”;
playerscorelab.Text=playerScore.ToString();
}
其他的
{
球员[计数]。得分++;
stateLabel.Text=“丢失”;
enemyCoreLabel.Text=playerScore.ToString();
}
CheckPlayerWon();
}
私人玩家CheckPlayerWon()
{
foreach(玩家在玩家中玩)
{
如果(play.score>=3)
{
//赢得
打破
}
}
}
私人职业球员
{
string name=string.empty;
int playerID=0;
智力得分=0;
玩家()
{
//设置
}
}

一个简单的方法是执行以下操作。显然,这可能要优雅得多

int playerScore = 0;
int enemyScore = 0;
private GameState UpdateScore(bool playerWon)
{
    const int scoreToReach = 3;

    if (playerWon)
    {
        playerScore++;
        stateLabel.Text = "win";
        playerScoreLabel.Text = playerScore.ToString();

        if (playerScore == scoreToReach)
            return GameState.PlayerWon;
    }
    else
    {
        enemyScore++;
        stateLabel.Text = "lose";
        enemyScoreLabel.Text = playerScore.ToString();

        if (enemyScore == scoreToReach)
            return GameState.EnemyWon;
    }

    return GameState.GameInProgress;
}

public enum GameState
{
    GameInProgress,
    PlayerWon,
    EnemyWon
}

你知道
if
语句吗
if(playerScore>=3 | | enemyScore>=3){//Game Over}
您清楚地知道if语句是如何工作的-当然您可以使用自己的逻辑来确定在何处添加额外的代码然后,在更新分数后,检查其中是否有人达到了3Well,最简单的解决方案是提供一个方法参数,指定哪个玩家赢了。您不需要使用
playerWon
parameter,因为此方法仅在两名玩家中的一人获胜时使用。你只需要知道是哪个玩家。你最好写一条评论,而不是投票否决它。为什么我们要为某人提供如此简单的解决方案?这是基本的东西,没什么特别的。我只是想给他留下印象,让他自己去做。