codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
NO_PLAYER = 0 PLAYER_1 = 1 PLAYER_2 = 2 def search(seq): num_since_start = 0 # number of same-color chips + empties in the sequence num_in_line = 0 # number of same-color chips in the sequence num_consecutive_empty = 0 # number of consecutive empty cells i = 0 for player_id in seq: # have we found 4 chips such that we can make a win? if num_since_start >= 5: if num_in_line >= 4 and num_in_line >= (num_since_start - 2): yield (i-5, i) elif num_in_line <= -4 and (num_since_start + num_in_line) <= 2: yield (i-5, i) # have we found 3 chips in a row that might be used to construct a # double threat? if num_since_start >= 4 and num_consecutive_empty == 1: if num_in_line == 3: yield (i-num_since_start, i) elif num_in_line == -3: yield (i-num_since_start, i) if player_id is NO_PLAYER: num_since_start += 1 num_consecutive_empty += 1 if num_consecutive_empty > 1: num_in_line = 0 num_since_start = 1 # reset elif num_since_start > 6 and num_in_line < 4: num_since_start = 1 # reset elif player_id is PLAYER_1: if num_in_line < 0: num_in_line = 1 if num_consecutive_empty >= 1: num_since_start = 2 # reset else: num_since_start = 1 else: num_in_line += 1 num_since_start += 1 num_consecutive_empty = 0 else: if num_in_line > 0: num_in_line = -1 if num_consecutive_empty >= 1: num_since_start = 2 # reset else: num_since_start = 1 else: num_in_line -= 1 num_since_start += 1 num_consecutive_empty = 0 i += 1 print "id:", player_id, "since start:", num_since_start, "in line:", num_in_line, "consecutive empty:", num_consecutive_empty # have we found 4 chips such that we can make a win? if num_since_start >= 5 and (num_since_start - num_in_line) > 0: if num_in_line >= 4 and num_in_line >= (num_since_start - 2): yield (i-5, i) elif num_in_line <= -4 and (num_since_start + num_in_line) <= 2: yield (i-5, i) # have we found 3 chips in a row that might be used to construct a # double threat? if num_since_start >= 4 and num_consecutive_empty == 1: if num_in_line == 3: yield (i-num_since_start, i) elif num_in_line == -3: yield (i-num_since_start, i) raise StopIteration() def do(func, ls): for (start, end) in func(ls): print ls[start:end] print do(search, (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,)) do(search, (0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0,)) do(search, (0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,)) do(search, (0, 1, 2, 2, 2, 2, 0, 0, 0, 0, 0,)) do(search, (0, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0,)) do(search, (0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1,)) do(search, (0, 0, 0, 1, 0, 1, 1, 1, 2, 0, 0,)) do(search, (0, 0, 2, 1, 1, 0, 1, 1, 2, 0, 0,)) do(search, (1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0,)) do(search, (0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0,)) do(search, (0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0,)) do(search, (0, 1, 1, 1, 1, 0, 1, 2, 2, 0, 0,)) do(search, (0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0,)) do(search, (0, 1, 1, 1, 0, 0, 2, 2, 0, 2, 0,)) do(search, (0, 1, 1, 1, 0, 0, 0, 2, 2, 0, 2,)) do(search, (2, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 2,)) do(search, (0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,))
Private
[
?
]
Run code