dartgame

Netsid

Member
Joined
Feb 10, 2020
Messages
21
Programming Experience
Beginner
Im doing another school project and im kinda stuck.. Im building a pretty simple dartgame and have made a class for creating players with a string for name and int for points. I made a list of that class and i think i managed to get it working this far. this is where im stuck tho. Which would be the best way to roll through the list of players, showing the current player name and points, im thinking im gonna have the player enter their points manually or choose to get 3 random points between 1-20 that are written out before added to their current points and then moves on to the next player. All ideas are welcome im a proper noob!

dartgame:
using System;
using System.Collections.Generic;
using System.Text;

namespace dartboardskiss
{
    class Game
    {
        private List<Player> players = new List<Player>();

        public void AddPlayer()
        {
            Console.WriteLine("Type the name of the player and press enter.");
            string name = Console.ReadLine();
            Player person = new Player(name, 0);
            players.Add(person);
        }
        private int PlayerCounter()                    //method to keep track of players
        {
            int playercounter = 0;
            foreach (Player _player in players)
            {
                if (_player != null)
                    playercounter++;
            }
            return playercounter;
        }


        public Game()
        {
            do
            {
                //switch statement menu
                Console.WriteLine("Welcome to the Dartgame, what would you like to do?");
                Console.WriteLine("1. Add player");
                Console.WriteLine("2. Start the game");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        this.AddPlayer();
                        break;
                    case 2:
                        Console.WriteLine("");
                        break;
                    default:
                        Console.WriteLine("");
                        break;
                }
            } while (true);
        }
    }
    class Player                    //class for creating player                               
    {
        private string name;
        private int points;

        public Player(string name, int points)
        {
            this.name = name;
            this.points = points;
        }
        //Tuple for returning values
        public static implicit operator Player((string nname, int npoints) v)
        {
            return new Player(v.nname, v.npoints);
        }
    }
}
 
If I'm interpreting your question correctly, something like this is all you need:
C#:
private int currentPlayerIndex = 0;
private readonly List<Player> players = new List<Player>();

private Player GetCurrentPlayer()
{
    return players[currentPlayerIndex];
}

private void SelectNextPlayer()
{
    // Increment the index and wrap back to zero when passing the upper bound.
    currentPlayerIndex = (currentPlayerIndex + 1) % players.Count;
}
 
If I'm interpreting your question correctly, something like this is all you need:
C#:
private int currentPlayerIndex = 0;
private readonly List<Player> players = new List<Player>();

private Player GetCurrentPlayer()
{
    return players[currentPlayerIndex];
}

private void SelectNextPlayer()
{
    // Increment the index and wrap back to zero when passing the upper bound.
    currentPlayerIndex = (currentPlayerIndex + 1) % players.Count;
}

Thanks for helping out! im not sure how to implement that tho, i have rewritten the code a bit and it looks like this now.. These are all the classes and methods im forced to work with to pass the school project. line 12-23 is where im at rn trying to type out the current player and points. I also have to figure out how i add points to the current player via the Turns class.

C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace testdartboard
{

    class Game
    {
        private List<Player> players = new List<Player>();

        public List<Player> ReturnPlayer()
        {
            return players;
        }

        public void PlayGame()
        {
                Console.WriteLine("Enter points 1-20 and press enter, to randomize type 21!");
                Console.WriteLine("Currently playing: ");
                Console.WriteLine(players);
                Console.ReadLine();   
        }
        public Game()
        {
            do
            {
                //switch statement menu
                Console.WriteLine("Welcome to the Dartgame, what would you like to do?");
                Console.WriteLine("1. Add player");
                Console.WriteLine("2. Start the game");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        this.AddPlayer();
                        break;
                    case 2:
                        this.PlayGame();
                        break;

                    default:
                        Console.WriteLine("");
                        break;
                }
            } while (true);
        }
        public void AddPlayer()                         //method for adding players
        {
            Console.WriteLine("Type the name of the player and press enter.");
            string name = Console.ReadLine();
            Player person = new Player(name, 0);
            players.Add(person);
        }
        /*      private int PlayerCounter()                    //method to keep track of players
          {
              int playercounter = 0;
              foreach (Player _player in players)
              {
                  if (_player != null)
                      playercounter++;
              }
              return playercounter;
          }*/
    }
    class Player                    //class for creating player                               
    {
        private string name;
        private int points;

        public Player(string name, int points)      //constructor to access class
        {
            this.name = name;
            this.points = points;
        }
    }
    class Program       //runs Game
    {
        static void Main(string[] args)
        {
            Game game = new Game();
        }
    }
    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("Throw #1 : {0}, Throw #2 : {1},  Throw #3 : {2}", turnOne, turnTwo, turnThree);
        }
    }
}
 
You implement it exactly as I showed. You call GetCurrentPlayer when you want to get the current player's name to display or to update their score. When that player's turn is finished you call SelectNextPlayer. GetCurrentPlayer will then return the next player. As suggested in the code, SelectNextPlayer will automatically wrap back to the first player after the last. As long as you add at least one player to the list before calling those methods, you're good to go.
 
You implement it exactly as I showed. You call GetCurrentPlayer when you want to get the current player's name to display or to update their score. When that player's turn is finished you call SelectNextPlayer. GetCurrentPlayer will then return the next player. As suggested in the code, SelectNextPlayer will automatically wrap back to the first player after the last. As long as you add at least one player to the list before calling those methods, you're good to go.
Man im not getting it to work, im not sure if there is some underlying problem before. The only messaage i get in the console is either "namespace.Classname" or "System.Collections.Generic.List`1[testdartboard.Player]" Is should not be a problem making a list like this from a class with an string and an int right? Im sorry i feel like im buggin you :LOL: o_Oo_O
 
The behavior you are seeing is as expecting. The ToString() needs to be overridden by each class implementation if they want to change what is displayed. The default implementation of ToString() is to just return the type information of the class.
 
What exactly do you think this should do?
C#:
Console.WriteLine(players);
players is a List<Player>. What do you think should happen when you write a complex object like that to the console and why? Is that even what you actually want to write at that point?
 
What exactly do you think this should do?
C#:
Console.WriteLine(players);
players is a List<Player>. What do you think should happen when you write a complex object like that to the console and why? Is that even what you actually want to write at that point?
i left Console.WriteLine(players); more as a signal of what i was trying to do, i maybe should have left a comment instead.. I tried it like
Console.WriteLine("Currently playing: {0} ", GetCurrentPlayer());
or Console.WriteLine("Currently playing: {currentPlayerIndex} ", GetCurrentPlayer()); and about 1000 other ways..
 
The behavior you are seeing is as expecting. The ToString() needs to be overridden by each class implementation if they want to change what is displayed. The default implementation of ToString() is to just return the type information of the class.
so i need a method in the targeted class with the outvalue i want to display?
 
i left Console.WriteLine(players); more as a signal of what i was trying to do, i maybe should have left a comment instead.. I tried it like
Console.WriteLine("Currently playing: {0} ", GetCurrentPlayer());
or Console.WriteLine("Currently playing: {currentPlayerIndex} ", GetCurrentPlayer()); and about 1000 other ways..
GetCurrentPlayer returns a Player object. What exactly do you want displayed and why do you think that that should happen automatically? If you want to display the name of that player then get the name and display that. If you want the name and score formatted in a particular way then get those values, format them and display that. If you want that to happen automatically then that functionality has to be inside the Player class. Specifically, inside the ToString method, as already stated. Have you researched the ToString method?
 
so i need a method in the targeted class with the outvalue i want to display?
ToString returns a String representation of the current object. If you don't want to accept the default implementation of that, i.e. the name of the object's type, then you have to provide that implementation yourself, i.e. override the method. If you're still not sure how to proceed, the next step is to open your favourite search engine and type in "c# override tostring".
 
ToString returns a String representation of the current object. If you don't want to accept the default implementation of that, i.e. the name of the object's type, then you have to provide that implementation yourself, i.e. override the method. If you're still not sure how to proceed, the next step is to open your favourite search engine and type in "c# override tostring".
i really didnt know, gotta blame my teacher :LOL: this course im doing is lacking any descent learning material and im really going crazy. Thank you, i think i should be able to figure it out now..Hopefully :ROFLMAO::ROFLMAO:
 
Thank you guys so much for the help but im stuck again and its not the last time for sure :geek::LOL: how would i go about changing the value of points for the current player without changing the name? Actually i cant even figure out how i change them both even? This is where im at RN nyways:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dartboardtostringproblems
{
    class Game
    {
        private List<Player> players = new List<Player>();
        private List<Turns> turn_list = new List<Turns>();      //WILL BE USED TO ADD POINTS LATER
        public void PlayGame()
        {   

            for (int i = 0; i < players.Count; i++)
            {
                Player _name = players[i];
                Console.WriteLine("Enter points 1-20 and press enter! First to 301 wins!!");
                Console.WriteLine(_name);
                Console.ReadLine();
                //int v = int.Parse(Console.ReadLine());            THIS IS WHERE IM TRYING TO CHANGE THE POINTS
                //players[i] = v;
            }
            
        }
        public Game()
        {
            do
            {
                //switch statement menu
                Console.WriteLine("Welcome to the Dartgame, what would you like to do?");
                Console.WriteLine("1. Add player");
                Console.WriteLine("2. Start the game");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        this.AddPlayer();
                        break;
                    case 2:
                        this.PlayGame();
                        break;

                    default:
                        Console.WriteLine("");
                        break;
                }
            } while (true);
        }
        public void AddPlayer()                         //method for adding players
        {
            Console.WriteLine("Type the name of the player and press enter.");
            string name = Console.ReadLine();
            Player person = new Player(name, 0);
            players.Add(person);
        }
    }
    class Player                    //class for creating player                               
    {
        private string name;
        private int points;

        public override string ToString()
        {
            return string.Format("Player: {0} has Points: {1}", this.name, this.points);
        }
        public Player(string Name, int Points)      //constructor to access class
        {
            name = Name;
            points = Points;
            Console.WriteLine("A new player was added.");
        }
    }
    class Program       //runs Game
    {
        static void Main(string[] args)
        {
#pragma warning disable IDE0059 // Unnecessary assignment of a value
            Game game = new Game();
#pragma warning restore IDE0059 // Unnecessary assignment of a value
        }
    }
    class Turns
    {
        private readonly int turnOne;
        private readonly int turnTwo;
        private readonly 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("Throw #1 : {0}, Throw #2 : {1},  Throw #3 : {2}", turnOne, turnTwo, turnThree);
        }
    }
}
 
The reason you're having trouble is because your Player class is poorly implemented. You only have private fields for those values. You should have public properties:
C#:
class Player                    //class for creating player                              
{
    public string Name { get; set; }
    public int Points { get; set; }

    public override string ToString()
    {
        return string.Format("Player: {0} has Points: {1}", this.Name, this.Points);
    }
   
    public Player(string name, int points)      //constructor to access class
    {
        Name = name;
        Points = points;
        Console.WriteLine("A new player was added.");
    }
}
Note that I have used Pascal-casing for the public properties and, as is the most common convention, camel-casing for the (constructor) method parameters. Now you can treat Name and Points like you would any other property, e.g. the Text of a TextBox.

By the way, I would tend to use Score rather than Points.
 
Last edited:
No class outside of the player class needs access to the scope of Name and Points properties if the the OP is passing values to the constructor. From a scoping prospective; it defeats the purpose of them being there. There also shouldn't be any semicolon after the get setters access block and they would be best set private in my opinion. Despite the syntax errors, it'll do the job though. +1
public string Name { get; set; }; public int Points { get; set; };
 
Back
Top Bottom