Click here to Skip to main content
15,918,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i have one problem left whit this program as i see it now. The program should do: Once a player has won , his name appears on the screen, after which all his dart printed for how he threw . Each series of three dart to appear so that you understand how each round has been thrown.

My program shows the name but not more.
C#
if (name.Sum >= 501)
  {
   winner = name;
   Console.WriteLine("\nVinnare är: " + winner.Name + " med " +"????" );
   break;
   }
This is the part of the code that i have problem whit ???? should be somthing like a list or somthing
and the code line is in the mid of class Game.
Anyone now this problem?
Sorry for all the code.





C#
namespace _501
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Title = "###### Nicklas Dart Räknare ######";
            Game game = new Game();
            game.PlayGame();

        }
    }

    //***************************************************************************

    class Game
    {
        
        private List<player> players = new List<player>();
       
        public void AddPlayer(string name)
        {
            Player person = new Player(name);
            players.Add(person);
        }

        public void PlayGame()
        {
            
            int a1 = 0;
            int a2 = 0;
            int a3 = 0;
            Arrows arrow = new Arrows(a1, a2, a3);
            int s1 = 0;
            Player score = new Player(s1);

            try
            {
                Console.Write("Hur många spelare? ");
                int numberOfPlayers = Convert.ToInt32(Console.ReadLine());
                Player winner = null;
                

                for (int i = 0; i < numberOfPlayers; i++)
                {
                    Console.Write("Skriv in spelarens namn: ");
                    string playerName = Console.ReadLine();
                    AddPlayer(playerName);
                }

                while (players.Max((a) => a.Sum) < 500)
                {
                     
                    foreach (Player name in players)
                    {
                        
                        string str1 = name.Name;
                        string str2 = "Dator";

                        // Computer input
                        if (str1 == str2)
                        {
                            //if (name.Sum < 501)
                            //{
                                Console.WriteLine(name + "ns första poäng (mellan 0-60)");
                                arrow.ArrowOne = Random();
                                Console.WriteLine(arrow.ArrowOne);
                                name.Sum += arrow.ArrowOne;
                               
                                Console.WriteLine(name + "ns andra poäng (mellan 0-60)");
                                arrow.ArrowTwo = Random();
                                Console.WriteLine(arrow.ArrowTwo);
                                name.Sum += arrow.ArrowTwo;
                                
                                Console.WriteLine(name + "ns tredje poäng (mellan 0-60)");
                                arrow.ArrowThree = Random();
                                Console.WriteLine(arrow.ArrowThree);
                                name.Sum += arrow.ArrowThree;
                                
                                if (name.Sum >= 501)
                                {
                                    
                                    winner = name;
                                    Console.WriteLine("Vinnare är: " + winner.Name +" med "+ name.Sum);
                                    break;
                                }
                                
                            int sum = arrow.GetScore();
                            Console.WriteLine("\n" + name + "ns totala summan av all tre kast blev: " + sum.ToString());
                            Console.WriteLine(name + "ns totala summan av all kast hitils: " + name.Sum+"\n");
                            
                        }

                            // Humen input
                        else
                        {
                           // if (name.Sum < 501)
                            //{
                            Console.WriteLine(name + " första poäng (mellan 0-60)");
                                arrow.ArrowOne = Int32.Parse(Console.ReadLine());
                               
                                if ((arrow.ArrowOne > 0) && (arrow.ArrowOne < 60))
                                {
                                    name.Sum += arrow.ArrowOne;
                                }
                                else
                                {
                                    Console.WriteLine("Värdet var inte mellan 0-60 och blir då 0");
                                    arrow.ArrowOne = 0;
                                    name.Sum += arrow.ArrowOne;
                                }

                            
                                Console.WriteLine(name + " andra poäng (mellan 0-60)");
                                arrow.ArrowTwo = Int32.Parse(Console.ReadLine());
                                
                                if ((arrow.ArrowTwo > 0) && (arrow.ArrowTwo < 60))
                                {
                                name.Sum += arrow.ArrowTwo;
                                }
                                else
                                {
                                    Console.WriteLine("Värdet var inte mellan 0-60 och blir då 0");
                                    arrow.ArrowTwo = 0;
                                    name.Sum += arrow.ArrowTwo;
                                }
                           
                                Console.WriteLine(name + " tredje poäng (mellan 0-60)");
                                arrow.ArrowThree = Int32.Parse(Console.ReadLine());
                                if ((arrow.ArrowThree > 0) && (arrow.ArrowThree < 60))
                                {
                                name.Sum += arrow.ArrowThree;
                                }
                                else
                                {
                                    Console.WriteLine("Värdet var inte mellan 0-60 och blir då 0");
                                    arrow.ArrowThree = 0;
                                    name.Sum += arrow.ArrowThree;
                                }
                            
                            if (name.Sum >= 501)
                            {
                                winner = name;
                                Console.WriteLine("\nVinnare är: " + winner.Name + " med " +"????" );
                                break;
                            }

                            int sum = arrow.GetScore();
                            Console.WriteLine("\n" + name + " totala summan av all tre kast blev: " + sum.ToString());
                            Console.WriteLine(name + " totala summan av all kast hitils: " + name.Sum + "\n");
                            
                        }
                        
                    }

                    if (winner != null)

                        //Console.WriteLine("\nVinnare är: " + winner.Name + " med " + arrowList);
                        Console.ReadKey();
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Var snäll och skriv in ett nummer");
                Console.ReadKey();

            }
            catch (OverflowException)
            {
                Console.WriteLine("Bara nummer mellan {0} och {1}", Int32.MinValue, Int32.MaxValue);
                Console.ReadKey();
            }
           
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message+" troligen valt 0 spelare");
                Console.ReadKey();
            }

       }
        //Random for Dator
        public int Random()
        {
          Random random = new Random();
          int randomNumber = random.Next(0, 60);
          System.Threading.Thread.Sleep(10);
          return randomNumber;
         }
    }
    
    //***************************************************************************

    class Player
    {
        public int Sum { get; set; }
        public string Name { get; set; }
        
        public List<arrows> arrowList = new List<arrows>();
        
        public Player(string name = "")
        {
            Name = name;
            
        }
        public Player(int sum = 0)
        {
            Sum = sum;
        }
        public void AddArrows(Arrows arrows)
        {
            arrowList.Add(arrows);
        }

        public int CalculatePoints()
        {

            foreach (Arrows arrow in arrowList)
                arrow.GetScore();

            return Sum;
        }

       public override string ToString()
        {
            return string.Format("{0}", Name);
        }
    }

    //***************************************************************************

    class Arrows
    {
        private int arrowOne;
        private int arrowTwo;
        private int arrowThree;
       
        public int ArrowOne 
        { 
            get { return arrowOne; }
            set { arrowOne = value;} 
        
            }
        public int ArrowTwo
        {
            get { return arrowTwo; }
            set { arrowTwo = value; }
        }
        public int ArrowThree
        {
            get { return arrowThree; }
            set { arrowThree = value; }

        }
        public Arrows(int arrowOne, int arrowTwo, int arrowThree )
        {
            ArrowOne = arrowOne;
            ArrowTwo = arrowTwo;
            ArrowThree = arrowThree;

        }

        public int GetScore()
        {
            
         return  arrowOne + arrowTwo + arrowThree;
        }
       
        public override string ToString()
        {
        return string.Format("Din totala summa är {0}",GetScore());
        }
    }   
}


What I have tried:

I have tried to get the arrowList values
I have been searching on google to find something similar problem
Posted
Updated 5-Apr-16 3:26am
v2
Comments
Ronnie Kong 7-Apr-16 12:32pm    
It's generally accepted that you will mark a solution as the answer if it answers your question.

1 solution

For a temporary availability of this information you could us an array or list.

I did not type this out in VS and I don't usually write console apps, so this may be riddled with bugs, but it should serve to give you some ideas...

C#
class Program
    {
        static void Main(string[] args)
        {
 
            Console.Title = "###### Nicklas Dart Räknare ######";

List<player> players = new List<players>();

Boolean cont = true;
while(cont)
{
String selection = Console.ReadLine();
switch(selection)
{
case "1": // play game

            Game game = new Game();
// notice... for this to work this way you will need to return a player.
// There are plenty of other ways to make it work though.
            players.add(game.PlayGame());
foreach(Player player in players)
{
Console.WriteLine(player.name);
Console.WriteLine(player.score);
}
break;
case "2" // exit
cont = false;
// do fancy exit stuff here
break;
default:
Console.WriteLine("Invalid Input");
break;
}
 
        }
    }</players></player>


For a perminent limited length list, you could use the Settings like Properties.Settings.Default.yourVariableHere... I usually use this for user options and connection strings.

Of course you could experiment with serialization...

For a perminent huge length list you might want to use a database.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900