Beginner problems....

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
Hey.

I need some help when it comes to my code, its not meant to have a "main" method.
It is basically a chess program that takes values and compares them, checks players and how much they score.

This is what i need to do :

We organize a chess tournament in the local club. The members are divided into
"regular" players and grandmasters.
Write a class ChessTournament (line 33) that has:
Two private lists containing each class above (row 8, row 20)
Stuck here == > A suitable constructor
A Clear () method that removes all elements in both lists
A NumberPlayer () method that returns the number of players in the tournament
Two methods for adding a new player to the correct list
Stuck here == > A Maximum Score () method that returns the highest score any player has.
If all players have zero points, return zero instead.

(b) It's a bit clumsy to have two separate lists just because we have two different player classes.
Redesign the player classes, possibly with a third class or interface, to make it easier the handling. Write an updated version of ChessTournament that uses this.'

This is how far i've gotten:

Chess, NUnit testing:
 class Program
    {
        class NormalPlayer
        {
            public string Name;
            public int Score;
            public int Handicap;
            public NormalPlayer(string n, int h)
            {
                Name = n;
                Score = 0;
                Handicap = h;
            }
        }
        class Grandmaster
        {
            public string Name;
            public int Score;
            public string Country;
            public Grandmaster(string n, string c)
            {
                Name = n;
                Score = 0;
                Country = c;
            }
        }

        class ChessTournament
        {
            /// <summary>
            /// List for "NP = Normal players" and "GM = Grandmaster players".
            /// </summary>
           public List<NormalPlayer> NP = new List<NormalPlayer>();
           public List<Grandmaster> GM = new List<Grandmaster>();

            /// <summary>
            /// Add normal players to the list.
            /// </summary>
            /// <param name="normalPlayer"></param>
            public void AddNP(NormalPlayer normalPlayer)
            {
                NP.Add(normalPlayer);
            }
            /// <summary>
            /// Add grand master players to the list.
            /// </summary>
            /// <param name="grandmaster"></param>
            public void AddGM(Grandmaster grandmaster)
            {
                GM.Add(grandmaster);
            }

            /// <summary>
            /// Stuck here!
            /// </summary>
            /// <param name="score"></param>
            /// <returns></returns>
            public string HighestScore(int score)
            {
              
            }
            /// <summary>
            /// Write the total amount of players in the game.
            /// </summary>
            /// <param name="players"></param>
            /// <returns></returns>
            public int TotalPlayers(int players)
            {
                players = GM.Count + NP.Count;
                return players;
            }

            public void Clean()
            {
                NP.Clear();
                GM.Clear();
            }
        }

How should i proceed to make this all work?

Regards, :)
 
Last edited:
Implement the pseudo code would be your fastest route to getting some working code.
 
Well all i am going to do is testing it in the end. And trying to implement what you wrote earlier is almost more difficult to put into my code than LinQ is.
All i need to do right now is to check so that i can try stuff in my test class. At least be able to check other methods to see if they work with the lists because i am more afraid of Testing stuff and it not working than having one method that doesn't do the right job. I am going to fix this method but i really want to get some testing done first.
 
LOL! If your teacher is forcing you guys to do Test Driven Development, then you were supposed to write the tests first, then the code to make the tests succeed afterwards.

Anyway here's a freebie:
C#:
[TestMethod]
public void CanHandleAllGrandmasters()
{
    // Arrange
    var tournament = new ChessTournament();
    tournament.AddGM(new Grandmaster("Karpov", "USSR"));
    tournament.AddGM(new Grandmaster("Fischer", "US"));

    // Act
    var highScore = tournament.HighestScore(1974);

    // Assert
    int expected = 0;
    Assert.AreEqual(0, highScore, $"High score of {expected} expected from list of GMs with no scores.");
}
 
@Skydiver Yeah... its weird because we were taught to begin with tests first but... this assignment isn't following that lol.
Thanks.... now my problem is that my program doesn't want to run the tests... nice.
Also i did as you said before and deleted the "int = k" on the "HighestScore" method because it doesn't do anything so the only score we get is from the lists.
 
So now i want to create another class with some better values that i later want to test aswell.

This is how far I've gotten, quesiton: Should i use get/set instead of "this.Rank = rank" etc?

Updated:
 class UpdatedChess
    {
        class Player
        {
            public string Name;
            public int Score;
            public int Rating;
            public string Rank;
            public Player(string name, int score, int rating, string rank)
            {
                this.Rating = rating;
                this.Score = score;
                this.Name = name;
                this.Rank = rank;

            }
        }


        class UpdatedChessTournament
        {
            public List<Player> Players = new List<Player>();
            public void AddPlayer(Player P)
            {
                Players.Add(P);
            }

            public int TotalPlayers()
            {
                int player;
                player = Players.Count;
                return player;
            }

            public void Ranking(Player P)
            {
                foreach(var item in Players)
                {
                    if(P.Score > 500)
                    {
                    P.Rank = "Bronze";
                    }
                    if(P.Score > 1000)
                    {
                        P.Rank = "Silver";
                    }
                    if(P.Score > 2999)
                    {
                        P.Rank = "GrandMaster";
                    }
                }
            }
        }
    }
 
You have to right click on the project and select Run tests... :)
 

Latest posts

Back
Top Bottom