Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 [Solved] count up to 16 with one digit  (Read 3772 times) 0 Members and 1 Guest are viewing this topic.
Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Posted 2014-01-13 19:24:33 »

I'm working on a game where the player going to encounter a alien race, which is suppose to have a digit up to 16. I have manage to make the code that can translate Alien to Human. But not Human to Alien. I'm using A-G in the code for the 10-16 digits. How do I make the Human to Alien method?

Just in case someone wants/need to see my code that translate Alien to Human.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43 `public static int fromAlienNumber(String nr){        boolean isNegative = nr.contains("-");                //Code to see if the number is compatible with the alien alphabet.        Matcher m = Pattern.compile("(-?[0-9A-G]+)").matcher(nr);        if(!m.matches()){            System.err.println(nr+" is not an alien number");            return -1;        }                int result = 0;        nr = nr.replace("-","");                for(int i=0; i
Dxu1994
 « Reply #1 - Posted 2014-01-13 19:33:27 »

 1  2 `System.out.println(Integer.parseInt("GGG", 17));System.out.println(Integer.toString(1111, 17).toUpperCase());`

works perfectly fine for me? Why reinvent the wheel?

also your code is wrong:

it will parse GG-G as -GGG not to mention it's a lot less efficient than Integer.

dermetfan
 « Reply #2 - Posted 2014-01-13 19:35:07 »

toAlienNumber -> Integer#toHexString(int)
fromAlienNumber -> Integer#decode(String)
EDIT: Dxu1994 was faster and more exact.

 Games published by our own members! Check 'em out!
Opiop
 « Reply #3 - Posted 2014-01-13 19:35:20 »

A little more context would be nice.
Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Reply #4 - Posted 2014-01-13 19:42:43 »

 1  2 `System.out.println(Integer.parseInt("GGG", 17));System.out.println(Integer.toString(1111, 17).toUpperCase());`

works perfectly fine for me? Why reinvent the wheel?

1. Because I didn't know the well existed
2. That code don't work.

 1  2 `System.out.println(Integer.parseInt("A0", 17)); //170System.out.println(fromAlienNumber("A0")); //100`

if we had a digit for 10, I don't think A0 would be 170, since A means 10, and 0 means 0. I have no clew how a 7 got into the mix
DavidW

Junior Devvie

Medals: 3
Exp: 7 years

 « Reply #5 - Posted 2014-01-13 19:43:57 »

This isn't too hard, you just have to convert from base 10 to base 17 or vice versa (it's base 17 since you have 17 possible digits).  How you do this is successively divide your number by whatever base you are converting to and keep track of the remainders.

This is untested so it might have a typo or something

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `public static String convertToBase(int input, int base) { String output = ""; int value = input; while(value > 0) {   remainder = value % base;   output = getDigit(value).concat(output);  // have getDigit return a string containing the desired digit for this value and tack it on the front   value /= base; } return output;}`

In your case, just call convertToBase(whateverNumebr, 17). That should work fine.  Converting from one base to another is a similar process.

EDIT: I didn't notice that you wanted it to work for negative values as well.  Just check if the integer is negative at the beginning of the method and multiply by -1.  Then put a final "-" string at the front of output before you return.

2nd EDIT: Oh wow, I didn't realize that the Integer.parseInt() method could do this for you.  I agree with those above, just use that instead of writing your own method.  Still, it's nice to know how it works!

Hello!
Dxu1994
 « Reply #6 - Posted 2014-01-13 19:47:30 »

No, 170 is correct, 100 is incorrect since you're working in base 17.

A = 10, the 0 means carry 17 over.

17 * 10 = 170 not 100.

Here's how you work it out:

17^3       17^2       17        1
0             0            A (10)   0

17^3 x 0 + 17^2 x 0 + 17 x 10 + 1x0 = 170, not 100.

Similarly in base 10 (decimal):

10^3       10^2       10        1
0             0            10        0

10^3 x 0 + 10^2 x 0 + 10x10 + 1x0 = 100

DavidW

Junior Devvie

Medals: 3
Exp: 7 years

 « Reply #7 - Posted 2014-01-13 19:55:42 »

No, 170 is correct, 100 is incorrect since you're working in base 17.

A = 10, the 0 means carry 17 over.

17 * 10 = 170 not 100.

Here's how you work it out:

17^3       17^2       17        1
0             0            A (10)   0

17^3 x 0 + 17^2 x 0 + 17 x 10 + 1x0 = 170, not 100.

Similarly in base 10 (decimal):

10^3       10^2       10        1
0             0            10        0

10^3 x 0 + 10^2 x 0 + 10x10 + 1x0 = 100

This is correct.  You are not in hexidecimal since you have 17 symbols (0-9 and A-G) representing values.  That means that each place value represents a power of 17.  In regular old decimal we call these the 1's place, 10's place, 100's place, etc., which you learned about in school.  So your alien numerical system works on base 17 so you have the 1's place, 17's place, 289's place (17*17), 4913's place (17*17*17), and so on.

Hello!
Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Reply #8 - Posted 2014-01-13 20:08:02 »

Thanks, now I understand how it works. I was so stuck in base 10 that I didn't know what changing to base 17 was going to do ^^
EgonOlsen
 « Reply #9 - Posted 2014-01-13 21:17:40 »

Thanks, now I understand how it works. I was so stuck in base 10 that I didn't know what changing to base 17 was going to do ^^
However, if even you as the developer had problems to understand this, maybe it's not such a good idea to put it into the game...

 Games published by our own members! Check 'em out!
Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Reply #10 - Posted 2014-01-13 21:36:01 »

Thanks, now I understand how it works. I was so stuck in base 10 that I didn't know what changing to base 17 was going to do ^^
However, if even you as the developer had problems to understand this, maybe it's not such a good idea to put it into the game...

And your reason behind that is, what exactly? I want to push my boundaries so I can learn something. If I didn't I wouldn't been able to code at all since I don't understand it there for I shouldn't learn it.
EgonOlsen
 « Reply #11 - Posted 2014-01-13 21:41:53 »

And your reason behind that is, what exactly? I want to push my boundaries so I can learn something. If I didn't I wouldn't been able to code at all since I don't understand it there for I shouldn't learn it.
It was just a hint that the player might have the same or worse problems. So if its important to the game to be able to get these numbers, almost nobody will be able to. If it's not important, then it doesn't matter...

Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Reply #12 - Posted 2014-01-13 21:57:31 »

And your reason behind that is, what exactly? I want to push my boundaries so I can learn something. If I didn't I wouldn't been able to code at all since I don't understand it there for I shouldn't learn it.
It was just a hint that the player might have the same or worse problems. So if its important to the game to be able to get these numbers, almost nobody will be able to. If it's not important, then it doesn't matter...

That's what play testers are for. But I had just planned to use this as a count down in seconds to things like self-destruct. But also have it spread around the game to clew the players that it's numbers.
DavidW

Junior Devvie

Medals: 3
Exp: 7 years

 « Reply #13 - Posted 2014-01-13 22:26:29 »

@Yemto,

If you want something a little more interesting you should look into alternate numerical systems.  For example, the ancient Mayans had a cool counting system based on base 60(!) and writing a translator could be a lot of fun actually.  You could change the symbols to be more "alieny" as well.  I like the idea of aliens having their own language in general, I think that's pretty cool.

Good luck with your game.

Hello!
Yemto

Senior Devvie

Medals: 4
Exp: 3 years

 « Reply #14 - Posted 2014-01-13 22:56:17 »

@Yemto,

If you want something a little more interesting you should look into alternate numerical systems.  For example, the ancient Mayans had a cool counting system based on base 60(!) and writing a translator could be a lot of fun actually.  You could change the symbols to be more "alieny" as well.  I like the idea of aliens having their own language in general, I think that's pretty cool.

Good luck with your game.

Thanks, I think I stay at base 17, at least for this race. But I will use another font so the alien words looks different, I'm to lazy to come up with a whole new language.
Pages: [1]
 ignore  |  Print

 DesertCoockie (21 views) 2018-05-13 18:23:11 nelsongames (69 views) 2018-04-24 18:15:36 nelsongames (66 views) 2018-04-24 18:14:32 ivj94 (750 views) 2018-03-24 14:47:39 ivj94 (80 views) 2018-03-24 14:46:31 ivj94 (604 views) 2018-03-24 14:43:53 Solater (96 views) 2018-03-17 05:04:08 nelsongames (169 views) 2018-03-05 17:56:34 Gornova (387 views) 2018-03-02 22:15:33 buddyBro (1047 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org