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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
What do you mean by:
Stuck here == > A suitable constructor
?

To me, it looks like you have two perfectly good constructors.

What does have me a bit worried is that your class has all those public member variables. If they truly must be public and the variables can be set to anything, then you don't even need the constructor that takes parameters. Just use a constructor that has no parameters and initialize the member variables. On the other hand, if the class members must be set to particular values at instantiation time, and the values cannot be changed arbitrarily without some kind of validation, then keep your current constructors, but also consider changing the public members into properties and implement getters and/or setters with validation.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Stuck here == > A Maximum Score () method that returns the highest score any player has.
That's because you wrote your method signature as:
C#:
public string MaximumScore(int score)

Consider how much easier it would be if you wrote your method signature as:
C#:
public int MaximumScore()
 
Last edited:

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
Would this be good enough to compare which has the highest score?
I am not sure how to say which specific player that had the highest score tho.

HighestScore:
 public int HighestScore(int k)
            {
                var item = NP.Max(x => x.Score);
                var item2 = GM.Max(y => y.Score);
                if(item > item2)
                {
                    return item;
                }
                if(item2 > item)
                {
                    return item2;
                }
                else if(item == item2)
                {
                    return 0;
                }

                return 0;

            }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Why do you need to return the player? All that was asked for was the highest score any player may have.
 

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
Well it says "A Maximum Score () method that returns the highest score any player has." So i need to return it.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Any score. A score is an integer. It is not asking which player has that highest score.

Now if the question were "A high score method that returns the highest scoring player", then you would have to find that player.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
No. If the highest score out of the grandmasters is equal to the highest score from the non-ranked players, then that is still the highest score regardless. For some reason you are returning 0 in that case.
 

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
Well now it should return the highest in both cases. And i return 0 if both are less than 1.

Update-HighestScore:
            public int HighestScore(int k)
            {
                var item = NP.Max(x => x.Score);
                var item2 = GM.Max(y => y.Score);
                if(item > item2)
                {
                    return item;
                }
                if(item2 > item)
                {
                    return item2;
                }
                if(item < 1 && item2 < 1)
                {
                    return 0;
                }
                else if(item == item2)
                {
                    return item & item2;
                }

                return 0;
            }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Lines 2 or 3 will throw an exception if either of the lists are empty.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
I think part of the issue here is that as you said in your title this a beginner level problem, but you are using an advanced level solution by using the LINQ Max() extension method to try to solve it.

If you stuck to the basics, the solution is very easy. Here's the pseudo code:
C#:
maxScore = int.MinValue

for each item in the grand masters list
    if item.Score > maxScore
        maxScore = item.Score

for each item in the unranked players list
    if item.Score > maxScore
        maxScore = item.Score

return maxScore

Take time to study the pseudo code. Step through it with pen and paper to see how it will find the highest score in either of the lists. Consider how it deals with the cases when one list or both lists are empty.
 
Last edited:

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
I have added an ex for it to check if it works or not.
And i've read your pseudo code and i understand it.


My main problem now is to try to test all of this in my test class.

How should i proceed? My test class is at the end of the code!

TestClass:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework.Internal;
using System.Collections.Generic;
using System.Linq;

namespace Uppgift_6
{
    class Program
    {
        /// <summary>
        /// A class for normal players.
        /// </summary>
        class NormalPlayer
        {
            /// <summary>
            /// Variables for the class.
            /// </summary>
            public string Name;
            public int Score;
            public int Handicap;
            /// <summary>
            /// Information about the player.
            /// </summary>
            /// <param Name="n"></param>
            /// <param Rating to reach grandmaster="h"></param>
            public NormalPlayer(string n, int h)
            {
                Name = n;
                Score = 0;
                Handicap = h;
            }
        }
        /// <summary>
        /// A class for grandmaster players.
        /// </summary>
        class Grandmaster
        {
            /// <summary>
            /// Variables for the class.
            /// </summary>
            public string Name;
            public int Score;
            public string Country;
            /// <summary>
            /// Information about the player.
            /// </summary>
            /// <param Name="n"></param>
            /// <param Country="c"></param>
            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>
            private List<NormalPlayer> NP = new List<NormalPlayer>();
            private 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>
            /// Testing to see which is higher.
            /// </summary>
            /// <param name="score"></param>
            /// <returns></returns>
            public int HighestScore(int k)
            {
                //This is to check before pushing scores to see if there is people or scores in them at all!
                try
                {
                    var Test1 = NP.Max(x => x.Score);
                    var Test2 = GM.Max(y => y.Score);
                }
                catch
                {
                    throw Exception("One, or both, lists of players have no scores.");
                }

                var item = NP.Max(x => x.Score);
                var item2 = GM.Max(y => y.Score);

                if(item > item2)
                {
                    return item;
                }
                if(item2 > item)
                {
                    return item2;
                }
                if(item < 1 && item2 < 1)
                {
                    return 0;
                }
                else if(item == item2)
                {
                    return item & item2;
                }

                return 0;
            }
            /// <summary>
            /// Throw ex at lists if one or both are empty.
            /// </summary>
            /// <param Ex="v"></param>
            /// <returns></returns>
            private System.Exception Exception(string v)
            {
                throw new System.NotImplementedException();
            }

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

            /// <summary>
            /// Clear both lists of all players.
            /// </summary>
            public void Clear()
            {
                NP.Clear();
                GM.Clear();
            }
        }


        [TestClass]
        public class Uppgift6
        {
            [TestMethod]
            public void Uppg()
            {

            }
        }
    }
}
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
That's terrible. It's okay for one of both lists to be empty. Recall that a chess tournament can have all grandmasters, or all unranked players, or in the degenerate case, nobody shows up for a tournament due to a boycott, natural disaster, or some emergency. Therefore, if one of the lists empty, just get the highest score from the other list. If both lists are empty, then the highest score is the lowest possible score that could be given -- it could either be zero or int.MinValue.

I highly suggest abandoning using the Max() LINQ extension method. It is not helping you learn the basics of programming. Instead it is leading you down a rabbit hole. Or if you must insist of using the LINQ extension methods, then also use the Concat() and potentially the Count() or the FirstOrNull() as well.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Implement the pseudo code would be your fastest route to getting some working code.
 

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
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.");
}
 

mrlowbot

Member
Joined
Apr 30, 2021
Messages
11
Programming Experience
Beginner
@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.
 
Top Bottom