Archive for Rebels A Star Wars Roleplaying Community
 



       Rebels Forum Index -> General Discussion
Rive Caedo

Rive's Programming Thread of Doom

As mentioned in another thread, I'm thinking of restarting work on The Battlesystem (AKA: Project Maelstrom).

For those of you that don't remember it, here's what it looked like in its last incarnation:
Spoiler:



Also as mentioned in another thread, the project is going to be a bit more ambitious in scope this time around (that last time around I had the system built from scratch to "nearly complete" in under a week since it had a very small scope).

That said, it's likely that sooner or later - I could use some help.

So, I'll be teaching some basic programming skills in this thread to give you the ability to do that if you're so inclined/ambitious/awesome.

The only recommendation is that you download Eclipse, since it's what I work in and will be pretty much required if you want to help. You can wait a few days on that.
You'll want "Eclipse IDE for Java Developers (91 MB)".

So here we go. The plan is for me to post a basic little tidbit of information that'll help you help me (and yourself if you find this sort of thing interesting and decide to pursue it on your own) every day. We'll see how that goes. As long as 2 or more people are following along, I'll probably keep it up.

Day 1
Spoiler:

Data is obviously pretty key to computer systems. 1's and 0's. Thankfully, Java is a "high level" programming language - so we don't need to worry about 1's and 0's.

What we do need to worry about is "primitives" and "objects".

Today, we'll just cover primitives.

A primitive is a data type that stores a value. Java's primitives include: byte, short, int, long, float, double, boolean, and char.

We'll mostly be interested in the ones in bold.

An int is short for "integer" - meaning it can hold whole numbers (2, 5, -3, 7, 0, etc).

A double is similar to an int, but it allows decimals (2.556, -3.56, 1334, etc).

A boolean can only hold two values. True - or false.



Day 2:
Spoiler:

Review from day 1:
Java uses primitives and objects to hold data.
The primitives we're mainly concerned with are ints, doubles, and booleans.

So how do we actually create a primitive to hold data?

It's a relatively simple process. We simply need to tell Java what primitive we want and give it a name.

For example:
int counter;

I've just declared an int named 'counter'.

Then I can initialize it by giving it a value.
counter = 3;


It's worth noting that you CAN declare and initialize on the same line:
int counter = 3;

That's perfectly valid. In either case the end result is storing the integer value 3 to our 'counter'.

You may have noticed the semicolon at the end of those lines. A semicolon is how you tell Java you're done writing a particular line of code.

Now let's talk a bit about manipulating data once we have it.

int value1 = 5;
int value2 = 7;
int value3;


You can see we have 3 ints here. The first two have been initialized by associating data with them, the last has not.

value3 = value1 + value2;

That's obviously valid. value3 = 5 + 7. Thus, we've just assigned 12 to value3.

Not everything is so obvious though:
value3 = value3 + 2;

Well that's crazy! 12 = 12 +2?  12 = 14? That's madness! This is some 1984 2 + 2 = 5 lies! LIES!

... But this is valid in Java.

Why?

We're not actually saying "12 equals 14"
What we're saying is: "value3 is assigned the value of value3 plus 2."

'=' in Java should be read as "is assigned" not "equals".

There are two other operator symbols we should be aware of at this point:
'++' meaning "increase by 1"
'+=' meaning "increase by the following value"

Example:
int value1 = 5;

value1++;


value1 was assigned the value 5 and was then increased to 6 on the next line.

value1 += 1;

That's valid and does the same thing, but it's longer. So we generally use the '++' symbol when we want to increment something by 1. That sort of thing comes up very often.

So now value1 has increased to 7.

value1 += -7;

That reads a bit oddly in english, "value1 is increased by negative 7" --- but it is valid.

value1 is now 0.

It's worth noting we could have done the same thing with a '-=' operator instead and made it much more easily read:

value1 -= 7;
"value1 is decreased by 7"

value1 is now -7.



Day 3:
Spoiler:

We're going to talk about objects tomorrow. But today we're just going to deal with a special object: String.
A string is an object, but it's used soooo often for soooo many things that Java has special rules for using them. In fact, it makes them look a lot like primitives.

So today we're going to write a program that's pretty much the first program every programmer being taught programming after 1970 has done.

First, let's remember how we declare a variable:
String myString;

I've declared a String object.

Now, as I said, for most objects this part would look different, but for Strings - initializing it will look pretty much the same as it did for primitives.
myString = "Hello World!";

So now I've assigned "Hello World!" to myString.
You might wonder why it's called a string. A string object is actually a bunch of char (short for "character") primitives that have been 'strung' together. It's much easier to work with a String than a bunch of individual letters. Imagine if we had to say:
char char1 = 'H'
char char2 = 'e'
char char3 = 'l'


And, in fact, that IS similar to what we had to do back in the old days of programming! Be thankful that's no longer the case! Very Happy

Alright, so now we have the phrase "Hello World!" stored in Java code. But how do I display that to the world? How do I access that data?

1. Open up your Eclipse program. Just press "Ok" when it asks you to select a workspace.
2. You're probably on some sort of welcome screen. Find and click the button (should look like a bent arrow) that says "Workbench". It's probably in the upper-right hand corner.
3. With any luck, you're on a screen that looks like:


4. Select the "File" menu, then hover over "New", then select "New Java Project"
5. Type in a name for the project folder, maybe "Rebels Stuff". Then click "finished" at the bottom. You can ignore the rest of the settings.


6. The project folder should have appeared on the left side of the screen (as in the screenshot above) now. Right click it and say "New" then "Class" (we'll talk more about classes later). It should be the fourth one down.
7. Name the class "HelloWorld" - you can ignore the rest of the settings and just click "Finished". Make sure you didn't put a space between the words in the name or it won't work.

Now things should look like this:


Alright, fantastic!

Now, you know that a semicolon ends a line of code.
Now you need to know that curly braces ( '{' and '}' )enclose sections of code.
We'll talk more exactly about when you to use those later.

So, between the two existing curly braces, copy and paste this line in:

public static void main(String[] args){

That line is quoted on Urban Dictionary.com as "The most annoying line a beginner Java programmer has to type in each one of their programs." and they're right Smile

Now, you'll notice that line ends with a curly brace. That means you need to close it.

Fortunately, you can just press "enter" now and Eclipse should add the closing curly brace for you - since Eclipse is cool like that.

Make sure things look like this now:


Now declare and initialize a String saying "Hello World!". You can look back up above if you need help doing that, or you can just scroll down and look at the next screenshot:








You'll notice that 'myString' (or whatever you chose to call it) is underlined in yellow. That's an Eclipse warning. It's Eclipse telling us (in this case), "You just declared and initialized a variable... but you aren't using it anywhere? That's a waste of space. What's up?"

So let's use it.

Here's another piece of code you can just copy and paste - you don't need to understand it perfectly yet:
System.out.println();

That's how we tell Java to print something to our console. You'll see the console in a moment.

Now, put that line of code in your program - BELOW where you initialized your string.

Between the parenthesis in that line of code, put the name of your string.

You'll notice the yellow warning line went away. Because now we're using that variable.

Try to finish that step on your own, but if you have trouble then you can click here for a solution: [Solution]

Now, finally, go up to the "Run" menu (far to the right of "File") and press "Run" - the first option in the dropdown.
Or you can just hold ctrl and press F11 on your keyboard.

It's possible that this screen will come up, maybe it won't:

If so, just select "Java Application" and press "Ok".

If everything's gone right. This should show up at the bottom of your Eclipse window!


Hello Java!



Day 4:
Spoiler:

Today we're going to do two things. We're going to introduce the concept of an object - and we're going to run through another program (or, if you're more ambitious, you can write it yourself).

Pretty much everything in Java is a class, an object, or a method - or one of those primitives we've already talked about.
You've already used one class, actually, since you probably noticed your first program started with public class HelloWorld. But we're still going to hold off on talking about those, for a moment.

And, as I told you, you already used an object - the String.

But, as I ALSO told you, Strings are special in Java - since they're used so often. BUT you can also use Strings like regular objects - skipping the shortcuts Java lets you use for them. So let's do that.

Here's how we declared and initiated a string using Java's shortcuts:
String myString = "Hello World!";
Which was pretty much how we did primitives.

If, however, we use our String like most objects... it changes a bit.
String myString = new String("Hello World!");

The declaration stayed the same, but the initiation (right of the '=') changed.

That basically reads, in English, as: "I declare a String object named 'myString' and then I initiate it as a new String object with "Hello World!" as its arguments we're passing it.

We'll talk more about passing arguments and objects tomorrow. For now, we're going to pull back and do another program and look at another concept.

-----------------

Recall that we could modify ints we'd declared and initiated using operators like '+=' or '++'

Now we're going to introduce a new concept: The While loop.

A while loop looks like this:
while(){

}


Fairly simple looking. Inside the parenthesis we can place a condition. The while loop will keep repeating - over and over - as long as that condition is true. For instance, we could say:
while(true){

}


Well, you don't want to say THAT because that's ALWAYS going to be true. Your program will start repeating forever in an "infinite loop" and lock up - which is bad Smile

But we could also do something like:
int counter = 16;
while(counter >= 4){
   System.out.println(counter);
   counter = counter / 2;
}
System.out.println(counter);

So we started with 16 stored to our 'counter'
Then we got into the loop. The condition is: while 'counter' is greater than or equal to ('>=') 4 - keep going.

16 is greater than or equal to 4, so we go into the loop.
We print out 'counter' (16).
counter is assigned the value of counter (16) divided by 2. So now it's 8.

Now, we go back up to the top of the while loop to see if we need to keep going.

'counter' (Cool is still greater than or equal to 4. So we keep going.
We print out 'counter' (Cool.
We assign it the value of itself divided by 2. So now it's 4.

Again, we go to the top of the while loop. Is 4 greater than or equal to 4? Yes, it's equal.

We print out 'counter' (4).
We assign it the value of itself divided by 2. So now it's 2.

We go back to the top of the while loop. Is 2 greater than or equal to 4? No. So now we're done and we exit the while loop.

Finally, we print out 'counter' one last time, since that's the next line of code outside the loop. (2).

Okay. Hopefully we understand while loops now. Let's apply them to a different situation.

------------------------

"Sir, the possibility of successfully navigating an asteroid field is 3,720 to 1"
"Never tell me the odds!"

I want you to write a program in a class called "NeverTellMeTheOdds" in which your program prints out all the numbers up to 50 - starting with 0 - that are even. No odds should be printed out.
Try to figure out how to do this on your own. But, if you have trouble - click below for a solution. I say 'a' solution because there are many different ways you could solve this problem.

[A Solution to this Problem]



Day 5:
Spoiler:

Today should be very easy if you understood yesterday's stuff.

We talked about while loops - and how they're based on conditions.

It's VERY often the case that we want to check a condition - but we don't want to loop.

For this we use:
if(){

}


Again, with a condition being placed in the parenthesis.

If we take a look at my solution to yesterday's problem:


We could add something like:
if(counter == 24){
System.out.println("Halfway there!");
}


We could add that just after our main print line, so our results would come out as, now:

...
20
22
24
Halfway there!
26
28
...


There was a new symbol there '=='
As we discussed, '=' roughly equates to "is assigned" in Java.
'==' does, in fact, mean "equals".



Day 6:

Spoiler:

Let me start by saying this: Anything you can do with a for loop can also be done with the while loops we already learned.

But, for loops are still highly useful and can make things a lot "cleaner".

Let's look at our "NeverTellMeTheOdds" program again (or, at least, my version):



It turns out that we want to do things like that a lot. That is, use a loop to go through a sequence of values or objects.

So, we have the for loop. Which is constructed like so:

for(initialization; termination; incrementation){

}


Initialization let's us create a counter-type variable. Termination lets us define the condition to end the loop. Incrementation says what to each time we loop.



By using a for loop instead of a while loop with an externally defined counter - we saved 2 lines of code and reduced chances for error.



Day 7:
Spoiler:

Recall that a few days ago we talked about how the String class in Java is special and normally you declare/instantiate it pretty much like you would a primitive. BUT we could also declare/instantiate it like a regular Object. Which looked like this:

String myString = new String("Hello World!");

So let's talk a bit more about objects.

An object is defined, in Java, by a Class. We've previously used classes to run our programs, but we can also use them to define objects.

Let's say, for instance, we want to create a class that defines Force Powers in a video game. We might have something along the lines of:



So look there at that:
public ForcePower(String powerName, int powerCost, String powerDesc){

That's called a constructor. It's how was say we want an object to be instantiated.

So now we'd be able to say - in another class that contains our program (that's in the same project/directory as our ForcePower class):

ForcePower forceChoke = new ForcePower("Force Choke", 15, "Through the power of the darker aspects of the Force, one can constrict the airway of their target.");

Huzzah! We've instantiated a new ForcePower object based on our construction rules in our ForcePower class!

That's all well and good. But what can we do with it?

To do something with our class, we need to define a method. We've already seen at least one method before.
System.out.println("Hello World!");

Somewhere in Java - in the code for the class System - you'd find the code for the method println. And then it would have code defining how to output data to the console.

Here's a theoretical method we could add to our ForcePower class:



As you can see, it looks fairly similar to our constructor.

The biggest difference is that constructors have no return type, while methods do.

public String use(...){

String is our return type for this method. We'll talk a bit more about return types on another day, but, let's suffice to say for now: after a method does its work, it's often useful for it to report something back to the class that called that method.

Our method uses a theoretical class "Player" (there is a red line under it because it doesn't presently exist in our codebase - but let's pretend it does).

A Player, in our system, has a number of "Force Points" to use Force Powers. In fact, let's say that the constructor for Player looks like this:

...
public Player(int startingForce){
...


Player has a method to report back (return) how many Force Points they have right now: getForcePoints()
A player also has a method to change their Force Points to another number: setForcePoints(int newPoints)

Rather than explaining exactly what this method does step-by-step, let's look at a concrete examples.

Let us imagine we have another class that we're defining a runnable program.

Player testPlayer = new Player(100);
ForcePower forceChoke = new ForcePower("Force Choke", 15, "Through the power of the darker aspects of the Force, one can constrict the airway of their target.");

System.out.println(testPlayer.use(forceChoke));
System.out.println(testPlayer.getForcePoints());


In this case, a Player is constructed with 100 force points to start with. Then we define our "Force Choke" ForcePower - it costs 15 points.

Therefore, when we call the use method of that power - by our player. It runs through just fine. When it got to this check in our ForcePower's use method:

if(caster.getForcePoints() < cost)

Java said: if(100 < 15).... no, 100 isn't less than 15, let's skip the code in this if section.

so then it moved on to:
caster.setForcePoints(caster.getForcePoints() - cost);
return "You use " + name;


So we'd print out the return statement: "You use Force Choke"

The next line of code in our program:
System.out.println(testPlayer.getForcePoints());

Would print out 85, since our testPlayer used 15 points in that use method of Force Choke (cast.setForcePoints(100 - 15)Wink.

Let's look at a modification of that same example:
Player testPlayer = new Player(10);
ForcePower forceChoke = new ForcePower("Force Choke", 15, "Through the power of the darker aspects of the Force, one can constrict the airway of their target.");

System.out.println(testPlayer.use(forceChoke));
System.out.println(testPlayer.getForcePoints());


In this case, when we got to the testPlayer.use(forceChoke):
if(caster.getForcePoints() < cost)

Java said: if(10 < 15).... Yes, that's true. Let's do the code in this if block.

return "You don't have the Force power to cast " + name;

When a method returns - it stops executing any more code in that method. So we don't need to worry about anything below that in ForcePower's use method, in this case.

So our println statement would come out: "You don't have the Force power to cast Force Choke"

And the next line of code:
System.out.println(testPlayer.getForcePoints());

That would print out as "10" - since we never modified the testPlayer's Force points. We only determined they didn't have enough and returned.

Xander Vos

Yeah I'll be interested in helping you out. I'll download the program in the next few days.
Rive Caedo

*nods*

Presuming this system gets laid out the way I envision, there'll basically end up being 4 tiers of help.

I. No help at all (/bioshock The Parasite Wink )
II. Able to make new basic force powers, enemies, and maps via easily modified text files.
III. Able to make some basic new features by knowing the basics of programming (example: adding a new force power that doesn't work like any previously created force powers)
IV. Rive (able to create the backbones that support things like force powers and maps in general)
V. Above Rive (Get away! Make your own program! Razz )

So my goal with these "lessons" is to - eventually - get people up to level III. That way I can spend more time on building backbones while others are able to flesh them out with content.

I am, of course, getting ahead of myself. So I'll try to pull back from grand plans for the moment and focus on little plans Smile
Xander Vos

Ha no worries, happy to help in anyway possible. Smile
Dakoth

Well its downloading.  The primitives remind me a lot of the WCIII editor.
Rive Caedo

Dakoth wrote:
Well its downloading.  The primitives remind me a lot of the WCIII editor.

You'll absolutely find any experience you have with the advanced features of the WCIII editor to be very helpful learning programming - because a lot of the WCIII editor is basically programming with training wheels.

I mean, take the very first trigger condition. It's just a flat boolean comparison. Something is equal to Something Else.
I liked unit-type comparisons.
(Unit-type of (Triggering Unit)) equal to (Footman)

We're not quite ready to get into actual Java syntax yet, but it shouldn't be hard to see that this is the same idea:
if(triggerUnit.getType().equals(footmanType))

Essentially, while the Warcraft III editor gave you lots of specific comparisons to use, when you get down to the actual code you can compare whatever you want.
The trick is that you also need to know exactly what your options are, hehe.
Xander Vos

Uhh, why would you want to compare things? Your post made very little sense to me. Razz
Rive Caedo

Well Dakoth was the one asking about the Warcraft III unit editor, not you - so hopefully it made sense to him Smile

By about day 3 or 4 you'll probably understand what types of things we can compare. But, to give you a quick example of why you'd want to do that.

Let's say the characters in our system have "types" attached to them - IE: Human, Droid, Rancor, etc.

Once we've established types, then we're able to do things like, for example, make it so that when you press your "Jedi Mind Trick" button on a unit, it'll check to make sure that unit is biological.
And then, if the comparison says "no, that thing you're targeting is not biological" we can make the ability not work and spit back to the user:
"Jedi Mind Trick failed, does not work on droids."
Xander Vos

Well yeah but I was trying to understand what you said.

Ah k that kind of makes sense now, so you'd want to use the like function to give greater detail on what a character is.

For instance could you use it to make different classes of Jedi?
Rive Caedo

Xander Vos wrote:
For instance could you use it to make different classes of Jedi?

Could I use comparisons to do that? No, not really.

What I CAN do (oh my, now we're really getting ahead of ourselves) is have, say:

My generic "Jedi" type. Which has force points and can learn Force powers.
Then I can have a more specific Jedi type, say "Jedi Guardian" that extends that generic Jedi type.
From the program's point of view, if I create a Jedi Guardian - it'll count as a Jedi Guardian (so I can run comparisons/checks and let them learn specific lightsaber attacks that other Jedi can't learn) AND a Jedi (so it'll have force points and such).

Don't worry at all if you don't understand the concept of "extends" or types or anything at all with this. As I said, we're way way ahead of ourselves.

Just make sure you understand an int can hold whole numbers, a double can hold decimal numbers, and a boolean can be true or false Smile
Xander Vos

How do they 'hold' whole numbers?
Rive Caedo

That's exactly the question you should be asking Very Happy

And you'll receive the answer as Day 2's lesson tomorrow, hehe.
Dakoth

I can't wait to drown myself in an endless  sea of conditions! yay!  Saving variables is fun!
Xander Vos

I eagerly anticipate. Smile
Sirak Sazen

Uhhhh, what?  Shocked
Xander Vos

Geeze Sazen, keep up.
Rive Caedo

Day 2 has been posted - check the first post. It's a bit longer than Day 1 and covers some important topics - so definitely ask questions if you need clarification or expansion.

I'm probably going to have you write your first program (a very, very, very small one) on Day 3 or 4. So anyone participating, try to get Eclipse downloaded by then.
Xander Vos

I actually understand Day 2's lesson a lot more.

Uh, I'm trying to download it and it's not working.. It says the link is broken.

EDIT: Don't worry, I got it through Bit Torrent.
Dakoth

Easy as pie, overlord.
Xander Vos

OK, I've downloaded it, now I'm not sure what to do with it. Razz
Dakoth

I downloaded it yesterday, but its just sitting on my desktop, i'm far too afeared to open it.
Xander Vos

I opened it and it asked something about a Workshop, so I promptly closed it again.
Rive Caedo

What?! That was your ONLY chance to set a workshop! Now you have an anti-workshop saved to your registry in Windows! You'll need to reformat to get a regular workshop working Sad

Wink

Anyway, rest assured I will tell you exactly what you need to press to get things spinning. That's pretty much unavoidable, I mean:

public static void main(String[] args) {

That line? That's the line that starts off pretty much every Java program. Don't believe me? Put it in quotes and do a Google search - over 35 million results Razz

And yet it'll be another day before you know what a String is, another few days before you know what "public" means, a week or two I'd figure before you know what "void" means, and I'm not sure if you'll EVER need to know what "static" and "args" are for within the context of this project. You might need to know "[]" - but I'm not sure when it'll come up.

... So you'll just type that line in before your program and need to trust me when I say it's required  Laughing
Dakoth

I demand to know exactly what "args" means! Unless of course the explanation is over 18 words in length.
Xander Vos

Haha fair enough.

BTW, you me and Dakoth are online, where's Emby? We could get our WCIII came going.
Dakoth

I don't think anyone wants to get thier "came" going with you, Xandus.
Rive Caedo

Dakoth wrote:
I demand to know exactly what "args" means! Unless of course the explanation is over 18 words in length.


Let's see:

"args" stands for "arguments". If your program were intended to be run from the command line then it--------

Darn, out of words.
Sirak Sazen

Still no clue what's going on.
Xander Vos

Oh you know what I mean Dakoth.

AND IT'S ALIVE!! MY WRECK IS ALIVE!! Very Happy I drove it home. Very Happy Much more rattly but road-worthy! Smile
Dakoth

Huzzah! Tis a miracle!
Xander Vos

So happy! Haha they said the smoke will lessen the more I drive so for now I have to drive it round at night to get it all out, then it should be ok. Smile
Rive Caedo

Sirak Sazen wrote:
Still no clue what's going on.

In essence, we're planning to make a computer game from scratch.

A year ago I did a much smaller project related to it on my own. This time, since it's larger - I'm hoping to get some help.

Dakoth and Xander so far are the only ones to step up to the plate and brave the perilous madness that is computer programming Smile

I'm teaching basic programming concepts day by day. They're contained in spoiler boxes of the first post.
Xander Vos

Yeah Sazen, step up to the plate.
Sirak Sazen

I'd probably just hit the backspace button on everything.  Razz
Xander Vos

Hey, I'm one of the most computer illiterate people going around. Just give it a try.
Sirak Sazen

Sure. I'll download everything on a night when my computer isn't laggy.
Xander Vos

Sweet. So with four people working on this - well three semi-competently and one efficiently - maybe we can make progress. Smile
Sirak Sazen

More like one efficiently, two semi-completely, and one monkey with a keyboard writing the works of Shakespeare.

IT KOOD HAPPEN
Xander Vos

If you had enough monkeys.
Dakoth

And enough computers.
Xander Vos

Imagine a monkey writing all of Romeo and Juliet by chance, except for one word. Would that count?
Sirak Sazen

That depends. If it's a harmless chimpanzee, than no. It can't hurt you. Just beware the AIDS. Now, if it's some King Kong gargantuan, give him whatever he wants and hope you're not eaten.
Rive Caedo

It depends on the word. I mean, if you end it with:

Go hence, to have more talk of these sad things;
Some shall be pardon'd, and some punished:
For never was a story of more awesomesauce
Than this of Juliet and her Romeo.

I don't think that counts Razz
Xander Vos

Anywho, back on topic.

I assume we'll be learning how to write a simple program tomorrow Rive? What's the point of the numbers? Like, what do they let you do?
Rive Caedo

Mostly just keep track of things I suppose?

I mean, here's a code snippet from the system I put together last year:

private int bank;
private int maxHP;
private int currentHP;
private int maxForce;
private int currentForce;
private int activePointsPerRound;


So those were all the plain ints that helped to define a character in the battle system.

And, we're ahead of ourselves, but here's a snip of code affecting on of those numbers:

public void saberAttack() {
          p[pN].setCurrentHP(p[pN].getCurrentHP() - investedPoints);
}


That's obviously a bit hard to read since.
A. You didn't write the code.
B. You haven't written any code, so you have no frame of reference.

But let's go through it step by step:

public void saberAttack() {

That just means I'm defining this method as being used for lightsaber attacks. (We'll talk a LOT more about methods later).

p[pN].setCurrentHP(p[pN].getCurrentHP() - investedPoints);

Without going into too much detail about what '[]' means yet, let's just say:

p[pN] means "the player being attacked"
setCurrentHP is another method. It's used, obviously, to set the health of something.

When we say p[pN].setCurrentHP --- the period means we're using the setCurrentHP method of "p[pN]"
In other words, I'm going to change the health of the player being hit by the lightsaber. Makes sense, right?

(p[pN].getCurrentHP() - investedPoints)
That's the bit inside the parenthesis of the "setCurrentHP"
p[pN].getCurrentHP() will give us the current health of the player being attacked.
"investedPoints" is the amount of points that were put into the saber attack being made - so that's how much we want to slice off their health.

So we're saying: Set the HP of the other player to (whatever their HP is right now minus the damage of this lightsaber attack).

Again, don't worry if that seemed confusing to look at since that's a ton of stuff we haven't gone over yet. But hopefully it shows a decently easy to understand real example of using numbers to track things in a program?
Xander Vos

Ah k, and where would the numbers go in an example like that? Would you previously have set up that SaberAttack has a certain number which is the damage?
Rive Caedo

If I had showed you the code for a Force Push, yeah - you would have seen something like "damage = 2;"
But the code for Force Powers is more complex than the lightsaber strikes, so that'd be a bad idea Laughing

Basically, under the ruleset a year ago, you started off the round with a set number of points (8 was the default I think). All those counted as "invested points".
You could put those points in the "bank" to be used for a later round - or leave them where they were.

However many points you still had invested at the end of the round (could be higher than 8 if you'd banked points in a prior combat round and had taken them out) would become the damage your lightsaber attack would do.

So there's no set certain number for SaberAttack. It's based on that investedPoints value - which is modified in other places.
Xander Vos

Oh ok, but yeah that's what I mean, so it refers to pieces of code which DO have numbers in them. Just because the bit of code you just showed had no numbers in it so I wasn't sure how it linked in with what I'd already been shown.
Rive Caedo

Right, it's a bit harder to follow in a larger system since basically - somewhere in that code there's probably something like:

maxHP = 25;
currentHP = maxHP;

So there's a hard number: 25. Then we copied that over to currentHP (since a character should start at their max).

After that though, we won't see many hard numbers. Since you'll just see stuff like:
setCurrentHP(player.getCurrentHP() - forcePush.getDamage());

Now what's REALLY nice about that is that it means that if I go into my code defining Force Push and say "You know what? I think it should do 3 damage, not 2" I can just change that one single number: "int damage = 2;" to "int damage = 3;" and then that change will be applied EVERYWHERE --- since we're not using hard numbers, we're using variables.
Xander Vos

I get it! That makes a whole heap of sense now. Smile

I still don't get how I'd write it myself, granted, but looking at what you just wrote makes a whole bunch more sense to me.
Dakoth

Well tweaking values is much much easier than understanding fully and writing code, so we've got that going for us.
Rive Caedo

Day 3 has been added - remember, go look in the first post. It's fairly long because I hold your hand through pretty much every step of writing your first program. Most of these steps will become second-nature to you if you keep at this for awhile.

Still, it's a lot of stuff to take in and there's a lot of things that can go wrong. So don't get frustrated if they do and just take a screenshot of what's going wrong and post it here so I can help Very Happy

Hello World is, as I say in Day 3 post, THE beginner program. I've actually made it a tiny bit more complex for us (because we've been talking about declaring and initializing variables) - instead of just printing the phrase directly.

Anyway, if you want to see the wide-wide range of "Hello World" programs out there - in pretty much every computer language out there (even a lot of really obscure ones - and some joke ones): Wikibooks has a pretty amusing (or daunting) list:
http://en.wikibooks.org/wiki/List_of_hello_world_programs
Dakoth

Do I win?

Spoiler:

Xander Vos

Ok, well, slightly cheated using Dakoth's image because I guess I didn't quite understand your instructions. From the bit about "System.out.println(myString);}" I didn't put the myString in brackets.

Spoiler:

Dakoth

Yeah I was a little confused over that part too, I thought you wanted me to put the "hello World!" bit in the parenthesies, even though that didn't make much sense from my end.
Xander Vos

My only problem was not putting "myString" in the brackets, and then thinking the line was in the wrong place and subsequently stuffing up further. Razz
Dakoth

Thats what Rive gets for skipping a screenshot!
Xander Vos

Ha yeah, it was like, ok everything's going fine, then he gave some information without a screenshot and I was like eep!
Scion

Hah, I actually get this because I'm learning Java RIGHT NOW IN SCHOOL!!!

That being said I really don't want to download Java on this PC, but maybe I will, this could be great.

EDIT: Changed my mind, this will probably help me for the class anyways.  Now just to find time in between Borderlands segments.
Dakoth

Lend us your sord, sir scion!
Scion

DLing now, I hope Eclipse works like BlueJ?  That's what I'm working on anywho.
Xander Vos

Huzzah! Now I just need to learn it adequately to write things without heavily relying on a step-by-step from Rive. Razz
Scion

Gah, now I can't figure out were to unzip the files, I totally forgot how i did this the first time.
Xander Vos

I just extracted it into a folder called Eclipse.
Rive Caedo

Hm. You might try just going ahead and running Eclipse to see if it works? Many computers these days have Java pre-installed.
Scion wrote:
DLing now, I hope Eclipse works like BlueJ?  That's what I'm working on anywho.

I just took a look at some screenshots of BlueJ.
It does look like a pretty good tool for learning Java. But it's overly simplified from Eclipse. Due to that, it doesn't look like it'd be able to handle some of the stuff I want to do with this project. Therefore, it's better to just to jump into Eclipse instead of learning 2 systems.

Xander Vos wrote:
Ha yeah, it was like, ok everything's going fine, then he gave some information without a screenshot and I was like eep!

Yeah, that was deliberate Smile

I actually wanted to say:
"Try to figure out this step on your own, but if you have trouble - you can click on this spoiler box for a screenshot of the solution."

But...

Spoiler:

[spoiler]

[/spoiler]

Spoiler boxes inside spoiler boxes don't work.

In hindsight, I should have just said: "If you have trouble - click here for the solution" and just done a regular link to the photo."

I've gone ahead and edited that into Day 3 for anyone else following through from this point on.

Also, good work Dakoth/Xander on the program - even if you didn't say hello to the world Wink
This is a good point to introduce one of my favorite Eclipse features: Format.

Holding control and shift and pressing F will format your code (or at least do its best) into the way Java professionals like to format code. You can also click on the "source" menu and find it about a third of the way down the dropdown menu.

The difference you'll see happen is you'll see your ending bracket move from the end of the line, where both of you currently have it, onto its own line.

This makes it easier to see that section of code as... a section.

Spoiler:

Dakoth

Yay! Rive praise!
Rive Caedo

Day 4 has been added. We might end up talking about this one for 2 days instead of 1 since...

a.) There's quite a bit of material there.
b.) I want to give Sirak a chance to catch up, if he wants, and Scion a chance to get into it.
Scion

Nah, It says Eclipse executable launcher was unable to locate its companion shared library.

If you can figure out what I am doing wrong I'd be gald to help.
Rive Caedo

You could see if this helps:
http://vsingleton.blogspot.com/20...ecutable-launcher-was-unable.html
Scion

I'll try it tomorrow, tired now.

Then I'll build a Rock Paper Scissors game the likes of which have never been seen!
Rive Caedo

Scion wrote:
Then I'll build a Rock Paper Scissors game the likes of which have never been seen!

Pfft, Rock Paper Scissors? Waaaay too easy. I will give you credit if you implement these rules though: Smile
Io Lee

"I prefer George Lucas disappoint me in the order he intended."

LOL
Xander Vos

Well I have to go study for a few hours, I'll have a look at it tonight.
Dakoth

Nooooooooo! you must warcraft!~
Xander Vos

Nao?
Sirak Sazen

Rive Caedo wrote:
Day 4 has been added. We might end up talking about this one for 2 days instead of 1 since...

a.) There's quite a bit of material there.
b.) I want to give Sirak a chance to catch up, if he wants, and Scion a chance to get into it.


I think I'll let you guys work on this. Maybe next year.  Wink
Scion

OK, got it up and running.  I did what the second guy did, reDLed and Unzipped.  Worked Fine.

That being said, it is much more overwhelming than BLueJ, I can't actually get to a programming page. Embarassed

But I will call you on your Rock Paper Scissors Lizard Spock challenge.  (If it's even possible that is).
Dakoth

Here's my program, it works and I didn't cheat with the link Smile

Spoiler:

public class NeverTellMeTheOdds {public static void main(String[] args){
int counter = 0;
while(counter <=50){
System.out.println(counter);
counter = counter += 2;

}
}


}

Rive Caedo

Day 5 has been posted, but it's very short. If you understood Day 4's stuff, Day 5's concept should be a no-brainer.

It's worth noting that we're probably about halfway to having enough stuff defined for you to be at the "Level III" I previously defined.
Rive wrote:
III. Able to make some basic new features by knowing the basics of programming (example: adding a new force power that doesn't work like any previously created force powers)


Dakoth's Day 4: Looks good to me. Although...

Spoiler:

counter = counter += 2;

The 'counter =' on the far left is basically doing nothing. You're saying:

counter is assigned the value of counter being assigned the value of itself plus 2.

counter += 2; ("counter is assigned the value of itself plus 2.") would have done the job on its own.

It's definitely more important that your code works (which yours does) while you're learning. So don't worry about it too much Smile

Dakoth

Spoiler:

 It would seem that I did indeed subscribe myself to the redundant department of redundancy.  I shall throw myself upon my sword posthaste.



Day 5's simple enuff.
Rive Caedo

For bonus credit you could read this, I suppose.

We call stuff like if statements and while loops (and for loops, which we'll talk about briefly soon)  control flow statements - since they control how you move through your program's code.

The link talks about switch statements - an alternative to if statements that are good to use in some specific situations.

I don't generally use them myself. I'll write up a big group of if statements and - when I'm finished - think "Oh, you know, that would have been a good place to do a switch block... oh well, I've already written these if statements" Razz
Xander Vos

Woo! I figured Day 4 out all by myself!! Very Happy

I wasn't sure what to do because initially I had it around the wrong way, then I tried making it NOT equal odds, and that didn't work, so eventually I made it Counter = Counter -2 and it worked Very Happy



EDIT: Just realised 0 comes up, but when I replace >= 2 with > 2 it's all good.
Rive Caedo

Actually 0 does count as an even number, and I said starting with 0, so it should be included Smile

Anyway, good work. If I had specified, "print the numbers in order - least to greatest" - then your answer would be wrong, but I didn't - so I suppose you're fine, hehe.

I'm actually a bit surprised both of you got it without needing to ask questions. You either have a more natural knack for programming than most people - or I'm better teaching things than I give myself credit for Razz
Xander Vos

Well.... I copy pasted the chunks of text from the start of this because I wasn't sure how to get it all right, then I looked at the 16 and the counter / 2 and tried to figure it out. Believe me, it took a while and I got into a loop where it kept printing 0s so I knew I'd done something wrong. Wink
Rive Caedo

There is another pretty good way to solve this problem, but it involves a mathematical concept you may not have even heard of - or, at least, not being used in this fashion. And it involves using an if statement anyway - which we didn't see until the next day, hehe.

Most students being taught division for the first time learn the concept of a "remainder".
Then they start doing more precise division allowing for decimals and such and the concept of a remainder is quickly abandoned.

But remainders are useful for computers! We obtain them via modulo operations. Designated in Java as '%'

So, a clever little test if something is odd is to say:
Does 2 evenly divide this number?
If so, it's even. The remainder would be 0.
If not, it's odd. The remained would be 1.

So this:


Is another, completely valid, way to solve the problem.

A good example of if statements from today and a good example that there can be many, many, many ways to approach a problem in programming Very Happy
Xander Vos

In the "public static void main(String[] args) {" line, do we ever put anything in the []s?
Dakoth

.....Did you just insinuate that we've never heard of remainders?!

But yeah, it was made a lot easier by having the  println and starting statement of doom available for copypasta.
Xander Vos

Yea... dunno how I'm gonna go when you say, "Hey Alex, can you create a new character for me? kkthxbai"
Rive Caedo

Dakoth wrote:
.....Did you just insinuate that we've never heard of remainders?!

I insinuated you had heard of remainders - but had never heard of an operation called modulo to specifically obtain them - and had, most likely, never used a remainder for doing any useful work Smile

I was certainly surprised when I learned how often remainders are used for computers. If you weren't, good for you Razz

Xander Vos wrote:
In the "public static void main(String[] args) {" line, do we ever put anything in the []s?

No.
What the [] is doing is defining an array - which we'll talk about later.

For a specific answer about what that array is used for, you can read the somewhat complex answer here.

But, since our system will use a GUI (buttons and stuff - like the last one did), we don't really need to worry about the command line.
Dakoth

Yippie! GUI!
Xander Vos

Rive Caedo wrote:
Dakoth wrote:
.....Did you just insinuate that we've never heard of remainders?!

I insinuated you had heard of remainders - but had never heard of an operation called modulo to specifically obtain them - and had, most likely, never used a remainder for doing any useful work Smile


Remainder theorem ?
Rive Caedo

Fair enough, I suppose, if you used that. I can't recall ever using that back in the day (though I do remember things like completing the square, factoring, and quadratic formulas).

Still, my only point was that I highly doubted either of you had run into modular arithmetic and its applications yet.
Wikipedia wrote:
Modular arithmetic is referenced in number theory, group theory, ring theory, knot theory, abstract algebra, cryptography, computer science, chemistry and the visual and musical arts.

Usually anything with "theory" after it isn't taught at the high school level Laughing

We're interested in the computer science applications, obviously. Though I'm edging our lessons here far more towards the "computer" than the "science".

But, just for fun, let's take a look at the "Introduction to Computer Science" course at MIT

Things in cyan we've already talked about to some degree, things in bold are things I intend to talk about before saying "alright, that's as far as I feel confident teaching - and it's enough for you to do a ton with this project."
MIT wrote:
1 Goals of the course; what is computation; introduction to data types, operators, and variables
2 Operators and operands; statements; branching, conditionals, and iteration
3 Common code patterns: iterative programs
4 Decomposition and abstraction through functions; introduction to recursion
5 Floating point numbers, successive refinement, finding roots
6 Bisection methods, Newton/Raphson, introduction to lists
7 Lists and mutability, dictionaries, pseudocode, introduction to efficiency
8 Complexity; log, linear, quadratic, exponential algorithms
9 Binary search, bubble and selection sorts
10 Divide and conquer methods, merge sort, exceptions
11 Testing and debugging
12 More about debugging, knapsack problem, introduction to dynamic programming
13 Dynamic programming: overlapping subproblems, optimal substructure
14 Analysis of knapsack problem, introduction to object-oriented programming
15 Abstract data types, classes and methods
16 Encapsulation, inheritance, shadowing


So, if we keep at this, by the end of what I intend you should - hopefully - be able to walk into a Computer Science class and get a 40% or so on a test - without even hearing one lecture Razz

"Recursion" might be the last topic we talk about. It's very, very useful - but it's also a hard concept to grasp for most people. Lots of things can go wrong Smile
Dakoth

Yay!  I hate all things quadratic!
Xander Vos

Well I don't need to care about it anymore because I've finished Maths. Wink
Scion

I'd try to post but I can't get to the actual programming of Eclispe.  Kind of sad really.
Rive Caedo

Scion wrote:
I'd try to post but I can't get to the actual programming of Eclispe.  Kind of sad really.

Have you looked at Day 3 in the first post?
After discussing Strings for a moment, it gives a step-by-step tutorial on getting a Java program done in Eclipse.
Scion

Thanks, I missed that in the jumble.  I am attempting your challenge posthaste.
Xander Vos

No new lesson?
Rive Caedo

I was planning to post a long one about Objects and Classes today. But Dragon Age sort of consumed all my time >_<

So, Day 6 has been posted, but it's just another quick concept like yesterday.

But, also like yesterday, it's an important one since they show up all over the place Very Happy
Dakoth

Got it.

       Rebels Forum Index -> General Discussion Page 1, 2  Next
Page 1 of 2
Create your own free forum | Buy a domain to use with your forum

The Star Wars Combine Banner Exchange