简体   繁体   English

更改 C# 列表中玩家的前景色

[英]Changing foreground color for players in a C# list

I am studying a course in programming (C#).我正在学习编程课程(C#)。 Right now I am doing a task where I will create a version of a Dart 301 game.现在我正在做一个任务,我将创建一个 Dart 301 游戏的版本。

I'm basically done with the game but would like to change the text color depending on which player is playing.我基本上完成了游戏,但想根据正在玩的玩家更改文本颜色。 This is not really necessary, it is not listed as something you should do but after I got the idea in my head I can not let go either lol.这并不是真正必要的,它没有被列为您应该做的事情,但是在我脑中有了这个想法之后,我也不能放手,哈哈。 I really just want to know how to, been stuck now trying out different solutions.我真的只是想知道如何做,现在一直在尝试不同的解决方案。

When the game starts, you have to choose how many players to play.游戏开始时,您必须选择要玩的玩家数量。 If it's 1 player, it plays against the computer, which I then solved to add the change in foreground color inside my foreach loop - for example:如果是 1 个玩家,它会与计算机对战,然后我解决了在 foreach 循环中添加前景色的变化 - 例如:

Console.ForegroundColor = ConsoleColor.DarkCyan;

But when there is more than one player in the game I'm stuck.但是当游戏中有不止一个玩家时,我就被卡住了。

Ideally I'd like to assign a color to each player joining the game.理想情况下,我想为每个加入游戏的玩家分配一种颜色。 The player gets added through a AddPlayer method that I've created and then gets stored in a list of players in the Game class.玩家通过我创建的 AddPlayer 方法添加,然后存储在 Game 类中的玩家列表中。 There is also a Player class which holds a list of turns for each player.还有一个 Player 类,其中包含每个玩家的回合列表。

What I've tried so far is到目前为止我尝试过的是

  1. Creating a method for a random color (which is not ideal cause I'd like each player to get assigned a specific color):为随机颜色创建一个方法(这不是理想的,因为我希望每个玩家都被分配一个特定的颜色):
private static ConsoleColor GetRandomColor()
{
    Random randomColor = new Random();
    var consoleColors = Enum.GetValues(typeof(ConsoleColor));
    return (ConsoleColor)consoleColors.GetValue(randomColor.Next(consoleColors.Length));
}
  1. In the foreach loop then changing the color for each player in the list在 foreach 循环中,然后更改列表中每个玩家的颜色
    foreach (var player in ListOfPlayers)
    {
        for (int i =0; i <= ListOfPlayers.Count -1; i++)
        {
            Console.ForegroundColor = GetRandomColor(); 
        }
        
        Console.WriteLine("\n\t{0} it's your turn! What numbers did your arrows hit? " +
                          "\n\tREMEMBER: Each turn includes three throws with a possible score of 1-20 for each arrow. \n", player);
        arrowOne = GetPlayerThrow();
        arrowTwo = GetPlayerThrow();
        arrowThree = GetPlayerThrow();
        player.AddTurn(arrowOne, arrowTwo, arrowThree);
        totalScore = player.CalculatePoints();
    }
  1. I've also tried different types of if and foreach我也尝试过不同类型的 if 和 foreach

Hope my explanation did not get too messy.希望我的解释不会太乱。 I would prefer to assign a color to a specific player in the list.我更愿意为列表中的特定玩家分配一种颜色。

Ex:前任:

private List <Player> ListOfPlayers = new List <Player> ();

List item 0 If this player exists, the text will appear in DarkYellow.列表项 0 如果此播放器存在,则文本将显示为深黄色。

List item 1 If this player exists, the text will appear in DarkCyan.列表项 1 如果此播放器存在,则文本将显示为深青色。

List item 2 If this player exists, the text will appear in DarkMagneta.列表项 2 如果此播放器存在,则文本将显示在 DarkMagneta 中。

List item 3 If this player exists, the text will appear in DarkGreen.列表项 3 如果此播放器存在,则文本将显示为深绿色。

Here's a picture of what I've done when the game is computer vs player:这是当游戏是计算机对玩家时我所做的图片:
When the computer is playing the text is red but when it's the players turn it is blue:当计算机正在播放时,文本是红色的,但当玩家转向时,它是蓝色的:
当电脑播放时,文字是红色的,但当播放器转动时,它是蓝色的

Create a list with the number of desired players and assign to each the desired color with a switch .创建一个包含所需玩家数量的列表,并使用switch为每个玩家分配所需的颜色。 Then start the game.然后开始游戏。 An example below.下面举个例子。

public class Game {
  private List<Player> ListOfPlayers = new List<Player>();
  public Game(int nbplayers) {
     InitPlayers(nbplayers);
  }
  public void InitPlayers(int nbplayers) {
     for(int i=0; i < nbplayers; i++) {
        var player = new Player();
        switch(i) {
           case 0: { player.Color = ConsoleColor.DarkYellow; break;}
           case 1: { player.Color = ConsoleColor.DarkCyan; break;}
           case 2: { player.Color = ConsoleColor.DarkMagneta; break;}
           case 3: { player.Color = ConsoleColor.DarkGreen; break;}
        }
        ListOfPlayers.Add(player); 
     }
  }
  public void StartGame() {
     while(!ListOfPlayers.Any(p => p.TotalScore > 100) {
        StartTurn();
     }
     EndGame();
  }

  public void StartTurn() {
    //each player throw
    foreach(var player in ListOfPlayers) {
       player.Play();
    }
    //calculate each player total score
    ...
    
  }

  public void EndGame() {
     var winner;
  }
}

public class Player {
   private Random = new Random();
   public ConsoleColor Color {get; set;}
   public List<int> Throw = new List<int>();
   public int TotalScope { get; set; } = 0;
   public void Play() {
      Throw.Clear();
      for(int i=0; i < 3; i++) {
        Throw.Add(random.Next(1, 20));
      }
   }
}

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

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