Advertisement Jump to content
Sign in to follow this  
macho

Grade me?

This topic is 1862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it alright to post code here to be reviewed for advice?  I'm just starting so its nothing complex but I'd like to make sure I'm not making any early mistakes or slipping up.

 

I'm taking the Beginning Game Programming with C# on Coursera (its free and open to all if anyone is interested https://class.coursera.org/gameprogramming-001/class/index) but I started after the class had finished and can't get any feedback on my work.

 

Appreciate the time and attention.  First bit is posted below.  If its fine and/or there is a better place to put this kind of stuff (was thinking a journal?) please let me know.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assessment1Score
{
    /// <summary>
    /// Print a welcome message to the user telling them that this application will calculate their average gold-collecting performance.
    /// Prompt user for the total gold they've collected in game.
    /// Read in total gold collected and put the value into a variable of the appropriate type.
    /// Prompt user for total number of hours they've played.
    /// Read in the total number of hours played and put the value into a variable of the appropriate type.
    /// Convert the hours to minutes and put the value into a variable of the appropriate type.
    /// Calculate gold per minute statistic and put the result into a variable of the appropriate type.
    /// Print out the gold, hours played, and gold per minute statistic.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            int gold;
            float hoursPlayed;
            float minutesPlayed;
            float goldPerMinute;

            Console.WriteLine("Welcome, this program will calculate the average amount of gold you've earned over your adventures!");
            Console.Write("Please input the amount of gold you currently have: ");
            gold = int.Parse(Console.ReadLine());
            Console.Write("Please input the amount of hours you've played: ");
            hoursPlayed = float.Parse(Console.ReadLine());
            minutesPlayed = hoursPlayed * 60;
            goldPerMinute = (float)gold / minutesPlayed;
            Console.WriteLine("Your current amount of gold is: " + gold);
            Console.WriteLine("You have played for " + hoursPlayed + " hours.");
            Console.WriteLine("Your gold per minute score is: " + goldPerMinute);
            Console.WriteLine("Thanks for using this calculation program!");


        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement

I appreciate your feedback.  I'd assume using if statements would solve that, but I haven't made it that far yet.  I worked with them a bit in JavaScript when I took web programming a bit ago.

 

I'm not sure yet what the C# equivalent of NaN is but I'd go with using

if (variable = NaN)
    Console.WriteLine("Not a number.");

or something in that ballpark.  That's all stuff I've barely seen in action though.

 

Again, I really, truly appreciate the feedback!

Share this post


Link to post
Share on other sites

Looks fine to me. 

 

You could also use TryParse instead of just plain Parse. It returns a bool indicating wether the parse was successful. i.e it would return false when trying to parse something thats NaN.

if(!int.TryParse(Console.ReadLine(), out gold))
    Console.WriteLine("Not a number.");
Edited by tharealjohn

Share this post


Link to post
Share on other sites

Ah, that makes sense.  I haven't seen some of those statements yet ( try/catch etc.). But I get the idea.

 

I suppose if I did: 

int gold = 0;
do {
    Console.Write("Please input the amount of gold you currently have: ");
    if ( !Int32.TryParse(Console.ReadLine(),out gold) || gold <= 0 ) {   // use "out" to pass gold by reference; TryParse will fill in this variable
        Console.WriteLine("Please enter an integer");
        continue;
    }
    break;
} while ( true );

That would catch instances of non-integer and negative integer values in one swoop?

 

I appreciate you guys pointing this out to me.  I haven't touched loops or anything but I do grasp the ideas behind them.

 

Edit:  I misread the if statement to testing if true instead of testing if false, changed && >= 0 to || <=0

Edited by macho

Share this post


Link to post
Share on other sites

I appreciate your feedback.  I'd assume using if statements would solve that, but I haven't made it that far yet.  I worked with them a bit in JavaScript when I took web programming a bit ago.

 

I'm not sure yet what the C# equivalent of NaN is but I'd go with using

if (variable = NaN)
    Console.WriteLine("Not a number.");

or something in that ballpark.  That's all stuff I've barely seen in action though.

 

Again, I really, truly appreciate the feedback!

 

Not sure if that was just an example, orif you actually use that code snippet, but if you do, be sure too use "equal to" == instead of "assign operator" =.

 

leftHandValue == rightHandValue "is the left had value equal to the right hand value?"

leftHandValue = rightHandValue "assign the right hand value to the left hand value"

 

you probably know this, but just to make it clear.

Share this post


Link to post
Share on other sites

I wasn't using it but I certainly would have messed that up!  Thank you for the catch and correction, I'll pay attention to it in the future.

 

Edit with new code!  And another to remove a paragraph I copied into the code block..oops.

 

This one was fun and was the first primer to objects and classes.  I feel as though I 70% get the idea.  I'm not quite sure what Members means in regards to a class.  I get that Methods are essentially the actions of an object and properties store data for an object.  That's probably simplifying them a good deal though.  I haven't made any classes or objects yet, so I don't know their inner workings.

 

I'm hoping more exposure to them will clear up the blank spots.

 

This one was fun and was the first primer to objects and classes. I feel as though I 70% get the idea. I'm not quite sure what Members means in regards to a class. I get that Methods are essentially the actions of an object and properties store data for an object. That's probably simplifying them a good deal though. I haven't made any classes or objects yet, so I don't know their inner workings.

I'm hoping more exposure to them will clear up the blank spots.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab4
{
    /// <summary>
    /// Follow the directions given in the comments.
    /// The Deck class is provided.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // create a new deck and print the contents of the deck
            Deck newDeck = new Deck();

            newDeck.Print();

            Console.WriteLine("///////////////////*Shuffle Seperator*\\\\\\\\\\\\\\\\");

            // shuffle the deck and print the contents of the deck

            newDeck.Shuffle();
            newDeck.Print();

            Console.WriteLine("///////////////////*Top Card Seperator*\\\\\\\\\\\\\\\\");

            // take the top card from the deck and print the card rank and suit
            Card topCard = newDeck.TakeTopCard();
            Console.WriteLine("The top card was the " + topCard.Rank + " of " + topCard.Suit);

            // take the top card from the deck and print the card rank and suit
            topCard = newDeck.TakeTopCard();
            Console.WriteLine("The top card was the " + topCard.Rank + " of " + topCard.Suit);


        }
    }
}

Edited by macho

Share this post


Link to post
Share on other sites

I suppose you wouldn't know the difference between a free function and a method as I think C# is purely OOP (I know Java is for example).

 

The word member means to be a part of. In terms of a class, you say "member variable of ClassA" meaning that variable is contained by ClassA. The same thing with functions. A member function, otherwise known as a method, is a function that's a part of that class. It has access to all members of that class and is a means for outside code to work with the data of that class (preferably without exposing it). If I remember correctly, C# does not allow freestanding functions, which means all you get to work with are methods.

 

Methods are special because they add an extra "this" pointer to the argument list that you can't see is there, but perhaps it's best not to confuse things early on biggrin.png

Edited by boogyman19946

Share this post


Link to post
Share on other sites

 

I appreciate your feedback.  I'd assume using if statements would solve that, but I haven't made it that far yet.  I worked with them a bit in JavaScript when I took web programming a bit ago.

 

I'm not sure yet what the C# equivalent of NaN is but I'd go with using

if (variable = NaN)
    Console.WriteLine("Not a number.");

or something in that ballpark.  That's all stuff I've barely seen in action though.

 

Again, I really, truly appreciate the feedback!

 

Not sure if that was just an example, orif you actually use that code snippet, but if you do, be sure too use "equal to" == instead of "assign operator" =.

 

leftHandValue == rightHandValue "is the left had value equal to the right hand value?"

leftHandValue = rightHandValue "assign the right hand value to the left hand value"

 

you probably know this, but just to make it clear.

 

 

C# doesn't allow assignments within comparison blocks.  That code will cause a compilation error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!