head	1.2;
access;
symbols;
locks; strict;
comment	@# @;


1.2
date	2002.08.20.04.00.04;	author kewlbear;	state Exp;
branches;
next	1.1;

1.1
date	2002.08.20.03.56.51;	author kewlbear;	state Exp;
branches;
next	;


desc
@@


1.2
log
@README nemo.pyw  ǹٲ ħ -o-;
@
text
@contrib ͸ ִ   Դϴ.
 Ϳ    ʽϴ.

nemo.pyw:
	Python ׸׸
	pyfeanor@@hanmail.net  
@


1.1
log
@nemo.pyw, */README ߰
@
text
@d1 2
a2 1
import random
d4 3
a6 151
def scan(bitseq):
    def bitstr(bitseq):
        bitstr = ''
        for bit in bitseq:
            bitstr += str(bit)
        return bitstr
    return [ len(seg) for seg
             in bitstr(bitseq).split('0')
             if seg ]

def mines(xs, ys):
    res = {}
    for x in range(xs):
        for y in range(ys):
            res[x, y] = random.randrange(2)
    return res

def colhintnum(mines, xs, ys):
    temp = []
    for x in range(xs):
        temp2 = []
        for y in range(ys):
            temp2.append(mines[x, y])
        temp.append(scan(temp2))
    res = {}
    for x in range(xs):
        tempcnt = (ys+1)/2-len(temp[x])
        for tempitem in temp[x]:
            res[x, tempcnt] = tempitem
            tempcnt += 1
    return res

def rowhintnum(mines, xs, ys):
    temp = []
    for y in range(ys):
        temp2 = []
        for x in range(xs):
            temp2.append(mines[x, y])
        temp.append(scan(temp2))
    res = {}
    for y in range(ys):
        tempcnt = (xs+1)/2-len(temp[y])
        for tempitem in temp[y]:
            res[tempcnt, y] = tempitem
            tempcnt += 1
    return res


import Tkinter

size = 20
margin = 5

color = {}
color['close'] = 'gray'
color['num'] = 'lightgray'
color['open'] = 'white'
color['mark'] = 'black'
color['mine'] = 'red'

class Board:

    def __init__(self, parent, xs, ys, state, num):
        self.xs = xs
        self.ys = ys
        self.width = xs*size+margin*2
        self.height = ys*size+margin*2
        self.state = {}
        for x in range(self.xs):
            for y in range(self.ys):
                self.state[x, y] = state
        self.num = num
        self.canvas = Tkinter.Canvas(parent)
        self.canvas.config(width=self.width, height=self.height)
        self.items = {}
        for x in range(xs):
            for y in range(ys):
                start = x*size+margin, y*size+margin
                end = x*size+size+margin, y*size+size+margin
                id = self.canvas.create_rectangle(*(start+end))
                self.items[x, y] = id

    def redraw(self, x, y):
        self.canvas.itemconfig(
            self.items[x, y],
            fill=color[self.state[x, y]])

    def numdraw(self, x, y):
        pos = x*size+size/2+margin, y*size+size/2+margin
        if (x, y) in self.num:
            self.canvas.create_text(text=self.num[x, y], *pos)

    def redrawall(self):
        for x in range(self.xs):
            for y in range(self.ys):
                self.redraw(x, y)
                self.numdraw(x, y)

    def grid(self, col, row):
        self.canvas.grid(col=col, row=row)
        self.redrawall()

    def bind(self, event, xycallback):
        def callback(event):
            x, y = (event.x-margin)/size, (event.y-margin)/size
            board = self
            xycallback(board, x, y)
        self.canvas.bind(event, callback)

def do_open(board, x, y):
    state = board.state
    if state[x, y] == 'close':
        if mines[x, y]:
            state[x, y] = 'mine'
        else:
            state[x, y] = 'open'
        board.redraw(x, y)

def do_mark(board, x, y):
    state = board.state
    if state[x, y] == 'close':
        state[x, y] = 'mark'
    elif state[x, y] == 'mark':
        state[x, y] = 'close'
    board.redraw(x, y)
    
xs = 10
ys = 10
halfxs = (xs+1)/2
halfys = (ys+1)/2

root = Tkinter.Tk()
frame = Tkinter.Frame(root)

mines = mines(xs, ys)
colhintnum = colhintnum(mines, xs, ys)
rowhintnum = rowhintnum(mines, xs, ys)

nemoboard = Board(frame, xs, ys, 'close', {})
colhint = Board(frame, xs, halfys, 'num', colhintnum)
rowhint = Board(frame, halfxs, ys, 'num', rowhintnum)

nemoboard.bind('<Button-1>', do_open)
nemoboard.bind('<Button-3>', do_mark)

nemoboard.grid(col=1, row=1)
colhint.grid(col=1, row=0)
rowhint.grid(col=0, row=1)

frame.pack()
root.mainloop()
@

