The Journey of a New Computer Programmer

A longtime computer-literate who wants to learn "real" programming language(s).

Tuesday, March 18, 2008

Here we are, several months later.
I regret to say that I haven't had much involvement in programming work since the project was completed.

Starting college in Fall 2007 (RIT freshman, Business Management) is a prime reason, albeit amongst others.

I'm still a Magic the Gathering addict, and my written-in-Python-program to replicate the game's randomized booster packs is thus likely to see development earliest once I
get back to work.

http://shock.easthighschool.net/projects/aproj/trunk/BoosterPackMaker/BoosterPackMaker.zip provides the most-current version of the aforementioned program.

http://shock.easthighschool.net/projects/aproj/
My program-file data (linked here as an SVN repository) still works. :)

Monday, May 28, 2007

What now?

Even though Naval Duels is working now, I may want to adjust the statistics, so that each ship type is appropriately powerful compared to the others. (However, this is a matter of the game's "flavor", rather than of coding skill.)

As you probably well know, Lee is big on breaking down functions into smaller parts that are easy to analyze/work with in and of themselves.

I have started on that road- The Naval Duels combat engine now looks like this:


def CombatEngine(fleet_a, fleet_b):

main_battle_calc(fleet_a, fleet_b)
main_battle_calc(fleet_a, fleet_b)
main_battle_calc(fleet_a, fleet_b)
main_battle_calc(fleet_a, fleet_b)
main_battle_calc(fleet_a, fleet_b)

# main_battle_calc basically calls a for loop.
# The for loop goes through a list (the firing order in this case) once and does certain things/
# But it doesn't go through that main list more than once.
# Sometimes, going through the loop once won't eliminate one side's ships entirely.

winner = find_winner(fleet_a, fleet_b)

return winner


So, it's broken down into two pieces. I may want to break down main_battle_calc() further.
Also, I will want to break down the user interface into smaller function 'pieces'.

My Admiral feature, whereby the character chosen modifies the ship statistics, does not do anything in the current version. I aim to fix that.

I need to write a flowchart explaining what the program does. Even though I know, a lot of other people don't, and such a flowchart would be very nice for my Senior Project Night/Day presentation...Speaking of that, I also need to throw that together.

Random noises

From my last post-

(insert random celebratory noises here) [for instructions how to do so, see the next post]

The following script does just that:


from random import choice

noises = ["Woohoo!", "Booyah!", "w00t!", "Finally!", "Yay!"]

how_many_noises = int(raw_input("How many noises do you want to make?"))

for x in range(0, how_many_noises):
print noises[choice(range(0, len(noises)))]


Strictly speaking, it doesn't play the noises, displays them. But anyway...

Dare I say it?...

...I have Naval Duels working!
(insert random celebratory noises here) [for instructions how to do so, see the next post]

This weekend, I was paying a visit to Grandma's house.
She does have a computer, and I needed to work on Naval Duels, so I emailed the workign versions of the two ND files to myself. (I didn't want to bother rigging up the whoel Subversion system for just a couple files.)

Saturday night, I was tired anyway (late-night coding runs being only partially behind that, but my sleep habits are another issue), and I probably was knocked out by an (albeit needed) dose of allergy medication.
Plus, there was the normal futzing around on the 'Net.

I got really cracking on writing code a few hours after I got up on Sunday. That led to working pseudocode.

Now how can pseudocode be working, you ask? I wrote the pseudocode as print statements, so as the computer was "running" the pseudocode, it displayed it.
The pseudocode display the calculations I would later set up the program to make, and it was displaying all 49. (each possible combination of 7 firing shiptypes times 7 target ship types.)

My previous approaches would have copy/pasted a similar block of code 49 times. Here, I cut that down to 7. The basic structure is as follows:

for every shiptype in the firing order:
for every target in that shiptype's targets:
if that target is Fighter:
do so-and-so
if that target is Carrier:
do such-and-such

'for every shiptype in the firing order' goes through 'for every target in that shiptype's targets' seven times, because there are 7 items in the firing order.

Of course, ti was time to leave Grandma's once I had "hit my stride", so to speak. Murphy's Law. Congress needs to repeal that cr*p, or something. :)

Once I got home, my task was to write lines of code that perform the simple mathematical calculations based on the ship stats.

fleetA_target_killed = (fleet B's amount of the current firing shiptype, multiplied by the current shiptype's firepower), divided by the armor of the current target shiptype
fleetB_target_killed = (fleet A's amount of the current firing shiptype, multiplied by the current shiptype's firepower), divided by the armor of the current target shiptype

That took very easily. I think the big challenge of this program was properly applying classes (for the ship types and each fleet), and setting up the loop structure. These were just simple mathemetical calculations, albeit simple mathematical calcuations using specific variables.

Tuesday, May 22, 2007

More on the Senior Meeting

I must thank DJ for a printing an copying run of his: all the emails I had sent out to the entire committee. (Obviously, he wouldn't have the here's-a-question emails I sent just to lee, etc.) At least the ones from March 12th onward.
I didn't look at the packet much myself, but I figured it has to be illimunating. Thanks DJ for providing that.

(If I dug through my Drafts folder, I probably could have found most, but not all, of them, but they were mixed in, and DJ had diverted 'agilfoy@frontiernet.net' emails to a special folder.)

I had obviously thought of Lee and perhaps Nick as community resources, but it had never occurred to me to classify the Python Tutor mailing list as such, or even the library computers that were an end-run around my computer-crash situation back in April.

I knew I had learned a lot about the subject at hand, perhaps more than I thought.
The stuff about the process is true, but bring that out/discussing it was prompted by Larry and Ken's requests to include it in my meeting. Although it's still there, I suppose it's less obvious than "I learned how to do this, this, and this. And I wrote a program that does X, Y and Z."

Sheesh, the voting session (which involves me being asked to leave the room) is nerve-wracking. Or, from another perspective, it's excessively anticipatory - can you tell me I passed already?

Most S-Dub students scrawl "I passed", or some variant, in very large letter son the copy of their senior project proposal that's on the Senior Project Wall near the front door. I plan to write 'i passed' in tiny letters in some corner of the piece of paper.

Of course, I don't quite pass...Larry expressed it as 'I have good news and I have medium news'. Before I'm officially passed, I have to fix up Naval Duels.
That makes sense, honestly.
I made the mistake of including 'I will continue to work on Naval Duels' in my PowerPoint. I meant that I was going to get to it on my own terms, now I just have to get it working by a few days before June 9th. (It is "suggested" that I have it done earlier than that.)

June 9th is this year's date for an important step in the SWW Senior Project process -Seniro project Night. This basically is showing off your projects to the staff as a whole, to the community, and to your parents. June 10th is Senior Project Day, a similar thing where they SWW students look at your project. In that case, since the Senior Project Day visitors are underclassmen, they get idea and advice on the process.

My Senior Meeting

Can you say that my senior meeting went well?
No you can't, it went great!
This isn't me being full of myself; I got that as a rather clear impression from the six committee member sthat that was so.

Was I nervous? You betcha! With something so important, as it basically says whether I graduate from high school or not, even if I'm confident, I'm going to be nervous.
In a couple of other instances, I was nervous going into the SAT (last spring) and going into my 3 AP exams (just recently), even though I was very confident that I was going to ace them.

The day was defined by prepping for the meeting, and I did have to cut out of a large portion of most of my classes, although the teachers darn well understood that I had something else very important on my mind. I applaud them for not giving me any hassles about it. Although I don't believe they shouldn't have, the fact remains that they could have. Thank you.

One morning class block was spent largely with Ken going through the PowerPoint I planned to deliver at the meeting.
During Extended Class, Larry let me cut out to copy materials. (I forgot to open the meeting with a moment of silence for all the trees I killed... :D)
That kept going through when I would normally leave for for SOTA, for a 7th Period class. (I had already warned the teacher of that class that I would have Senior Meeting prep to do.)

The Economics teacher (9th Period) wanted to see me for a few minutes, to hand me the class materials before I left. (Ironically, they were questions ona video that I can't really do without seeing the video). So I went over to SOTA anyway. Despite the situation with the Economics materials, I still wanted to head over to SOTA because 8th Period there is essentially my lunch break, and I get to play Magic the Gathering and run my (relatively profitable) gray-market soda-resale operation. :D

I presented the latter to Larry as a stress reliever or somesuch, and I honestly think ti was.
Beside,s yesterday was hardly a bad day to be walking around the city. :)

I get back to SOTA at about 3:00, for the 3:30 meeting, as planned. Since I thought Larry's room computer didn't have Python installed (turns out it did), I chased Ken down beforehand to get one of the school's Apple laptops, which I Know have Python installed. (And if they don't, Ken has more admin rights on that network *to* install Python) Also, I got into Walter's room (which contains a coffee machine) to provide Nick with his drug, er, beverage, of choice. [I had intended to use one of Dad's French presses, but I couldn't find the small, easily portable one.]

I forgot to bring chips too, but Ken covered that. Alantech Beverage Enterprises, i.e, myself, covered the pop for the other committee members. Free, of course. Shoot, Larry and DJ, even if they weren't members of my committee, would probably deserve frequent-customer discounts. :)

The PowerPoint presentation was partially designed to keep me from meandering too much int he available time, and it helped with that function (I've very capable of talking for extended periods of time to an appreciative audience, and of going off on tangents, period.) I did some of that during the meeting anyway, but I honestly believe it was controlled.

I didn't demonstrate as much code as I would have liked to, but: As I reiterated several times during the meeting, Senior Projects are also very largely about the process in addition to the product.

Monday, May 21, 2007

The couple of days leading up to my final meeting

...In the couple of days leading up to my final meeting (May 21st, 3:30), I still had a non-working Naval Duels. And I also had a couple of Boy Scout events scheduled. I've been in that twice as long as SWW. :) Also, I feel confident that I can do both, and I want to do both.

But anyway, I *have* been aware of this meeting date for weeks, and I have been working on the presentation and on more code. I figure that the time left after we return on Sunday afternoon will be enough to get my project in presentable shape.

Also, a post-campout activity is a Court of Honor for six newly-minted Troop 31 Eagle Scouts. Not only am I close to this myself, I wasn't too far behind eahc of these guys, going through Scouting.

During the course of the camping trip, I ran into two SWW graduates - Steve Chaba (Class of 1977- dang!) and Claire Chaba (Class of 2005?). For some reason, Senior Project discussion didn't go much beyond incidentals, although Claire warned that trying to bribe your committee members is a bad idea, the jokes nonwithstanding.

But, I suppose that's another story.

Friday afternoon, there wasn't much time between leaving school and heading to the campground, although I did squeeze some coding in.

Sunday afternoon/evening coding work morphed into early Monday morning coding work. Naval Duels was the main target, although Lee and I had some incidental ideas for my English-to-Pig Latin converter.

Although Naval Duels' combat isn't working as planned, the point remains that, even though it isn't running through all the calculations properly, it's still exiting the loop and reporting a result, as opposed to getting stuck in the loop. That is a (relatively) good thing.

Also, I am trying the new approach, using Python 'classes' to store the statistics used by Naval Duels, as opposed to separate variables. Even if the calculation isn't working, I am demonstrating the proper structure that I am to use.

I'm surprised at how much easier things seem to be writing the program the 'proper' way- with classes. For instance, it's making it easier to reference pieces of my stats when I need them.

(Thanks Lee, for correcting the improper way I had been attempting to use classes, and providing examples that show how to get the most out of them in the case of this specific program.)

In some ways, this seems like a Eureka moment. A good bit of confidence going into the day of the meeting itself.

Good night, and good luck.
May the Force be with me. (My latest joke, accurate or not, is that if it takes Jedi mind tricks to help me pass my senior project, then so be it. :D )

Thursday, May 17, 2007

Rebuilding Naval Duels

I am trying to rebuild Naval Duels from scratch, since my last approach was obviously messed up.
I am trying to use classes this time.

class a(object):
fi = 0
ca = 0
su = 0
fr = 0
de = 0
cr = 0
bs = 0
Admiral = 0
# these are the defaults, modified by the user-interface.

Instead defining each of them as a separate variable and referencing them as separately-named variables, I can refer to a.whatever, instead of the various variable names I was using as separate global variables.

I asked some pertinent questions on Python Tutor on arrays, lists within lists, and how to "call" a specific item from within the array.
I'll use arrays for the ship statistics, instead of 7 separate lists.

A simple array would be like this:

array = [["0.0", "0.1"], ["1.0", "1.1"]]

My previous setup would have been separated. Like this:

somevariable = ["0.0", "0.1"]
anothervariable = ["1.0", "1.1"]

Now array[number][another_number] can pull any piece of data out of my ship statistics, instead of having to use somevariablename[number].

array[0][1] means - Get the 1st item from the the zeroth item in the array.

The zeroth item in the array is the list '["0.0", "0.1"]'. The 1st item in *it* is the string "0.1".

I will have the ship stats for each shiptype each as a list within the overall shipstatistics array. A simple example, with only two shiptypes and 3 statistics values (in reality, it will be 7 shiptypes, and 10 statistics values, but 2 and 3 is easier to show on the blog):

ship_stats = [["fighter", 9, 12], ["submarine", 100, 70], ["destroyer", 210, 200]]

ship_stats[first_number][second_number]

ship_stats[0][1] gets what happens to be the fighter's firepower, which would be 9.

fighter[firepower_location] fetches the same under my old version of the code.

When I want to run a similar calculation for a different shiptype (the calculation being 'find out the firepower' in this case), changing ship_stats[0][1] to ship_stats[1][1] should be a hell of a lot easier than manually writing in the totally separate variables that I named before. I would merely need to change first_number, which would call a different thing from the ship_stats array. Changing first_number could even be an automatic feature of a 'for' loop.

This isn't a Great Scientific Moment(TM), but I think it's revolutionary in terms of how I think about this problem.
I would be able to write one loop, with the needed batches of 'if' statements, instead of manually copy/paste-ing the needed batches of 'if' statements over and over 7 times (once for each shipclass). This would fix the clunkiness and non-modularity of the previous approach. Furthermore, the problem with the earlier version is that after one run-through of the loop, the program wasn't relooping. (A second run through the loop may have been necessary to completely eliminate enemy ships.
In a smaller, more compact, loop, I may not have that problem at all, or I would at least be able to diagnose it easier.

The relief that comes from figuring this out is amazing, especially with the meeting looming. I haven't tried to cod ethe full combat loop in this style yet, but, at the very least, it's a new approach.

The annoyance that comes at not realizing this is also amazing - amazingly annoying. I had the components (knowledge of lists/arrays and classes), but I had been too thickheaded to put it all together and realize that not only was classes and lists/arrays the best approach, it was the only proper approach.

If what I just realized was merely the best approach (as opposed to what seems like the only approach), as I had previously thought, I wouldn't have needed to start over again to get a result that worked. So I had thought I could continue with an imperfect approach for a short while, to reach my immediate goal of a program that worked at all.

It was a fork in the road, so to speak, to immediately start over with the new approach, or to modify my old approach. I made the wrong decision at that fork, and I spent some time going down a dead end.

May the Force be with me...

Groundhog Day?

Today's May 17th, not February 2nd? What's going on here? It seems liek Groundhog Day.

In terms of the movie, it seems that the process of attempting to "fix" Naval Duels is happenning over and over, resulting in it not getting fixed.

In terms of the holiday, I suppose you can compare "more winter" to "more frustration".

My original plan was to modify my brute-force attack, and polish it up later afte rit was working. But that isn't happening, it seems, so I'm trying to write the main calculation from scratch 'properly'.

Wednesday, May 16, 2007

I understand, but...

I understand that my current brute-force, copy-and-paste, method, isn't the ideal way to code this program, but:

I have a more immediate problem, in that the program is "freezing up" in extremely common cases.

I'd rather have something working, even if it's not 'perfect' from an ideal standpoint. Once I get the program working, then I will create another version that fits the programming ideals better.

Much like I don't want to throw too many features on at once, I don't want to try to fix too many errors all at once.

At any rate, I might be able to achieve some structure improvement, even with the non-ideal 'plan of attack' I have at the moment.

What Naval Duels is..

Here's what my design plan for Naval Duels is, in whole, rather than in the snippets I may have distributed in the past...

The game is about a clash between two fleets of warships, as the name implies.
There are certain types of warships: my current setup has 7 - Carrier-based fighter plane (Fighter), Aircraft Carrier (Carrier), Submarine, Frigate, Destroyer, Cruiser, Battleship.
Each fleet has roughly the same statistics, but the fleets (Fleet A and Fleet B as I call them) can have a different 'Admiral', an avatar-type character whose abilities affect the fleet's statistics.

Each of the ship types, as a whole, fires in a certain order. The particular order simply has to do with the game's "flavor" - i.e. which ships would be "fastest". For example, it's good flavor that Submarines, as they strike quickly, would fire early. Thus, Submarines are early in the firing order. Big ponderous stuff like battleships would be "slow", and are scheduled to fire after many of the other shiptypes. Thay, they are towards the end of the 'firing order'.

FiringOrder = ["Fighter", "Submarine", "Destroyer", "Frigate," "Cruiser", "Battleship", "Carrier"]

Currently, I will handle this simply by putting the processing code for Fighters before Submarines, and *that* before the processing code for Destroyers, etc.

Each ship class has a 'targeting priorities' list. The first type in shiptype X's 'targeting priorities' list is what shiptype X most wants to fire at; only if the enemy has none of those available will shiptype X fire at the second type in its list.
The 'targeting priorities' lists are different for each of the 7 shiptypes, and are based on the "flavor" I want the game to have.

For example, the current 'targeting priorities' list for the Fighter is:
Fighter, Carrier, Frigate, Cruiser, Destroyer, Battleship, Submarine.

Thus, Fighters will first fire at enemy Fighters, then at enemy Carriers, *then* at enemy Frigates, and so on. (After one gets to the end of a shiptype's targeting priorities list, what should happen next is that the processing moves to the shiptype in the firing order after 'Fighter'. (With the current setup, that's 'Submarine'.)

(Fleet A's ships of one type, and Fleet B's ships of that same type, should fire at the same time.)

To fire at a enemy shiptype with one of 'your' shiptypes:

(Amount of yours * Firepower of yours) / Armor of theirs

This means that the amount of enemy ships destroyed is equal to 'your' total firepower value divided by the armor of one of 'theirs'. The amount of ships destorye dis, basically, subtracted from the amount of ships that were left at the start of the calculation.

After the program has gone through the targeting-priorities list for each shiptype, and has worked its way through all the shiptypes, the program checks to see if either side is out of ships.

If either or both sides are out of ships, the battle is over, represented by the calculation loop being stopped. If neither side is out of ships, the program ideally should go back to the beginning, which is why I want to use loops.

And another thing...

...One thing that came up in my discussion with Larry is that I thought that the Roman Numeral calculation would be easier to program than it actually was. That sure as heck can apply to the Naval Duels calculation.

Perhaps that's because computers are fundamentally 'dumb' (can only follow instructions). A human mind can intuitively take care of some things that the computer can't.

With Roman Numerals, when the process to be followed is well known, ti was difficult enough. Yet with Naval Duels, it's a concept that only I really know of, and only I can really describe.

Ken, today, asked for a description of what I'm actually trying to do, i.e., a description of what the Naval Duels concept is. That shall be forthcoming.

And another thing...

...One thing that came up in my discussion with Larry is that I thought that the Roman Numeral calculation would be easier to program than it actually was. That sure as heck can apply to the Naval Duels calculation.

Perhaps that's because computers are fundamentally 'dumb' (can only follow instructions). A human mind can intuitively take care of some things that the computer can't.

With Roman Numerals, when the process to be followed is well known, ti was difficult enough. Yet with Naval Duels, it's a concept that only I really know of, and only I can really describe.

Ken, today, asked for a description of what I'm actually trying to do, i.e., a description of what the Naval Duels concept is. That shall be forthcoming.

Tuesday, May 15, 2007

Discussion with Larry today

I had a discussion with Larry today mainly focusing on an outline for the meeting itself: according to his suggestion, my presentation should run 30 minutes or so, the rest of the meeting to focus on an extended question/answer session, and the actual vote.

In discussing the Senior Project process itself, Larry wants me to discuss more the 'why' of SPs than the 'what', and to discuss the personal journey I went through on the project.

Then comes the content itself.
Larry said I still could have something to discuss if Naval Duels wasn't working, but he'd much rather see that working, and I agree with that sentiment, and that's what I'm currently working on, fixing that up.

Larry, please correct me if I misinterpreted today's conversation. :)