| 1 | importˆàengine |
|---|
| 2 | |
|---|
| 3 | defˆàpreprocess(line): |
|---|
| 4 | ˆà ˆà ifˆà'//'ˆàinˆàline: |
|---|
| 5 | ˆà ˆà ˆà ˆà line =ˆàline[:line.index('//')] |
|---|
| 6 | ˆà ˆà line =ˆàline.rstrip() |
|---|
| 7 | ˆà ˆà returnˆàline |
|---|
| 8 | |
|---|
| 9 | classˆàSnake(object): |
|---|
| 10 | ˆà ˆà defˆà__init__ˆà(self,ˆàcells,ˆàcolor): |
|---|
| 11 | ˆà ˆà ˆà ˆà self.cells =ˆàcells |
|---|
| 12 | ˆà ˆà ˆà ˆà self.color =ˆàcolor |
|---|
| 13 | ˆà ˆà ˆà ˆà self.rules =ˆà[] |
|---|
| 14 | |
|---|
| 15 | ˆà ˆà defˆàloadˆà(self,ˆàfile): |
|---|
| 16 | ˆà ˆà ˆà ˆà magic,ˆàname =ˆàpreprocess(file.readline()).split(' ',ˆà1) |
|---|
| 17 | ˆà ˆà ˆà ˆà assertˆàmagic ==ˆà"snake",ˆà"This is not snake file" |
|---|
| 18 | ˆà ˆà ˆà ˆà whileˆàTrue: |
|---|
| 19 | ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(file.readline()) |
|---|
| 20 | ˆà ˆà ˆà ˆà ˆà ˆà ifˆàline ==ˆà'end': |
|---|
| 21 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà break |
|---|
| 22 | ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline ==ˆà'',ˆà"Rules must be separated by empty lines" |
|---|
| 23 | ˆà ˆà ˆà ˆà ˆà ˆà self.rules.append(Rule().load(file)) |
|---|
| 24 | |
|---|
| 25 | ˆà ˆà defˆàfillˆà(self): |
|---|
| 26 | ˆà ˆà ˆà ˆà forˆàcell inˆàself.cells: |
|---|
| 27 | ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆàself |
|---|
| 28 | ˆà ˆà ˆà ˆà snake.cells[0].type =ˆà'head' |
|---|
| 29 | ˆà ˆà ˆà ˆà snake.cells[-1].type =ˆà'tail' |
|---|
| 30 | ˆà ˆà ˆà ˆà snake.cells[1:-1].type =ˆà'body' |
|---|
| 31 | ˆà ˆà ˆà ˆà return |
|---|
| 32 | |
|---|
| 33 | ˆà ˆà defˆàerrorˆà(self): |
|---|
| 34 | ˆà ˆà ˆà ˆà pass |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | classˆàRule(object): |
|---|
| 38 | |
|---|
| 39 | ˆà ˆà codes =ˆà{ |
|---|
| 40 | ˆà ˆà ˆà ˆà 'h':ˆà'head', |
|---|
| 41 | ˆà ˆà ˆà ˆà 'b':ˆà'body', |
|---|
| 42 | ˆà ˆà ˆà ˆà 't':ˆà'tail', |
|---|
| 43 | ˆà ˆà ˆà ˆà '#':ˆà'wall', |
|---|
| 44 | ˆà ˆà ˆà ˆà ' ':ˆà'any', |
|---|
| 45 | ˆà ˆà ˆà ˆà '-':ˆà'empty', |
|---|
| 46 | ˆà ˆà } |
|---|
| 47 | |
|---|
| 48 | ˆà ˆà defˆà__init__ˆà(self,ˆàsnake): |
|---|
| 49 | ˆà ˆà ˆà ˆà self.snake =ˆàsnake |
|---|
| 50 | ˆà ˆà ˆà ˆà self.direction =ˆà(1,ˆà0) |
|---|
| 51 | ˆà ˆà ˆà ˆà self.pattern =ˆà{} |
|---|
| 52 | |
|---|
| 53 | ˆà ˆà defˆàloadˆà(self,ˆàfile): |
|---|
| 54 | ˆà ˆà ˆà ˆà y =ˆà0 |
|---|
| 55 | ˆà ˆà ˆà ˆà forˆàline inˆàfile: |
|---|
| 56 | ˆà ˆà ˆà ˆà ˆà ˆà line =ˆàpreprocess(line) |
|---|
| 57 | ˆà ˆà ˆà ˆà ˆà ˆà ifˆày ==ˆà0ˆàandˆàline ==ˆà'': |
|---|
| 58 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue |
|---|
| 59 | ˆà ˆà ˆà ˆà ˆà ˆà assertˆàline[-1]ˆà==ˆà';',ˆà"Rule lines must end with semicolon" |
|---|
| 60 | ˆà ˆà ˆà ˆà ˆà ˆà assertˆàlen(line)ˆà==ˆà8,ˆà"Rule lines must be exactly 7 chars long" |
|---|
| 61 | ˆà ˆà ˆà ˆà ˆà ˆà forˆàx,ˆàchar inˆàenumerate(line[:8]): |
|---|
| 62 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.parse_cell(x,ˆày,ˆàchar) |
|---|
| 63 | ˆà ˆà ˆà ˆà ˆà ˆà y +=ˆà1 |
|---|
| 64 | |
|---|
| 65 | ˆà ˆà defˆàparse_cell(self,ˆàx,ˆày,ˆàchar): |
|---|
| 66 | ˆà ˆà ˆà ˆà assertˆàchar.lower()ˆàinˆàself.codes,ˆà"Illegal symbol in rule: %s"ˆà%ˆàchar |
|---|
| 67 | ˆà ˆà ˆà ˆà cell =ˆàengine.Cell(x,ˆày,ˆàNone) |
|---|
| 68 | ˆà ˆà ˆà ˆà ifˆàchar inˆà'htb': |
|---|
| 69 | ˆà ˆà ˆà ˆà ˆà ˆà ifˆàchar.islower(): |
|---|
| 70 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆà'my' |
|---|
| 71 | ˆà ˆà ˆà ˆà ˆà ˆà else: |
|---|
| 72 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cell.snake =ˆà'enemy' |
|---|
| 73 | ˆà ˆà ˆà ˆà cell.type =ˆàself.codes[char.lower()] |
|---|
| 74 | ˆà ˆà ˆà ˆà self.pattern[x,ˆày]ˆà=ˆàcell |
|---|
| 75 | |
|---|
| 76 | ˆà ˆà defˆàappliesˆà(self,ˆàfield,ˆàx,ˆày): |
|---|
| 77 | |
|---|
| 78 | ˆà ˆà ˆà ˆà pass |
|---|
| 79 | ˆà ˆà defˆàrotateˆà(self,ˆàrot): |
|---|
| 80 | ˆà ˆà ˆà ˆà pass |
|---|
| 81 | |
|---|
| 82 | # vim: set ts=4 sts=4 sw=4 et: |
|---|