Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  How to get a special int?  (Read 1322 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Member


Medals: 1



« Posted 2013-08-27 08:49:03 »

Hi there JGO,

I was working yesterday and today on a small flaggscript. De borders of the flagg need to be darker then the inside flag. I have a special Colour-script to add colours. 0=dark colour, 5=lightest colour. So I say the inside of my flagg is yellow, 550 (RGB). but I want the outside a bit darker then I need to get from every number, 5 5 0, if it's higher then 0. so 5 is higher then 0 and return it as 1, then you'll get: 1 1 0. and finally to make the darker outside colour = 550-110 = 440. The part I have no idea how to do is the middle part(getting 110 from 550). I also don't know what it is called, so I couldn't search on google information or help.

Already thanks!
-RoseSlayer

Fundamentum W.I.P.
Offline Danny02
« Reply #1 - Posted 2013-08-27 09:29:23 »

no idea what you are talking about Cheesy
Offline roseslayer

Junior Member


Medals: 1



« Reply #2 - Posted 2013-08-27 09:38:24 »

Yeah, that is the problem. I don't know how to explain this but I can give some examples:

I have 550 and want to output 110,
I have 044 and want to output 011,
I have 303 and want to output 101,
I have 555 and want to output 111,
I have 003 and want to output 001,
I have 514 and want to output 111,

Hope this will help.
-RoseSlayer

Fundamentum W.I.P.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tdegroot96

Junior Member


Projects: 1



« Reply #3 - Posted 2013-08-27 09:53:57 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
int specialInt = 444;
String targetString = "";
String specialString = Integer.toString(specialInt);
for (int i = 0; i < specialString.length(); i++) {
   int workInt = Integer.parseInt(String.valueOf(specialString.charAt(i)));
   if (workInt > 0) {
      targetString += "1";
   } else {
      targetString += "0";
   }
}
int finalInt = Integer.parseInt(targetString);


This outputs me: 111
Offline Jeremy
« Reply #4 - Posted 2013-08-27 10:09:32 »

You can use this solution I wrote and avoid the strings. I am sure that has a performance increase but by how much I'm not sure. I am pretty sure it works for all cases, but I did a couple and it seems to be working properly.

1  
2  
3  
4  
5  
6  
7  
8  
9  
   public static int foo(int in)
   {
      int out = 0;
     
      for(int i = 1; i <= in; i *= 10)
         out += in % (i * 10 ) >= i ? i : 0;
     
      return out;
   }


*EDIT*
I just fixed a bug I found looking over the code. It should be <= in.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline roseslayer

Junior Member


Medals: 1



« Reply #5 - Posted 2013-08-27 10:19:29 »

Thanks for all the help, it worked.
Sorry I was a bit confusing but I even didn't know how to explain it Tongue

-RoseSlayer

Fundamentum W.I.P.
Offline nerb
« Reply #6 - Posted 2013-08-27 10:19:58 »

Here's anothery:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   public int whateverItsCalled(int flag) {
         
        int x = 0;
       
        if(flag/100 > 0) {
            x +=100;
        }
        if((flag - flag/100 * 100)/10 > 0) {
            x += 10;
        }
       
        if(flag - flag/10 * 10 > 0) {
            x += 1;
        }

        return x;
   
    }


It relies on integer arithmetic. It also assumes that flag is <1000 (you may or may not want to handle the case that it is >1000?). Although... I feel like a bit of a spanner now. I like Jeremy's solution; small & compact, go with that.
Offline Jeremy
« Reply #7 - Posted 2013-08-27 10:54:08 »

Thanks for all the help, it worked.
Sorry I was a bit confusing but I even didn't know how to explain it Tongue

-RoseSlayer

No problem, just note that I did change the code since you last read it. It should be <= in, not < in

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline tdegroot96

Junior Member


Projects: 1



« Reply #8 - Posted 2013-08-27 11:06:08 »

There you go, 3 different ways, however my way was pretty inefficient. Tongue
Offline Danny02
« Reply #9 - Posted 2013-08-27 12:22:49 »

I don't know why you scale your colors in the scala [0, 5] and I also don't know why you put them together like this into a single number.

Why not use some standard why to handle color, like one byte for red green and blue(0-255). Also there is already some classes in the standard java runtime which help you with color.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
Color flag = Color.yellow;

Color border = flag.darker();

//and to get your color scaled to your [0,5] interval
public int scale(Color c)
{
   int r = c.getRed() / 51;// -> 255 / 5 = 51
  int g = c.getGreen() / 51;
   int b = c.getBlue() / 51;

   return r*100+g*10+b;
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #10 - Posted 2013-08-27 12:24:52 »

Jeremy's code might look better. But it uses %. which is far worse then dividing on really low level Smiley Not that you will notice it. But sometimes short code isnt always the best

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline relminator
« Reply #11 - Posted 2013-08-27 16:22:36 »

Jeremy's code might look better. But it uses %. which is far worse then dividing on really low level Smiley Not that you will notice it. But sometimes short code isnt always the best

How so?  Mod is a by product of division(intel cpu integer data). So in essence % is as fast as a /.

ie:
dx:ax has the dividend
bx      has the divisor
after a div...
dx       has the modulos
ax       has the quotient

Not sure onother archs(arm7 and 9 ds) don't have intrinsic division.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #12 - Posted 2013-08-27 16:47:42 »

The two division instructions can be very slow for high numbers, less so for for low numbers. Division by constants is sometimes better because there are alternative ways to do division. But why bother with division at all?

1  
2  
3  
4  
5  
6  
7  
public int darker(int input)
{
  final int red = 0x7 & input; // 000 000 111
 final int green = 0x38 & input; // 000 111 000
 final int blue = 0x1C0 & input; // 111 000 000
 return (red != 0 ? red - 0x1 : 0) + (green != 0 ? green - 0x8 : 0) + (blue != 0 ? blue - 0x40);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public int pack(int r, int g, int b)
{
  return r << 6 + g << 3 + b;
}

// unpack all 3
int input = ...;
int r = input >>> 6;
int g = (input >>> 3) & 0x7;
int b = input & 0x7;


Edit: Fixed typo and added pack/unpack code.
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #13 - Posted 2013-08-27 16:57:13 »

Jeremy's code might look better. But it uses %. which is far worse then dividing on really low level Smiley Not that you will notice it. But sometimes short code isnt always the best

How so?  Mod is a by product of division(intel cpu integer data). So in essence % is as fast as a /.

ie:
dx:ax has the dividend
bx      has the divisor
after a div...
dx       has the modulos
ax       has the quotient

Not sure onother archs(arm7 and 9 ds) don't have intrinsic division.

Division can be optimized to bit shifting in some occasions as well. Which only takes like 1 clock cycle

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline roseslayer

Junior Member


Medals: 1



« Reply #14 - Posted 2013-08-27 17:49:51 »

I don't know why you scale your colors in the scala [0, 5] and I also don't know why you put them together like this into a single number.

Why not use some standard why to handle color, like one byte for red green and blue(0-255). Also there is already some classes in the standard java runtime which help you with color.

Honestly I wanted to make my game a challenge for me, where I can only use a few colours. And the colours I use are then also the bright ones. My game looks really "shiny" with this methode Cheesy

-RoseSlayer

Fundamentum W.I.P.
Offline Jeremy
« Reply #15 - Posted 2013-08-27 18:34:23 »

Jeremy's code might look better. But it uses %. which is far worse then dividing on really low level Smiley Not that you will notice it. But sometimes short code isnt always the best

Sorry I didn't consider the code at such an absurdly irrelevant microscopic level. Your argument is based severely on two things:

A ) Optimizations for particular architectures
B ) The underlying implementation on a OPCODE level

Which means unless your profiler makes a point about it, it means nothing in Java which has no concept of either.

I'm sure this will work fine for anyone, and if it doesn't they shouldn't consider hacking into bit-shifting operators unless their profiler tells them they have to.

Finally, this won't be used for large numbers as was illustrated by the OP.

My solution was general for all integers, not specifically for an RGB colour - however I am sure if you remove that abstraction you will get some better optimizations.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #16 - Posted 2013-08-27 18:44:45 »

Yea i know. As i said. You wont notice, i was just saying that short code is not always better.

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!