#!/local/bin/python
import sys
import string
import math
from whrandom import randint
from curses import *
class monster:
	def __init__(self,x_,y_,a_):
		self.x=x_
		self.y=y_
		self.cellval=0
		self.obj=a_
	def mmove(self,x_,y_):
		dist=math.hypot(80,23)
		nx=0
		ny=0
		x=self.x
		y=self.y
		if x > 0 and self.obj.a[x-1][y]<11:
			newdist=math.hypot(x-1-x_,y-y_)
			if newdist<dist:
				dist=newdist
				nx=x-1
				ny=y
		if x < 22 and self.obj.a[x+1][y]<11:
			newdist=math.hypot(x+1-x_,y-y_)
			if newdist<dist:
				dist=newdist
				nx=x+1
				ny=y
		if y > 0 and self.obj.a[x][y-1]<11:
			newdist=math.hypot(x-x_,y-1-y_)
			if newdist<dist:
				dist=newdist
				nx=x
				ny=y-1
		if y < 77 and self.obj.a[x][y+1]<11:
			newdist=math.hypot(x-x_,y+1-y_)
			if newdist<dist:
				dist=newdist
				nx=x
				ny=y+1
		if nx == 0: return
		self.obj.a[nx][ny]=12
		self.obj.a[self.x][self.y]=0
		self.x=nx
		self.y=ny
		if self.x == x_ and self.y == y_:
			self.obj.scr.move(23,20)
			self.obj.scr.addstr("you lose")
			self.obj.scr.refresh()
			self.obj.xit()
	def move(self,x_,y_):
		nx=self.x+x_
		sx=nx
		ny=self.y+y_
		sy=ny
		if nx < 0 or nx > 22 or ny < 0 or ny > 78: return
		if self.obj.a[nx][ny] == 11: # push a block
			sx=sx+x_
			sy=sy+y_
			while sx >= 0 and sx < 23 and sy >= 0 and sy < 80 and self.obj.a[sx][sy] == 11:
				sx=sx+x_
				sy=sy+y_
			if sx < 0 or sx > 22 or sy < 0 or sy > 78: return
			if self.obj.a[sx][sy] == 12: return
			self.obj.a[sx][sy]=11
		if self.obj.a[nx][ny] == 12: # push a monster
			self.obj.scr.move(23,20)
			self.obj.scr.addstr("you lose")
			self.obj.scr.refresh()
			self.obj.xit()
		self.obj.a[nx][ny]=10
		self.obj.a[self.x][self.y]=0
		self.x=nx
		self.y=ny
		self.obj.a[self.x][self.y]=10
class screen:
	def __init__(self,rate):
		self.mymap = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','X','>','O']
		self.a=[0] * 24
		for i in range(23):
			self.a[i]=[0] * 80
		for i in range(23):
			for j in range(79):
				k=randint(0,100)
				if k > rate:	self.a[i][j]=11
				else: self.a[i][j]=0
		self.m=[0] * 9
		for i in range(8):
			x=randint(0,22)
			y=randint(0,79)
			while self.a[x][y] != 0:
				x=randint(0,22)
				y=randint(0,79)
			self.m[i]=monster(x,y,self)
			self.a[x][y]=12
		x=randint(0,22)
		y=randint(0,79)
		while self.a[x][y] != 0:
			x=randint(0,22)
			y=randint(0,79)
		self.me=monster(x,y,self)
		self.a[x][y]=10
		initscr()
		cbreak()
		noecho()
		self.scr=newwin(0,0)
	def p(self):
		self.scr.move(0,0)
		for i in range(23):
			for j in range(79):
				t=self.mymap[self.a[i][j]]
				self.scr.move(i,j)
				self.scr.addstr(t)
		self.scr.refresh()
	def xit(self):
		endwin()
		sys.exit()
	def moveme(self):
		self.scr.move(23,0)
		char=self.scr.getch()
		# self.scr.addstr(char)
		x=y=0
		if char == 113 : self.xit()	# q
		if char == 107 : x= -1		# k (up)
		if char == 106 : x= 1		# j (down)
		if char == 104 : y= -1		# h (left)
		if char == 108 : y= 1		# l (right)
		self.me.move(x,y)
		self.p()
		self.scr.refresh()
sc=screen(50)
sc.p()
while 1 :
	sc.moveme()
	for i in range(8):
		sc.m[i].mmove(sc.me.x,sc.me.y)
sc.xit()

