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...