[Tutor] Improving My Simple Game Code for Speed, Memory and Learning

WolfRage wolfrage8765 at gmail.com
Sat Jan 3 16:36:26 CET 2015


On 01/02/2015 10:21 PM, Dave Angel wrote:
>
> This code is way too complex for what it accomplishes. See  below.
>
Yes, that it why it needs to be optimized. It is unfortunate that my 
first goes at many tasks are not the most efficient. But hopefully that 
will get better as I learn from all of you.
>
> If the self.rows is 3, this is equivalent to just:
>                 first, second, third = row
> If the self.rows is larger, it's equivalent to:
>                 first, second, third = row[p: p+3]  for some value of p
>
>>                  if third is not None:
>>                      self.check_total_and_eliminate(first, second, third)
>
> But even better, let the method   check_total_and_eliminate take a slice
> as its argument.
>                    self.check-total_and_eliminate(row[p: p+3])
>
I was able to implement this for the rows, but I was not able yet to get 
it to work with the column code, at least not before I went to sleep 
last night.
Here is what I did for the method find_eliminations, now called 
find_eliminations2.

def find_eliminations2(self):
         #First Down the columns.
         i = 0
         for col_num in range(self.cols):
             for row_list in self.grid:
                 try:  # row_list[col_num]
                     first, second, third = row_list[i: i + 3]
                     print(first, second, third, sep=' | ')
                     # The print above is not giving me the nodes down
                     # each column as I would expect.
                     # The above print is one of many tries. Will have
                     # to keep trying.
                     #self.check_total_and_eliminate(row_list[i: i + 3])
                     i += 1
                 except ValueError:
                     i = 0
                     break
         # Now across the rows.
         i = 0
         for row in self.grid:
             for node in row:
                 try:
                     self.check_total_and_eliminate(row[i: i + 3])
                     i += 1
                 except ValueError:
                     i = 0
                     break
         # Set all eliminated nodes to a value of 0.
         for col in self.grid:
             for node in col:
                 if node.eliminated is True:
                     node.eliminated = False
                     node.value = 0

     def check_total_and_eliminate(self, slices):
         first, second, third = slices
         total = None
         if first.value == second.value:
             total = first.value + second.value + third.value
         elif second.value == third.value:
             total = first.value + second.value + third.value
         if total == 17 or total == 21 or total == 28 or total == 29 or \
             total == 31 or total == 42 or total == 45 or total == 46 \
             or total == 49 or total == 58:
             first.eliminated = True
             second.eliminated = True
             third.eliminated = True

>
> This doesn't come close to implementing the test you describe above. For
> example, a total of 29 could be your first case, 5,5,19.  But it could
> also be 4,4,21.   Or 6,6,17.  Etc.

Sorry I did not fully explain myself. The test case could not be 4,4,21 
because I made a list of acceptable numbers and only acceptable numbers 
are allowed. Actually it is a table of letters on the top and side, the 
letters on the top occur once in the sequence, the letters on the side 
occur twice in the sequence. Each letter is representative of my earlier 
values. The table resolves all of the possible outcomes or totals from 
each sequence, allowing me to detect every possibility.

  |B  |N  |F  |H  |W  |E  |Z  |X  |Y   *1
B|0  |5  |6  |11 |19 |20 |52 |61 |67
N|10 |15 |16 |21 |29 |30 |62 |71 |77
F|12 |17 |18 |23 |31 |32 |64 |73 |79
H|22 |27 |28 |33 |41 |42 |74 |83 |89
W|38 |43 |44 |49 |57 |58 |90 |99 |105
E|40 |45 |46 |51 |59 |60 |92 |101|107
Z|104|109|110|115|123|124|156|165|171
X|122|127|128|133|141|142|174|183|189
Y|134|139|140|145|153|154|186|195|201

*2

B=0
N=5
F=6
H=11
W=19
E=20
Z=52
X=61
Y=67

NNH=21 | NNW=29
FFN=17 | FFW=31
HHF=28 | HHE=42
WWE=58 | WWH=49
EEF=46 | EEN=45


>
> I suggest instead that you compare the values against a table of
> interesting values.  If you make your table a list of 3-tuples, it can
> be searched simply by:
>        if tuple(nodes) in table:
>              do-something

I definitely want to improve the code so if there is a better way that 
is expandable then I would definitely like to implement it.
>
> So you don't need the separate variables first, second, and third at all.
SNIP
>
> for what you've described so far, it might be convenient to store the
> board and its transposed equivalent.  Then search the one for columns,
> and the other for rows.  That's a later optimization, but it might save
> time getting the more complicated parts figured out.
>
OK can you give me an example of how this would go.
On to reading and answering the next emails.



More information about the Tutor mailing list