Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
Before I start, I want to explain that my knowledge about classes is basic.
So, I know how to get a class into another class using fields/proporties (dont know the difference about that), but the problem is more how to use that, and why using this method.
 
I need to programme a dog race, the dogs are running!
The classes are created, but I hardly can understand the process while runing.
The extra function is about to bet on a dog with money.
 
So, I have following classes:
 
-Bet
-Player
and not so important - Dog
 
this is the Player class:
 
public string Name;
		public Bet Bet;
		public int Money = 50;
		public RadioButton PlayerActive;
		public Label PlayerLabel;
 
...
 
public bool BetNow(int amount, int dog)
		{
			bool ok = false;
			Bet now = new Bet(amount, dog);
			Bet = now;
 
			if (amount <= Money)
			{
				ok = true;
			}
 
			return ok;
		}
 
		public void GetMoney(int winner)
		{
			if (Bet.Dog != winner)
			{
				Money = Money - Bet.BettingMoney;
			}
		}
 
the bet class:
public int BettingMoney;
		public int Dog;
		public Player BettingPlayer;
		public Bet(int amount, int Dog)
		{
			this.BettingMoney = amount;
			this.Dog = Dog;
		}
 
		public int GiveMoney(int winner)
		{
			if (BettingPlayer.Bet.Dog != winner)
			{
			BettingPlayer.Money = BettingPlayer.Money - BettingMoney;
			}
			return BettingMoney;
		}
 
Here is one of my key problems, how to tell: whos the winning dog and who won the bet?? I tried it by acessing BettinPlayer Money, but it is hard to understand, why I have done this. I tried so many codes, until I come to this solution.
 
The form:
if (radioButton1.Checked == true)
			{
			player1BetVar = Convert.ToInt32(domainUpDown1.Text);
				DogVar = Convert.ToInt32(domainUpDown2.Text);
				Playerarray[0].BetNow(player1Wete,DogVar);
				Playerarray[0].Bet = new Bet(player1Wete,DogVar);
				label1.Text = Playerarray[0].Bet.BetInfo();
 
				Playerarray[0].Bet.BettingPlayer = Playerarray[0];
				 
			}
 
Thank you...
Posted 17-Oct-12 5:02am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Niko,

First of all, I think your code is a little bit confusing. The aim of Object Oriented Programming with clases is to get a clear structure but you created a class "Bet" that contains the class "Player" and vice versa. Usually in OOP objects (classes) represent objects of our real world with properties, fields and methods. Properties describe an object's status, charackteristics etc. (e.g. an object "Person" can have the property "age") Methods are what an object is able to do (e.g. the "Person" can "walk()" or something else) so it makes sence to stick to that: a "Bet" is not a person or thing that can do anything but a "Player" is someone who can:
public static void winOrLooseMoney(int dog) 
so you could define this method in your player class. All procedures to increase or decrease money can be declared here and the class "Bet" just contains the amount of the bet and the dog as fields or properties. And every Player has a "playersBet"
public static void winOrLooseMoney(int dog) 
{  
  if ( dog == playersBet.dog)
    {
      Money = Money + 2 * playersBet.amount; // or 3* or 4* what ever you want
    }
    else
      {
        Money = Money - playersBet.amount;
      };
}
 
In your Main Routine you can use the Random function or whatever you like to get the winning dog and call
Player1.winOrLooseMoney(randomValue);


If anything isn't clear write back, please.

The difference between a field and a property is: fields are just like usual variables that belong to a class (they should be private - just internal use) a property can be read or written only by functions you have to define. This can guard against logical errors in complex code (For Examples see MSDN reference - very good for c#)
Regards Martin





Edit: And check your code: there are some syntax errors Wink | ;)
  Permalink  
v3
Comments
niko_tells at 18-Oct-12 2:30am
   
I have get it like following: Classes are created on basic of "human thinking" like you explained with fields like "age". Ok get it!
 
With my new knowledge, i would code a "player" class with player proporties. But i dont know what i could put into the bet class? I event dont really know if i need a bet class, cause players could be able to bet with a method of the player class, am i right?
 
Danke Martin ;)
Apfelmuuus at 18-Oct-12 12:03pm
   
Yes, you do not need a "bet" class you can handle it directly in the "player" class. I just thought you wanted a bet class but it isn't necessary.(without a "bet" class you are saving some resources) So if it is ok for whom your writing this programm for, just handle the "Money" and the number of the winning dog as properties of the player class. If your are coding for your own just try what you like best.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 BillWoodruff 108
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100