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);
        }
    }
}
 
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.
The specific question was how to change the score for a player so there needs to be a way to modify that at least. In actual fact, I'd probably say that the constructor should only take a name as the score should probably default to zero. The name might be read-only as there probably shouldn't be a need to change the player name mid-game.
There also shouldn't be any semicolon after the get setters access block
That's what I get for editing directly in the site instead of VS. :rolleyes:
 
Old habits John, they'll always catch you out. ;)

I understood the question. I just thought you would have went the extra bit to implement it a little differently so that the class would implement only what is needed, and also make it strictly typed. Actually correcting myself, and yourself now. I think that the score shouldn't be stored in the player class either, as this also defeats one of the class building laws in C#. Don't exceed the purpose of what a class is meant to do, so the score should actually be handled in a separate class. (y)

I will be back soon hopefully. Speak to you soon!
 
In this context, I don't have an issue with that class. If you were talking about a league or tournament, where a player was a person who would play in multiple games then I can see what you're saying. In this context though, a player only exists for the duration of a single game so the score being part of that entity makes sense. In a more complex application, you would have a property of a type that represented the person rather than just a string for the name but you'd still have something that identified the person coupled with their score.
 
This assignment i got says i need to have all fields and lists set to private if i want any higher grade than just to pass, i really cant tell you why yet but i hope there is a reason.. :LOL: No but really i think its so we learn to take orders and build programs exactly how our boss/customer wants it, but some of these projects have been really dumb.. I read the assignment again rn and realized i messed this up. The class Player acually don´t need to contain the points field but is supposed to hold a <List>Turns, list of the Turns class that is... BUT i can store the points however i want, but the way i came up with is this , tho i cant figure it out i cant think of a better way either :confused:
 
This assignment i got says i need to have all fields and lists set to private if i want any higher grade than just to pass, i really cant tell you why yet but i hope there is a reason..
There is a reason. They're getting you to do it like this so you learn how scoping works. As I already explained above dartgame - and pointed out to @jmcilhinney, that there is no reason for the properties to be public and you should be doing it as I explained; with private properties. John is choosing to ignore the C# scoping guidelines, and the C# classes Single Responsibility Purpose rule which all C# programmers know and should know to follow. Never over-complicate the complexity of a class, and always use for its SRP. See more on some generic guidelines here : Generic Classes - C# Programming Guide

Further, if you screwed up your instructions when posting this topic initially, there is little point in going any further with this topic.
 
There is a reason. They're getting you to do it like this so you learn how scoping works. As I already explained above dartgame - and pointed out to @jmcilhinney, that there is no reason for the properties to be public and you should be doing it as I explained; with private properties. John is choosing to ignore the C# scoping guidelines, and the C# classes Single Responsibility Purpose rule which all C# programmers know and should know to follow. Never over-complicate the complexity of a class, and always use for its SRP. See more on some generic guidelines here : Generic Classes - C# Programming Guide

Further, if you screwed up your instructions when posting this topic initially, there is little point in going any further with this topic.
Well thats not true i still have to finish it and even if i wont get the highest grade since i messed that up i still have a chance to make a good program that will pass the assignment.
 
but is supposed to hold a <List>Turns, list of the Turns class that is... BUT i can store the points however i want, but the way i came up with is this , tho i cant figure it out i cant think of a better way either
You didn't come up with that statement unless you wanted to change the direction of your topic. Which isn't allowed due to the confusion it can cause.

Try doing something like this, but you should note, that
  1. I rushed it, as I don't have a lot of free time.
  2. There is lots of room for editing it and improving it.
  3. There may also be bugs.
  4. I won't be able to help you further with it, due to personal work loads.
  5. You still have some work to do like adding methods to see who's turn it is, and to get it running the way you want.
Code :
C#:
    public class Turns
    {
        List<int> Whos_Turn = new List<int>();
        public bool persons_Turn { get; set; }
        /* Implement method to check who's turn it is. */
    }
    public class Player
    {
        private static readonly Dictionary<string, string> Player_Info = new Dictionary<string, string>();
        internal protected string Name { get; set; }
        public enum Players : int
        {
            Player1 = 1,
            Player2 = 2
        }
        public void Create_Player(string player_Name)
        {
            if (Player_Info.Count == 0)
            {
                Player_Info.Add(Players.Player1.ToString(), player_Name);
                Console.WriteLine($"{Players.Player1.ToString()} \"{player_Name}\" was created.");
            }
            else if (Player_Info.Count == 1)
            {
                Player_Info.Add(Players.Player2.ToString(), player_Name);
                Console.WriteLine($"{Players.Player2.ToString()} \"{player_Name}\" was created.");
            }
        }
        public string Get_Player_Name(string of_Player)
        {
            if (of_Player == Players.Player1.ToString())
            {
                Player_Info.TryGetValue(Players.Player1.ToString(), out string NameOf_Player1);
                return NameOf_Player1;
            }
            else if (of_Player == Players.Player2.ToString())
            {
                Player_Info.TryGetValue(Players.Player2.ToString(), out string NameOf_Player2);
                return NameOf_Player2;
            }
            return string.Empty;
        }
    }
    public class GameScore : Player
    {
        public GameScore(int player1_Score, int player2_Score)
        {
            Player1_Score = player1_Score;
            Player2_Score = player2_Score;
            if (Player1_Score > Player2_Score)
            {
                Name = Get_Player_Name(Players.Player1.ToString());
                Console.WriteLine($"Score {Player1_Score} : {Player2_Score} To {Name}.");
            }
            else if (Player2_Score > Player1_Score)
            {
                Name = Get_Player_Name(Players.Player2.ToString());
                Console.WriteLine($"Score {Player1_Score} : {Player2_Score} To {Name}.");
            }
            else if (Player2_Score.Equals(Player1_Score))
                Console.WriteLine($"Score {Player1_Score} : {Player2_Score} We have a draw.");
        }
        private int Player1_Score { get; set; }
        private int Player2_Score { get; set; }
    }
Remember to debug it, so you can trace and step into the code and alter to your own desires. (If it works), as its wrote freehand and I haven't tested it.

Almost forgot, here is your calling code :
C#:
            Player player = new Player();
            player.Create_Player("John");
            player.Create_Player("Keith");
            GameScore game_Score = new GameScore(2, 0);
 
Back
Top Bottom