Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 569 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Member


Exp: 3 years



« Posted 2014-01-13 20: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<nr.length(); i++)          
            result += (int)(charToNumber(nr.charAt(i)) * Math.pow(10, nr.length()-(i+1)));              
        return isNegative ? -result : result;
    }
   
    public static int charToNumber(char c){
        switch(c){
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            case 'A': return 10;
            case 'B': return 11;
            case 'C': return 12;
            case 'D': return 13;
            case 'E': return 14;
            case 'F': return 15;
            case 'G': return 16;

        }
       
        //To make the compiler happy, it should never come here.
       return -1;
    }
Offline Dxu1994
« Reply #1 - Posted 2014-01-13 20: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.

Offline dermetfan

Senior Member


Medals: 11



« Reply #2 - Posted 2014-01-13 20: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!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2014-01-13 20:35:20 »

A little more context would be nice.

Offline Yemto

Junior Member


Exp: 3 years



« Reply #4 - Posted 2014-01-13 20: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)); //170
System.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
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #5 - Posted 2014-01-13 20: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!
Offline Dxu1994
« Reply #6 - Posted 2014-01-13 20: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

Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #7 - Posted 2014-01-13 20: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!
Offline Yemto

Junior Member


Exp: 3 years



« Reply #8 - Posted 2014-01-13 21: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 ^^
Offline EgonOlsen
« Reply #9 - Posted 2014-01-13 22: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... Huh

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Yemto

Junior Member


Exp: 3 years



« Reply #10 - Posted 2014-01-13 22: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... Huh

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.
Offline EgonOlsen
« Reply #11 - Posted 2014-01-13 22: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...

Offline Yemto

Junior Member


Exp: 3 years



« Reply #12 - Posted 2014-01-13 22: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.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #13 - Posted 2014-01-13 23: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!
Offline Yemto

Junior Member


Exp: 3 years



« Reply #14 - Posted 2014-01-13 23: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (36 views)
2014-07-18 06:55:21

Zero Volt (34 views)
2014-07-17 23:47:54

danieldean (27 views)
2014-07-17 23:41:23

MustardPeter (30 views)
2014-07-16 23:30:00

Cero (44 views)
2014-07-16 00:42:17

Riven (46 views)
2014-07-14 18:02:53

OpenGLShaders (35 views)
2014-07-14 16:23:47

Riven (35 views)
2014-07-14 11:51:35

quew8 (31 views)
2014-07-13 13:57:52

SHC (67 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!