2

I have a serious problem about printing a number as a graphic element, i have developed a game using c++, oriented object programming, and when a player wins he receives a certain points, so I want to transform these points in a sequence of a graphic digits.

For example:

Firstly the player has 0 points. He wins the level 1 and gets 100 points so I want to display 4 digits like this:

  1. Firtly: 0000

enter image description here

  1. Wins: 100 points to 0100

enter image description here

The game works with integers, What I did was: create a Graphic Array that contains all the graphic numbers from 0 to 9.

gGraphic digits[10];

gGraphic is a class that creates and draws an object.., hence digits[0] is a graphic element that represents 0, digits[1] represents 1, digits[2] represents 2...successively.. for example if I want to draw number 1, like 0001, this is what I do:

digits[0].draw(Xposition, Yposition);
digits[0].draw(Xposition + digits[0].ObtainScaleX(), Yposition);
digits[0].draw(Xposition + digits[0].ObtainScaleX()*2, Yposition);
digits[1].draw(Xposition + digits[0].ObtainScaleX()*3, Yposition);

As you can see I don't change the position Y, but I change the position X so I can get the sequence of digits represented at the X axis, the function ObtainScaleX() returns the width scale of an Object.

The problem is how can I do this for 1000 points, 1234 points or 3409???, the are thousands of possibilities and an if statement or switch case won't work, I really need an explanation, a basic idea, do I have to use pointers?

Hamza Díaz
  • 175
  • 1
  • 3
  • 12

3 Answers3

3

The very simplest solution for your problem would be to turn the number in question to a std::string and retrieve the single digits from the converted character values:

std::ostringstream oss;
oss << value;
int scale = 0;
for(std::string::const_iterator it = oss.str().begin();
    it != oss.str().end();
    ++it, ++scale) {
    int drawDigitIndex = *it - `0`;
    digits[drawDigitIndex].draw(Xposition + 
                                digits[drawDigitIndex].ObtainScaleX() * scale
                               , Yposition);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

You can use math to figure out the values you should use. For example

   7632
//    ^ ones
//   ^  tens
//  ^   hundreds
// ^    thousands

So you can find the digit to draw in each place as

int digits[4];
int value = 7632;
digits[0] = value % 10;              // ones
digits[1] = value % 100 / 10;        // tens
digits[2] = value % 1000 / 100;      // hundreds
digits[3] = value % 10000 / 1000;    // thousands

You can obviously do this in a loop, I just wanted to show step by step where each value would come from. This would result in the array digits having the values

{ 2, 3, 6, 7 }

Once you know the digits, you can use your drawing code that you already have.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

There are two parts to your problem:

  1. Get the digits of a given number.
  2. Display the digits.

You can use the method used in this SO answer to get the digits.

You already seem to have a method to display the digits.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270