Personal tools

Difference between revisions of "Eth1winter pygame example"

From Eth0Wiki

Jump to: navigation, search
(New page: <pre> # Standard modules import unittest # Tested module import areamax PATTERN_ONE = """83 17 48 41 50 85 56 98 87 20 38 12 76 11 89 27 47 15 38 45 14 79 74 42 85 16 14 41 91 78 70 60 8...)
 
(So I named these things wrong... and I fixed it)
 
Line 1: Line 1:
 
<pre>
 
<pre>
# Standard modules
+
def AreaMax(input_field):
import unittest
+
  field_strings = input_field.splitlines()
 +
  field_strings.reverse()
 +
  field = []
 +
  for line in field_strings:
 +
    field.append([int(number) for number in line.split()])
 +
  max_score = 0
 +
  max_coord = ()
 +
  for y_coord in range(1, 14):
 +
    for x_coord in range(1, 14):
 +
      score = AreaScore(field, x_coord, y_coord)
 +
      if score > max_score:
 +
        max_score = score
 +
        max_coord = (x_coord + 1, y_coord + 1)
 +
  return max_score, max_coord
  
# Tested module
+
def AreaScore(field, x_coord, y_coord):
import areamax
+
   neighbours = [(x_coord + x, y_coord + y) for x in range(-1, 2)
 
+
                for y in range (-1, 2) if not x == y == 0]
PATTERN_ONE = """83 17 48 41 50 85 56 98 87 20 38 12 76 11 89
+
   values = [field[y][x] for x, y in neighbours]
27 47 15 38 45 14 79 74 42 85 16 14 41 91 78
+
  score = 0
70 60 82 47 12 82 27 44 42 21 41 25 65 68 11
+
   for value in values:
51 59 88 54 17 14 87 81 87 33 68 18 84 40 95
+
     if value % 2 == 1:
52 12 91 66 35 13 43 24 59 23 25 92 67 31 89
+
      score = score - value
66 95 53 89 71 13 31 35 25 31 30 89 51 88 22
+
     else:
60 11 93 10 45 82 99 11 84 55 13 79 20 65 80
+
      score = score + value
70 44 12 49 92 39 32 22 55 58 16 46 69 96 48
+
   return score
49 52 30 45 68 45 62 85 99 89 43 11 65 52 31
 
13 38 81 96 19 88 14 74 12 47 88 45 93 74 64
 
24 40 46 63 99 35 55 94 41 66 78 66 77 27 96
 
25 62 36 67 36 48 71 34 75 41 21 65 24 48 45
 
16 73 71 80 59 59 25 28 30 57 83 42 89 76 74
 
40 20 96 86 46 87 90 40 55 39 72 92 98 76 37
 
89 99 41 95 56 96 99 83 71 23 10 63 73 94 56"""
 
 
 
PATTERN_TWO = """72 68 28 67 98 20 71 65 43 81 10 90 83 53 19
 
50 62 32 53 79 93 60 84 17 87 92 25 84 86 89
 
56 64 28 73 46 11 22 44 89 60 92 93 17 62 40
 
55 50 53 19 84 54 68 52 26 58 24 86 84 22 16
 
16 45 95 60 33 30 19 22 83 22 87 84 22 60 10
 
87 60 77 54 72 93 60 88 40 18 10 30 85 38 30
 
54 95 55 40 16 61 30 43 44 78 30 94 76 53 89
 
42 44 21 80 46 55 52 69 43 92 48 42 46 78 99
 
87 53 36 50 40 31 26 96 54 19 44 44 56 98 97
 
60 65 70 55 53 38 71 18 38 90 30 97 98 61 13
 
18 28 39 20 81 42 31 13 44 10 20 64 94 27 76
 
27 10 90 86 16 25 31 93 44 82 49 44 78 65 34
 
62 73 84 70 67 63 18 95 74 34 72 65 69 44 61
 
69 28 55 20 19 91 21 90 52 50 40 47 43 60 40
 
83 31 32 53 94 12 75 10 46 78 50 48 32 52 30"""
 
 
 
PATTERN_THREE = """35 68 63 93 97 57 17 36 56 70 95 23 13 88 82
 
78 52 71 47 27 45 80 82 96 89 71 56 75 26 93
 
74 63 49 67 65 90 61 29 49 31 91 85 60 27 13
 
22 49 70 30 71 87 78 48 68 98 89 40 71 24 60
 
42 49 49 69 86 52 23 77 77 95 45 51 58 90 73
 
11 28 86 62 88 47 28 10 99 22 67 54 44 58 17
 
97 54 11 47 78 38 77 79 31 97 12 87 56 23 33
 
63 35 85 29 44 55 40 84 33 18 23 66 60 15 99
 
53 38 75 12 49 69 96 78 89 20 48 12 34 44 40
 
43 82 27 84 58 40 59 24 54 11 87 39 40 99 65
 
47 81 16 61 56 87 62 53 56 80 41 60 73 99 72
 
96 45 64 77 41 34 97 41 99 86 29 84 98 34 69
 
79 17 83 37 73 95 13 65 36 20 74 98 56 41 50
 
47 57 46 17 98 99 25 31 49 72 12 85 67 34 88
 
69 38 59 98 14 73 86 72 22 63 80 47 62 32 38"""
 
 
 
PATTERN_FOUR = """82 54 50 21 43 56 30 82 44 31 37 84 91 96 11
 
77 57 21 32 35 46 52 94 15 73 86 42 32 29 37
 
23 59 32 12 30 83 47 89 94 31 60 89 62 25 32
 
98 36 88 81 76 74 81 86 15 25 55 29 57 54 21
 
17 11 84 17 96 98 77 50 34 47 41 45 75 90 24
 
31 28 14 86 56 16 50 50 80 78 22 66 55 11 23
 
70 43 96 55 71 22 53 76 85 27 45 52 49 52 36
 
82 92 41 67 44 62 72 55 70 78 85 26 29 56 55
 
82 56 91 79 55 84 52 40 49 51 68 14 75 97 51
 
16 28 19 33 81 10 88 94 26 25 96 42 83 10 99
 
11 64 93 84 37 83 45 54 42 41 62 80 72 79 11
 
57 41 28 92 27 26 26 69 65 55 62 94 87 91 14
 
90 12 68 93 55 47 39 92 93 65 74 40 22 98 69
 
34 97 64 39 90 17 82 24 19 33 74 64 47 24 93
 
78 71 83 79 20 79 85 77 53 71 19 78 33 80 67"""
 
 
 
 
 
class AreaMaxingTest(unittest.TestCase):
 
   def testPatternOne(self):
 
    maximum, coords = areamax.AreaMax(PATTERN_ONE)
 
    self.assertEqual(maximum, 391, 'Wrong maximum.')
 
    self.assertEqual(coords, (13, 3), 'Bad coordinates for given maximum.')
 
 
 
   def testPatternTwo(self):
 
    maximum, coords = areamax.AreaMax(PATTERN_TWO)
 
    self.assertEqual(maximum, 450, 'Wrong maximum.')
 
    self.assertEqual(coords, (12, 6), 'Bad coordinates for given maximum.')
 
 
 
   def testPatternThree(self):
 
     maximum, coords = areamax.AreaMax(PATTERN_THREE)
 
    self.assertEqual(maximum, 461, 'Wrong maximum.')
 
    self.assertEqual(coords, (11, 4), 'Bad coordinates for given maximum.')
 
 
 
  def testPatternFour(self):
 
    maximum, coords = areamax.AreaMax(PATTERN_FOUR)
 
     self.assertEqual(maximum, 419, 'Wrong maximum.')
 
    self.assertEqual(coords, (4, 11), 'Bad coordinates for given maximum.')
 
 
 
if __name__ == '__main__':
 
   unittest.main()
 
 
</pre>
 
</pre>

Latest revision as of 20:10, 31 January 2009

def AreaMax(input_field):
  field_strings = input_field.splitlines()
  field_strings.reverse()
  field = []
  for line in field_strings:
    field.append([int(number) for number in line.split()])
  max_score = 0
  max_coord = ()
  for y_coord in range(1, 14):
    for x_coord in range(1, 14):
      score = AreaScore(field, x_coord, y_coord)
      if score > max_score:
        max_score = score
        max_coord = (x_coord + 1, y_coord + 1)
  return max_score, max_coord

def AreaScore(field, x_coord, y_coord):
  neighbours = [(x_coord + x, y_coord + y) for x in range(-1, 2)
                for y in range (-1, 2) if not x == y == 0]
  values = [field[y][x] for x, y in neighbours]
  score = 0
  for value in values:
    if value % 2 == 1:
      score = score - value
    else:
      score = score + value
  return score