250,000 lines of code

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 10635 Views - Last Post: 07 April 2013 - 02:00 PM Rate Topic: -----

#1 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

250,000 lines of code

Posted 01 January 2013 - 05:51 PM

As a first dive into Swing graphics, I decided to make an arctic RPG for my sister as a Christmas present. Here's why this is here: 250,000 lines of code. The vast majority of this is graphics. Here's why: I didn't know that adding one JPanel in a grid layout removes the previous one that was there. So, in short:

3 different types of basic terrain
data for graphics is randomly generated and each square's terrain type is stored in its own int
25 different large squares, only one of which is visible at once
49 small squares which are JPanels, all visible at all times
each small squares consists of 49 other JPanels, giving the small square its appearance

This adds up to 3x25x49x49, or 180,075 lines of code setting the background color of the JPanels.

Going through all that code gives me nightmares!

Is This A Good Question/Topic? 0
  • +

Replies To: 250,000 lines of code

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: 250,000 lines of code

Posted 02 January 2013 - 03:18 PM

Sounds like you probably could do some serious refactoring. I bet with a few basic functions you would easily see 50% or more reduction in the lines of code. That or you are just going about it all the long way. For some reason, based on what you described, I find it hard to believe you need 180k lines to do it.

Might be a nice exercise for you to learn about refactoring and some design patterns. I would make it a game to see how far you can reduce the number of lines needed. :)
Was This Post Helpful? 3
  • +
  • -

#3 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 03:22 PM

I'll post some of the code here to show you why it has so many lines:

        else if (terrainE4D6 == 0)
        {
            d6a1.setBackground(Background);
            d6a2.setBackground(Background);
            d6a3.setBackground(Background);
            d6a4.setBackground(Background);
            d6a5.setBackground(Background);
            d6a6.setBackground(Background);
            d6a7.setBackground(Background);
            d6b1.setBackground(Background);
            d6b2.setBackground(Background);
            d6b3.setBackground(Purple);
            d6b4.setBackground(Background);
            d6b5.setBackground(Blue);
            d6b6.setBackground(Background);
            d6b7.setBackground(Background);
            d6c1.setBackground(Background);
            d6c2.setBackground(Background);
            d6c3.setBackground(Purple);
            d6c4.setBackground(Blue);
            d6c5.setBackground(Blue);
            d6c6.setBackground(Background);
            d6c7.setBackground(Background);
            d6d1.setBackground(Background);
            d6d2.setBackground(Purple);
            d6d3.setBackground(Purple);
            d6d4.setBackground(Purple);
            d6d5.setBackground(Blue);
            d6d6.setBackground(Blue);
            d6d7.setBackground(Background);
            d6e1.setBackground(Background);
            d6e2.setBackground(Purple);
            d6e3.setBackground(Purple);
            d6e4.setBackground(Purple);
            d6e5.setBackground(Blue);
            d6e6.setBackground(Blue);
            d6e7.setBackground(Background);
            d6f1.setBackground(Purple);
            d6f2.setBackground(Purple);
            d6f3.setBackground(Purple);
            d6f4.setBackground(Purple);
            d6f5.setBackground(Purple);
            d6f6.setBackground(Blue);
            d6f7.setBackground(Blue);
            d6g1.setBackground(Background);
            d6g2.setBackground(Background);
            d6g3.setBackground(Background);
            d6g4.setBackground(Background);
            d6g5.setBackground(Background);
            d6g6.setBackground(Background);
            d6g7.setBackground(Background);
        }
        if (terrainE4D7 == 1)
        {
            d7a1.setBackground(Background);
            d7a2.setBackground(Background);
            d7a3.setBackground(Background);
            d7a4.setBackground(Background);
            d7a5.setBackground(Background);
            d7a6.setBackground(Background);
            d7a7.setBackground(Background);
            d7b1.setBackground(Background);
            d7b2.setBackground(Background);
            d7b3.setBackground(Background);
            d7b4.setBackground(Background);
            d7b5.setBackground(Background);
            d7b6.setBackground(Background);
            d7b7.setBackground(Background);
            d7c1.setBackground(Background);
            d7c2.setBackground(Background);
            d7c3.setBackground(Background);
            d7c4.setBackground(Background);
            d7c5.setBackground(Background);
            d7c6.setBackground(Background);
            d7c7.setBackground(Background);
            d7d1.setBackground(Background);
            d7d2.setBackground(Background);
            d7d3.setBackground(Background);
            d7d4.setBackground(Background);
            d7d5.setBackground(Background);
            d7d6.setBackground(Background);
            d7d7.setBackground(Background);
            d7e1.setBackground(Background);
            d7e2.setBackground(Background);
            d7e3.setBackground(Background);
            d7e4.setBackground(Background);
            d7e5.setBackground(Background);
            d7e6.setBackground(Background);
            d7e7.setBackground(Background);
            d7f1.setBackground(Background);
            d7f2.setBackground(Background);
            d7f3.setBackground(Background);
            d7f4.setBackground(Background);
            d7f5.setBackground(Background);
            d7f6.setBackground(Background);
            d7f7.setBackground(Background);
            d7g1.setBackground(Background);
            d7g2.setBackground(Background);
            d7g3.setBackground(Background);
            d7g4.setBackground(Background);
            d7g5.setBackground(Background);
            d7g6.setBackground(Background);
            d7g7.setBackground(Background);
        }
        else if (terrainE4D7 == 2)
        {
            d7a1.setBackground(Background);
            d7a2.setBackground(Background);
            d7a3.setBackground(Background);
            d7a4.setBackground(Background);
            d7a5.setBackground(Background);
            d7a6.setBackground(Background);
            d7a7.setBackground(Background);
            d7b1.setBackground(Background);


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,376
  • Joined: 27-December 08

Re: 250,000 lines of code

Posted 02 January 2013 - 03:24 PM

*
POPULAR

You should really look into arrays, Collections, and looping. If you find yourself naming variables a1, a2, a3, etc., it is time for two things:
-An array or Collection
-A more descriptive variable name
Was This Post Helpful? 7
  • +
  • -

#5 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 03:32 PM

The variable names are named after their coordinates that tell me the precise location that they describe. I suppose I COULD use arrays, but then the arrays would end up getting the names a1, a2, a3, a4, a5, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, d1, d2, d3, d4, d5, e1, e2, e3, e4, and e5. This would be so that I can easily track down exactly which variable/array controls which square's background image.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,376
  • Joined: 27-December 08

Re: 250,000 lines of code

Posted 02 January 2013 - 03:33 PM

Sounds like a grid, which would be good for a 2D array.
Was This Post Helpful? 2
  • +
  • -

#7 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 03:42 PM

Good point. I'd completely forgotten about multi-dimensional arrays. It's actually three sets of grids: a top level 5x5 grid containing the information on backgrounds for the whole game (the square that you're in is the large location), 7x7 grids for each square in the 5x5 grid containing information on the backgrounds for your large location (the square that you're in is the small location), and a final set of 7x7 grids of JPanels in each small location (but all large locations share one set of this smallest grid) containing the color for the backgrounds (and foreground in the case of the square that you're in).

For that layout I'd need a 4-D grid (not 6-D, because the final set of grids contains JPanels, not ints).
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: 250,000 lines of code

Posted 02 January 2013 - 04:46 PM

You need objects. Let's suppose you have a 5X5 grid of things - could be anything, for now. Say they're parking lots in a given city, and you're running a service that directs drivers to the best parking lot for their needs. What you need to know about each parking lot is what concerns you at this level: how many spaces are available, location, price. You don't need to know how many cars are in each lot or where each one is parked, and you shouldn't have to care about that. You just want to know if Jones can put his jalopy there.

Now, let's suppose you're running a particular parking lot. Now you care about things like where each car is located, when it's come in and when it leaves, and such like, but you don't care at all about any other parking lot in the world. Cars come in, you park them, and you charge the customer when they leave. You surely don't care about the workings of any particular car - that's someone else's problem.

And now let's suppose you're operating a particular car. You're concerned with making that car go - keeping it rom occupying the same space as another vehicle at any given time, getting it to where it needs to be. You don't care about the parking lots or the other cars in the lot, and you really aren't paying attention to the firing of the spark plugs or the other mechanical details of operating the car.

You see how this goes? At any point, you might be working with a ParkingLot class that knows how to accept and discharge cars, and how to report certain facts when asked, or you might be working with a Car, or you might be working with a SpaceAllocator. By breaking up the world into correctly-scaled chunks, you allow yourself to think about just what matters right now.


The same logic can certainly be applied to your game map - I don't know exactly how it would look because I don't have the details, but I'm willing to be you can simplify this a lot by composing the map out of one 5X5 grid, whose elements are objects. And those objects happen to be 7X7 grids, but the 5X5 grids don't know about that or care. And each of the objects in each of those 7X7 grids happens to be a 7X7 grid, but the grid that contains them doesn't care about that.


I have to say again that I'm impressed by your persistence in making this program - now it's time to make it less painful.
Was This Post Helpful? 2
  • +
  • -

#9 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 05:27 PM

I think that my program already fulfills your analogy - using your analogy, I can tell you this: the large location is controlled by a single variable.

the small location is also like this, but it also contains 49 JPanels:

a1
a2
a3
a4

all the way through g7.

each of these JPanels contains 49 MORE JPanels, with names given to them based on their "parking space" (so that I, the programmer can distinguish the "parking spaces", and so that the compiler can pass each as a different JPanel) within the parking lot and their position within the "parking space" (to distinguish them from the other JPanels, which are the cars in your analogy). The owner of a parking lot can't very well say that the same car parked in two different places at once, now can he?

I'll still be using the arrays idea, though.

This post has been edited by extremeblueness: 02 January 2013 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: 250,000 lines of code

Posted 02 January 2013 - 06:29 PM

Okay, I can see I'm not getting the point across. How does this sound? Based on what I'm seeing here, you can write this program in 1/100th of the space it took you if you do it with objects, and you'll have fewer bugs and more flexibility.

Does that sound like it's more fun, or less fun?

Arrays are nice, but you're still going to be doing far more work than you need to do for code that'll be harder to maintain, harder to update, and harder to fix than it should be by a factor of ten, at least. (in terms of time spent on a given problem)
Was This Post Helpful? 0
  • +
  • -

#11 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 07:26 PM

You do realize that the vast majority of the code is me setting the color of JPanels? Those ARE objects. What's really killing me isn't lack of objects, its that I didn't format just 48 mountains, 48 plains, 48 trees, and one polar bear; I formatted 1,225 mountains, 1,225 plains, 1,225 trees, and 49 polar bears. Each mountain, plain, or tree HAS to have 49 JPanels in it. In other words, I could cut my code down by 90% just by changing the number of objects I format.

Then the arrays idea presented by mac would cut it down by another 50%, leaving me with approximately 10000 lines of code.

This post has been edited by extremeblueness: 02 January 2013 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,376
  • Joined: 27-December 08

Re: 250,000 lines of code

Posted 02 January 2013 - 07:37 PM

I think if you're doing that much work, you're not automating enough. It boils down to good architecture and design, and I frankly think you lack it in your program (no offense intended). You should be able to automate the design of the plains, trees, and mountains. Again- classes/objects and methods.
Was This Post Helpful? 1
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: 250,000 lines of code

Posted 02 January 2013 - 07:41 PM

So you're saying that most of your source code is data? Even better - we've already covered that. Unless it's code, it shouldn't be in the code. If it's just "there's a polar bear at point p", you can read that out of a file. That way your code is easier to work with and smaller, and you can have as many games out of this as you can imagine.

Quote

I formatted 1,225 mountains, 1,225 plains, 1,225 trees, and 49 polar bears.


I'm going to say this once more and then give up: once you've written the code for one mountain, one plain, one polar bear, that's it. You don't write it 48 more times! That's what objects are for!
Was This Post Helpful? 3
  • +
  • -

#14 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: 250,000 lines of code

Posted 02 January 2013 - 08:19 PM

You finally broke through. You mean creating my own object classes.
Was This Post Helpful? 1
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,376
  • Joined: 27-December 08

Re: 250,000 lines of code

Posted 02 January 2013 - 08:21 PM

Swing is a bulky API, and it is easy to get bogged down if one doesn't have a good grasp of basic design principles and OOP. I think that's the case here. Make sure to get a good grasp on the basics before delving into the Java GUI.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2