Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  What's with unorganized code?  (Read 3071 times)
0 Members and 1 Guest are viewing this topic.
Offline micecd

Senior Newbie





« Posted 2014-06-23 10:57:19 »

I've never had this problem unless I was running a very deep algorithm. My code looks godly.


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  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
package Core;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;

public class HexGrid {

   int size;
   ArrayList<Point> points = new ArrayList<Point>();
   ArrayList<Troint> troints = new ArrayList<Troint>();
   Point[][] pointA;

   public HexGrid(int size){
      this.size = size;
      setupGrid(size);
   }
   
   public void update() {
     
     
   }

   public void draw(Graphics2D g) {
     
      int offset = size*24;
      int spacing = 20;
      for(int c = 0; c < points.size(); c++){
         
         g.setColor(Color.green);
         
         Point pFill = points.get(c);
         Troint tFill = troints.get(c);
         
         if(tFill.n == 0 && tFill.v == 0){
            g.setColor(Color.red);
         }
         
         if(tFill.n > 0){
            g.fillOval(tFill.n*(spacing)+offset, tFill.v*(spacing)+offset-(size*(spacing/2))+((size-tFill.n)*(spacing/2)), (spacing), (spacing));
         }else
         if(tFill.n <= 0){
         g.fillOval(tFill.n*(spacing)+offset, tFill.v*(spacing)+offset+((tFill.n)*(spacing/2)), (spacing), (spacing));
         }
      }      
   }
   int numKeep = 0;
   public void setupGrid(int size){
     
      pointA = new Point[size*2+1][size*2+1];

     
      int count = 1;
      for(int o = 0; o < size; o++){
         
         for(int i = 0; i <  size+1+o; i++){
            System.out.println((o-size)+":"+(i-o)+":"+(Math.abs(o-size)-(i-o)));
            points.add(new Point(o-size,i-o));
            troints.add(new Troint(o-size,Math.abs(o-size)-(i-o),i-o));
            count++;
         }        
      }
     
      for(int o = 0; o < size*2+1; o++){
         System.out.println(0+":"+(o-size)+":"+-(o-size));
         points.add(new Point(0,o-size));
         troints.add(new Troint(0,-(o-size),o-size));
         count++;
      }
     
      for(int o = 0; o < size; o++){
         
         for(int i = 0; i <  Math.abs(o-size*2); i++){
            System.out.println((o+1)+":"+(i-o-1)+":n, "+(-(o+1)+Math.abs(i-o-1)));
            points.add(new Point(o+1,i+o-size+1));
            troints.add(new Troint(o+1,-((o+1)+Math.abs(i-o-1)),i+o-size+1));
            count++;
         }        
      }
   }
}


And you may be asking yourself, wtf is a TROINT? It's just a Point with 3 coordinates instead of 2 like a normal point. Dealing with Hexagons that is....

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  
package Core;

public class Troint {

   //n = negative line coordinate;
  //p = positive line coordinate;
  //v = vertical line coordinate;
 
   public int n;
   public int p;
   public int v;
   
   public Troint(int n, int p, int v){      
      this.n = n;
      this.p = p;
      this.v = v;      
   }
   
   public int getN(){
      return n;
   }
   public int getP(){
      return p;
   }
   public int getV(){
      return v;
   }
   
}
Offline trollwarrior1
« Reply #1 - Posted 2014-06-23 11:04:30 »

WtF IS TROINT? Isn't it just vec3i ?
Offline hwinwuzhere
« Reply #2 - Posted 2014-06-23 11:07:35 »

You forgot to place an enter between the two methods :O

What did the boolean say to the integer? You can't handle the truth.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Roquen
« Reply #3 - Posted 2014-06-23 11:16:57 »

Ah...oh nevermind.
Offline KevinWorkman

JGO Wizard


Medals: 70
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #4 - Posted 2014-06-23 12:41:22 »

So, uh, the point of this post is to brag about your code?

The code you posted is rather trivial, so it shouldn't be too hard to keep it "organized". And organization is subjective: I personally wouldn't have numKeep declared in the middle of the class, especially since you don't actually use it. I could further nitpick by pointing out that you violated standard naming conventions by capitalizing your package, plus you only have a single comment (we shouldn't have to guess at what a "Troint" is), as well as an empty function. You also have public variables as well as getters which is redundant. You've also coded to the class instead of the interface in a few places.

It becomes more difficult to keep your code "organized" when you're working on a larger project. It becomes impossible to keep your code "organized" when you're working with more than just yourself.

Work on a project of 100,000 lines of code split between 5 people, then brag about your code! :p

(I am not trying to start a fight or be overly critical, just trying to maybe provide some perspective.)

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline ipe369

Junior Member


Medals: 3
Exp: 3 years



« Reply #5 - Posted 2014-06-23 16:19:43 »

On a partially related topic
How do you all do your curly braces? I prefer

for (int i = 0; i < 10; i ++)
{
}

but some people have the { brace afterwards:

for (int i = 0; i < 10; i ++){

}

Offline KevinWorkman

JGO Wizard


Medals: 70
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #6 - Posted 2014-06-23 16:32:02 »

How do you all do your curly braces?

The official Java naming conventions say that the opening brace goes on the same line as the statement. Recommended reading: http://www.oracle.com/technetwork/java/codeconventions-150003.pdf

However, what you're touching on is one of those pointlessly debated conventions that don't actually matter:
http://stackoverflow.com/questions/4452288/what-is-the-name-of-this-convention-for-curly-braces
http://programmers.stackexchange.com/questions/2715/should-curly-braces-appear-on-their-own-line
http://en.wikipedia.org/wiki/Indent_style
http://theprofoundprogrammer.com/post/26955601996/text-braces-go-on-the-next-line-you-tremendous

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline ipe369

Junior Member


Medals: 3
Exp: 3 years



« Reply #7 - Posted 2014-06-23 17:04:39 »

^I know, I'm aware it's more widely done on the same line, was really just wondering why.

It's interesting to read how many code conventions there are though, it's quite surprising
Offline BurntPizza
« Reply #8 - Posted 2014-06-23 18:04:02 »

While you're prettifying your code, you might as well put spaces between operands and have consistent indentation in your if else blocks.
Or use a formatter.  Pointing
Offline KevinWorkman

JGO Wizard


Medals: 70
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #9 - Posted 2014-06-23 18:08:35 »

Not to mention the use of the String append operator inside of a loop. That should be a StringBuilder.

If this code is "godly", I wonder what it's the god of...? :p

Static Void Games - Play indie games, learn game programming, upload your own games!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #10 - Posted 2014-06-23 18:12:09 »

And really, one could say that most of that code could probably be reduced to only a few lines with some lambdas and map stuff.
Offline orange451

Senior Member


Medals: 7
Projects: 1
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #11 - Posted 2014-06-23 18:21:07 »

I'm getting such an ignorant vibe right now...

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline ipe369

Junior Member


Medals: 3
Exp: 3 years



« Reply #12 - Posted 2014-06-23 18:25:20 »

Also, in your 'troint' class, why is there public member variables but also functions for encapsulation? Isn't the point of encapsulation to make sure that values are only set in a certain way? Maybe I'm wrong.
Offline micecd

Senior Newbie





« Reply #13 - Posted 2014-06-23 21:34:03 »

numKeep was there to determine how my forloop was iterating, I would ++ it in the nested forloop to see how many iterations it would do in code that no longer exists in there.
Offline micecd

Senior Newbie





« Reply #14 - Posted 2014-06-23 21:35:47 »

Also, in your 'troint' class, why is there public member variables but also functions for encapsulation? Isn't the point of encapsulation to make sure that values are only set in a certain way? Maybe I'm wrong.

I don't care for encapsulation if I'm not sharing the code for someone else to work on. I just set everything to public.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #15 - Posted 2014-06-24 02:23:41 »

Also, in your 'troint' class, why is there public member variables but also functions for encapsulation? Isn't the point of encapsulation to make sure that values are only set in a certain way? Maybe I'm wrong.

I don't care for encapsulation if I'm not sharing the code for someone else to work on. I just set everything to public.
So, basically you are lazy and go against good programming practices just because you aren't sharing your code? Sorry, but that is just silly. Your code shows that you really don't have a care for many standards or conventions and your code is suffering because of it. Learn to write the best code you can write all the time, not just when other people are looking at it.

And honestly, Troint? Really? Your class is simply a container object for three integers, just call it a vector. A three dimensional vector. Or something along those lines. Don't use names that aren't common knowledge, that's another sort of convention. Code readability is super important.

Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #16 - Posted 2014-06-24 02:33:29 »

Hi

It's funny because you made this post just to show off your 'godly' programming and you've so far shown that your code is far from godly Smiley. Not only do you make variables public and then add getters for them, but your indentation is less than godly and your code sample is not nearly complex enough to uncover true coding practices Smiley

Just in case anybody is curious my post was meant to summarize the flaws in your 'godly' code and to share my views Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline micecd

Senior Newbie





« Reply #17 - Posted 2014-06-24 02:51:00 »

So many serious responses... lol.

I'm not lazy, using the public modifier isn't a bad practice, I just use that for everything. It can be changed later.

I use setters and getters so that I can go back and make changes.
Offline KevinWorkman

JGO Wizard


Medals: 70
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #18 - Posted 2014-06-24 02:55:22 »

I'm not lazy, using the public modifier isn't a bad practice, I just use that for everything.

This is pretty much the definition of bad practice. I can tell you've never worked on a team before! That's not necessarily a bad thing, but you might want to look at real code samples before calling your code "godly".

The public attributes are nowhere near the only thing wrong with your code.


Static Void Games - Play indie games, learn game programming, upload your own games!
Offline micecd

Senior Newbie





« Reply #19 - Posted 2014-06-24 02:58:47 »

Perhaps this was a bad example as the first snippet was something I was in the middle of debugging...
Offline BurntPizza
« Reply #20 - Posted 2014-06-24 03:00:07 »

I'll say my main reason for this code being less than "godly" is that it's function is as hard to determine as it is. Readability goes beyond code style.
Offline micecd

Senior Newbie





« Reply #21 - Posted 2014-06-24 03:07:26 »

How would I improve the readability any more than this?

As for the formulas, should I put each parameter for the drawing method on a separate line with comments
for what each object in the formula does?
Offline BurntPizza
« Reply #22 - Posted 2014-06-24 03:09:28 »

As for the formulas, should I put each parameter for the drawing method on a separate line with comments
for what each object in the formula does?

That's what [javadoc] comments are for.
Comments in general are a good idea, but starting with self-documenting variable names is also good.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #23 - Posted 2014-06-24 03:14:07 »

It's a function, not a formula, by the way.

Offline micecd

Senior Newbie





« Reply #24 - Posted 2014-06-24 03:54:33 »

1  
 g.fillOval(tFill.n*(spacing)+offset, tFill.v*(spacing)+offset-(size*(spacing/2))+((size-tFill.n)*(spacing/2)), (spacing), (spacing));


Sure that may be a function, that's not what I was referring to.


1  
tFill.v*(spacing)+offset-(size*(spacing/2))+((size-tFill.n)*(spacing/2))


That is what I was referring to as a formula.
Offline BurntPizza
« Reply #25 - Posted 2014-06-24 04:04:58 »

That'd be an expression.
Offline micecd

Senior Newbie





« Reply #26 - Posted 2014-06-24 04:05:29 »

Then what is a formula -___-
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #27 - Posted 2014-06-24 04:07:13 »

Well, two seconds on Google turned this up:
Quote
a mathematical relationship or rule expressed in symbols.
Your "formula" is a bunch of numbers that are unrelated, hence they are an expression, not a formula.

This is getting ridiculous.

Offline micecd

Senior Newbie





« Reply #28 - Posted 2014-06-24 04:09:18 »

Okay, so expressions then -.-
Offline BurntPizza
« Reply #29 - Posted 2014-06-24 04:11:05 »

Also, from the java language standpoint, there is no such thing as a "formula", however this is the definition of an expression: http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html
Pages: [1] 2
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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!