Hello... This is my version of the Chapter 18 CLASS exercise about throwing and popping a bunch of balls.
: NOTES : Random()
For the most part as I worked through the exercise things became clear but I am still confused about "scope" or w/e it is called that determines when a variable is available to be read by other sections of the code. I wanted to use Random(); to build random numbers on the fly, but it seemed that I was either calling the random very quickly in succession, so all the results were identical, or I wasn't able to access the random object I created when I wanted to.
I got around this problem by creating a custom random class called RAN. This initiated Random() during the creation of the class in the main program.. so that only happened once. Then it had functions to produce the random numbers I wanted, in the type I wanted.
Also I found the code to be really ugly and hard to read if I just used Random(). I was using Bytes for my 0-255 colour codes.. and as far as I know Random() is returning only floats.. so every time I had to use Random() object I had to use Convert()... this lead to it being a mess. So I build into the RAN class in a way to choose the types for me to clean this up a little.
This is only using Random() to for loop 5-50 times and make a list of balls with a size of 1-25
Random rand = new Random();
List<Ball> balls = new List<Ball>();
for (int i = 0; i < Convert.ToInt32(rand.Next(5, 50)); i++)
{
balls.Add(new Ball(Convert.ToInt32(rand.Next(1, 25))));
}
The Colour class that was using bytes was even worse.
balls[1].colour.red = Convert.ToByte(rand.Next(1, 255));
balls[1].colour.green = Convert.ToByte(rand.Next(1, 255));
balls[1].colour.blue = Convert.ToByte(rand.Next(1, 255));
And the BALL class was every worse worse!!
Ball ball = new Ball(Convert.ToByte(rand.Next(1, 255)), Convert.ToByte(rand.Next(1, 255)), Convert.ToByte(rand.Next(1, 255)), Convert.ToInt32(rand.Next(1, 25));
I found it really strange that using Random() to get specific types would cause such ugly and bloated looking code. So I ended up making my own Random Class called Ran(). This would change the above code to....
Ran Ran = new Ran();
List<Ball> balls = new List<Ball>();
for (int i = 0; i < Ran.Int(5, 50); i++)
{
balls.Add(new Ball(Ran.Int(1, 25)));
}
balls[1].colour.red = Ran.Byte();
balls[1].colour.green = Ran.Byte();
balls[1].colour.blue = Ran.Byte();
Ball ball = new Ball(Ran.Byte(), Ran.Byte(), Ran.Byte(), Ran.Int(1, 25));
Which seems to me to be a zillion times easier to read.
I has occurred to me now, while writing these notes on the lesson that I probably should have put the conversions into the BALL and COLOUR class themselves.. so it always takes a float in the constructor but stores it as a Byte.. or something like that.. but even doing that would change all the stuff like so....
Ran.Byte() -> random.Next(0,255)
Ran.Int(1,25) -> ranomd.Next(1,25)
I dunno.. even this looks worse than my custom thing I guess... it seems that there is something I may be missing here... as it seems ugly and hard to write. Maybe there is a better way to handle conversions instead of Convert.ToInt32 or w/e
: NOTES : Code Comments
I really need to start looking into making proper notes into my code. There should be a way using the ///sumemry type stuff to add ways to have VS use intense to show me what the members are expecting as I go. I will make a effort to comment more in my next one, but thought I would level this entire project with no comments.. just to see how little I understand it when I next look at it.
C# Players Guide dose cover comments but after reading it a few times it is still confusing.. I need to source google page on this before next time. - THING TO DO.
: NOTES : Possible Error
I think there is a error in the main program, not a fatal one.. but I believe now I am going though it for my notes that I should have used <= instead of < for the for loops. As I am counting from 1 and not from 0. I will not bother fixing this.. but it is something to keep in mind. The 0 start in the Arrays and Lists is a little hard to remember. I think it is not an error though as I am using +1 in the readouts... but I am done for now and will not be checking or changing anything lol. It is time to move on.
: CODE :
: EXAMPLE OUTPUT :
Quote
Lets create some balls!
We have just created : 12 : balls!
Lets throw those balls around, up to a dozen times each.
Ball 1 has been thrown 3 times.
Ball 2 has been thrown 2 times.
Ball 3 has been thrown 6 times.
Ball 4 has been thrown 2 times.
Ball 5 has been thrown 6 times.
Ball 6 has been thrown 3 times.
Ball 7 has been thrown 2 times.
Ball 8 has been thrown 4 times.
Ball 9 has been thrown 2 times.
Ball 10 has been thrown 3 times.
Ball 11 has been thrown 3 times.
Ball 12 has been thrown 4 times.
Now lets Pop at least 5 balls but no more then 10!
Ball 1 has been POPPED!!
Ball 2 has been POPPED!!
Ball 7 has been POPPED!!
Ball 8 has been POPPED!!
Ball 10 has been POPPED!!
Ball 12 has been POPPED!!
Now Lets throw the remaining balls around at most a three dozen more times
Now Ball 3 has been thrown 11 times.
Now Ball 4 has been thrown 7 times.
Now Ball 5 has been thrown 16 times.
Now Ball 6 has been thrown 5 times.
Now Ball 9 has been thrown 9 times.
Now Ball 11 has been thrown 9 times.*******************
Final Results
*******************We had 12 balls to play with.
The 6 Popped balls were Thrown a total of : 18 times
The 6 unPopped balls were Thrown a total of : 57 times
In total all the balls were Thrown a total of : 75 times: Thrown Values for each POPPED ball :
Ball 1 has been thrown 3 times.
Ball 2 has been thrown 2 times.
Ball 7 has been thrown 2 times.
Ball 8 has been thrown 4 times.
Ball 10 has been thrown 3 times.
Ball 12 has been thrown 4 times.: Thrown Values for each UNPOPPED ball :
Ball 3 has been thrown 11 times.
Ball 4 has been thrown 7 times.
Ball 5 has been thrown 16 times.
Ball 6 has been thrown 5 times.
Ball 9 has been thrown 9 times.
Ball 11 has been thrown 9 times.Press Any Key to Exit
Well, see ya next time!