在Dart游戏中循环播放c#

时间:2018-03-26 16:51:17

标签: c# while-loop

我是C#的新手,所以请耐心等待。我正在努力创造一款非常简单的飞镖游戏。用户首先选择玩家数量,然后输入玩家名称,然后开始投掷飞镖。每位玩家3支飞镖,每支飞镖最多20分。第一个到301分赢得比赛。我遇到的问题是,在所有玩家合计的总金额为301之后,我的while循环中断。当其中一名球员得到301分时,我希望它能打破。我的代码出了什么问题?我只是在寻找指导。

谢谢!

class Program
{
    static void Main(string[] args)
    {
        Game My_Game = new Game();
        My_Game.Playgame();

        Console.Write("Press any key to continue . . .");
        Console.ReadKey(true);
    }
}

class Game

{
    private List<Player> playerlist = new List<Player>();
    private List<int> total_Score = new List<int>();

    public void Playgame()
    {
        int turn1;
        int turn2;
        int turn3;


        Console.WriteLine("Welcome to the awesome Dart game !");

        Console.WriteLine("\nThe rules for this game are simple. Each player gets 3 darts to throw per round. \nFor every dart thrown you can get 0-20 points. First one to 301 points wins ! ");
        Console.WriteLine("\nPress Enter key to begin . . .\n");
        Console.ReadKey();

        Console.WriteLine("How many players are you ?");
        int numberofPlayers = Convert.ToInt32(Console.ReadLine());


        for (int i = 0; i < numberofPlayers; i++)
        {
            Console.WriteLine("Enter player name : ");
            playerlist.Add(new Player(Console.ReadLine()));
        }
        int totalScore = 0;

        while (totalScore <= 301)
        {

            foreach (var player in playerlist)
            {
                Console.WriteLine("Enter player score for {0} on turn one : ", player);
                turn1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter player score for {0} on turn two : ", player);
                turn2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter player score for {0} on turn three : ", player);
                turn3 = Convert.ToInt32(Console.ReadLine());
                player.addTurn(turn1, turn2, turn3);

                totalScore = turn1 + turn2 + turn3;
                total_Score.Add(totalScore);
                totalScore = total_Score.Sum(score => Convert.ToInt32(score));
                Console.WriteLine("----------------------------------------");
            }
        } 

        foreach (var player in playerlist)
        {
            player.Print_Turns();
        }

    }
}

class Player
{

    private string PlayerName;
    private List<Turns> playerturns = new List<Turns>();


    public Player(string _playername)
    {
        PlayerName = _playername;
    }

    public void addTurn(int firstTurn, int secondTurn, int thirdTurn)
    {

        playerturns.Add(new Turns(firstTurn, secondTurn, thirdTurn));

    }

    public int total_points()
    {
        int total = 0;
        foreach (var points in playerturns)
        {
            total = total + points.Get_Score();
        }
        return total;
    }

    public void Print_Turns()
    {
        Console.WriteLine("\nPlayerstatistics for {0} : ", PlayerName);
        Console.WriteLine("----------------------------------------");

        foreach (var turn in playerturns)
        {
            Console.WriteLine(turn);
        }
        Console.WriteLine("The total is : {0}", total_points());
    }

    public override string ToString()
    {
        return string.Format(PlayerName);
    }

}

class Turns
{

    private int turnOne;
    private int turnTwo;
    private int turnThree;

    public Turns(int _turnOne, int _turnTwo, int _turnThree)

    {

        turnOne = _turnOne;
        turnTwo = _turnTwo;
        turnThree = _turnThree;

    }


    public int Get_Score()
    {
        int total;
        total = turnOne + turnTwo + turnThree;
        return total;
    }

    public override string ToString()
    {
        return string.Format("First throw was : {0} points, Second throw was : {1} points , Third throw was : {2} points .", turnOne, turnTwo, turnThree);
    }

}

2 个答案:

答案 0 :(得分:0)

你加分的方式只需要工作。你在这些代码行中将所有玩家的总得分加在一起:

library(shiny)
library(xml2)


ui <- fluidPage(
  fileInput("File", "Choose file"),
  tableOutput("Data")
)

server <- function(input, output, session) {
  Data <- eventReactive(input$File, {
    # read_xml(input$File$datapath)
    read_xml("<p>Hello World!</p>")
  })
  
  
  output$Data <- renderTable({
    head(xml_text(Data()))
  })
  
}

shinyApp(ui, server)

total_score是所有玩家分数的列表,因此您不应将其与 totalScore = turn1 + turn2 + turn3; total_Score.Add(totalScore); totalScore = total_Score.Sum(score => Convert.ToInt32(score)); 一起添加,然后将其设置为total_Score.Sum(score => Convert.ToInt32(score));。相反,您需要做的就是将totalScore更改为totalScore = turn1 + turn2 + turn3;。这样做并取出totalScore= player.total_points()中的行将为您提供预期的行为。

答案 1 :(得分:0)

您只声明 totalScore 一次,这是您的循环所依据的条件。你的foreach循环中的总分数实际上是一个“回合”玩家的分数(称之为roundScore)而不是所有回合中每个玩家的总分。

通过逻辑推理,你的foreach循环将为玩家列表中的每个玩家循环,所以你最好的选择是(如Broots建议的那样)为每个玩家添加一个得分属性