7 Replies - 3408 Views - Last Post: 07 January 2011 - 12:05 PM Rate Topic: -----

#1 Guest_Muzza*


Reputation:

Classes in a console game (c#)

Posted 07 January 2011 - 07:30 AM

Im currently making a game for a project for uni, I have created a Rock,Paper,Scissors game using if statements however my project requires me to use classes. Can some explain what classes do and how to use them.
Is This A Good Question/Topic? 0

Replies To: Classes in a console game (c#)

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Classes in a console game (c#)

Posted 07 January 2011 - 07:43 AM

Classes are basically a way for you to create new data types. You create a shell of what that type of object should be and the actions that the type of object is allowed to perform. Once you create this "outline" of what that type of object is and can do, you'll then create instances of that object that you will then manipulate throughout your code to produce the desired effects.

Let's say we're working on an RPG. One of the most obvious things that you'll need is a character, most RPG's have multiple characters, but instead of creating a new class for each character, you'd just create one class that defines what exactly a character is (variables and other data types to denote name, position, stats, etc.) and does (methods for performing actions with this character based on some sort of input, whether that input be from triggered events in a game or from direct player input using a keyboard, mouse, gamepad, or any other type of input device). Using the instances of this class, you'll have multiple characters that are all at the most basic level, very similar. Every character is going to have a name, position, health, strength, etc. Each character's stats might differ, but the instances of those characters will allow you to decide which character has higher health, or choose the name for each character.

I'm kind of rambling on now, so I'll end the post here. If you want or need me to go into more detail, or have any other questions, feel free to ask and I'll answer them to the best of my ability and hopefully not go off on too many tangents in the process.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Muzza*


Reputation:

Re: Classes in a console game (c#)

Posted 07 January 2011 - 08:00 AM

Thanks for the response, I understand classes now however cant see where I can use them in my game due to the player only needing to enter 1,2 or 3. Any suggestions would be grateful?
Was This Post Helpful? 0

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Classes in a console game (c#)

Posted 07 January 2011 - 08:15 AM

I can't really see a reason to use classes here. I'm really not quite sure why your assignment is requiring it. Hopefully someone else here will have an idea or two for you.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Classes in a console game (c#)

Posted 07 January 2011 - 08:26 AM

Not worrying about why you would LOGICALLY choose to use classes for this... This being homework to learn the language the reason for using them becomes clear: To learn how to use them in a simple need.

You make a class called Possibility.
From that you inherit a Rock class, a Scissors class, a paper class.
The Possibility class has a list, 2 elements long of the classes this Possibility can beat.

You make a Player class with a single Possibility field.
Give the Player a random generator to pick is Possibility

Make two instances of the Player class
Have them each randomly pick their Possibility then compare the choices for a winner.


Going back to your original post

Quote

Can some explain what classes do and how to use them.

Was this not explained in your textbook? If it wasn't then you should drop the class now and get a refund while you can. You are paying money for the course. If they are not TEACHING you then they are ROBBING you.
When you asked the professor for an explanation what did he say? If he refused to hear your question then same thing: Drop the class.
If you couldn't have bothered to read the book or ask the professor. Same answer: Drop the class, this isn't the career field for you.
If it was explained in the book. You may just have to try harder. Read it over and over. Use Google and the MSDN site to get alternate wordings for the same explanation. If you try hard and apply yourself you should be able to learn it. SOmetimes you just can't rely on the text book. You may have to accept going to less parties to do more alternative research.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library...

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

  • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
  • Arrays: Shows how to use arrays.
  • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
  • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
  • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
  • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
  • Commandline: Demonstrates simple command-line processing and array indexing.
  • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
  • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
  • Events: Shows how to declare, invoke, and configure events in C#.
  • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
  • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
  • Hello World: A Hello World application.
  • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
  • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
  • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
  • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
  • Nullable: Demonstrates value types, such as double and bool, that can be set to null
  • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
  • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
  • Operator Overloading: Shows how user-defined classes can overload operators
  • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
  • PInvoke: Shows how to call exported DLL functions from C#
  • Properties: Shows how properties are declared and used; also demonstrates abstract properties
  • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
  • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
  • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
  • Structs: Shows how to use structs in C#.
  • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
  • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
  • User Conversions: Shows how to define conversions to and from user-defined types
  • Versioning: Demonstrates versioning in C# by using the override and new keywords
  • XML Documents: Shows how to document code by using XML
  • Yield: Demonstrates how to use the yield keyword to filter items in a collection

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Muzza*


Reputation:

Re: Classes in a console game (c#)

Posted 07 January 2011 - 10:31 AM

Thanks to all that responded, I have managed to input one class in my game.

And the reason I didn't know what a class was due to me missing classes in uni due to problems I had.
Was This Post Helpful? 0

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Classes in a console game (c#)

Posted 07 January 2011 - 11:40 AM

C# is an Object (read: Class) Orientated Programming (OOP) Language. Understanding classes and how the interact is essential to understanding C#. I suggest you get some extra help from the uni, professor, tutor, and/or fellow students to get caught up on what you missed. An analogy might be you having trouble in geometry class because you missed the day they talked about addition. You need to know the basics of adding in order to build up to the advanced stuff.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Classes in a console game (c#)

Posted 07 January 2011 - 12:05 PM

Let me recommend a couple tutorials here on DIC to help you with C# classes and inheritance therof.


Bulding an application - Part 1
Building an application - Part 2
Passing values between forms
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1