5 Replies - 719 Views - Last Post: 10 October 2012 - 02:22 PM Rate Topic: -----

#1 fatalpriapism  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 39
  • Joined: 17-August 12

Problem with Difficulty Level in Game

Posted 09 October 2012 - 06:36 PM

I am work on a hobbyist-level game with a couple friends. The guy that does the harder coding is out of town, and I'm up the creek trying to implement a new feature.

I am trying to implement difficulty levels. I have added the combobox at the new game screen using the following (it is an airline simulation):

 lbContent.Items.Add(new QuickInfoValue(Translator.GetInstance().GetString("PageNewGame", "1011"), cbDifficulty));

            cbOpponents = new ComboBox();
            cbOpponents.SetResourceReference(ComboBox.StyleProperty, "ComboBoxTransparentStyle");
            cbOpponents.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            cbOpponents.Width = 50;
            cbOpponents.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
            for (int i = 0; i < Airlines.GetAllAirlines().Count; i++)
                cbOpponents.Items.Add(i);

            cbOpponents.SelectedIndex = 3;// cbOpponents.Items.Count - 1;

        private void cbDifficulty_Selectionchanged(object sender, SelectionchangedEventArgs e)
        {

            Airline airline = (Airline)cbDifficulty.SelectedItem;
            int year = (int)cbStartYear.SelectedItem;

            setAirportsView(year, airline.Profile.Country);
        }



The combobox is displaying fine, however I am having a bit of trouble with setting the difficulty level when the Selectionchanged occurs.

This is the abbreviated code for the game object (where we set our initial settings for a new game):
public class GameObject
{
  public enum DifficultyLevel { Easy, Normal, Hard }
  public DifficultyLevel difficulty { get; set; }
private GameObject()
{
  this.difficulty = DifficultyLevel.Easy;
}

//returns the starting money
private long getStartMoney()
{
  long baseStartMoney = 125000000;
  if (difficulty == Easy)
  { baseStartMoney *= (3 / 2); }
  else if (difficulty == Normal)
  { baseStartMoney *= 1;}
  else if (difficulty == Hard)
  { baseStartMoney *= (1/2);}

  return Convert.ToInt64(GeneralHelpers.GetInflationPrice(baseStartMoney));
}



The difficulty levels are returning as not in context, but I have been staring at this screen for an hour or so and I'm running out of fresh ideas.

A gentle shove in the right direction would be greatly appreciated :) I'm still learning C#, so take it easy on the rookie

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Difficulty Level in Game

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Problem with Difficulty Level in Game

Posted 09 October 2012 - 07:11 PM

I suspect it's Easy, Normal and Hard that are giving the errors, you need to prefix those with the Enum name. And you should be using a case statement instead of the if/else/if.

This post has been edited by Momerath: 09 October 2012 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 fatalpriapism  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 39
  • Joined: 17-August 12

Re: Problem with Difficulty Level in Game

Posted 09 October 2012 - 07:25 PM

I changed it to the following, it took care of the code errors but still not functioning in game:
  if (difficulty == DifficultyLevel.Easy)
15
  { baseStartMoney *= (3 / 2); }
16
  else if (difficulty == DifficultyLevel.Normal)
17
  { baseStartMoney *= 1;}
18
  else if (difficulty == DifficultyLevel.Hard)
19
  { baseStartMoney *= (1/2);}



I know the case statement would do it more efficiently, but shouldn't it still function using the if elseif?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,115
  • Joined: 05-May 12

Re: Problem with Difficulty Level in Game

Posted 09 October 2012 - 11:38 PM

You said it's still not functioning. Have you set a breakpoint in the code and stepped through it to see what is happening and giving you the expected results?
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Problem with Difficulty Level in Game

Posted 10 October 2012 - 12:25 AM

So the user changes the difficulty, but what are you doing with year?????

private void cbDifficulty_Selectionchanged(object sender, SelectionchangedEventArgs e)
{
	//What does year have to do with difficulty?
   Airline airline = (Airline)cbDifficulty.SelectedItem;
   int year = (int)cbStartYear.SelectedItem;

   setAirportsView(year, airline.Profile.Country);
}



Aren't you supposed to set the difficulty proptery of your game object instead?


In your if statements you have to tell it what enum you are looking at


//returns the starting money
private long getStartMoney()
{
	long baseStartMoney = 125000000;
	if (difficulty == DifficultyLevel.Easy)
	{ 
		baseStartMoney *= (3 / 2); 
	}
	else if (difficulty == DifficultyLevel.Normal)
	{ 
		baseStartMoney *= 1;
	}
	else if (difficulty == DifficultyLevel.Hard)
	{ 
		baseStartMoney *= (1/2);
	}

  return Convert.ToInt64(GeneralHelpers.GetInflationPrice(baseStartMoney));
}




Was This Post Helpful? 0
  • +
  • -

#6 fatalpriapism  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 39
  • Joined: 17-August 12

Re: Problem with Difficulty Level in Game

Posted 10 October 2012 - 02:22 PM

The game also allows the user to change the start year, that was probably a straggler piece of code I forgot to edit out.

Anyway, I am glad to say I was able to resolve the issue. The problem was that it should have read:

if (GameObject.GetInstance().Difficulty == GameObject.DifficultyLevel.Hard)
{ baseStartMoney *= 0.5 }

etc



Thanks for the help guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1